This document covers how to use the Analytics Tracking Code with the latest asynchronous syntax. With this version of the tracking code, you can also place the Analytics snippet higher in the page without delaying subsequent content from rendering. See the Migration Examples page for a side-by-side comparison of the traditional and asynchronous syntaxes.
Tracking Code Quickstart
The Analytics snippet is a small piece of JavaScript code that you paste into your pages. It activates Google Analytics tracking by inserting
ga.js
into the page. To use this on your pages, copy the code snippet below, replacing UA-XXXXX-X
with your web property ID. Paste this snippet into your website template page so that it appears before the closing </head>
tag.
If you need to do more than basic page tracking, see the tracking reference for a list of methods available in the API and see theUsage Guide for details on using the asynchronous syntax. For step-by-step instructions on setting up tracking, see the Help Center article on setting up tracking.
<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _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>
The snippet above represents the minimum configuration needed to track a page asynchronously. It uses
_setAccount
to set the page's web property ID and then calls _trackPageview
to send the tracking data back to the Google Analytics servers.
Important: If you are updating your pages from the traditional snippet to the latest, asynchronous version, you should remove the existing tracking snippet first. We do not recommend using both snippets together on the same page. For migration instructions, see Migrating to Async.
How the Asynchronous Syntax Works
The
_gaq
object is what makes the asynchronous syntax possible. It acts as a queue, which is a first-in,first-out data structure that collects API calls until ga.js
is ready to execute them. To add something to the queue, use the _gaq.push
method.
To push an API call onto the queue, you must convert it from the traditional JavaScript syntax into a command array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a command array is the name of the tracker object method you want to call. It must be a string. The rest of the elements are the arguments you want to pass to the tracker object method. These can be any JavaScript value.
The following code calls
_trackPageview()
using the traditional syntax:var pageTracker = _gat._getTracker('UA-XXXXX-X'); pageTracker._trackPageview();
The equivalent code in the asynchronous syntax requires two calls to
_gaq.push
._gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_trackPageview']);
In the asynchronous syntax, the creation of the tracker object is implied, but we still need a way to set the web property ID for the tracker. The
_setAccount
method has been added to provide this capability. All the other tracker object methods are the same in both asynchronous and traditional tracking. Only the syntax is different.
For more information on the asynchronous syntax, see the Tracking Reference for the
_gaq.push
method.
沒有留言:
張貼留言