How to install Piwik PRO on sites with Matomo code

Piwik PRO is compatible with Matomo on the tracking code level to make it easy to switch between those two solutions. However, this also means that both tracker libraries use the same command queue to push analytics events. Because of that, it’s not possible to run both Matomo and Piwik PRO on one site without some additional configuration.

How to add Piwik PRO tracking to a site with Matomo code installed?
This guide assumes that Piwik PRO Consent manager is used and anonymous tracking is enabled.

  1. Go to Tag Manager module
  2. Deactivate the default Piwik PRO tag
  3. Add a new Custom code (async) tag
  4. Use the following tag code:
<script type="text/javascript">
    var _ppas = _ppas || [];
    (function () {
        var trackAnonymously = {{ Piwik PRO Anonymization }}('analytics');
        if (trackAnonymously) {
            _ppas.push(['setUserIsAnonymous', 1]);
            _ppas.push(['setVisitorCookieTimeout', 1800]);
        }
    }());
    _ppas.push(['enableLinkTracking']);
    _ppas.push(['setDomains', ['youdomainname.com/']]);
    _ppas.push(['enableHeartBeatTimer']);
    _ppas.push(['trackPageView']);
    _ppas.push(['enableJSErrorTracking']);
    (function(p,i,w,ik) {
        var g=ik.createElement('script'),s=ik.getElementsByTagName('script')[0];
        _ppas.push(['setTrackerUrl', p]);
        _ppas.push(['setSiteId', w]);
        g.type='text/javascript';g.async=true;g.defer=true;g.src=i;s.parentNode.insertBefore(g,s);
    })('//youraccountname.piwik.pro/ppms.php','//youraccountname.piwik.pro/ppas.js','yoursite ID',document)
</script>

Here you have to replace the following things according to your setup:

  • youdomainname.com/ to your own domain name
  • youraccountname.piwik.pro to your Piwik PRO account name
  • yoursite ID to your Piwik PRO site ID (you can find it in Administration → Sites & apps → your website (below the name).
  1. Set consent type “does not require consent” (this scenario assumes that anonymous tracking is in use)
  2. Select existing “All page views” trigger
  3. Save the tag
  4. Add an additional Custom code (async) tag with the following tag code:
<script type="text/javascript">
    var _ppas = window._ppas || [];
    (function () {
        var consents = {{ Consents }};
        var consentType = 'analytics';
        _ppas.push(['setVisitorCookieTimeout', 33955200]);
        if (typeof consents !== 'undefined' &&
            consents.hasOwnProperty('current_state') &&
            consents.current_state.hasOwnProperty(consentType) &&
            consents.current_state[consentType] === 1 &&
            consents.hasOwnProperty('previous_state') &&
            consents.previous_state.hasOwnProperty(consentType) &&
            consents.previous_state[consentType] !== 1
        ) {
            _ppas.push(['deanonymizeUser']);
        }
    }());
</script>
  1. Set tag’s consent type to Analytics (this tag will deanonymize user after the consent is given)
  2. Set tag’s trigger to All page views
  3. Save the tag
  4. Publish all tags

Important note: since there’s a non-default command queue in use (_ppas instead of _paq), all other tracker features have to be set up without using the predefined templates. All the snippet codes and docs can be found here. Just remember to change _paq to _ppas.

If you don’t use Piwik PRO Consent Manager, the code can be adjusted to skip the anonymous tracking part:

  1. Remove this code from the first tag:
(function () {
        var trackAnonymously = {{ Piwik PRO Anonymization }}('analytics');
        if (trackAnonymously) {
            _ppas.push(['setUserIsAnonymous', 1]);
            _ppas.push(['setVisitorCookieTimeout', 1800]);
        }
    }());
  1. (recommended) Add:
    _paas.push(['disableCookies']);
    after
    var _ppas = _ppas || [];
    This part makes sure that cookies are not used - it makes sense as this use case assumes that consent manager is not used.
  2. Do not add the second tag responsible for deanonymization since it’s not needed in this case.