Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Moved my matomo code to a file instead of inline, grav.user.username does not work anymore

Solved by pamtbaau View solution

Started by Japhy 4 years ago · 7 replies · 548 views
4 years ago

I have always had my mtm code inline but because I am setting up a csp security header I thought it would be a good idea to move it to a file. MTM still works but grav.user.username is not being picked up anymore. I am not a developer, maybe I am doing something really dumb, any one care to offer some help?

TWIG
<script type="text/javascript">
var _mtm = _mtm || [];
_mtm.push({'setUserId': '{{ grav.user.username }}', 'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src='container.js'; s.parentNode.insertBefore(g,s);
</script>
4 years ago

@Japhy, You said:

I have always had my mtm code inline but ... I thought it would be a good idea to move it to a file.

  • What file?
  • In which template is the file included?
4 years ago

@pamtbaau I added the following to basehtml.twig template. Matomo Tag manager seems to work perfectly fine, except for the username.

TWIG
{% do assets.addJs('theme://js/mtm.js', 96) %}
4 years ago

Thank you very much @pamtbaau will read up and try the code tonight after work. I'll let you know if it worked

4 years ago

Works perfectly. Thanks so much for your help!

Edit for future reference: to get it fully working with csp I ended up doing this

HTML
<script>{{ include(template_from_string(read_file('theme://js/mtm.js'))) }}</script>
<script src="https://domain.com/js/container_xxxxx.js" async defer></script>

in js file

TWIG
var _mtm = _mtm || [];
_mtm.push({'setUserId': '{{ grav.user.username }}', 'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});

See also: https://matomo.org/faq/general/faq_20904/

last edited 03/02/22 by Japhy
4 years ago Solution

@Japhy, Alternatively, you could do:

  • Create file /user/config/mtm.yaml containing:
    TWIG
    script: |
      var _mtm = _mtm || [];
      _mtm.push({'setUserId': '{{ grav.user.username }}', ...
    domain: https://domain.com
    id: xxxxxx
    
  • In base.html.twig:
    TWIG
    <script>{{ include(template_from_string(config.mtm.script)) }}</script>
    <script src="{{ config.mtm.domain }}/js/container_{{ config.mtm.id }}.js" async></script>
    
last edited 03/02/22 by pamtbaau
4 years ago

@pamtbaau:
mtm.yaml

I tried this code as well. Learning a lot here :) I think you missed a '/' in </script> and I added .js after the container.

TWIG
<script>{{ include(template_from_string(config.mtm.script)) }}</script>
<script src="{{ config.mtm.domain }}/js/container_{{ config.mtm.id }}.js" async></script>

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 95 14 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago