SHARE
TWEET

Pulse - Script

Davi0k Jun 2nd, 2020 (edited) 685 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Normal Usage: <script type="text/javascript" src="pulsing.js"></script>
  3.  
  4.     Cookie Usage: <script id="pulse" type="text/javascript" src="pulsing.js" data-cookie="true"></script>
  5.  
  6.     Debug Usage: <script id="pulse" type="text/javascript" src="pulsing.js" data-debug="true"></script>
  7.  
  8.     Test Usage: <script id="pulse" type="text/javascript" src="pulsing.js" data-test="true"></script>
  9.  
  10.     Change Identifier Label: <script id="pulse" type="text/javascript" src="pulsing.js" data-identifier="identifier"></script>
  11. */
  12.  
  13. const DOMAIN = "http://localhost:8000", API = `${DOMAIN}/api`; //https://api.pulsing.io
  14.  
  15. const DEFAULT = "pulse-identifier", CONSENT = "pulse-consent";
  16.  
  17. const METADATA = "pulse-data-";
  18.  
  19. var X = null, Y = null;
  20.  
  21. var response = null;
  22.  
  23. function start() {
  24.   track();
  25.  
  26.   window.addEventListener("locationchange", seconds);
  27.  
  28.   const push = history.pushState;
  29.  
  30.   history.pushState = function() {
  31.     push.apply(history, arguments);
  32.     seconds(); track();
  33.   }
  34. }
  35.  
  36. function consent(event) {
  37.   window.localStorage.setItem(CONSENT, true);
  38.  
  39.   document.getElementById("consent").remove();
  40.  
  41.   start();
  42. }
  43.  
  44. document.addEventListener("DOMContentLoaded", function (event) {
  45.   const consent = window.localStorage.getItem(CONSENT);
  46.  
  47.   if (consent == null) {
  48.     document.body.innerHTML += `
  49.       <div class="micromodal-slide" id="consent" aria-hidden="true" style="display: block;">
  50.         <div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
  51.           <header class="modal__header">
  52.             <h2 class="modal__title" id="modal-1-title">
  53.               Impostazioni della privacy
  54.             </h2>
  55.           </header>
  56.              
  57.           <main class="modal__content" id="modal-1-content">
  58.             <p>
  59.               Questa piattaforma protegge la tua privacy rispettando il <code>GDPR (European Union General Data Protection Regulation)</code>. Non utilizziamo i tuoi dati per nessuno scopo da te non espressamente autorizzato.
  60.             </p>
  61.                  
  62.             <li>
  63.               <span class="privacyHeader">Ci autorizzi a memorizzare e analizzare i dati relativi al tuo comportamento</span> sulla nostra piattaforma per poterti fornire un\'esperienza di navigazione personalizzata?
  64.              Questa procedura implica la <span class="privacyHeader">creazione di cookie sul tuo browser</span> per assicurare la consistenza dei nostri A/B test e una corretta profilazione dell\'utente.
  65.            </li>
  66.          </main>
  67.              
  68.          <footer class="modal__footer" >
  69.            <button class="modal__btn modal__btn-primary mrPrivacy-5" id="consentButton" onclick="consent(event)">
  70.              Concedi il consenso
  71.            </button>  
  72.                  
  73.            <button class="modal__btn modal__btn-secondary">
  74.              Termini e condizioni
  75.            </button>  
  76.          </footer>  
  77.        </div>
  78.      </div>
  79.    `;
  80.  
  81.    document
  82.      .getElementsByTagName("head")[0]
  83.      .insertAdjacentHTML(
  84.        "beforeend",
  85.        '<link rel="stylesheet" href="https://pulsing.io/css/modal.css"/>'
  86.      );
  87.  }
  88.  
  89.  if (consent) start();
  90. });
  91.  
  92. function cookie(window, name) {
  93.  const value = `; ${window.document.cookie}`, parts = value.split(`; ${name}=`);
  94.  
  95.  if (parts.length == 2) return parts.pop().split(";").shift();
  96. }
  97.  
  98. function track() {
  99.  const script = window.document.getElementById("pulse");
  100.  
  101.  const COOKIE = script ? script.getAttribute("data-cookie") == "true" : false;
  102.  
  103.  const DEBUG = script ? script.getAttribute("data-debug") == "true" : false;
  104.  
  105.  const TEST = script ? script.getAttribute("data-test") == "true" : false;
  106.  
  107.  const IDENTIFIER = script
  108.    ? script.getAttribute("data-identifier")
  109.      ? script.getAttribute("data-identifier")
  110.      : DEFAULT
  111.    : DEFAULT;
  112.  
  113.  const data = {
  114.    domain: window.location.hostname,
  115.    url: window.location.pathname + window.location.search,
  116.    metadata: new Object()
  117.  };
  118.  
  119.  const identifier = COOKIE
  120.    ? cookie(window, IDENTIFIER)
  121.    : window.localStorage.getItem(IDENTIFIER);
  122.  
  123.  if (identifier) data.identifier = identifier;
  124.  
  125.  if (TEST) data.test = TEST;
  126.  
  127.  for (const label in window.localStorage)
  128.    if (label.includes(METADATA) == true) {
  129.      const key = label.replace(METADATA, new String());
  130.      const value = window.localStorage.getItem(label);
  131.      data.metadata[key] = value;
  132.    }
  133.  
  134.  const json = JSON.stringify(data);
  135.  
  136.  const request = new XMLHttpRequest();
  137.  
  138.  request.open("POST", `${API}/logs`, true);
  139.  
  140.  request.setRequestHeader("Content-Type", "application/json");
  141.  
  142.  request.onreadystatechange = function () {
  143.    if (request.readyState == 4) {
  144.      switch (request.status) {
  145.        case 400:
  146.          console.warn("This browser is not supported, some essentials data cannot be retrieved.");
  147.          break;
  148.        case 404:
  149.          console.warn("No active Pulse Service found for this web-site.");
  150.          break;
  151.        case 500:
  152.          console.warn("The Pulse Service's server is under maintenence, your actions will not be logged until it return Online.");
  153.          break;
  154.      }
  155.  
  156.      const elements = document.body.getElementsByTagName("*");
  157.  
  158.      for (const element of elements)
  159.        if (element.localName == "a" || element.localName == "button" || element.style.cursor == "pointer")
  160.          element.addEventListener("click", function(event) {
  161.            const interaction = {
  162.              ...data,
  163.              content: element.innerText,
  164.              X: X, Y: Y,
  165.              width: screen.width,
  166.              element: element.nodeName
  167.            };
  168.  
  169.            if (element.id) interaction.label = element.id;
  170.  
  171.            const json = JSON.stringify(interaction);
  172.  
  173.            const request = new XMLHttpRequest();
  174.  
  175.            request.open("POST", `${API}/interactions`, true);
  176.  
  177.            request.setRequestHeader("Content-Type", "application/json");
  178.  
  179.            request.send(json);
  180.          });
  181.  
  182.      response = JSON.parse(this.responseText);
  183.  
  184.      if (DEBUG) console.log(response, this.status);
  185.    }
  186.  };
  187.  
  188.  request.send(json);
  189. }
  190.  
  191. function seconds() {
  192.  if (response) {
  193.    const request = new XMLHttpRequest();
  194.  
  195.    request.open("HEAD", `${API}/logs/${response.id}`, true);
  196.  
  197.    request.send();
  198.  }
  199. }
  200.  
  201. window.addEventListener("mousemove", function(event) {
  202.  X = event.clientX;
  203.  Y = event.clientY;
  204. });
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