Google網頁追蹤代碼 —— 筆記

來源:互聯網
上載者:User

標籤:class   node   添加元素   類型   原始碼   初始化   ==   page   googl   

看到很多網頁源碼有以下代碼:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([‘_setAccount‘, ‘UA-16407365-1‘]);
_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);
})();
</script>
含義:Google分析 GA的追蹤代碼,一段JS代碼。是用於網頁追蹤的,把這段代碼網到網頁上,當網頁被請求時,就會向Google的伺服器發送回相應的COOKIES資料,然後形成報告。

程式碼分析:
var _gaq = _gaq || [];//定義一個數組,若_gaq不為空白,則取自己的值,否則初始化為空白

_gaq.push([‘_setAccount‘, ‘UA-16407365-1‘]);//push方法向數組中添加2個字串元素_setAccount‘和‘UA-16407365-1‘

(function(){})是一個閉包的用法,在javascript中用的很多也很重要,可以去好好學一下,閉包必定會被調用。

var ga = document.createElement(‘script‘); ga.type = ‘text/javascript‘; ga.async = true;//建立一個script元素,指令碼類型為javasceipt,非同步類型

ga.src = (‘https:‘ == document.location.protocol ? ‘https://ssl‘ : ‘http://www‘) + ‘.google-analytics.com/ga.js‘;//設定這個script的原始碼為ga.js

var s = document.getElementsByTagName(‘script‘)[0]; //取得第一個tag名為script的元素,所謂tag名就是html中每一個元素的類型名字,比如div,img等

s.parentNode.insertBefore(ga, s);//在s前添加元素ga

Google網頁追蹤代碼 —— 筆記

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.