Advertisement
mikeg_de

Identify users visited websites by HTML5 Resource Timing API

Jul 9th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. /* The scripts purpose is to identify internal traffic but it might be used to verify if a user has visited a certain website.
  3. * By assuming a user visited website A, resources or connection information like DNS or SSL get's cached.
  4. * Leveraging the HTML5 Resource Timing API (https://www.w3.org/TR/resource-timing/) a known resources get's attached to the DOM.
  5. * If the resource or it's connection information was already cached the following metrics should be zero:
  6. * domainLookupStart, domainLookupEnd, connectStart, connectEnd, requestStart, responseStart
  7. *
  8. * This script is extended with Google Tag Manager events to track the findings. Later I will elaborate to implement the userID.
  9. */
  10.  
  11.   var imageCheck = document.createElement("img");
  12.   imageCheck.src = "YOUR-FULL-RESOURCE-PATH";
  13.   imageCheck.style = "display: block; visibility: hidden;";
  14.   imageCheck.id = "imageCheck";
  15.   document.body.appendChild(imageCheck);
  16.  
  17.   document.getElementById("imageCheck").addEventListener("load", function() {
  18.     console.log("Resource loaded");
  19.     console.log(document.getElementById("imageCheck"));
  20.     console.log("Resources complete: " + document.getElementById("imageCheck").complete);
  21.     console.log("Natural width: " + document.getElementById("imageCheck").naturalWidth);
  22.    
  23.     var resourceList = !(/MSIE (\d.\d+);/.test(navigator.userAgent) || window.performance.getEntriesByType == undefined) ? window.performance.getEntriesByType("resource") : "undefined",
  24.     resuorceUrl = document.getElementById("imageCheck").src,
  25.     found = resourceList.filter(function(item) { return item.name === resuorceUrl; }),
  26.     foundTiming = [
  27.       found[0].domainLookupEnd - found[0].domainLookupStart,
  28.       found[0].connectEnd - found[0].connectStart,
  29.       found[0].requestStart,
  30.       found[0].responseStart
  31.     ];
  32.  
  33.     console.log("domainLookupStart: " + found[0].domainLookupStart);
  34.     console.log("domainLookupEnd: " + found[0].domainLookupEnd);
  35.  
  36.     console.log("connectStart: " + found[0].connectStart);
  37.     console.log("connectEnd: " + found[0].connectEnd);
  38.  
  39.     console.log("requestStart: " + found[0].requestStart);
  40.     console.log("responseStart: " + found[0].responseStart);
  41.  
  42.     if(found[0].name.indexOf("https") !== -1) {
  43.       console.log("secureConnectionStart: " + found[0].secureConnectionStart);
  44.       foundTiming[foundTiming.length] = found[0].secureConnectionStart;
  45.     }
  46.    
  47.     if (foundTiming.join("|").indexOf("0") !== -1) {
  48.       //console.log("Zero's baby: " + foundTiming.reduce((a, b) => a + b, 0) === 0);
  49.       console.log("foundTiming: " + foundTiming.join("|"));
  50.       dataLayer.push ({ 'event': 'internalTraffic', 'eventCategory': 'Internal traffic', 'eventAction': foundTiming.join("|"), 'eventLabel': undefined, 'eventValue': undefined, 'nonInteractive': 1});
  51.     } else {
  52.       //console.log("Zeor's baby: " + foundTiming.reduce((a, b) => a + b, 0) === 0);
  53.       console.log("foundTiming: " + foundTiming.join("|"));
  54.       dataLayer.push ({ 'event': 'internalTraffic', 'eventCategory': 'External traffic', 'eventAction': foundTiming.join("|"), 'eventLabel': undefined, 'eventValue': undefined, 'nonInteractive': 1});
  55.     }
  56.   }, false);
  57.   document.getElementById("imageCheck").addEventListener("error", function() {
  58.     console.log("Resource didn't load");
  59.     dataLayer.push ({ 'event': 'internalTraffic', 'eventCategory': 'Resource not found', 'eventAction': undefined, 'eventLabel': undefined, 'eventValue': undefined, 'nonInteractive': 1});
  60.   }, false);   
  61. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement