feat: add respectDnt option

This commit is contained in:
Revath S Kumar 2020-12-12 16:50:15 +05:30
parent 403092eff7
commit 95c4730168
No known key found for this signature in database
GPG Key ID: 5938AD6CCB200939
3 changed files with 16 additions and 2 deletions

View File

@ -78,6 +78,7 @@ _NOTE: By default, this plugin only generates output when run in production mode
| `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`. |
| `respectDnt` | (optional) If false, will load all scripts without respecting user preference to `Do Not Track` on browsers. 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

@ -92,6 +92,14 @@ describe('gatsby-plugin-google-analytics', () => {
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).toMatch(/TEST_LOCAL_SCRIPT/)
})
it('sets respectDnt to false', () => {
const { setPostBodyComponents } = setup({
respectDnt: false
})
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0])
expect(result).not.toMatch(/navigator.doNotTrack/)
})
})
})
})

View File

@ -11,14 +11,19 @@ function buildTrackingCode(pluginOptions) {
localScript,
requireConsent,
disableCookies,
cookieDomain
cookieDomain,
respectDnt = true
} = pluginOptions
const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`
const dntCondition = respectDnt
? `!(navigator.doNotTrack === '1' || window.doNotTrack === '1')`
: `true`
const html = `
window.dev = ${dev}
if (window.dev === true || !(navigator.doNotTrack === '1' || window.doNotTrack === '1')) {
if (window.dev === true || ${dntCondition}) {
window._paq = window._paq || [];
${requireConsent ? "window._paq.push(['requireConsent']);" : ''}
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}