From 8aa48895ca3075bfbaeff8b6f91081e022b8e3c7 Mon Sep 17 00:00:00 2001 From: Robin Palotai Date: Fri, 12 Jan 2024 18:16:18 +0100 Subject: [PATCH] Add options to transform urls before sending. --- README.md | 15 +++++++++++++++ src/index.js | 23 +++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1859441..43954df 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,21 @@ Vue.use(VueMatomo, { // Set this to include crossorigin attribute on the matomo script import // Default: undefined, possible values : 'anonymous', 'use-credentials' crossOrigin: undefined, + + // Set this to a function that takes the referrer url and transforms it + // before sending to Matomo. + // + // For example: + // + // (u) => { return u.split('#')[0]; } + // + referrerTransform: undefined, + + // Set this to a function that takes the origin url and transforms it + // before sending to Matomo. + // + // See referrerTransform for example. + urlTransform: undefined, }); // Now you can access piwik api in components through diff --git a/src/index.js b/src/index.js index 255f399..3a86704 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,9 @@ const defaultOptions = { cookieDomain: undefined, domains: undefined, preInitActions: [], - crossOrigin: undefined + crossOrigin: undefined, + referrerTransform: undefined, + urlTransform: undefined } export const matomoKey = 'Matomo' @@ -60,14 +62,27 @@ function trackMatomoPageView (options, to, from) { } options.debug && console.debug('[vue-matomo] Tracking ' + url) - title = to.meta.title || url + title = to.meta.title || (typeof options.urlTransform === 'function' + ? options.urlTransform(url) : url) } if (referrerUrl) { - Matomo.setReferrerUrl(window.location.origin + referrerUrl) + let u = window.location.origin + referrerUrl + if (typeof options.referrerTransform === 'function') { + u = options.referrerTransform(u, referrerUrl) + } + Matomo.setReferrerUrl(u) } + if (url) { - Matomo.setCustomUrl(window.location.origin + url) + let u = window.location.origin + url + if (typeof options.urlTransform === 'function') { + u = options.urlTransform(u, url) + } + Matomo.setCustomUrl(u) + } else if (typeof options.urlTransform === 'function') { + const u = options.urlTransform(window.location.href) + Matomo.setCustomUrl(u) } Matomo.trackPageView(title)