Advertisement
VKirill

Untitled

Aug 18th, 2022
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var settings = {
  2.     /* Настройки которые надо менять */
  3.     need: 30, // секунды
  4.     checkTime: 10, // секунды. период проверки.
  5.     //желательно, чтобы need было кратно checkTime
  6.     /* Настройки которые надо менять */
  7. }
  8.  
  9.  
  10.  
  11. var metricsFn = function () {
  12.     console.log(ActiveScore.timer);
  13.     console.log(ActiveScore.need);
  14.     var c1 = this.getCookie(this.cookieName);
  15.     console.log(c1);
  16.     if (ActiveScore.timer >= ActiveScore.need) {
  17.         console.log("событие отправилось");
  18.         /* Тут перечислять все что нужно будет вызвать по достижению цели */
  19.         ym(89588318, "reachGoal", this.cookieName.slice(0, -3));
  20.         /* Тут перечислять все что нужно будет вызвать по достижению цели */
  21.     }
  22. };
  23.  
  24. var ActiveScore = {
  25.     need: settings.need,
  26.     checkTime: settings.checkTime,
  27.     loop: true,
  28.     counter: 0,
  29.     cookieName: "60sec_ap",
  30.     sendFn: null,
  31.     parts: 0,
  32.     active_parts: 0,
  33.     timer: 0,
  34.     events: [
  35.         "touchmove",
  36.         "blur",
  37.         "focus",
  38.         "focusin",
  39.         "focusout",
  40.         "load",
  41.         "resize",
  42.         "scroll",
  43.         "unload",
  44.         "click",
  45.         "dblclick",
  46.         "mousedown",
  47.         "mouseup",
  48.         "mousemove",
  49.         "mouseover",
  50.         "mouseout",
  51.         "mouseenter",
  52.         "mouseleave",
  53.         "change",
  54.         "select",
  55.         "submit",
  56.         "keydown",
  57.         "keypress",
  58.         "keyup",
  59.         "error",
  60.     ],
  61.  
  62.     setEvents: function () {
  63.         for (var index = 0; index < this.events.length; index++) {
  64.             var eName = this.events[index];
  65.             window.addEventListener(eName, function (e) {
  66.                 if (e.isTrusted && ActiveScore.period.events == false) {
  67.                     ActiveScore.period.events = true;
  68.                 }
  69.             });
  70.         }
  71.     },
  72.  
  73.     period: {
  74.         start: 0,
  75.         end: 0,
  76.         events: false,
  77.     },
  78.  
  79.     init: function (fn) {
  80.         this.calcParts();
  81.         this.setEvents();
  82.         this.setStartCounter();
  83.         if (this.checkCookie()) {
  84.             this.sendFn = fn;
  85.             this.start();
  86.         }
  87.     },
  88.  
  89.     readLastCookie: function () {
  90.         var absurdlyLarge = 100000;
  91.         for (var i = 1; i < absurdlyLarge; i++) {
  92.             var cookie = this.getCookie(i * this.need + 'sec_ap');
  93.             if (cookie != this.parts * this.parts) return i;
  94.         }
  95.         return 1;
  96.     },
  97.  
  98.     setStartCounter: function () {
  99.         this.counter = this.readLastCookie() - 1;
  100.         this.cookieName = (this.counter + 1) * this.need + "sec_ap";
  101.     },
  102.  
  103.     calcParts: function () {
  104.         this.parts = Math.ceil(this.need / this.checkTime);
  105.     },
  106.  
  107.     setPeriod: function () {
  108.         this.period.start = this.microtime();
  109.         this.period.end = this.period.start + this.checkTime;
  110.         this.period.events = false;
  111.     },
  112.  
  113.     microtime: function () {
  114.         var now = new Date().getTime() / 1000;
  115.         var s = parseInt(now);
  116.         return s;
  117.     },
  118.  
  119.     start: function () {
  120.         this.setPeriod();
  121.         this.runPeriod();
  122.     },
  123.  
  124.     timeoutId: null,
  125.  
  126.     checkPeriod: function () {
  127.         if (this.period.events == true) {
  128.             this.active_parts = this.active_parts + 1;
  129.             // console.log('В этой секции были действия');
  130.         } else {
  131.             // console.log('В этой секции НЕБЫЛО действия');
  132.         }
  133.         this.timer = this.active_parts * this.checkTime;
  134.         console.log(
  135.             this.active_parts + " / " + this.parts + " [" + this.timer + "]"
  136.         );
  137.  
  138.         if (this.checkSecs()) {
  139.         } else {
  140.             this.start();
  141.         }
  142.         this.setCookie(this.cookieName, this.active_parts);
  143.     },
  144.  
  145.     checkSecs: function () {
  146.         if (this.timer >= this.need) {
  147.             this.send();
  148.             if (this.loop == true) {
  149.                 this.counter++;
  150.                 this.timer = 0;
  151.                 this.active_parts = 0;
  152.                 this.cookieName = (this.counter + 1) * this.need + "sec_ap";
  153.                 return false;
  154.             } else {
  155.                 // console.log('Завершили проверку активности');
  156.                 return true;
  157.             }
  158.         }
  159.         return false;
  160.     },
  161.  
  162.     timeoutFn: function () {
  163.         ActiveScore.checkPeriod();
  164.     },
  165.  
  166.     runPeriod: function () {
  167.         this.timeoutId = setTimeout(this.timeoutFn, this.checkTime * 1000);
  168.     },
  169.  
  170.     send: function () {
  171.         if (this.getCookie(this.cookieName) == this.parts * this.parts) {
  172.             this.setStartCounter();
  173.         } else {
  174.             this.setCookie(this.cookieName, this.active_parts * this.active_parts);
  175.         }
  176.         this.sendFn();
  177.     },
  178.  
  179.     checkCookie: function () {
  180.         var c = this.getCookie(this.cookieName);
  181.         if (c == null) {
  182.             return true;
  183.         } else {
  184.             if (c == '') return true;
  185.             c = parseInt(c);
  186.             if (c >= this.parts) {
  187.                 // console.log('Скрипт даже не запустился...');
  188.                 if (this.loop == true) {
  189.                     return true;
  190.                 }
  191.                 return false;
  192.             } else {
  193.                 this.active_parts = c;
  194.                 return true;
  195.             }
  196.         }
  197.     },
  198.  
  199.     setCookie: function (name, value, days) {
  200.         var expires = "";
  201.         if (days) {
  202.             var date = new Date();
  203.             date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
  204.             expires = "; expires=" + date.toUTCString();
  205.         }
  206.         document.cookie = name + "=" + (value || "") + expires + "; path=/";
  207.     },
  208.     getCookie: function (name) {
  209.         var nameEQ = name + "=";
  210.         var ca = document.cookie.split(";");
  211.         for (var i = 0; i < ca.length; i++) {
  212.             var c = ca[i];
  213.             while (c.charAt(0) == " ") c = c.substring(1, c.length);
  214.             if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  215.         }
  216.         return null;
  217.     },
  218.     eraseCookie: function (name) {
  219.         document.cookie =
  220.             name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  221.     },
  222. };
  223.  
  224. ActiveScore.init(metricsFn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement