Skip to content

Commit 64b33d2

Browse files
authored
Merge pull request #23 from smooth-code/enable-ga-in-dev-mode
feat(google-analytics): enable google analytics in dev mode
2 parents 70b683c + bbdecfc commit 64b33d2

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
/* eslint-env browser */
22
export function onRouteUpdate({ location }) {
33
// Don't track while developing.
4-
if (process.env.NODE_ENV === 'production' && typeof ga === 'function') {
5-
if (
6-
location &&
7-
typeof window.excludeGAPaths !== 'undefined' &&
8-
window.excludeGAPaths.some(rx => rx.test(location.pathname))
9-
) {
10-
return
11-
}
4+
if (typeof ga !== 'function') return
125

13-
// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
14-
// reactHelmet is using requestAnimationFrame so we should use it too: https://github.com/nfl/react-helmet/blob/5.2.0/src/HelmetUtils.js#L296-L299
15-
const sendPageView = () => {
16-
window.ga('set', {
17-
page: location
18-
? location.pathname + location.search + location.hash
19-
: undefined,
20-
title: document.title,
21-
})
22-
window.ga('send', 'pageview')
23-
}
6+
if (
7+
location &&
8+
typeof window.excludeGAPaths !== 'undefined' &&
9+
window.excludeGAPaths.some(rx => rx.test(location.pathname))
10+
) {
11+
return
12+
}
13+
14+
// wrap inside a timeout to make sure react-helmet is done with it's changes (https://github.com/gatsbyjs/gatsby/issues/9139)
15+
// reactHelmet is using requestAnimationFrame so we should use it too: https://github.com/nfl/react-helmet/blob/5.2.0/src/HelmetUtils.js#L296-L299
16+
function sendPageView() {
17+
window.ga('set', {
18+
page: location
19+
? location.pathname + location.search + location.hash
20+
: undefined,
21+
title: document.title,
22+
})
23+
window.ga('send', 'pageview')
24+
}
2425

25-
if ('requestAnimationFrame' in window) {
26-
requestAnimationFrame(() => {
27-
requestAnimationFrame(sendPageView)
28-
})
29-
} else {
30-
// simulate 2 rAF calls
31-
setTimeout(sendPageView, 32)
32-
}
26+
if ('requestAnimationFrame' in window) {
27+
requestAnimationFrame(() => {
28+
requestAnimationFrame(sendPageView)
29+
})
30+
} else {
31+
// simulate 2 rAF calls
32+
setTimeout(sendPageView, 32)
3333
}
3434
}

packages/smooth-plugin-google-analytics/src/smooth-node.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ exports.onRenderBody = (
2020
{ setHeadComponents, setPostBodyComponents },
2121
options,
2222
) => {
23-
if (process.env.NODE_ENV !== 'production') {
24-
return null
25-
}
26-
2723
// Lighthouse recommends pre-connecting to google analytics
2824
setHeadComponents([
2925
<link
@@ -78,13 +74,19 @@ exports.onRenderBody = (
7874
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
7975
}
8076
if (typeof ga === "function") {
77+
8178
ga('create', '${options.trackingId}', '${
8279
typeof options.cookieDomain === `string`
8380
? options.cookieDomain
8481
: `auto`
8582
}', ${
8683
typeof options.name === `string` ? `'${options.name}', ` : ``
8784
}${JSON.stringify(gaCreateOptions)});
85+
${
86+
process.env.NODE_ENV !== 'production'
87+
? `ga('set', 'sendHitTask', null);`
88+
: ''
89+
}
8890
${
8991
typeof options.anonymize !== `undefined` && options.anonymize === true
9092
? `ga('set', 'anonymizeIp', true);`

packages/smooth/src/router/HiddenRouter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function HiddenRouter({ children }) {
4242
const { history } = useRouter()
4343
const hiddenHistory = useHiddenHistory()
4444
const promises = useRef([])
45+
const timeoutRef = useRef()
4546

4647
const flush = useCallback(() => {
4748
if (promises.current.length || !hiddenHistory.state) return
@@ -60,7 +61,8 @@ export function HiddenRouter({ children }) {
6061
}
6162
// 65ms is 4 frames, pretty invisible when we change page
6263
// it ensures that React have time to start a new request for an example
63-
setTimeout(() => flush(), 65)
64+
clearTimeout(timeoutRef.current)
65+
timeoutRef.current = setTimeout(() => flush(), 65)
6466
})
6567
promises.current.push(promise)
6668
},

0 commit comments

Comments
 (0)