A Vue plugin exposing common tracking methods for piveau web apps using Matomo or Piwik Pro.
npm i @piveau/piveau-universal-piwik
for latest builds:
npm i @piveau/piveau-universal-piwik@beta
Setup the plugin in main.js
. This will add $piwik
to Vue
to be accessible as app.prototype.$Vue
or this.$piwik
in all Vue components.
Refer to the demo for an example on how to use this package.
Common usage could look like this:
// In main.ts
import UniversalPiwik from '@piveau/piveau-universal-piwik';
import { router } from './router'; // Optional
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App);
app.use(UniversalPiwik, {
router, // Optional
isPiwikPro: true
trackerUrl: 'http://piwik-pro-host.example.org/',
siteId: 'your-site-id',
debug: import.meta.env.NODE_ENV === 'development',
});
<template>
<div id="app">
<div id="cookies">
Change consent:
<div class="flexy consent-action">
<button class="btn" @click="accept">Accept</button>
<button class="btn" @click="decline">Decline</button>
<button class="btn" @click="postpone">Postpone</button>
</div>
</div>
<div id="control-panel">
<h2>Simulate interactions</h2>
<div class="flexy">
<button class="btn" @click="external">External link</button>
<button class="btn" @click="download">Download</button>
</div>
</div>
<div id="nav">
<router-link to="/home">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view/>
</div>
</template>
<script>
export default {
name: 'App',
created() {
this.$piwik.init();
},
methods: {
accept() {
this.$piwik.consentGiven();
},
decline() {
this.$piwik.consentDeclined();
},
postpone() {
this.$piwik.consentNoDecision();
},
external() {
this.$piwik.trackOutlink('https://example.org/outlink');
},
download() {
this.$piwik.trackDownload('https://example.org/download');
}
}
}
</script>
options = {
router: VueRouter,
isPiwikPro: true,
trackerUrl: 'http://piwik-pro-host.example.org/',
siteId: 'your-site-id',
debug: import.meta.env.NODE_ENV === 'development',
immediate: false,
removeCookiesWhenNoConsent: true,
stopWhenNoConsent: true,
verbose: false,
pageViewOptions: { onlyTrackWithLocale: true, delay: 1000, beforeTrackPageView: (tracker, to, from) => {} },
disabled: false,
requireConsent: 'consent',
}
- type:
VueRouter
when specified, uses default router hooks in order to track page changes. Set it to undefined
when it is desired to handle page views manually.
- type:
boolean
- default:
false
if true
, uses Piwik Pro. If false
, uses Matomo.
- type:
string
Tracker url for Matomo or Piwik Pro
- type:
string
Tracker site id for Matomo or Piwik Pro
- type:
Object<{ useDatasetsMinScoreFix: boolean, onlyTrackWithLocale: boolean, delay: number, beforeTrackPageView(tracker, to, from): Function, documentTitleResolver(to, from, title) }>
Options for the default page view tracking implementation. Set onlyTrackWithLocale
to true to only track when there is a locale
query in the destination route when navigating. This avoids duplicated page views for piveau applications. Set useDatasetsMinScoreFix
to true
to only track pages named 'Dataset' when there is a minScore
query.
Provide beforeTrackPageView
hook to do tasks before a page is being tracked but after a navigation is registered as valid.
Provide documentTitleResolver
to override the default document title resolver. If not given, use document.title
as document title.
Set delay
for Piwik Pro tracker for delayed page view tracking after a navigation has been confirmed in milliseconds. This avoids undefined
page titles for piveau applications.
- type:
boolean
- default:
false
If true, enables debug logging on console. Useful for development.
- type:
boolean
- default:
false
If true
and debug
is true
, additional logging on console.
- type:
boolean
- default:
false
if true
, executes piwik init script immediately.
- type:
boolean
- default:
true
(PiwikPro only) if true
, removes all PiwikPro related cookies when user does not give consent (called by $piwik.declineConsent()
)
- type:
boolean
- default:
true
(PiwikPro only) if true
, stops tracking when uder does not give consent (called by $piwik.declineConsent()
)
- type:
boolean
- default:
false
If true
, disables all tracker instance methods (the tracker instance still exists). Cannot be changed after initialization.
- type:
string
- default:
'consent'
If 'consent'
, requires user consent to track using using requireConsent
. If 'cookieConsent'
, requires user consent to set cookies using requireCookieConsent
. If falsy, does not require consent.
- type:
boolean
- default:
false
(PiwikPro only) If true
, enables suspendFilter
and resume
methods.
- type:
boolean
if true
, indicates that $piwik is stopped.
- Signature:
$piwik.init()
Initializes Tracker script. Note that this may set cookies and execute external $piwik.JavaScript code.
- Signatures:
$piwik.consentGiven()
$piwik.consentDeclined()
$piwik.consentNoDecision()
Accepts, declines, and postpones cookies for tracking
- Signatures:
$piwik.trackPageView(url, title)
$piwik.trackDatasetDetailsPageView()
$piwik.trackDownload(url, dimensions)
$piwik.trackOutlink(url, dimensions)
$piwik.trackEvent(category, action, name, value)
$piwik.trackGotoResource()
- Signature:
$piwik.trackInteraction(eventType = 'screen_load', variables = {})
(Piwik Pro tracker only) track interaction with specific event type and variables.
- Signature:
$piwik.stop()
- Signature:
$piwik.suspendFilter(filterFn: (data: Object) => boolean)
(Piwik Pro tracker only) Suspends PiwikPro by intercepting all incoming events.
If filterFn
is given, will not intercept incoming event, if filterFn(event) === true
- Signature:
$piwik.resume()
(Piwik Pro tracker only) Resumes PiwikPro by emitting all events that were emitted during suspension.
Calls callbackFn()
when a valid route is entered and is going to track a page view.
Only works when router
option is set on initialization.
- Signature:
$universalPiwik.beforeTrackPageView(callbackFn)
-
callbackFn
Signature:
callbackFn(to: Route, from: Route)
npm install
npm run build:lib
npm run lint