Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could someone explain what the following JavaScript is doing?

This was inside the script tag of 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...

like image 258
Zeeno Avatar asked Mar 22 '26 03:03

Zeeno


1 Answers

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.
like image 143
James Hill Avatar answered Mar 23 '26 17:03

James Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!