2015-01-07 25 views
0

我只想知道谷歌分析如何工作,這個代碼是什麼意思?如何創建類似於谷歌分析的自己的JavaScript api

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'some value']); 
_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); 
})(); 

預先感謝您..

+0

應該檢查https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview –

+0

首先上方,歡迎來到StackOverflow!因爲你不是這個地方別人會免費做你的工作。嘗試先對該主題進行一些研究,詢問具體問題,以及遇到什麼問題。 –

回答

3

這是一個非常廣泛的問題,但你會發現這裏https://developers.google.com/analytics/resources/concepts/gaConceptsTrackingOverview

一些有用的信息,這表明你what the code actually does以及describing all of the parameters

+0

謝謝你給我的鏈接。知道任何文章或網站,可以幫助我寫自己的API,如谷歌分析和zopim做。 –

+0

這似乎是一個相當大的任務(如果您想要相同的功能),如果可能的話,我建議您使用Google Analytics,而不是構建自己的解決方案。有沒有理由不可能做到這一點? –

0

它做的是創建一個具有一些屬性的對象(前3行),然後它將腳本標籤注入到文檔中,一旦腳本加載它將採用該js對象並將其用於跟蹤。

2

你需要在谷歌分析的閱讀,但在代碼方面:

(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); 
})(); 

是注入外部JavaScript資源到您的網頁

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'some value']); 
_gaq.push(['_trackPageview']); 

創造(如果犯規存在)並將值添加到javascript數組中

要了解它的工作原理,您需要查找文檔或進行一門課程(其中我認爲有數百萬課程)

0

此代碼在您的頁面中插入外部scrypt。
通過_gaq握着你的具體設置,和下方的功能增加了一個<script>在你的頁面的第一個腳本標籤