Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Make load tracking configurable #25

Merged
merged 1 commit into from
Apr 6, 2020
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = () => {
Expand All @@ -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}`)
Expand Down