-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathanalyticsEvents.js
More file actions
85 lines (82 loc) · 3.35 KB
/
Copy pathanalyticsEvents.js
File metadata and controls
85 lines (82 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(function () {
analytics.ready(() => {
// Track clicks on all links and anchor tags
document.addEventListener('click', (e) => {
// Targeting anchor tags at for all click events because all links are not rendered at the time of script execution to attach event listeners
if (e.target.nodeName === 'A') {
const href = e.target.getAttribute('href');
const label = e.target.innerText;
const page_section = getLinkPageSection(e.target);
if (href && href.startsWith('http')) { // External link
analytics.track('Clicked Link', {
link_url: href,
label: label,
link_type: 'external',
page_section: page_section,
});
} else if (href && href.startsWith('/')) { // Internal link
analytics.track('Clicked Link', {
link_url: href,
label: label,
link_type: 'internal',
page_section: page_section,
});
} else { // Anchor link
analytics.track('Clicked Link', {
link_url: href,
label: label,
link_type: 'anchor',
page_section: page_section,
});
}
}
});
})
})();
// Get the page section of the link
function getLinkPageSection(linkElement) {
let pageSectionElement = linkElement.closest('nav') ?? (linkElement.closest('.table-of-contents') ?? linkElement.closest('footer')); // Get the closest nav, table of contents or footer element
let pageSectionLabel = '';
if (pageSectionElement) {
switch (pageSectionElement.nodeName) {
case 'NAV':
pageSectionLabel = pageSectionElement.getAttribute('aria-label');
break;
case 'FOOTER':
pageSectionLabel = 'Footer';
break;
case 'UL':
pageSectionLabel = 'Table of contents';
break;
default:
pageSectionLabel = '';
}
} else {
// If the link is not in a nav, table of contents or footer, then get the page section from the heading above the link
let anchor = getAboveAnchor(linkElement);
if (anchor) {
pageSectionLabel = anchor.innerText;
} else {
let pageSectionElement = linkElement.parentElement;
while (pageSectionElement) {
anchor = getAboveAnchor(pageSectionElement);
if (anchor) {
pageSectionLabel = anchor.innerText;
break;
}
pageSectionElement = pageSectionElement.parentElement;
}
}
}
return pageSectionLabel;
}
function getAboveAnchor(element) {
let sibling = element.previousElementSibling;
while (sibling) {
if (sibling.matches('.anchor') || sibling.matches('h1') || sibling.matches('h2') || sibling.matches('h3') || sibling.matches('h4') || sibling.matches('h5') || sibling.matches('h6')) {
return sibling;
}
sibling = sibling.previousElementSibling;
}
return null;
}