Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to transform urls before sending. #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const defaultOptions = {
cookieDomain: undefined,
domains: undefined,
preInitActions: [],
crossOrigin: undefined
crossOrigin: undefined,
referrerTransform: undefined,
urlTransform: undefined
}

export const matomoKey = 'Matomo'
Expand Down Expand Up @@ -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)
Expand Down