I was able to add Matomo Tag Manager to my Nuxt Web App and combine it with nuxt-cookie-control by using vue-matomo. I’ve tried it only on development mode. I hope it can help somebody.
First I installed vue-matomo and created a plugin:
import Vue from 'vue'
import VueMatomo from 'vue-matomo'
export default ({ app }) => {
Vue.use(VueMatomo, {
router: app.router,
host: 'http://localhost/matomo',
siteId: 1,
trackInitialView: true,
requireConsent: true,
requireCookieConsent: true,
})
}
I added it into plugins in the nuxt.config.js file:
plugins: [
// ... ,
{ src: "~/plugins/vue-matomo.js", ssr: false }
],
In the nuxt.config.js file I already had nuxt-cookie-control installed in my modules:
modules: [
// ... ,
"nuxt-cookie-control"
],
Then I configured the nuxt-cooke-control options:
cookies: {
// ... ,
necessary: [
// ...
],
optional: [
{
name: "Matomo",
identifier: "matomo",
description: "Matomo Analytics",
initialState: false,
async: true,
src: "http://localhost/matomo/js/container_XXXXX.js", // the address to your container js-file, you can find it in Matomo under 'MyContainer > install code'
accepted: () => {
window._mtm = window._mtm || [];
window._paq = window._paq || [];
window._mtm.push({
"mtm.startTime": new Date().getTime(),
event: "mtm.Start",
});
window._paq.push(["rememberConsentGiven"]);
window._paq.push(["rememberCookieConsentGiven"]);
},
declined: () => {
window._paq = window._paq || [];
window._paq.push(["forgetConsentGiven"]);
window._paq.push(["forgetCookieConsentGiven"]);
window.$nuxt.$cookies.remove("matomo");
},
},
]
}