Skip to content

Commit 94b743e

Browse files
committed
Updated the googleAnalytics plugin to take into account page reloads. Special thanks @bender2k14
1 parent 3d0de68 commit 94b743e

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

.codedoc/plugins/ga/index.tsx

+40-10
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,52 @@ import { StaticRenderer } from '@connectv/sdh';
22
import register from 'jsdom-global';
33
import { ConfigOverride } from '@codedoc/core';
44

5-
const renderer = new StaticRenderer(); // --> create a static renderer
6-
register(); // --> register jdom global so that we can create DOM elements
5+
const renderer = new StaticRenderer();
6+
register();
77

8+
// Documentation
9+
// Coppied from https://github.com/bender2k14/tyson-williams-blog/blob/2501a6e55ebbf0e559d91ac1494ef2b412739c7d/.codedoc/components/GoogleAnalytics/plugin.tsx#L28
10+
// Thumbs up to @bender2k14
11+
// Use gtag.js
12+
// not analytics.js https://developers.google.com/analytics/devguides/collection/upgrade
13+
// not ga.js https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples#basic-page-tracking
14+
// Default gtag code
15+
// https://developers.google.com/analytics/devguides/collection/gtagjs#install_the_global_site_tag
16+
// Send manual page views for single page applications, which codedoc creates
17+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages#manual_pageviews
18+
// Default page view arguments
19+
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages#default_behavior
20+
// Test with...
21+
// https://support.google.com/analytics/answer/1008080#verify:~:text=Verify%20that%20your%20global%20site%20tag%20is%20working,-To
22+
// Real-time view in Google Analytics
23+
// Google's Google's Tag Assistant extension for Google Chrome
24+
// https://chrome.google.com/webstore/detail/tag-assistant-by-google/kejbdjndbnbjgmefkgdddjlbokphdefk
25+
// Example use of Google's Tag Assistant
26+
// https://www.youtube.com/watch?v=EkNsaBDT8Cc
27+
// But Google Tag Assistant contains some bugs for single page applications
28+
// https://www.analyticsmania.com/post/multiple-installations-of-google-tag-manager-detected/
829

9-
export function googleAnalytics(gacode: string) {
30+
export function googleAnalytics(trackingId: string) {
31+
let gtagUrl = `https://www.googletagmanager.com/gtag/js?id=${trackingId}`
32+
let functionName = 'sendPageViewToGoogle'
33+
let functionBody =
34+
`gtag('js', new Date());` +
35+
`gtag('config', '${trackingId}');`
36+
let localCode =
37+
'function gtag(){dataLayer.push(arguments);}' +
38+
`function ${functionName}(){${functionBody}}` +
39+
'window.dataLayer = window.dataLayer || [];' +
40+
`window.addEventListener("navigation", ${functionName});` +
41+
`${functionName}();`
42+
let anonymousFunction =
43+
`(function(){${localCode}})();`
1044
return function(): ConfigOverride {
1145
return {
1246
page: {
1347
scripts: [
14-
<script>{`
15-
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
16-
ga('create', '${gacode}', 'auto');
17-
ga('send', 'pageview');
18-
`}</script>,
19-
<script async src='https://www.google-analytics.com/analytics.js'/>
20-
],
48+
<script async src={gtagUrl}></script>,
49+
<script>{anonymousFunction}</script>
50+
]
2151
}
2252
}
2353
};

0 commit comments

Comments
 (0)