Supporting "Do Not Track" for Gogle Analytics and Sentry error reporting

This commit is contained in:
Jeff Avallone 2018-05-24 17:01:17 -04:00
parent 7698901451
commit 1a8bb762cb
4 changed files with 49 additions and 10 deletions

View File

@ -1,10 +1,12 @@
import ReactGA from 'react-ga'; import ReactGA from 'react-ga';
const setupAnalytics = () => { const setupAnalytics = () => {
ReactGA.initialize(process.env.GA_PROPERTY, { if (navigator.doNotTrack !== '1' && window.doNotTrack !== '1') {
debug: (process.env.NODE_ENV !== 'production') ReactGA.initialize(process.env.GA_PROPERTY, {
}); debug: (process.env.NODE_ENV !== 'production')
ReactGA.pageview(document.location.pathname); });
ReactGA.pageview(document.location.pathname);
}
}; };
export default setupAnalytics; export default setupAnalytics;

View File

@ -34,4 +34,24 @@ describe('setupAnalytics', () => {
setupAnalytics(); setupAnalytics();
expect(ReactGA.pageview).toHaveBeenCalled(); 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();
});
});
}); });

View File

@ -1,12 +1,14 @@
import Raven from 'raven-js'; import Raven from 'raven-js';
const setupRaven = () => { const setupRaven = () => {
Raven.config(process.env.SENTRY_KEY, { if (navigator.doNotTrack !== '1' && window.doNotTrack !== '1') {
whitelistUrls: [/https:\/\/(.*\.)?regexper\.com/], Raven.config(process.env.SENTRY_KEY, {
environment: process.env.DEPLOY_ENV, whitelistUrls: [/https:\/\/(.*\.)?regexper\.com/],
debug: (process.env.NODE_ENV !== 'production'), environment: process.env.DEPLOY_ENV,
release: process.env.BUILD_ID debug: (process.env.NODE_ENV !== 'production'),
}); release: process.env.BUILD_ID
});
}
}; };
export default setupRaven; export default setupRaven;

View File

@ -45,4 +45,19 @@ describe('setupRaven', () => {
release: 'test ID' 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();
});
});
}); });