Advertisement
VKirill

Yandex.Metrika - 60sec

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