SHARE
TWEET

AO — scrolling blocks

snegir Aug 28th, 2018 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.  
  3. //get time  
  4. jQuery.fixTime = function() {
  5.     var dateObj = new Date();
  6.     return Math.floor(dateObj.getTime() / 1000);
  7. }
  8.  
  9. var startTime = jQuery.fixTime();
  10.  
  11. jQuery(window).bind('beforeunload', function(){
  12.    
  13.     var endTime = jQuery.fixTime() - startTime;
  14.  
  15.     // get and convert headers time
  16.     var headers = JSON.parse(localStorage.getItem('headers'));
  17.     var str = []
  18.     for(var i = 0; i < headers.length-1; i++) {
  19.  
  20.         var time =  parseInt(headers[i+1].timeEnd) - parseInt(headers[i].timeEnd);
  21.         str.push({'name': headers[i].name, 'time': time})
  22.     }
  23.     // for last element
  24.     var time =  parseInt(headers[headers.length-1].timeStart) - parseInt(endTime);
  25.     str.push({'name': headers[headers.length-1].name, 'time': time})
  26.  
  27.     var headers_name = {},
  28.         headers_count = {},
  29.         newArraySeries = []
  30.  
  31.     // count sum of time for unique headers
  32.     str.forEach(
  33.         function(e){
  34.             if(!headers_name[e.name]){
  35.                 headers_name[e.name] = 0
  36.                 headers_count[e.name] = 0
  37.             }
  38.             // convert msec to second
  39.             headers_name[e.name] += parseInt(e.time)/1000
  40.             headers_count[e.name]++
  41.         }
  42.     )
  43.     for(var names in headers_name){
  44.         newArraySeries.push({name : names, data : headers_name[names]})
  45.     }  
  46.  
  47.     // send autoEvents to GA for each header
  48.     newArraySeries.forEach(function(e) {
  49.         dataLayer.push({'event': 'autoEvent', 'eventCategory': 'AO — scrollBlocks', 'eventAction': document.title, 'eventLabel': e.name, 'eventValue': Math.round(e.data)});
  50.         fbq('track', 'AO — scrollBlocks', {
  51.           'eventAction': document.title,
  52.           'eventLabel': e.name,
  53.           'eventValue': Math.round(e.data)
  54.         });
  55.     })
  56.  
  57.     localStorage.removeItem('headers');
  58.                                    
  59. })  
  60.  
  61.  
  62.  
  63. </script>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top