prashandip

tracker.js

Feb 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // const news = document.getElementById("news");
  2.  
  3. // ================== output div for testing/debugging ===================
  4. // var div = document.createElement("div");
  5. // div.style.background = "rgba(0,0,0,0.9)";
  6. // div.style.overflow = "hidden";
  7. // div.style.zIndex = "10";
  8. // div.style.position = "fixed";
  9. // div.style.top = "10px";
  10. // div.style.left = "50%";
  11. // div.style.transform = "translateX(-50%)";
  12. // div.style.padding = "10px";
  13. // div.style.marginTop = "10px";
  14. // div.style.color = "white";
  15. // div.style.borderRadius = "10px";
  16.  
  17. // document.body.appendChild(div);
  18.  
  19. // ================== find if news article is in viewport =================
  20. var news = document.getElementById("news");
  21. news.style.background = "orange";
  22.  
  23. prevElemTop = news.getBoundingClientRect().top;
  24. prevElemBottom = news.getBoundingClientRect().bottom;
  25.  
  26. function newsArticleIsInViewport() {
  27.   elemTop = news.getBoundingClientRect().top;
  28.   elemBottom = news.getBoundingClientRect().bottom;
  29.   if (elemTop > window.innerHeight || elemBottom < 0) {
  30.     return false;
  31.   } else {
  32.     return true;
  33.   }
  34. }
  35. // ========================= init newsArticleWasInViewport =======
  36. var prevTimestamp = new Date(Date.now());
  37. var newsArticleWasInViewport = false;
  38. if (newsArticleIsInViewport()) {
  39.   newsArticleWasInViewport = true;
  40. } else {
  41.   newsArticleWasInViewport = false;
  42. }
  43. console.log("newsArticleIsInViewport = " + newsArticleWasInViewport);
  44.  
  45. // ========================== scroll track ========================
  46. var readTimeInSecs = 0;
  47. window.addEventListener("scroll", function() {
  48.   var currentTimestamp = new Date(Date.now());
  49.   timeDiffInMillis = currentTimestamp - prevTimestamp;
  50.   timeDiffInSecs = timeDiffInMillis / 1000;
  51.   prevTimestamp = currentTimestamp;
  52.   console.log("Difference = " + timeDiffInSecs);
  53.   if (newsArticleWasInViewport) {
  54.     // div.innerHTML = "ID=" + newsId + " | " + new Date(Date.now()).toUTCString();
  55.     if (newsArticleIsInViewport()) {
  56.       readTimeInSecs += timeDiffInSecs;
  57.       newsArticleWasInViewport = true;
  58.     } else {
  59.       //send data
  60.       readTimeInSecs = Math.floor(readTimeInSecs * 100) / 100;
  61.       alert("readTimeInSecs = " + readTimeInSecs);
  62.       sendData(readTimeInSecs);
  63.       console.log("readTimeInSecs = " + readTimeInSecs);
  64.       newsArticleWasInViewport = false;
  65.     }
  66.   } else {
  67.     // div.innerHTML = "ID=" + newsId + " | " + new Date(Date.now()).toUTCString();
  68.     readTimeInSecs = 0;
  69.     if (newsArticleIsInViewport()) {
  70.       newsArticleWasInViewport = true;
  71.     } else {
  72.       newsArticleWasInViewport = false;
  73.     }
  74.   }
  75. });
  76.  
  77. // ================= send data ===============
  78. function sendData(readTimeInSecs) {}
  79.  
  80. // ================== cookie =================
  81. //6.04e+8 --> a week in milliseconds
  82. function createCookie() {
  83.   var now = new Date();
  84.   var year = now.getUTCFullYear();
  85.   var expireYear = year + 10;
  86.   now.setYear(expireYear);
  87.   document.cookie =
  88.     "clientId=" + newsId + ";expires=" + now.toUTCString() + ";path=/";
  89.   console.log("cookie -> " + document.cookie);
  90. }
  91.  
  92. function getCookie(name) {
  93.   var value = "; " + document.cookie;
  94.   var parts = value.split("; " + name + "=");
  95.   if (parts.length == 2)
  96.     return parts
  97.       .pop()
  98.       .split(";")
  99.       .shift();
  100. }
  101.  
  102. function updateExpirationDate(clientId) {
  103.   var now = new Date();
  104.   var year = now.getUTCFullYear();
  105.   var expireYear = year + 10;
  106.   now.setYear(expireYear);
  107.   document.cookie =
  108.     "clientId=" + clientId + ";expires=" + now.toUTCString() + ";path=/";
  109. }
  110.  
  111. // ================= check cookie ==================
  112. if (document.cookie.indexOf("clientId") >= 0) {
  113.   alert("Client ID found ... expiration updated");
  114.   updateExpirationDate(getCookie("clientId"));
  115.   console.log(getCookie("clientId"));
  116. } else {
  117.   alert("Client ID not found ... hence created");
  118.   createCookie();
  119. }
  120.  
  121. // ================= ajax call post =======================
  122. // =================== single news tag fix =============
  123. var singleNewsContent = document.getElementById("single-news-content");
  124. var convert = function(convert) {
  125.   return $("<span />", { html: convert }).text();
  126. };
  127. singleNewsContent.innerHTML = convert(singleNewsContent.innerHTML);
  128.  
  129. // =================== remove feature tag from single news ===================
  130. var figureArray = singleNewsContent.getElementsByTagName("figure");
  131. // console.log(figureArray);
  132. $.each(figureArray, function(i, el) {
  133.   // console.log(el);
  134.   el.style.display = "none";
  135. });
Add Comment
Please, Sign In to add comment