From fd6f427fb483c9e4de192fe21f95a60580083cfe Mon Sep 17 00:00:00 2001 From: Jarne Date: Sun, 23 May 2021 11:06:07 +0200 Subject: [PATCH] Add cookie consent option (#102) * Add cookie consent option * Add unit testing for cookie consent option Co-authored-by: Jarne --- README.md | 2 ++ src/__tests__/gatsby-ssr.js | 8 ++++++++ src/gatsby-ssr.js | 2 ++ 3 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 337dbe5..eebf49d 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ _NOTE: By default, this plugin only generates output when run in production mode | `matomoJsScript` | (optional) The name of your Matomo JS script. Defaults to `piwik.js` | | `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']);`. | +| `requireCookieConsent` | (optional) If true, no cookies will be stored or used until you call `window._paq.push(['setCookieConsentGiven']);`. | | `disableCookies` | (optional) If true, no cookie will be used by Matomo. | | `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`. | @@ -94,6 +95,7 @@ plugins: [ matomoJsScript: 'piwik.js', exclude: ['/offline-plugin-app-shell-fallback/'], requireConsent: false, + requireCookieConsent: false, disableCookies: false, cookieDomain: '*.example.org', localScript: '/piwik.js', diff --git a/src/__tests__/gatsby-ssr.js b/src/__tests__/gatsby-ssr.js index d9b577b..efb016e 100644 --- a/src/__tests__/gatsby-ssr.js +++ b/src/__tests__/gatsby-ssr.js @@ -77,6 +77,14 @@ describe('gatsby-plugin-google-analytics', () => { expect(result).toMatch(/requireConsent/) }) + it('sets requireCookieConsent', () => { + const { setPostBodyComponents } = setup({ + requireCookieConsent: true + }) + const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) + expect(result).toMatch(/requireCookieConsent/) + }) + it('sets disableCookies', () => { const { setPostBodyComponents } = setup({ disableCookies: true diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js index 61e57bb..048a750 100644 --- a/src/gatsby-ssr.js +++ b/src/gatsby-ssr.js @@ -10,6 +10,7 @@ function buildTrackingCode(pluginOptions) { dev, localScript, requireConsent, + requireCookieConsent, disableCookies, cookieDomain, respectDnt = true @@ -26,6 +27,7 @@ function buildTrackingCode(pluginOptions) { if (window.dev === true || ${dntCondition}) { window._paq = window._paq || []; ${requireConsent ? "window._paq.push(['requireConsent']);" : ''} + ${requireCookieConsent ? "window._paq.push(['requireCookieConsent']);" : ''} ${disableCookies ? "window._paq.push(['disableCookies']);" : ''} ${ cookieDomain