From 95c4730168ad0e4812acdfa0d53ae1e0d6b26988 Mon Sep 17 00:00:00 2001 From: Revath S Kumar Date: Sat, 12 Dec 2020 16:50:15 +0530 Subject: [PATCH] feat: add respectDnt option --- README.md | 1 + src/__tests__/gatsby-ssr.js | 8 ++++++++ src/gatsby-ssr.js | 9 +++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ff3088b..337dbe5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/__tests__/gatsby-ssr.js b/src/__tests__/gatsby-ssr.js index fc55bec..d9b577b 100644 --- a/src/__tests__/gatsby-ssr.js +++ b/src/__tests__/gatsby-ssr.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/) + }) }) }) }) diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js index 8f4e059..61e57bb 100644 --- a/src/gatsby-ssr.js +++ b/src/gatsby-ssr.js @@ -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']);" : ''}