1

我試圖弄清楚跟蹤/分析腳本是如何工作的。還有的the Google Analytics code的優化版本:立即使用構造函數/ Universal Analytics調用函數

<script> 
    (function(window, document, variableName, scriptElement, firstScript) { 
    window['GoogleAnalyticsObject'] = variableName; 
    window[variableName] || (window[variableName] = function() { 
     (window[variableName].q = window[variableName].q || []).push(arguments); 
    }); 
    window[variableName].l = +new Date; 
    scriptElement = document.createElement('script'), 
    firstScript = document.scripts[0]; 
    scriptElement.src = 'https://127.0.0.1:3000/analytics.js'; 
    firstScript.parentNode.insertBefore(scriptElement, firstScript) 
    }(window, document, 'ga')); 

    ga('create', 'UA-XXXX-Y'); 
    ga('send', 'pageview'); 
</script> 

加載自定義腳本,我無法弄清楚如何GA()函數的工作。我已經嘗試了各種IIFE和構造函數,但沒有獲得「創建」和「發送」事件。

如何在服務器上看到這些事件?

更新

我設法抽象我的方式來排隊,現在想知道怎樣才能創建一個異步隊列這些事件發送到服務器。有什麼建議麼?

(function() { 
    var ga = function(a) { 
    return void 0 != a && -1 < (a.constructor + '').indexOf('String'); 
    }; 
    var sa = function(a) { 
    return a ? a.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '') : ''; 
    }; 
    var gb = ga(window.GoogleAnalyticsObject) && sa(window.GoogleAnalyticsObject) || 'ga'; 
    var Window = window; 
    var Document = document; 

    console.log(Window[gb].q); 

})(window); 
+0

*您的*代碼在哪裏? – Pointy

回答

1

該函數只是將所有調用參數推送到數組中。它後來被analytics.js通過window.GoogleAnalyticsObject拾取。谷歌似乎並沒有提供你最終的analytics.js非縮小的版本,但快速debeautify和搜索:

var gb = qa(window.GoogleAnalyticsObject) && sa(window.GoogleAnalyticsObject) || "ga" 

qa檢查,如果它是一個字符串和sa功能只是清除名字有點位。

那麼,他們還使用gb?分配只發生在別處:

N.N功能:

N.N = function() { 
     "ga" != gb && J(49); 
     var a = O[gb]; 
     if (!a || 42 != a.answer) { 
      N.L = a && a.l; 
      N.loaded = !0; 
      var b = O[gb] = N; 
      X("create", b, b.create); 
      X("remove", b, b.remove); 
      X("getByName", b, b.j, 5); 
      X("getAll", b, b.getAll, 6); 
      b = pc.prototype; 
      X("get", b, b.get, 7); 
      X("set", b, b.set, 4); 
      X("send", b, b.send); 
      b = Ya.prototype; 
      X("get", b, b.get); 
      X("set", b, b.set); 
      if (!Ud() && !Ba) { 
       a: { 
        for (var b = M.getElementsByTagName("script"), c = 0; c < b.length && 100 > c; c++) { 
         var d = b[c].src; 
         if (d && 0 == d.indexOf("https://www.google-analytics.com/analytics")) { 
          J(33); 
          b = !0; 
          break a 
         } 
        } 
        b = !1 
       } 
       b && (Ba = !0) 
      } 
      Ud() || Ba || !Ed(new Od) || (J(36), Ba = !0); 
      (O.gaplugins = O.gaplugins || {}).Linker = Dc; 
      b = Dc.prototype; 
      Yd.set("linker", Dc); 
      X("decorate", b, b.ca, 20); 
      X("autoLink", b, b.S, 25); 
      Yd.set("displayfeatures", fd); 
      Yd.set("adfeatures", fd); 
      a = a && a.q; 
      ka(a) ? Z.D.apply(N, a) : J(50) 
     } 
    }; 

分配發生在:

var a = O[gb]; //O is window 

你正在使用你的腳本ga功能,很快就會被別的東西來代替( N):

var b = O[gb] = N; 

這是N:

var N = function(a) { 
     J(1); 
     Z.D.apply(Z, [arguments]) 
    }; 

該隊列在哪裏被使用?

a = a && a.q; 
ka(a) ? Z.D.apply(N, a) : J(50) 

Z.D函數似乎是執行您的參數的函數。其中使用更多縮小的功能。我建議你繼續從這裏看。

+0

感謝提示。谷歌分析是相當多的經歷。管理抽象的代碼,但仍然卡住。 – Patrick