Make load tracking configurable

This commit is contained in:
Arne Zeising 2020-04-04 17:30:35 +02:00
parent 7d0e1d8094
commit 5f610910b9
2 changed files with 10 additions and 2 deletions

View File

@ -77,6 +77,7 @@ _NOTE: By default, this plugin only generates output when run in production mode
| `disableCookies` | (optional) If true, no cookie will be used by Matomo. |
| `cookieDomain` | (optional) Specify cookie domain. |
| `localScript` | (optional) If set, load local `piwik.js` script from the given path, instead of loading it from your `matomoUrl`. |
| `trackLoad` | (optional) If true, it will track the loading of the matomo library. Defaults to `true`. |
| `dev` | (optional) Activate dev mode by setting to `true`. Will load all scripts despite not running in `production` environment. Ignores your local browser's DNT header too. Outputs some information in console about what it is doing. Useful for local testing but careful: all hits will be send like in production. |
```js

View File

@ -12,7 +12,7 @@ function getDuration() {
return difference
}
export const onRouteUpdate = ({ location, prevLocation }) => {
export const onRouteUpdate = ({ location, prevLocation }, pluginOptions) => {
if (process.env.NODE_ENV === 'production' || window.dev === true) {
if (!window._paq) return
@ -22,6 +22,10 @@ export const onRouteUpdate = ({ location, prevLocation }) => {
prevLocation &&
prevLocation.pathname + prevLocation.search + prevLocation.hash
const {
trackLoad = true
} = pluginOptions
// document.title workaround stolen from:
// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
const sendPageView = () => {
@ -45,7 +49,10 @@ export const onRouteUpdate = ({ location, prevLocation }) => {
if (first) {
first = false
_paq.push(['trackEvent', 'javascript', 'load', 'duration', getDuration()])
if (trackLoad) {
_paq.push(['trackEvent', 'javascript', 'load', 'duration', getDuration()])
}
if (dev) {
console.debug(`[Matomo] Tracking duration for: ${url}`)