开发者

Could someone explain what the following JavaScript is doing?

This was inside the script tag o开发者_开发百科f a website.

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

I'm not very conversant with Javascript or jQuery. I saw this on a website and I'm wondering what it does...


The code in your post is Google's analytic tracking code. It's not jQuery, it's pure JavaScript. Google does a good job explaining what their tracking code does.

Google Docs Resources

  • What is Google Analytics
  • Google Analytics Code Explained

Quote from Google's docs:

In general, the Google Analytics Tracking Code (GATC) retrieves web page data as follows:

  1. A browser requests a web page that contains the tracking code.
  2. A JavaScript Array named _gaq is created and tracking commands are pushed onto the array.
  3. A element is created and enabled for asynchronous loading (loading in the background).
  4. The ga.js tracking code is fetched, with the appropriate protocol automatically detected. Once the code is fetched and loaded, the commands on the_gaq array are executed and the array is transformed into a tracking object. Subsequent tracking calls are made directly to Google Analytics.
  5. Loads the script element to the DOM.
  6. After the tracking code collects data, the GIF request is sent to the Analytics database for logging and post-processing.


This dynamically inserts a script tag which then loads and executes a Google Analytics script. Google Analytics is typically installed by a site owner in order to gather web usage statistics for the site (page hits, user viewing patterns, browser types, etc...).

The main reason for doing it this way is that the script is loaded over the same protocol (http/https) as the the page is and it's loaded asychronously and therefore the loading of the page content does not wait for this script to load. It loads on it's own time and executes when it's loaded. The page can display before it is done loading. If it was specified in a normal script tag, the page would have to wait for this to finish loading before continuing.

  1. The first three liens set up and initialize a global variable _gaq which the script will then use the data from.
  2. Then, a script element is created.
  3. It's type is set to "text/javascript".
  4. The .async property is set to true.
  5. The .src property is set to use the same http or https protocol that the page uses.
  6. The script tag is inserted into the head element of the page.


It's loading the google analytics javascript code by creating a script element and placing it in the document.

You can see the documentation here:

http://www.google.com/support/googleanalytics/bin/answer.py?answer=55488&utm_source=DiscoverList&utm_medium=et&utm_campaign=en_us&hl=en


It's google analytics. It allows the website to view stats, like how long you have been on the page, where you went to, where you came from...


That is google analytics code, but it's creating a script tag, , and setting the source of that to google-analytics.com/ga.js

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜