Skip to content

Commit dcc8caa

Browse files
authored
Merge pull request scratchfoundation#5397 from cwillisf/init-ga-only-if-id
initialize Google Analytics only if GA_ID is set
2 parents 874371d + 2e1c1b0 commit dcc8caa

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/lib/analytics.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import GoogleAnalytics from 'react-ga';
22

3-
GoogleAnalytics.initialize(process.env.GA_ID || window.GA_ID, {
4-
debug: (process.env.NODE_ENV !== 'production'),
5-
titleCase: true,
6-
sampleRate: (process.env.NODE_ENV === 'production') ? 100 : 0,
7-
forceSSL: true
8-
});
3+
import log from './log';
4+
5+
const GA_ID = (process.env.GA_ID || window.GA_ID);
6+
if (GA_ID) {
7+
GoogleAnalytics.initialize(GA_ID, {
8+
debug: (process.env.NODE_ENV !== 'production'),
9+
titleCase: true,
10+
sampleRate: (process.env.NODE_ENV === 'production') ? 100 : 0,
11+
forceSSL: true
12+
});
13+
} else {
14+
log.info('Disabling GA because GA_ID is not set.');
15+
window.ga = () => {
16+
// The `react-ga` module calls this function to implement all Google Analytics calls. Providing an empty
17+
// function effectively disables `react-ga`. This is similar to the `testModeAPI` feature of `react-ga` except
18+
// that `testModeAPI` logs the arguments of every call into an array. That's nice for testing purposes but
19+
// would look like a memory leak in a live program.
20+
};
21+
}
922

1023
export default GoogleAnalytics;

0 commit comments

Comments
 (0)