Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var trackMultitouch = function(trackerType, domain, path, trackerName) {
  2.     // ------------------------------------------------------------------------
  3.  
  4.     this.conversion = function() {
  5.         var d = domain || ('.' + (/(www.)?([^\/]+)/.exec(document.location.hostname)[2]));
  6.         var p = path || '/';
  7.         if (!getCookie('__mtvis')){
  8.             trackSession(function() {
  9.                 trackGAEvent(trackerName, trackerType, getCookie('__mtvis'));
  10.                 deleteCookie('__mtvis', p, d);
  11.             });
  12.         }
  13.         else {
  14.             trackGAEvent(trackerName, trackerType, getCookie('__mtvis'));
  15.             deleteCookie('__mtvis', p, d);
  16.         }
  17.     };
  18.  
  19.     // ------------------------------------------------------------------------
  20.     var getCurrentSourceMedium = function(callback) {
  21.         var utmz = getCookie('__utmz'); //__utmz – переменная содержит информацию о переходе на сайт (реферер, реклама, ввод в адресной строке). Время жизни - 6 месяцев
  22.         if (_gaq instanceof Array) { // если массив _gaq принадлежит классу Array
  23.             setTimeout(arguments.callee(callback), 4000); // arguments.callee(callback) возвращает callback выполняется через 4 секунды
  24.         }
  25.  
  26.         var medium, source, m_changed, s_changed;
  27.  
  28.         m_changed = false;
  29.         s_changed = false;
  30.  
  31.         if (/utmgclid/.exec(utmz)) {
  32.             return callback("adw");
  33.         }
  34.         else {
  35.             try {
  36.                 medium = /utmcmd=\(?([^\|;]+)\)?/.exec(utmz)[1];
  37.                 source = /utmcsr=\(?([^\|;\)]+)/.exec(utmz)[1];
  38.             }
  39.             catch (err) {
  40.                 medium = 'error';
  41.                 source = 'error';
  42.             }
  43.         }
  44.  
  45.         if (source.indexOf("fashion") !== -1) {
  46.             source = source.replace(/fashion/g, "f-");
  47.             s_changed = true;
  48.         }
  49.         else if (source.indexOf("yandex_") !== -1){
  50.             source = source.replace(/yandex_/g, "y-");
  51.             s_changed = true;
  52.         }
  53.         else if (source.indexOf("yandex") !== -1){
  54.             source = source.replace(/yandex/g, "y-");
  55.             s_changed = true;
  56.         }
  57.         else if (source.indexOf("rambler") !== -1){
  58.             source = source.replace(/rambler/g, "r-");
  59.             s_changed = true;
  60.         }
  61.         else if (source.indexOf("vkont_") !== -1){
  62.             source = source.replace(/vkont_/g, "vk-");
  63.             s_changed = true;
  64.         }
  65.         else if (source.indexOf("rasprodaga") !== -1){
  66.             source = source.replace(/rasprodaga/g, "rp-");
  67.             s_changed = true;
  68.         }
  69.         else if (source.indexOf("gnezdo") !== -1){
  70.             source = source.replace(/gnezdo/g, "gn-");
  71.             s_changed = true;
  72.         }
  73.         else if (source.indexOf("shopping") !== -1){
  74.             source = source.replace(/shopping/g, "s-");
  75.             s_changed = true;
  76.         }
  77.         else if (source.indexOf("widget") !== -1){
  78.             source = source.replace(/widget/g, "w-");
  79.             s_changed = true;
  80.         }
  81.         else if (source.indexOf("mamba_") !== -1){
  82.             source = source.replace(/mamba_/g, "m-");
  83.             s_changed = true;
  84.         }
  85.  
  86.  
  87.         if (medium.indexOf("partner") !== -1) {
  88.             medium = medium.replace(/partner_/g, "p-");
  89.             m_changed = true;
  90.         }
  91.         else if (medium.indexOf("vkont_") !== -1) {
  92.             medium = medium.replace(/vkont_/g, "vk-");
  93.             m_changed = true;
  94.         }
  95.  
  96.  
  97.         return callback( (s_changed ? source : source.slice(0, 5)).replace(/_/g, "-") + '/' + (m_changed ? medium : medium.slice(0, 5)).replace(/_/g, "-") );
  98.  
  99.  
  100.         /*
  101.          switch(medium) {
  102.          case "organic":
  103.          return  callback(source.slice(0,2) + "/org");
  104.          case "referral":
  105.          return callback("ref");
  106.          case "(none)":
  107.          return callback("drct");
  108.          case "error":
  109.          return callback("err");
  110.          case '':
  111.          return callback("err");
  112.          case "yandex_cpc":
  113.          return callback("ynd");
  114.          case "begun_cpc":
  115.          return callback("beg");
  116.          case "vk_cpc":
  117.          return callback("vk");
  118.          case "partner":
  119.          return callback("vk");
  120.          default:
  121.          return callback(source.slice(0,3) + '/' + medium.slice(0,3));
  122.          }
  123.          */
  124.     };
  125.  
  126.     var updateMTCookie = function(value, path, domain) {
  127.         var mtvis = getCookie('__mtvis');
  128.         var res;
  129.  
  130.         if (!mtvis) {
  131.             res = value;
  132.         }
  133.  
  134.         else {
  135.             var current_value = mtvis.split("_");
  136.             var last_value = parseInt(current_value[current_value.length - 1], 10) ? { val : current_value[current_value.length - 2], isNumber : true } :
  137.                 { val : current_value[current_value.length - 1], isNumber : false };
  138.  
  139.             if (last_value.val === value) {
  140.                 if (last_value.isNumber) {
  141.                     current_value[current_value.length - 1] = parseInt(current_value[current_value.length - 1], 10) + 1;
  142.                 }
  143.                 else {
  144.                     current_value.push('2');
  145.                 }
  146.             }
  147.             else {
  148.                 current_value.push(value);
  149.             }
  150.  
  151.             res = current_value.join("_");
  152.         }
  153.  
  154.         var date = new Date();
  155.         date.setDate(date.getDate() + 30);
  156.         setCookie('__mtvis', res, date.getFullYear(), date.getMonth(), date.getDate(), path, domain, '');
  157.     };
  158.  
  159.     var trackSession = function(callback) {
  160.         var p = path || '/';
  161.         var d = domain || ('.' + (/(www.)?([^\/]+)/.exec(document.location.hostname)[2]));
  162.  
  163.         getCurrentSourceMedium(function(value) {
  164.             updateMTCookie(value, p, d);
  165.             setCookie('__mtsess', 1, '', '', '', p, d, '');
  166.             if (callback instanceof Function) {
  167.                 callback();
  168.             }
  169.         });
  170.     };
  171.  
  172.     var trackGAEvent = function(trackerName, trackerType, value) {
  173.         if (trackerType === 'async') {
  174.             var trackerFunction = trackerName ? trackerName + '._trackEvent' : '_trackEvent';
  175.             _gaq.push([trackerFunction, 'Mtouch', value]);
  176.         }
  177.         else if (trackerType === 'sync') {
  178.             trackerName._trackEvent('Mtouch', value);
  179.         }
  180.     };
  181.  
  182.  
  183.     // ------------------------------------------------------------------------
  184.     var getCookie = function(cookie_name) {
  185.         try {
  186.             var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
  187.  
  188.             if (results) {
  189.                 return (unescape(results[2]));
  190.             }
  191.             else {
  192.                 return null;
  193.             }
  194.         } catch(e) {}
  195.     };
  196.  
  197.     var setCookie = function(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
  198.         var cookie_string = name + "=" + escape (value);
  199.  
  200.         if (exp_y) {
  201.             var expires = new Date (exp_y, exp_m, exp_d);
  202.             cookie_string += "; expires=" + expires.toGMTString();
  203.         }
  204.         if (path) {
  205.             cookie_string += "; path=" + escape (path);
  206.         }
  207.  
  208.         if (domain) {
  209.             cookie_string += "; domain=" + escape (domain);
  210.         }
  211.  
  212.         if (secure) {
  213.             cookie_string += "; secure";
  214.         }
  215.  
  216.         document.cookie = cookie_string;
  217.     };
  218.  
  219.     var deleteCookie = function(name, path, domain) {
  220.         if (document.cookie.match(name)) {
  221.             document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") +
  222.                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  223.         }
  224.     };
  225.  
  226.     // ------------------------------------------------------------------------
  227.  
  228.     if (getCookie('__mtsess')) {
  229.         return;
  230.     }
  231.  
  232.     else {
  233.         trackSession();
  234.     }
  235. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement