allow exclusion of paths

This commit is contained in:
Matthias Kretschmann 2018-12-05 20:33:18 +01:00
parent 0a5cc27f63
commit 9464d47ae1
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 40 additions and 7 deletions

View File

@ -57,11 +57,31 @@ Option | Explanation
`siteId` | Your Matomo site ID configured in your Matomo installation.
`matomoUrl` | The url of your Matomo installation.
`siteUrl` | The url of your site, usually the same as `siteMetadata.siteUrl`. Only used for generating the url for `noscript` image tracking fallback.
`exclude` | (optional) Specify an array of pathnames where tracking code will be excluded. The pathname `/offline-plugin-app-shell-fallback/` is excluded by default.
`requireConsent` | (optional) If true, tracking will be disabled until you call `window._paq.push(['setConsentGiven']);`.
`disableCookies` | (optional) If true, no cookie will be used by Matomo.
`localScript` | (optional) Set path to load local `piwik.js` script, instead of loading it from your `matomoUrl`.
`localScript` | (optional) If set, load local `piwik.js` script from the given path, instead of loading it from your `matomoUrl`.
`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
plugins: [
{
resolve: 'gatsby-plugin-matomo',
options: {
siteId: 'YOUR_SITE_ID',
matomoUrl: 'https://YOUR_MATOMO_URL.COM',
siteUrl: 'https://YOUR_LIVE_SITE_URL.COM'
// All the optional settings
exclude: ['/offline-plugin-app-shell-fallback/'],
requireConsent: false,
disableCookies: false,
localScript: '/piwik.js',
dev: false
}
}
]
```
## Development
```bash

View File

@ -7,7 +7,7 @@ function buildTrackingCode(pluginOptions) {
const html = `
window.dev = ${pluginOptions.dev}
if (window.dev === true || !(navigator.doNotTrack == '1' || window.doNotTrack == '1')) {
if (window.dev === true || !(navigator.doNotTrack === '1' || window.doNotTrack === '1')) {
window._paq = window._paq || [];
${pluginOptions.requireConsent ? 'window._paq.push([\'requireConsent\']);' : ''}
${pluginOptions.disableCookies ? 'window._paq.push([\'disableCookies\']);' : ''}
@ -50,11 +50,24 @@ function buildTrackingCodeNoJs(pluginOptions, pathname) {
}
exports.onRenderBody = ({ setPostBodyComponents, pathname }, pluginOptions) => {
if (process.env.NODE_ENV === 'production' || pluginOptions.dev === true) {
return setPostBodyComponents([
buildTrackingCode(pluginOptions),
buildTrackingCodeNoJs(pluginOptions, pathname)
])
let excludePaths = ['/offline-plugin-app-shell-fallback/']
if (typeof pluginOptions.exclude !== 'undefined') {
pluginOptions.exclude.map(exclude => {
excludePaths.push(exclude)
})
}
const isPathExcluded = excludePaths.some(path => pathname === path)
if (
(process.env.NODE_ENV === 'production' || pluginOptions.dev === true) &&
!isPathExcluded
) {
return setPostBodyComponents([
buildTrackingCode(pluginOptions),
buildTrackingCodeNoJs(pluginOptions, pathname)
])
}
return null
}