1/* 2 * Source: http://cutroni.com/blog/2012/02/21/advanced-content-tracking-with-google-analytics-part-1/ 3 * Original authors: 4 * - Nick Mihailovski 5 * - Thomas Baekdal 6 * - Avinash Kaushik 7 * - Joost de Valk 8 * - Eivind Savio 9 * - Justin Cutroni 10 * 11 * Google Tag Manager dataLayer events added by: 12 * Thomas Geiger 13 * gtm4wp.com 14 */ 15 16if ( "undefined" == typeof console ) { 17 window.console = { 18 log: function () {} 19 }; 20} 21 22(function( ) { 23 // Debug flag 24 const debugMode = gtm4wp_scrollerscript_debugmode; 25 26 // Default time delay before checking location 27 const callBackTime = gtm4wp_scrollerscript_callbacktime; 28 29 // # px before tracking a reader 30 const readerLocation = gtm4wp_scrollerscript_readerlocation; 31 32 // Set some flags for tracking & execution 33 let timer = 0; 34 let scroller = false; 35 let endContent = false; 36 let didComplete = false; 37 38 // Set some time variables to calculate reading time 39 const startTime = new Date(); 40 const beginning = startTime.getTime(); 41 let totalTime = 0; 42 43 // Track the aticle load 44 if ( !debugMode ) { 45 window[ gtm4wp_datalayer_name ].push({ 46 'event': 'gtm4wp.reading.articleLoaded' 47 }); 48 } else { 49 console.log( 'Article loaded' ); 50 } 51 52 // Check the location and track user 53 function trackLocation() { 54 //const bottom = $( window ).height() + $( window ).scrollTop(); 55 const bottom = window.innerHeight + window.scrollY; 56 //const height = $( document ).height(); 57 const height = document.body.offsetHeight 58 let scrollStart = 0; 59 60 // If user starts to scroll send an event 61 if ( bottom > readerLocation && !scroller ) { 62 const currentTime = new Date(); 63 scrollStart = currentTime.getTime(); 64 const timeToScroll = Math.round( ( scrollStart - beginning ) / 1000 ); 65 66 if ( !debugMode ) { 67 window[ gtm4wp_datalayer_name ].push({ 68 'event': 'gtm4wp.reading.startReading', 69 'timeToScroll': timeToScroll 70 }); 71 } else { 72 console.log( 'Started reading ' + timeToScroll ); 73 } 74 scroller = true; 75 } 76 77 // If user has hit the bottom of the content send an event 78 //if ( bottom >= $( '#' + gtm4wp_scrollerscript_contentelementid ).scrollTop() + $( '#' + gtm4wp_scrollerscript_contentelementid ).innerHeight() && !endContent ) { 79 const scroll_top = document.querySelector('#' + gtm4wp_scrollerscript_contentelementid)?.scrollTop || 0; 80 const client_height = document.querySelector('#' + gtm4wp_scrollerscript_contentelementid )?.clientHeight || 0; 81 if ( bottom >= scroll_top + client_height && !endContent ) { 82 83 const currentTime = new Date(); 84 const contentScrollEnd = currentTime.getTime(); 85 const timeToContentEnd = Math.round( ( contentScrollEnd - scrollStart ) / 1000 ); 86 87 if ( !debugMode ) { 88 window[ gtm4wp_datalayer_name ].push({ 89 'event': 'gtm4wp.reading.contentBottom', 90 'timeToScroll': timeToContentEnd 91 }); 92 } else { 93 console.log( 'End content section ' + timeToContentEnd ); 94 } 95 96 endContent = true; 97 } 98 99 // If user has hit the bottom of page send an event 100 if ( bottom >= height && !didComplete ) { 101 const currentTime = new Date(); 102 const end = currentTime.getTime(); 103 totalTime = Math.round( ( end - scrollStart ) / 1000 ); 104 105 if ( !debugMode ) { 106 if ( totalTime < gtm4wp_scrollerscript_scannertime ) { 107 window[ gtm4wp_datalayer_name ].push({ 108 'event': 'gtm4wp.reading.readerType', 109 'readerType': 'scanner' 110 }); 111 } else { 112 window[ gtm4wp_datalayer_name ].push({ 113 'event': 'gtm4wp.reading.readerType', 114 'readerType': 'reader' 115 }); 116 } 117 118 window[ gtm4wp_datalayer_name ].push({ 119 'event': 'gtm4wp.reading.pagebottom', 120 'timeToScroll': totalTime 121 }); 122 } else { 123 if ( totalTime < gtm4wp_scrollerscript_scannertime ) { 124 console.log( 'The visitor seems to be a "scanner"' ); 125 } else { 126 console.log( 'The visitor seems to be a "reader"' ); 127 } 128 129 console.log( 'Bottom of page ' + totalTime ); 130 } 131 132 didComplete = true; 133 } 134 } 135 136 // Track the scrolling and track location
137 //$( window ).scroll(function() { 138 document.addEventListener("scroll", function(event){ 139 if ( timer ) { 140 clearTimeout( timer ); 141 } 142 143 // Use a buffer so we don't call trackLocation too often. 144 timer = setTimeout( trackLocation, callBackTime ); 145 }); 146})(window, document);
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.