How to Add Matamo/Piwiki Tracking Code?
Hi, I wanted to add some tracking code to my WackoWiki instance. I successfully did this in the 5.4 version in the common _header.php. I just upgraded to the latest, 5.5.7 and it's changed to _header.tpl. I'm not familiar enough with the template being used and not able to add my tracking JS in there like I did before. An example of the JS is as follows:
<!-- Matomo --> <script type="text/javascript"> var _paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//myTrackerUrl/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', 'mySiteId123']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code -->
If JS no longer works or is allowed, I can do it by a hidden image but I was curious how I can get the Name of the page so I can accurately track which pages a user visited by passing it as a query string parameter to my image src.
Thanks
Comments
Log in or create an account to post a comment.
Re: How to Add Matamo/Piwiki Tracking Code?
[A]
Just copy the JS or HTML code to theme/_common/_header.tpl
If you want pass a variable to the template use
$tpl->h_variable = $value;
in the corresponding file, e.g. _header.php.Add the variable in the template as following
[ ' name | e js ' ]
.$tpl->h_*
-h_
because it becomes part of the definition[''' h HtmlHead ''']
in header.php of your themee js
- escapes the provided dataExample 1 for Image Tracker:
_header.php
_header.tpl
Example 2 for JavaScript Tracker:
UPDATE: This example does not work, the JavaScript syntax interferes with template syntax, please see point B below for a working solution.
_header.php
_header.tpl
All examples untested.
Adding Conditions
You may want add conditions like tracking only pages (show handler) or guests.
_header.php
_header.tpl
[B]
Or set it for example in the _header.php with
$this->add_html('header', '<script> ... </script>');
.Links:
If you're interested we can write and maintain a Matomo integration HowTo.
[ ' db: idsite | e js ' ]
- pull value from configSOLVED
[]
so the template interpreter thinks['setTrackerUrl', u+'piwik.php']
is some sort of variable information from the php file. Is there an escape character?Anyways, I tried what was suggested in [B], using
add_html
and that worked! However, one more useful instruction isadd_html
should be called before$tpl->h_additions = $this->get_html_addition('header');
in the _header.php file.Yeah, would be interested in helping with Matomo integration HowTo. However, would need some more reference about what is mentioned, extensions/config. I do agree the variable approach would be great. Also, same about the admin panel module and creating one for Matamo, would need some guidance about modules in admin panel.