mirror of
https://github.com/kremalicious/gatsby-plugin-matomo.git
synced 2024-12-22 09:13:19 +01:00
Merge pull request #129 from qdhayana/dnt-option
feat: add respectDnt option
This commit is contained in:
commit
07b850fe84
@ -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
|
||||
|
@ -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/)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -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']);" : ''}
|
||||
|
Loading…
Reference in New Issue
Block a user