Tag Manager in Nuxt Application?

Hello,

does anybody know how to integrate matomo tag manager into a nuxt project/application?

The module nuxt-matomo (https://github.com/pimlie/nuxt-matomo) is just for tracking code and not for tag manager.

Has anyone experience with this?

Best regards,

Timo

Hello,

No, sorry, but it seems very similar to Angular and React no? So I guess it will be all about tweaking the template to add a datalayer event when a new page is generated no?
So it is more about discussing with a Nuxt developed to make her/him add the line of code when the new page is loaded and finished, no?

The question is about how to integrate the tag manager code the right way and how to handle route changes (and also dynamic page titles) to get the right data to the tag manager.

I work on a solution but it’s very hard because I have no experience with this. So my question if heres somebody with some experiences to share.

@iparker I am looking into the same task. Did you come up with a solution?

Not in production for now, but it works pretty well so far: in my Matomo container I have a tag (e.g. Matomo Analytics) with two triggers: ‘Pageview’ - for the initial page request on the server (SSR/static site) and ‘History change’ for all further requests when nuxt switches to SPA mode.

In nuxt I have a plugin (plugins/matomo.client.js):

/* eslint-disable */

export default () => {
  // manually add the script to the DOM
  const script = document.createElement('script')

  script.innerHTML = `
    <!-- Your Matomo Tag Manager Code -->
  `

  document.head.appendChild(script)
}

Then add the plugin to nuxt.config.js.

I think a better and more elegant way would be to write something that triggers _mtm.push({‘event’: ‘mtm.PageView’}); everytime a route changes, but for now I’m happy to have something.

1 Like

I have a version here that you can use with Matomo tag manager. The container tag fires both on page load and on virtual navigation via nuxt’s routing.

npm i @agency-undone/nuxt-module-matomo --save

1 Like

Nuxt 3 very recently added an option to append scripts to the body with UseHead. There’s an example in the official Nuxt 3 documentation at https://v3.nuxtjs.org/guide/features/head-management/

I added this to my AppLayout.vue component

<script setup>
useHead({
  script: [
    {
      src: '/scripts/your-filename.js',
      body: true
    }
  ]
})
</script>

Then I placed the tagmanager code in a .js file in the public folder (I know, that’s not great, but it works).