

$( document ).ready(function() {

});
@@ -1,39 +1,1279 @@
-%0A%0A$( document ).ready(function() %7B%0A%0A%7D);
+$(document).ready(function() %7B%0A  addToViewCounter();%0A%7D);%0A%0Aconst addToViewCounter = () =%3E %7B%0A  if (sessionStorage) %7B%0A    if (!sessionStorage.getItem('view-counter')) %7B%0A      const url =%0A        'https://mn7724hpl4.execute-api.ca-central-1.amazonaws.com/Prod/new-view';%0A%0A      const timestamp = Date.now(); // in millis%0A      const randomId = Math.floor(Math.random() * 99);%0A      const id = %60$%7Btimestamp%7D-$%7BrandomId%7D%60;%0A      const hostname = window.location.hostname %7C%7C 'unknown';%0A%0A      const postBody = %7B id, hostname %7D;%0A      const postFormData = Object.keys(postBody)%0A        .map(key =%3E %7B%0A          return (%0A            encodeURIComponent(key) + '=' + encodeURIComponent(postBody%5Bkey%5D)%0A          );%0A        %7D)%0A        .join('&');%0A%0A      fetch(url, %7B%0A        method: 'POST',%0A        mode: 'cors',%0A        headers: %7B%0A          'Content-Type': 'application/x-www-form-urlencoded',%0A        %7D,%0A        body: postFormData,%0A      %7D)%0A        .then(response =%3E %7B%0A          if (response.status === 200 %7C%7C response.status === '200') %7B%0A            console.log('Success adding to view-counter');%0A            sessionStorage.setItem('view-counter', 'true');%0A          %7D%0A        %7D)%0A        .catch(error =%3E %7B%0A          console.log('Error adding view-counter', error);%0A        %7D);%0A    %7D%0A  %7D%0A%7D;%0A

Add to view count, javascript