Google Web tracking code--Notes

Source: Internet
Author: User
Tags tag name

see a lot of Web source code has the following:
<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>
meaning: Google Analytics ga tracking code, a section of JS code. is used for web tracking, the code to the Web page, when the page is requested, the Google server will send back the corresponding cookie data, and then form a report.

Code Analysis:
var _gaq = _gaq | | [];//defines an array that, if _gaq is not empty, takes its own value, otherwise it is initialized to null

_gaq.push ([' _setaccount ', ' ua-16407365-1 ']);//push method adds 2 string elements to the array _setaccount ' and ' ua-16407365-1 '

(function () {}) is a closure usage, and a lot of it is important in JavaScript, so you can learn about it and the closure is bound to be called.

var ga = document.createelement (' script '); ga.type = ' text/javascript '; Ga.async = true;//Creates a SCRIPT element, Script type is javasceipt, asynchronous type

ga.src = (' https: ' = = Document.location.protocol? ' Https://ssl ': ' http://www ') + '. Google-analytics.com/ga.js ';//Set the source code for this script is Ga.js

var s = document.getelementsbytagname (' script ') [0];//Get the first element with tag named script, the so-called tag name is the type name of each element in HTML, such as DIV,IMG, etc.

S.parentnode.insertbefore (GA, s);//add element GA before S

Google web tracking code--Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.