Implement Matomo tracker code
It is possible to implement the Matomo tracker code in different ways. Whitespace standard implementation, if the client don't have any prefrencies, is to use Matomo wihtout cookies if the user deny cookies and Matomo with cookies if the user accept cookies.
Here follows instructions about how we want to implement the Matomo tracking code and which setup to use in Matomo Tag Manager
Setup in Matomo Tag Manager
- Edit the variable Matomo konfiguration.
- Scroll down and disable all cookies.
- Save the variable and publish a new version of the container.
Adjust the Matomo tracker code
This is the code we should use if the client have a cookie consent solution on their website:
<script>
var _paq = (window._paq = window._paq || []);
var _mtm = (window._mtm = window._mtm || []);
var wsGetCookie = function (name) {
var v = document.cookie.match("(^|;) ?" + name + "=([^;]*)(;|$)");
return v ? v[2] : null;
};
var wsSetCookie = function (name, value, days) {
var d = new Date();
d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
};
var matomo = function () {
_mtm.push({ "mtm.startTime": new Date().getTime(), event: "mtm.Start" });
var d = document,
g = d.createElement("script"),
s = d.getElementsByTagName("script")[0];
g.type = "text/javascript";
g.async = true;
g.src = "https://matomo.uppsala.se/js/container_G8XG3tBl.js";
s.parentNode.insertBefore(g, s);
};
//Add the specifc code to handle if the user accept or reject cookies.
matomo();
</script>