Home >Web Front-end >JS Tutorial >How to load google-analytics (or other third party) JS_javascript tips

How to load google-analytics (or other third party) JS_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:27:341326browse

After registering ga, ga will generate a js script. Many people directly copy this js to the end of (including Blog Park, CSDN, BlogJava). But is this JS automatically generated by ga really the most reasonable?

What is reasonable and what is unreasonable? Because ga is just an analysis tool, its use must not affect our program. If it does, it is unreasonable. It is reasonable if it does not affect it.

Current use of ga:
First look at the js script automatically generated by ga, as follows:

Copy code The code is as follows:

"));





"));





Monitoring picture:
How to load google-analytics (or other third party) JS_javascript tips
As can be seen from the above picture, ga cannot be loaded. After a 20-second timeout, our ajax request is executed. Our ajax request only takes 0.173s, but it waits. 20s.
Use ga properly:

To use ga properly, you need to solve 2 problems:
1. How to non-load ga’s js,
2. How to use ga in ga Immediately after the ja is loaded, execute the var pageTracker = _gat._getTracker("UA-123456-1"); pageTracker._trackPageview(); code.

There are two main ways to load js non-blockingly:
1. Dynamically create

The above code is modified from jquery’s ajax code. The above code is easy to understand. It dynamically creates a script to load js, and executes the code after loading through onload or onreadystatechange events.
After the code modification is completed, monitor and test as follows;
How to load google-analytics (or other third party) JS_javascript tips
It can be seen from the picture that ga still loaded for 20s, but our ajax request did not wait for 20s to be executed, but was executed immediately.
jquery loads ga:
Maybe you think the above code is too much to write and cumbersome. If you use jquery, it can be simplified as follows:
Copy code The code is as follows:

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http:// www.");
$.getScript(gaJsHost "google-analytics.com/ga.js",function(){
try {
var pageTracker = _gat._getTracker("UA-123456-16 ");
pageTracker._trackPageview();
} catch(err) {}
});

If necessary, please check out: High-Performance WEB Development Series

[Statement] Please indicate the source for reprinting: http://www.cnblogs.com/BearsTaR/. Commercial use prohibited!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Code for changing color and background implemented by jquery change(fontsize,background) supplement 2_jqueryNext article:Code for changing color and background implemented by jquery change(fontsize,background) supplement 2_jquery

Related articles

See more