Add cookie consent option (#102)

* Add cookie consent option

* Add unit testing for cookie consent option

Co-authored-by: Jarne <jarne@users.noreply.github.com>
This commit is contained in:
Jarne 2021-05-23 11:06:07 +02:00 committed by GitHub
parent 07b850fe84
commit fd6f427fb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

View File

@ -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',

View File

@ -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

View File

@ -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