From 8c9c533943963380b758ee5d269b9c77d6c77da4 Mon Sep 17 00:00:00 2001 From: Govert Brinkmann Date: Mon, 22 Apr 2024 11:59:24 +0000 Subject: [PATCH 1/2] Disable link tracking Doesn't seem to work with router links: https://github.com/AmazingDreams/vue-matomo/blob/52f6a8dc3bc333b1218dd1b51295a10f544a28bf/demo/vue-3/src/main.js#L18 --- public/matomo-tracking-code.js | 1 - 1 file changed, 1 deletion(-) diff --git a/public/matomo-tracking-code.js b/public/matomo-tracking-code.js index f83abc85..aa76461d 100644 --- a/public/matomo-tracking-code.js +++ b/public/matomo-tracking-code.js @@ -3,7 +3,6 @@ var _paq = (window._paq = window._paq || []) /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['disableCookies']) _paq.push(['trackPageView']) -_paq.push(['enableLinkTracking']) ;(function () { var u = 'https://clariah-mediasuite.innocraft.cloud/' _paq.push(['setTrackerUrl', u + 'matomo.php']) From 77580a690b55234b7dc61c2f4c443c4bd13c5f83 Mon Sep 17 00:00:00 2001 From: Govert Brinkmann Date: Mon, 22 Apr 2024 14:29:09 +0000 Subject: [PATCH 2/2] Track navigation after initial load Vue Router seems to be most suited, since Nuxt middleware can only run before navigation. https://github.com/vuejs/router/blob/960662db6497011b51a5dec729befd26fcee305a/packages/docs/guide/advanced/navigation-guards.md#global-after-hooks --- plugins/matomo.client.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 plugins/matomo.client.ts diff --git a/plugins/matomo.client.ts b/plugins/matomo.client.ts new file mode 100644 index 00000000..9b723d02 --- /dev/null +++ b/plugins/matomo.client.ts @@ -0,0 +1,18 @@ +export default defineNuxtPlugin(() => { + // https://router.vuejs.org/guide/advanced/navigation-guards.html#Global-After-Hooks + const router = useRouter() + router.afterEach((to, from, failure) => { + if (to.path !== from.path && !failure) { + const { origin } = window.location + + // https://developer.matomo.org/guides/spa-tracking + const _paq = window._paq + _paq.push(['deleteCustomVariables', 'page']) + _paq.push(['deleteCustomDimension', 1]) + _paq.push(['setReferrerUrl', origin + from.path]) + _paq.push(['setCustomUrl', origin + to.path]) + _paq.push(['trackPageView']) + console.debug(`trackPageView ${from.path}->${to.path}`) + } + }) +})