This is a comment on How to Add Matamo/Piwiki Tracking Code?, posted by WikiAdmin at 17.06.2022 07:49
View source for Re: How to Add Matamo/Piwiki Tracking Code?
Both cases should be possible. **[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 theme ##e js## - escapes the provided data **Example 1 for Image Tracker:** %%(html)<!-- Matomo Image Tracker --> <img src="https://piwik.example.org/piwik.php?idsite={$IDSITE}&rec=1" style="border:0" alt=""> <!-- End Matomo -->%% _header.php %% // Matomo Tracker $tpl->h_idsite = $value; $tpl->h_trackerurl = 'https://piwik.example.org/piwik.php?idsite='; // you might want add the values in the the config table %% _header.tpl %%<!-- Matomo Image Tracker --> <img src="[ ' trackerurl ' ][ ' idsite ' ]&rec=1" style="border:0" alt=""> <!-- End Matomo -->%% **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. %%(hl javascript)<!-- Matomo --> <script type="text/javascript"> var _paq = window._paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//{$PIWIK_URL}/"; _paq.push(['setTrackerUrl', u+'piwik.php']); _paq.push(['setSiteId', {$IDSITE}]); 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+'piwik.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code -->%% _header.php %% // Matomo Tracker $tpl->h_idsite = $value; $tpl->h_trackerurl = 'https://piwik.example.org/piwik.php?idsite='; // you might want add the values in the the config table %% _header.tpl %%<!-- Matomo --> <script type="text/javascript"> var _paq = window._paq || []; _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//[ ' trackerurl | e js ' ]/"; _paq.push(['setTrackerUrl', u+'piwik.php']); _paq.push(['setSiteId', [ ' idsite | e js ' ]]); 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+'piwik.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code -->%% All examples untested. **Adding Conditions** You may want add conditions like tracking only pages (show handler) or guests. _header.php %% if ($condition) { $tpl->h_c_idsite = $value; $tpl->h_c_trackerurl = 'https://piwik.example.org/piwik.php?idsite='; } %% _header.tpl %% // Inline definition [= c _ = <img src="[ ' trackerurl ' ][ ' idsite ' ]&rec=1" style="border:0" alt=""> =]%% **[B]** Or set it for example in the **_header.php** with ##$this->add_html('header', '<script> ... </script>');##. Links: 1. ((https://developer.matomo.org/guides/tracking-javascript-guide Matomo JavaScript Tracking Code)) 2. ((https://matomo.org/docs/tracking-api/ Matomo Tracking API)) 3. ((/Dev/Projects/Templatest/Usage Using the Template engine)) If you're interested we can write and maintain a Matomo integration HowTo. 1. e.g. set variables via extension -> config * ##[ ' db: idsite | e js ' ]## - pull value from config 2. Admin panel module for Matamo