2011-12-12 179 views
2

這是一個谷歌Analytics(分析)代碼谷歌分析JavaScript的

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-20366831-2']); 
_gaq.push(['_trackPageview']); 

(function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
})(); 

我的客戶端如何調用谷歌匿名函數?

回答

5

這就是所謂的,因爲匿名函數與()

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
})(); // <--- The() calls the anonymous code 

結束正如你看到的,這個代碼基本上注入一個script標籤到DOM,它獲取瀏覽器上運行。

0

該代碼段已經自行調用。

(function() { 
 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
 
})();

它實際上做的是包括你的頁面,這是類似這樣的的ga.js

<script src="//google-analytics.com/ga.js" />

休息取決於你向_gaq添加事件(谷歌分析tic隊列)。然後該事件將自動處理。