A typed framework agnostic wrapper for Matomo js including SPA tracking.
npm install @certible/use-matomo
import { initMatomo } from '@certible/use-matomo';
const matomo = initMatomo({
host: 'https://your-matomo-instance.com',
siteId: 1,
// trackRouter: true, automatically tracks SPA page changes via history tracking
});
// Manually track router changes
// router.afterEach((to, from) => {
// matomo.trackPageView(to.path, opts);
// });
// Track an event
matomo.trackEvent('Category', 'Action', 'Name', 1);
import { initMatomo } from '@certible/use-matomo';
import { createApp } from 'vue';
import App from './App.vue';
const app = createApp(App);
app.use(initMatomo({
host: 'https://your-matomo-instance.com',
siteId: 1,
// trackRouter: true, automatically tracks SPA page changes via history tracking
}));
app.provide('matomo', matomo);
// In your components, you can access Matomo like this:
import { inject } from 'vue';
const matomo = inject<MatomoTracker>('matomo');
// quasar boot file: src/boot/matomo.js
import { defineBoot } from '#q-app/wrappers';
import { initMatomo } from '@certible/use-matomo';
export default defineBoot(({ app }) => {
const matomo = initMatomo({
host: 'https://your-matomo-instance.com',
siteId: 1,
// trackRouter: true, automatically tracks SPA page changes via history tracking
});
app.provide('matomo', matomo);
});
The initMatomo
function accepts a configuration object with the following options:
Option | Type | Description |
---|---|---|
host |
string |
The URL of your Matomo instance (e.g., https://your-matomo-instance.com ) |
siteId |
number | string |
The site ID in your Matomo instance |
Option | Type | Default | Description |
---|---|---|---|
trackerFileName |
string |
'matomo' |
The filename of the tracker script and endpoint |
trackerUrl |
string |
'' |
Custom tracker endpoint URL. If empty, uses {host}/{trackerFileName}.php |
trackerScriptUrl |
string |
'' |
Custom tracker script URL. If empty, uses {host}/{trackerFileName}.js |
trackerScriptDisable |
boolean |
false |
Disable automatic loading of the tracker script (useful if already loaded) |
enableLinkTracking |
boolean |
true |
Enable automatic tracking of outbound links and downloads |
requireConsent |
boolean |
false |
Require user consent before tracking (GDPR compliance) |
userId |
string |
'' |
Set a user ID for tracking across sessions |
disableCookies |
boolean |
false |
window._paq.push(['disableCookies']) |
requireCookieConsent |
boolean |
false |
window._paq.push(['requireCookieConsent']) |
enableHeartBeatTimer |
boolean |
false |
window._paq.push(['enableHeartBeatTimer', options.heartBeatTimerInterval]) |
heartBeatTimerInterval |
number |
15 |
Heartbeat timer interval in seconds |
cookieDomain |
string |
'' |
Set the cookie domain for cross-subdomain tracking, window._paq.push(['setCookieDomain', options.cookieDomain]) |
domains |
string[] |
[] |
List of domains to track as internal (for cross-domain tracking), window._paq.push(['setDomains', options.domains]) |
crossOrigin |
'anonymous' | 'use-credentials' |
undefined |
Cross-origin attribute for the tracker script |
preInitActions |
any[] |
[] |
Array of Matomo commands to execute before initialization |
trackRouter |
boolean |
false |
Automatically track SPA page changes via history API monitoring |
const matomo = initMatomo({
host: 'https://analytics.example.com',
siteId: 1,
trackerFileName: 'piwik', // For older Matomo instances
enableLinkTracking: true,
requireConsent: true, // GDPR compliance
disableCookies: false,
enableHeartBeatTimer: true,
heartBeatTimerInterval: 30,
cookieDomain: '.example.com',
domains: ['*.example.com', 'subdomain.example.com'],
trackRouter: true, // Automatic SPA tracking
preInitActions: [
['setCustomDimension', 1, 'Premium User']
]
});
Once initialized, the Matomo tracker returns an object with the following methods:
Tracks a custom event.
Parameters:
category
(string): The category of the eventaction
(string): The action of the eventname
(string, optional): The name of the eventvalue
(number, optional): The value of the event
Example:
// Track a button click
matomo.trackEvent('Navigation', 'Click', 'Header Logo');
// Track with a value
matomo.trackEvent('Purchase', 'Complete', 'Premium Plan', 99.99);
Tracks a page view in a single-page application.
Parameters:
url
(string, optional): The URL of the page to track. Defaults towindow.location.href
props
(object, optional): Additional properties for the page viewdeleteCustomVariables
(boolean): Whether to delete custom variables. Default:true
documentTitle
(string): Custom document title. Default:document.title
Example:
// Track current page
matomo.trackPageView();
// Track specific URL
matomo.trackPageView('/dashboard');
// Track with custom title
matomo.trackPageView('/profile', {
documentTitle: 'User Profile - MyApp'
});
Sets or resets the user ID for tracking across sessions.
Parameters:
userId
(string | null): The user ID to set, ornull
to reset
Example:
// Set user ID
matomo.setUserId('user123');
// Reset user ID
matomo.setUserId(null);
Pushes any instruction directly to the Matomo tracker. This allows you to use any Matomo tracking method not explicitly exposed by this wrapper.
Parameters:
args
(array): Array of arguments to pass to Matomo
Example:
// Set custom dimension
matomo.push(['setCustomDimension', 1, 'Premium User']);
// Track site search
matomo.push(['trackSiteSearch', 'keyword', 'category', 5]);
// Set custom variable
matomo.push(['setCustomVariable', 1, 'UserType', 'Premium', 'visit']);
For a complete list of available Matomo tracking methods, see the Matomo JavaScript Tracking Guide.
This project is licensed under the MIT License.