Skip to content

Commit afea9b2

Browse files
committed
feat(docs): optimize GA4 redirect tracking with beacon transport and event callback
1 parent 1541cfd commit afea9b2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

docs/go/repo.html

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<meta charset="utf-8">
4+
<meta charset="utf-8" />
55
<title>Redirecting…</title>
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
<script async src="https://www.googletagmanager.com/gtag/js?id=G-4NH21XZLBN"></script>
88
<script>
99
window.dataLayer = window.dataLayer || [];
1010
function gtag(){ dataLayer.push(arguments); }
1111
gtag('js', new Date());
12-
gtag('config', 'G-4NH21XZLBN');
12+
gtag('config', 'G-4NH21XZLBN', { transport_type: 'beacon' });
1313
</script>
14+
<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin>
15+
<link rel="dns-prefetch" href="//github.com">
1416
</head>
1517
<body>
1618
<p>Redirecting to GitHub repo…</p>
@@ -20,16 +22,24 @@
2022
const campaign = params.get('cmp') || 'repo';
2123
const content = params.get('c') || '';
2224

25+
const target = 'https://github.com/bsayli/spring-boot-openapi-generics-clients';
26+
const utm = `?utm_source=${encodeURIComponent(src)}&utm_medium=referral&utm_campaign=${encodeURIComponent(campaign)}${content ? `&utm_content=${encodeURIComponent(content)}` : ''}`;
27+
const go = () => location.href = target + utm;
28+
29+
let redirected = false;
30+
const safeGo = () => { if (!redirected) { redirected = true; go(); } };
31+
2332
gtag('event', 'outbound_to_github', {
2433
destination: 'github_repo',
2534
source_label: src,
2635
campaign,
27-
content
36+
content,
37+
transport_type: 'beacon',
38+
event_callback: safeGo,
39+
event_timeout: 500
2840
});
29-
setTimeout(() => {
30-
const utm = `?utm_source=${encodeURIComponent(src)}&utm_medium=referral&utm_campaign=${encodeURIComponent(campaign)}${content ? `&utm_content=${encodeURIComponent(content)}` : ''}`;
31-
location.href = 'https://github.com/bsayli/spring-boot-openapi-generics-clients' + utm;
32-
}, 250);
41+
42+
setTimeout(safeGo, 550);
3343
</script>
3444
</body>
3545
</html>

0 commit comments

Comments
 (0)