1// ToDo this file can be deleted once we are done collecting Universal Analytics data (July 2023) 2$(function () { 3 function gaEventFromDataAttributes() { 4 var $this = $(this); 5 6 if (window.ga) { 7 ga('send', { 8 hitType: 'event', 9 eventCategory: evalDataAttribute($this.data('ga-event-category')), 10 eventAction: evalDataAttribute($this.data('ga-event-action')), 11 eventLabel: evalDataAttribute($this.data('ga-event-label')) 12 }); 13 } 14 } 15 16 /** 17 * ToDo I need to understand the reporting functionality this provides so I can replicate. 18 * The GA4 built in "scroll tracking" only tracks when a user scrolls to the bottom of a page. 19 * This sends an event whenever the trigger hits. We place this trigger on specific elements and track scroll 20 * percentages for each. 21 * @param event 22 * @param element 23 */ 24 function gaEventFromScroll(event, element) { 25 let $scrolledElement= $(element); 26 27 // Todo remove console log once testing is done 28 console.log($scrolledElement.data('ga-scroll-label')) 29 30 if (window.ga) { 31 ga('send', { 32 hitType: 'event', 33 eventCategory: evalDataAttribute($scrolledElement.data('ga-scroll-category')), 34 eventAction: evalDataAttribute($scrolledElement.data('ga-scroll-action')), 35 eventLabel: evalDataAttribute($scrolledElement.data('ga-scroll-label')) 36 }); 37 } 38 } 39 40 /** 41 * Watch for clicks on elements with the listed data ga attributes and record them as events. 42 */ 43 $(document).on('click', '*[data-ga-event-category][data-ga-event-action]', gaEventFromDataAttributes); 44 $(document).on('scrollViewEvent', (event, element) => gaEventFromScroll(event, element)); 45 46 /** 47 * If an element is collapse, then it won't fire on the above handler, even though it's a click. The BS 48 * collapse listener must stop propagation, so it doesn't fire. 49 * 50 * So we listen for collapse events, and if they happen on an element that is controlled by a GA element, 51 * then fire that GA element's listener. 52 */ 53 $(document).on('show.bs.collapse', function(e) { 54 var $shownElement = $(e.target); 55 if (!$shownElement.attr('id')) { 56 return; 57 } 58 59 var $controllingElement = $('*[aria-controls="' + $shownElement.attr('id') + '"]'); 60 if ($controllingElement.length !== 1) { 61 return; 62 } 63 64 if ($controllingElement.data('ga-event-category') && $controllingElement.data('ga-event-action')) { 65 gaEventFromDataAttributes.apply($controllingElement); 66 } 67 }); 68 69 /** 70 * Allows users to use dynamic values in their ga data attributes. Using the strings defined here will be replaced 71 * with their derived values. 72 */ 73 function evalDataAttribute(dataAttribute) { 74 if (dataAttribute === 'window.location') { 75 return window.location.pathname; 76 } 77 78 return dataAttribute; 79 } 80 /** 81 * ToDo This functionality is "built in" as an "Enhanced measurement" in our GA4 Data Stream. 82 * Once we verify the new functionality replicates this the way we need, we can remove it. 83 * Track all outbound, external, links. 84 */ 85 $(document).on('click', 'a[href]', function (e) { 86 var $link = $(this); 87 var href = $link.attr('href'); 88 89 // Don't track mailtos. 90 if (href.match(/^mailto\:/i)) { 91 return; 92 } 93 94 // The href is not absolute, so it's internal, don't track it. 95 if (!href.match(/^(https?:|\/\/)/i)) { 96 return; 97 } 98 99 // It's an absolute URL, but it's internal, don't track. 100 if (href.match(document.domain)) { 101 return; 102 } 103 104 ga('send', { 105 hitType : 'event', 106 eventCategory: 'External', 107 eventAction : window.location.pathname, 108 eventLabel : href 109 }); 110 }); 111});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.