Skip to content

Commit ccbb1d6

Browse files
authored
feat: tracking for on-page navigation (#281)
1 parent a70971d commit ccbb1d6

File tree

6 files changed

+2777
-227
lines changed

6 files changed

+2777
-227
lines changed

actions/redoc/template.hbs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,60 @@
6767
function gtag(){dataLayer.push(arguments);}
6868
gtag('js', new Date());
6969
gtag('config', 'UA-41294851-1');
70+
71+
class AnalyticsTracker {
72+
static initialize () {
73+
const selectorQs = 'ul.sc-euEtCV > li';
74+
75+
// Retain the current hash as the last seen
76+
AnalyticsTracker._lastSeenTag = window.location.hash;
77+
78+
// Listen for click events on all relevant li tags
79+
for (const ele of Array.from(document.querySelectorAll(selectorQs))) {
80+
ele.addEventListener('click', () => {
81+
// On recipt of the event, the on page navigation has not happened
82+
// so we introduce a delay before we attempt to handle the navigationNotification
83+
setTimeout(AnalyticsTracker.onPageNavigationNotificationHandler, 10);
84+
});
85+
}
86+
87+
// Listen for Scroll-stop
88+
let scrollTimer = null;
89+
window.addEventListener('scroll', () => {
90+
if (scrollTimer !== null) {
91+
clearTimeout(scrollTimer);
92+
}
93+
// We notify on page rest of at least half a second
94+
scrollTimer = setTimeout(AnalyticsTracker.onPageNavigationNotificationHandler, 500);
95+
}, false);
96+
97+
// Log the initial view with the hash
98+
if (window.location.hash) {
99+
AnalyticsTracker.dispatch('page_view');
100+
}
101+
}
102+
static onPageNavigationNotificationHandler() {
103+
if (AnalyticsTracker._lastSeenTag === window.location.hash) {
104+
return; // Attempt to navigate to the same section. Return early, no-op
105+
}
106+
107+
// Update the last seen and dispatch
108+
AnalyticsTracker._lastSeenTag = window.location.hash;
109+
AnalyticsTracker.dispatch('page_view')
110+
}
111+
112+
static dispatch (eventType) {
113+
gtag('event', eventType, {
114+
page_title: document.title,
115+
page_location: window.location.pathname + window.location.hash,
116+
page_path: window.location.pathname + window.location.hash
117+
});
118+
}
119+
}
120+
121+
window.addEventListener('load', (event) => {
122+
AnalyticsTracker.initialize();
123+
});
70124
</script>
71125

72126
<script src="js/determine_widget.js"></script>

docs/chunks/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)