Interpreting Google Analytics Code allows you to fully DIY

Source: Internet
Author: User
Tags try catch

Our marketing activities are basically liberal arts, and our understanding of code is not enough, but sometimes we still have to learn some code, especially the Code related to Google ads, today I will share my analyticsdiy article with you.

This is the tracking code of Google Analytics:

 

Reference content is as follows:
<SCRIPT type = "text/JavaScript">
VaR gajshost = ("https:" = Document. Location. Protocol )? "Https: // SSL.": "http: // www .");
Document. Write (Unescape ("% 3 cscript src = '" + gajshost + "google-analytics.com/ga.js'type = 'text/JavaScript' % 3E % 3C/script % 3E "));
</SCRIPT>
<SCRIPT type = "text/JavaScript">
Try {
VaR pagetracker = _ gat. _ gettracker ("UA-8459969-1 ″);
Pagetracker. _ trackpageview ();
} Catch (ERR) {}</SCRIPT>

 

I. Part 1 (blue text)
1. ("https:" = Document. Location. Protocol )? "Https: // SSL.": http: // www .);
This JavaScript ternary operator defines the gajshost variable so that analytics can identify the type of URL address on the page where the code is located, such as HTTPS.
2. use the DOM object function document. write, write a string of HTML code to the page where it is located. The purpose of this string of HTML code is to let the page go to the GA server to download a GA code. JS statistical function file for Ga statistics. In addition, the above gajshost variable also determines the different ga. js files on different pages.

The first part of the code implements the above two goals, but we can simplify it based on the situation of our website:

 

Reference content is as follows:
1. First, Ga. js code is downloaded to the local, such as the GA. js in the http://www.mysem.net/ga.js, the only advantage of doing so is to make ga. js loading speed = site speed. However, as the connection speed of GA increases, and if the GA code is updated after the download, we may not be the latest, so this is basically unnecessary.
2. If your website clearly begins with http: //, rather than https. The first part can be written as follows: <SCRIPT src = "http://www.mysem.net/ga.js"> </SCRIPT>. (Type = "text/JavaScript" can be left empty. All browsers use the JS language as the script element by default .)

 

Ii. Part 2
1.Try catchThe function of the statement is to prevent the page from stopping loading due to unexpected errors when communicating with the GA server.
2. DefinitionPagetrackerInstance Object and associate it with your account ID.
3.Pagetracker. _ trackpageview ()A function records a PV and returns the address of the page to the GA server.

The purpose of the second part is also obvious, that is, corresponding to your account ID, the number of PV records and the page url address. We can also modify it based on the purpose:

1. define that the page is opened for the first time before it is recorded as PV. If not, do not remember. Determine whether the window. Name is null for the first time in the browser. Write Other auxiliary code to filter the spam traffic.

 

Reference content is as follows:
Try {
VaR pagetracker = _ gat. _ gettracker ("UA-8459969-1 ″);
If (! Window. Name ){
Pagetracker. _ trackpageview ();}
} Catch (ERR ){}

 

2. One PV record. You can establish a connection with two IDs so that the access data is recorded in the statistical database of the two IDs at the same time.

 

Reference content is as follows:
<SCRIPT type = "text/JavaScript">
Try {
VaR pagetracker = _ gat. _ gettracker ("UA-8459969-1 ″);
VaR pagetracker1 = _ gat. _ gettracker ("UA-8360212-1 ″);
Pagetracker. _ trackpageview ();
Pagetracker1. _ trackpageview ();}
Catch (ERR ){}
</SCRIPT>

 

I don't know if you understand it. If you understand it, let's go over it. I suddenly remembered that when I used to convert my competitor adwords, I just got someone else's conversion code and put it on a page.

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.