Skip to content

Commit dc30ff7

Browse files
committed
Track CloudFormation install link clicks in docs
1 parent f23d0ce commit dc30ff7

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

src/theme/Root.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React, {useEffect} from 'react';
2+
3+
const CLOUDFORMATION_HOST = 'console.aws.amazon.com';
4+
const CLOUDFORMATION_EVENT = 'cloudformation_install_click';
5+
6+
function getHashParams(url) {
7+
const hashQuery = url.hash.includes('?') ? url.hash.split('?')[1] : '';
8+
return new URLSearchParams(hashQuery);
9+
}
10+
11+
function getTemplateName(templateUrl) {
12+
if (!templateUrl) return '';
13+
14+
try {
15+
const url = new URL(templateUrl);
16+
const pathParts = url.pathname.split('/').filter(Boolean);
17+
return pathParts[pathParts.length - 1] || '';
18+
} catch (error) {
19+
return '';
20+
}
21+
}
22+
23+
function isCloudFormationLink(url) {
24+
return url.hostname === CLOUDFORMATION_HOST && url.pathname.includes('/cloudformation/');
25+
}
26+
27+
function trackCloudFormationClick(link) {
28+
if (typeof window.gtag !== 'function') return;
29+
30+
try {
31+
const url = new URL(link.href);
32+
if (!isCloudFormationLink(url)) return;
33+
34+
const hashParams = getHashParams(url);
35+
const templateUrl = hashParams.get('templateURL') || '';
36+
37+
window.gtag('event', CLOUDFORMATION_EVENT, {
38+
region: url.searchParams.get('region') || '',
39+
stack_name: hashParams.get('stackName') || '',
40+
template_url: templateUrl,
41+
template_name: getTemplateName(templateUrl),
42+
docs_path: window.location.pathname,
43+
link_text: link.textContent.trim(),
44+
transport_type: 'beacon',
45+
});
46+
} catch (error) {
47+
// Ignore malformed links.
48+
}
49+
}
50+
51+
function CloudFormationInstallTracking() {
52+
useEffect(() => {
53+
function handleClick(event) {
54+
const target = event.target;
55+
const link = target && typeof target.closest === 'function'
56+
? target.closest('a[href]')
57+
: null;
58+
if (!link) return;
59+
60+
trackCloudFormationClick(link);
61+
}
62+
63+
document.addEventListener('click', handleClick);
64+
return () => document.removeEventListener('click', handleClick);
65+
}, []);
66+
67+
return null;
68+
}
69+
70+
export default function Root({children}) {
71+
return (
72+
<>
73+
<CloudFormationInstallTracking />
74+
{children}
75+
</>
76+
);
77+
}

0 commit comments

Comments
 (0)