From 1a8bb762cb9304c0e0d48af38b4a0c6c3fa480cb Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Thu, 24 May 2018 17:01:17 -0400 Subject: [PATCH] Supporting "Do Not Track" for Gogle Analytics and Sentry error reporting --- src/setup/analytics.js | 10 ++++++---- src/setup/analytics.test.js | 20 ++++++++++++++++++++ src/setup/raven.js | 14 ++++++++------ src/setup/raven.test.js | 15 +++++++++++++++ 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/setup/analytics.js b/src/setup/analytics.js index 7669152..4a3940c 100644 --- a/src/setup/analytics.js +++ b/src/setup/analytics.js @@ -1,10 +1,12 @@ import ReactGA from 'react-ga'; const setupAnalytics = () => { - ReactGA.initialize(process.env.GA_PROPERTY, { - debug: (process.env.NODE_ENV !== 'production') - }); - ReactGA.pageview(document.location.pathname); + if (navigator.doNotTrack !== '1' && window.doNotTrack !== '1') { + ReactGA.initialize(process.env.GA_PROPERTY, { + debug: (process.env.NODE_ENV !== 'production') + }); + ReactGA.pageview(document.location.pathname); + } }; export default setupAnalytics; diff --git a/src/setup/analytics.test.js b/src/setup/analytics.test.js index 629a158..48ce61b 100644 --- a/src/setup/analytics.test.js +++ b/src/setup/analytics.test.js @@ -34,4 +34,24 @@ describe('setupAnalytics', () => { setupAnalytics(); expect(ReactGA.pageview).toHaveBeenCalled(); }); + + describe('when "Do Not Track" is set', () => { + beforeEach(() => { + navigator.doNotTrack = '1'; + }); + + afterEach(() => { + navigator.doNotTrack = undefined; + }); + + it('does not initialize', () => { + setupAnalytics(); + expect(ReactGA.initialize).not.toHaveBeenCalled(); + }); + + it('does not trigger a pageview', () => { + setupAnalytics(); + expect(ReactGA.pageview).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/setup/raven.js b/src/setup/raven.js index 2c74d4e..19d9132 100644 --- a/src/setup/raven.js +++ b/src/setup/raven.js @@ -1,12 +1,14 @@ import Raven from 'raven-js'; const setupRaven = () => { - Raven.config(process.env.SENTRY_KEY, { - whitelistUrls: [/https:\/\/(.*\.)?regexper\.com/], - environment: process.env.DEPLOY_ENV, - debug: (process.env.NODE_ENV !== 'production'), - release: process.env.BUILD_ID - }); + if (navigator.doNotTrack !== '1' && window.doNotTrack !== '1') { + Raven.config(process.env.SENTRY_KEY, { + whitelistUrls: [/https:\/\/(.*\.)?regexper\.com/], + environment: process.env.DEPLOY_ENV, + debug: (process.env.NODE_ENV !== 'production'), + release: process.env.BUILD_ID + }); + } }; export default setupRaven; diff --git a/src/setup/raven.test.js b/src/setup/raven.test.js index e63fcb6..0bf573a 100644 --- a/src/setup/raven.test.js +++ b/src/setup/raven.test.js @@ -45,4 +45,19 @@ describe('setupRaven', () => { release: 'test ID' })); }); + + describe('when "Do Not Track" is set', () => { + beforeEach(() => { + navigator.doNotTrack = '1'; + }); + + afterEach(() => { + navigator.doNotTrack = undefined; + }); + + it('does not intialize', () => { + setupRaven(); + expect(Raven.config).not.toHaveBeenCalled(); + }); + }); });