From e91713c2621559ad04b38a6c0d3af89fed3c8e6b Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Mon, 27 Jan 2020 17:28:15 -0800 Subject: [PATCH 1/3] initialize Google Analytics only if GA_ID is set --- src/lib/analytics.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib/analytics.js b/src/lib/analytics.js index a411ed958..7a233ff32 100644 --- a/src/lib/analytics.js +++ b/src/lib/analytics.js @@ -1,10 +1,13 @@ import GoogleAnalytics from 'react-ga'; -GoogleAnalytics.initialize(process.env.GA_ID || window.GA_ID, { - debug: (process.env.NODE_ENV !== 'production'), - titleCase: true, - sampleRate: (process.env.NODE_ENV === 'production') ? 100 : 0, - forceSSL: true -}); +const GA_ID = (process.env.GA_ID || window.GA_ID); +if (GA_ID) { + GoogleAnalytics.initialize(GA_ID, { + debug: (process.env.NODE_ENV !== 'production'), + titleCase: true, + sampleRate: (process.env.NODE_ENV === 'production') ? 100 : 0, + forceSSL: true + }); +} export default GoogleAnalytics; From 18186fb4c15f541b7d3334b9ddc5ff6b9339bdc6 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Tue, 28 Jan 2020 13:28:21 -0800 Subject: [PATCH 2/3] provide empty window.ga(...) when GA_ID is absent --- src/lib/analytics.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib/analytics.js b/src/lib/analytics.js index 7a233ff32..cd08719dd 100644 --- a/src/lib/analytics.js +++ b/src/lib/analytics.js @@ -8,6 +8,13 @@ if (GA_ID) { sampleRate: (process.env.NODE_ENV === 'production') ? 100 : 0, forceSSL: true }); +} else { + window.ga = () => { + // The `react-ga` module calls this function to implement all Google Analytics calls. Providing an empty + // function effectively disables `react-ga`. This is similar to the `testModeAPI` feature of `react-ga` except + // that `testModeAPI` logs the arguments of every call into an array. That's nice for testing purposes but + // would look like a memory leak in a live program. + }; } export default GoogleAnalytics; From 2e1c1b078461581349f2b1b96eadc7ca2f741eaf Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Fri, 28 Feb 2020 11:19:03 -0800 Subject: [PATCH 3/3] log a message if we skip GA init --- src/lib/analytics.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/analytics.js b/src/lib/analytics.js index cd08719dd..c498c26f3 100644 --- a/src/lib/analytics.js +++ b/src/lib/analytics.js @@ -1,5 +1,7 @@ import GoogleAnalytics from 'react-ga'; +import log from './log'; + const GA_ID = (process.env.GA_ID || window.GA_ID); if (GA_ID) { GoogleAnalytics.initialize(GA_ID, { @@ -9,6 +11,7 @@ if (GA_ID) { forceSSL: true }); } else { + log.info('Disabling GA because GA_ID is not set.'); window.ga = () => { // The `react-ga` module calls this function to implement all Google Analytics calls. Providing an empty // function effectively disables `react-ga`. This is similar to the `testModeAPI` feature of `react-ga` except