|
67 | 67 | function gtag(){dataLayer.push(arguments);}
|
68 | 68 | gtag('js', new Date());
|
69 | 69 | 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 | + }); |
70 | 124 | </script>
|
71 | 125 |
|
72 | 126 | <script src="js/determine_widget.js"></script>
|
|
0 commit comments