I double checked this setup and it turned out it’s prone to race conditions between Matomo and Piwik PRO tracking libraries. Let’s switch to the initial idea that should be safe.
- Disable the default Piwik PRO tag.
- Create a new custom code tag that would send data to Piwik PRO using an alternative namespace. Trigger: All pages. Consent type: Does not require consent (since the tag handles anonymous tracking as well):
<script>
var _ppas = _ppas || [];
(function () {
var trackAnonymously = {{ Piwik PRO Anonymization }}('analytics');
if (trackAnonymously) {
_ppas.push(['setUserIsAnonymous', 1]);
_ppas.push(['disableCookies']);
}
}());
_ppas.push(["trackPageView"]);
_ppas.push(["enableLinkTracking"]);
(function() {
var u = "https://explore-esch.piwik.pro/";
_ppas.push(["setTrackerUrl", u + "ppms.php"]);
_ppas.push(["setSiteId", "2c481c7b-7c0e-4a3e-ba23-5233f3af713a"]);
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 + "ppas.js";
s.parentNode.insertBefore(g, s);
})();
</script>
- Create an additional custom code tag that handles the deanonymization right after the consent is granted. Trigger: All pages. Consent type: Analytics:
<script>
var _ppas = window._ppas || [];
(function () {
var consents = {{ Consents }};
var consentType = 'analytics';
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(['enableCookies']);
_ppas.push(['deanonymizeUser']);
}
}());
</script>
- Revert the Matomo tag to the original code that you used before (the one with
_paq.push
commands).