vue-matomo icon indicating copy to clipboard operation
vue-matomo copied to clipboard

How to make it work with Nuxt 3?

Open maxbellec opened this issue 3 years ago • 14 comments

Hello, Sorry if this question seems dumb, but I'm having a hard time getting this to work. As documented, I have my plugin in the Nuxt plugin folder.

analytics.client.ts

import VueMatomo from "vue-matomo"
import { defineNuxtPlugin } from "#app"

export default defineNuxtPlugin((nuxtApp) => {
  console.log("### analytics", nuxtApp.vueApp, VueMatomo)
  nuxtApp.vueApp.use(VueMatomo, {
    debug: true,
    host: "xxx",
    siteId: 1,
    requireConsent: false,
    // router: nuxtApp.router,
    trackInitialView: true,
  })
})

My console.log is printed to the console, so the vueApp.use(VueMatomo, ...) should be run, but nothing else happens. I had hoped the with debug: true I would have some info that matomo is starting or having issue, but I don't get anything. My Chrome plugins are disabled, and I don't have any other warning / error in the console.

Since this plugin works with Vue 3, I don't see any reason that Nuxt 3 would get in the way. I am on Nuxt 3.0.0-27420403.16e2a54 and Vue 3.2.31.

maxbellec avatar Sep 06 '22 08:09 maxbellec

Hi @maxbellec ,

import { defineNuxtPlugin } from '#app'
import VueMatomo from 'vue-matomo'

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(VueMatomo, {
    host: 'https://myhost.io',
    siteId: 2,
    // Enables automatically registering pageviews on the router
    router: nuxtApp.$router,
    enableLinkTracking: true,
    requireConsent: false,
    trackInitialView: true,
    disableCookies: true,
    requireCookieConsent: false,
  })
})

Works for me without any problems. U should consider to upgrade to the latest Nuxt RC too, because you are still in beta right?

markus-gx avatar Sep 21 '22 13:09 markus-gx

Thanks, but how to access $matomo in a component? Following is always "undefined".

const { $matomo } = useNuxtApp()

thorau avatar Jan 30 '23 14:01 thorau

@thorau Have you found a solution for it ?

makerovski avatar Feb 06 '23 12:02 makerovski

Unfortunately it doesn't work somehow. Debug window doesn't open either when you write debug: true. And in Matomo I don't see any activity

vincent41 avatar Mar 05 '23 16:03 vincent41

Hi @vincent41

I managed to make it work using Matomo as Nuxt plugin.

I have it in the plugins folder (will be automatic initialized) as matomo.client.ts

i use this configuration:

import VueMatomo from 'vue-matomo'
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.vueApp.use(VueMatomo, {
    
    router: nuxtApp.$router,
    host: 'yourHost.com',
    siteId: yourSiteID,
    enableLinkTracking: true,
    requireConsent: false,
    trackInitialView: true,
    disableCookies: false,
    requireCookieConsent: false,
    enableHeartBeatTimer: true,
    heartBeatTimerInterval: 5,
    trackerFileName: 'piwik',
    trackerUrl: 'yourUrl.php',
    trackerScriptUrl: 'yourUrl/matomo.js',
    userId: '',
    preInitActions: [
      ['setCustomVariable', '1', 'VisitorType', 'Member'],
      ['appendToTrackingUrl', 'new_visit=1']
    ]
  })
})

No need to make any other changes, and this is enough for what I need, maybe it helps you to get it working.

Can you send your actual code ? Would help to understand your problem better

makerovski avatar Mar 06 '23 08:03 makerovski

Hello @makerovski,

Thanks for your answer, something is definitely happening. But it's not like I would normally include the script. something is different for example,if I set debug: true and in the url https://domain.de/?mtmPreviewMode=1XybHsKe I don't see a preview window for it. Is it normal?

vincent41 avatar Mar 07 '23 07:03 vincent41

Hello @vincent41, May I see your code ?

makerovski avatar Mar 07 '23 15:03 makerovski

Hi @vincent41

I managed to make it work using Matomo as Nuxt plugin.

I have it in the plugins folder (will be automatic initialized) as matomo.client.ts

i use this configuration:

import VueMatomo from 'vue-matomo'
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.vueApp.use(VueMatomo, {
    
    router: nuxtApp.$router,
    host: 'yourHost.com',
    siteId: yourSiteID,
    enableLinkTracking: true,
    requireConsent: false,
    trackInitialView: true,
    disableCookies: false,
    requireCookieConsent: false,
    enableHeartBeatTimer: true,
    heartBeatTimerInterval: 5,
    trackerFileName: 'piwik',
    trackerUrl: 'yourUrl.php',
    trackerScriptUrl: 'yourUrl/matomo.js',
    userId: '',
    preInitActions: [
      ['setCustomVariable', '1', 'VisitorType', 'Member'],
      ['appendToTrackingUrl', 'new_visit=1']
    ]
  })
})

No need to make any other changes, and this is enough for what I need, maybe it helps you to get it working.

Can you send your actual code ? Would help to understand your problem better

Thanks for that!

BUT i get a new hit every time i click on a menu item as it would be a new person. I deleted the preInitActions and now it works. One User - one session - multiple pages

maxleistner avatar Mar 13 '23 11:03 maxleistner

Hi @vincent41

I managed to make it work using Matomo as Nuxt plugin.

I have it in the plugins folder (will be automatic initialized) as matomo.client.ts

i use this configuration:

import VueMatomo from 'vue-matomo'
import { defineNuxtPlugin } from '#app'

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.vueApp.use(VueMatomo, {
    
    router: nuxtApp.$router,
    host: 'yourHost.com',
    siteId: yourSiteID,
    enableLinkTracking: true,
    requireConsent: false,
    trackInitialView: true,
    disableCookies: false,
    requireCookieConsent: false,
    enableHeartBeatTimer: true,
    heartBeatTimerInterval: 5,
    trackerFileName: 'piwik',
    trackerUrl: 'yourUrl.php',
    trackerScriptUrl: 'yourUrl/matomo.js',
    userId: '',
    preInitActions: [
      ['setCustomVariable', '1', 'VisitorType', 'Member'],
      ['appendToTrackingUrl', 'new_visit=1']
    ]
  })
})

No need to make any other changes, and this is enough for what I need, maybe it helps you to get it working.

Can you send your actual code ? Would help to understand your problem better

Hello @maxbellec

How do you access to $matomo with your configuration ? I try const { $matomo } = useNuxtApp() in <script setup> but it's always undefined... I need it to call trackSiteSearch and push events

Do you have samples ?

Thank's.

WithAlex avatar Mar 17 '23 16:03 WithAlex

Remember that its nuxtApp.$router and not nuxtApp.router

rnenjoy avatar Jul 06 '23 15:07 rnenjoy

Hi guys, I followed this tutorial to make vue-matomo work in my nuxt3 project. However, it can't get the page title from the meta as it's not rendered yet when the route changes.

I wonder if there is a way to send the event to matomo API sometimes after the route change or listen on the meta change event like what this library does? https://github.com/pimlie/nuxt-matomo?tab=readme-ov-file#caveats

phoenixgao avatar Dec 16 '23 12:12 phoenixgao

Hi Guys,

I Managed it using the thid code, with that I can change some Matomo properties

const trackSearch = () => {
  window._paq.push(["trackEvent", "searched:", `${props.modelValue}`]);
};

It shows an error but it works

makerovski avatar Jan 03 '24 06:01 makerovski