VKirill

Untitled

Aug 12th, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  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(89820019, "reachGoal", "180sec");
  11. /* Тут перечислять все что нужно будет вызвать по достижению цели */
  12. }
  13. };
  14.  
  15. var ActiveScore = {
  16. /* Настройки которые надо менять */
  17. need: 180, // секунды
  18. checkTime: 20, // секунды. период проверки
  19. loop: false, // Повторять набор?
  20. /* Настройки которые надо менять */
  21.  
  22. cookieName: "180sec_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.timer = 0;
  128. this.active_parts = 0;
  129. return false;
  130. } else {
  131. // console.log('Завершили проверку активности');
  132. return true;
  133. }
  134. }
  135. return false;
  136. },
  137.  
  138. timeoutFn: function () {
  139. ActiveScore.checkPeriod();
  140. },
  141.  
  142. runPeriod: function () {
  143. this.timeoutId = setTimeout(this.timeoutFn, this.checkTime * 1000);
  144. },
  145.  
  146. send: function () {
  147. this.setCookie(this.cookieName, this.active_parts * this.active_parts);
  148. this.sendFn();
  149. },
  150.  
  151. checkCookie: function () {
  152. var c = this.getCookie(this.cookieName);
  153.  
  154. if (c == null) {
  155. return true;
  156. } else {
  157. c = parseInt(c);
  158. if (c >= this.parts) {
  159. // console.log('Скрипт даже не запустился...');
  160. if (this.loop == true) {
  161. return true;
  162. }
  163. return false;
  164. } else {
  165. this.active_parts = c;
  166. return true;
  167. }
  168. }
  169. },
  170.  
  171. setCookie: function (name, value, days) {
  172. var expires = "";
  173. if (days) {
  174. var date = new Date();
  175. date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
  176. expires = "; expires=" + date.toUTCString();
  177. }
  178. document.cookie = name + "=" + (value || "") + expires + "; path=/";
  179. },
  180. getCookie: function (name) {
  181. var nameEQ = name + "=";
  182. var ca = document.cookie.split(";");
  183. for (var i = 0; i < ca.length; i++) {
  184. var c = ca[i];
  185. while (c.charAt(0) == " ") c = c.substring(1, c.length);
  186. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  187. }
  188. return null;
  189. },
  190. eraseCookie: function (name) {
  191. document.cookie =
  192. name + "=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  193. },
  194. };
  195.  
  196. ActiveScore.init(metricsFn);
  197. </script>
Advertisement
Add Comment
Please, Sign In to add comment