1
0
mirror of https://github.com/kremalicious/gatsby-plugin-matomo.git synced 2024-12-22 17:23:23 +01:00

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. `siteId` | Your Matomo site ID configured in your Matomo installation.
`matomoUrl` | The url of 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. `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']);`. `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. `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. `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 ## Development
```bash ```bash

View File

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