Any homepage with a commercial background needs to be tracked for user behavior. The tracking software Matomo (Piwik) is a great alternative to Google Analytics. The widely used open source web analytics platform is based on PHP and uses a MySQL database. The visitor counting is done with JavaScript or pixel, via an API or a log file analysis.

But what if two Matomo (Piwik) systems exist and you want to cover both with one tracking code?

Initial situation

  • one Piwik object
  • two tracker
  • one Piwik script request

Matomo specifies the tracking code for a system. Only the parameters for the tracking system have to be adjusted.

Solution

With two tracking systems, it is not just adding the same tracking code twice. The tracking code has to be extended by another tracker:

_paq.push(['addTracker', ST, STId]);

A simple example

<script type="text/javascript">
  //Generell setting
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);

  (function() {
    //Url from one Matomo (Piwik) Tracking System
    var u="//localhost/piwik/";
    //use the first tracking Url
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    //Project/use the Website ID for this Piwik system
    _paq.push(['setSiteId', '2']);

    //URL from the second Piwik Tracking System
    var ST = '//localhost/piwik-system-2/piwik.php';
    //Project/Website ID from the second Piwik System
    var STId = 1;
    //add second configured tracker
    _paq.push(['addTracker', ST, STId]);

    //Unique for both trackers
    //specify the script url to one of the two systems
    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>

Conclusion

Through one tracking code both Matomo (Piwik) systems can now be recorded and tracked.