This reference describes the methods that you use for customizing all aspects of Google Analytics reporting.
ga.js Basic Methods
-
_deleteCustomVar(index)
-
_getName()
-
_getAccount()
-
_getVersion()
-
_getVisitorCustomVar(index)
-
_initData()
deprecated -
_setAccount(accountID)
-
_setCookiePersistence(milliseconds)
deprecated -
_setCustomVar(index, name, value, opt_scope)
-
_setSampleRate(newRate)
-
_setSessionTimeout(newTimeout)
deprecated -
_setSessionCookieTimeout(cookieTimeoutMillis)
-
_setSiteSpeedSampleRate(sampleRate)
-
_setVar(newVal)
deprecated -
_setVisitorCookieTimeout(cookieTimeoutMillis)
-
_trackPageLoadTime()
deprecated -
_trackPageview(opt_pageURL)
Method Details
_deleteCustomVar()
_deleteCustomVar(index)
This method deletes the variable assigned to the supplied index, if one exists. For example, you might set a visitor-level custom variable and later decide that you no longer want to use this visitor-level variable.
Async Snippet (recommended)
_gaq.push(['_deleteCustomVar', 1]);
parameters
Int index
The index of the custom variable to delete.
_getName()
_getName()
Returns the name the tracker was given when it was created.
Async Snippet (recommended)
_gaq.push(function() { var pageTracker = _gat._getTrackerByName(); // Gets the default tracker. var trackerName = pageTracker._getName(); });
returns
String
The tracker's name.
_getAccount()
_getAccount()
Returns the Google Analytics ID for this tracker object. If you are tracking pages on your website in multiple accounts, you can use this method to determine the account that is associated with a particular tracker object.
Async Snippet (recommended)
_gaq.push(function() { var pageTracker = _gat._getTrackerByName(); // Gets the default tracker. var accountId = pageTracker._getAccount(); });
returns
String
Account ID this tracker object is instantiated with.
_getVersion()
_getVersion()
Returns the GATC version number.
Async Snippet (recommended)
_gaq.push(function() { var pageTracker = _gat._getTrackerByName(); // Gets the default tracker. var version = pageTracker._getVersion(); });
returns
String
GATC version number.
_getVisitorCustomVar()
_getVisitorCustomVar(index)
Returns the visitor level custom variable value assigned for the specified index.
Async Snippet (recommended)
_gaq.push(function() { var pageTracker = _gat._getTrackerByName(); // Gets the default tracker. var visitorCustomVar1Value = pageTracker._getVisitorCustomVar(1); });
parameters
Int index
The index of the visitor level custom variable.
returns
String
The value of the visitor level custom variable. Returns undefined if unable to retrieve the variable for the specified index.
_initData()
_initData()
Deprecated. initData() now executes automatically in the ga.js tracking code.
Initializes or re-initializes the GATC (Google Analytics Tracker Code) object.
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._trackPageview();
_setAccount()
_setAccount(accountId)
Used exclusively in asynchronous tracking. Sets the web property ID for the tracking object.
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
parameters
String accountID
The full web property ID (e.g. UA-65432-1
) for the tracker object.
_setCookiePersistence()
_setCookiePersistence(milliseconds)
This method is deprecated. Please use
_setVisitorCookieTimeout(cookieTimeoutMillis)
instead.
Sets the Google Analytics visitor cookie expiration in milliseconds. By default, the visitor cookie is set to expire in 2 years. If you prefer, you can change the expiration date of the visitor cookie using this method. For example, to set the expiration of the visitor cookie to 7 days you would use the following code:
pageTracker._setCookiePersistence(604800000);
parameters
Number milliseconds
New visitor cookie expiration time.
_setCustomVar()
_setCustomVar(index, name, value, opt_scope)
Sets a custom variable with the supplied name, value, and scope for the variable. There is a 128-byte character limit for the name
and value
combined.
Async Snippet (recommended)
_gaq.push(['_setCustomVar', 1, 'Section', 'Life & Style', 3]);
returns
Boolean
This method returns true
if the custom variable has been set successfully, and false
if it has not (e.g. if your name/value string length exceeds 128 bytes, or if you use an incorrect slot).
parameters
Int index
Required. The slot used for the custom variable. Possible values are 1-5
, inclusive.
String name
Required. The name for the custom variable.
String value
Required. The value for the custom variable.
Int opt_scope
Optional. The scope used for the custom variable. Possible values are 1
for visitor-level, 2
for session-level, and 3
for page-level.
_setSampleRate()
_setSampleRate(newRate)
Sets the new sample rate. If your website is particularly large and subject to heavy traffic spikes, then setting the sample rate ensures un-interrupted report tracking. Sampling in Google Analytics occurs consistently across unique visitors, so there is integrity in trending and reporting even when sampling is enabled, because unique visitors remain included or excluded from the sample, as set from the initiation of sampling.
You only need to specify this method at the same time that _setAccount() is called, typically once per page or application (wherever you initialize the tracking code itself). Keep in mind that the sample rate value you specify remains in effect for as long as the tracking object itself persists.
Async Snippet (recommended)
_gaq.push(['_setSampleRate', '80']);
parameters
String newRate
New sample rate to set. Provide a numeric string as a whole percentage.
_setSessionTimeout()
_setSessionTimeout(newTimeout)
This method is deprecated. Please use
_setSessionCookieTimeout(cookieTimeoutMillis)
instead.
Sets the new session timeout in seconds. By default, session timeout is set to 30 minutes (1800 seconds). Session timeout is used to compute visits (see Session in our Help Center). If you want to change the definition of a "session" for your particular needs, you can pass in the number of seconds to define a new value. This will impact the Visits reports in every section where the number of visits are calculated, and where visits are used in computing other values. For example, the number of visits will generally increase if you shorten the session timeout, and will generally decrease if you increase the session timeout.
parameters
String newTimeout
New session timeout to set in seconds.
_setSessionCookieTimeout()
_setSessionCookieTimeout(cookieTimeoutMillis)
Sets the new session cookie timeout in milliseconds. By default, session timeout is set to 30 minutes. Session timeout is used to compute visits, since a visit ends after 30 minutes of browser inactivity or upon browser exit. If you want to change the definition of a "session" for your particular needs, you can pass in the number of milliseconds to define a new value. This will impact the Visits reports in every section where the number of visits are calculated, and where visits are used in computing other values. For example, the number of visits will increase if you shorten the session timeout, and will decrease if you increase the session timeout. You can change the expiration timeout to 0 to indicate that this cookie should be deleted when the browser is closed.
Async Snippet (recommended)
_gaq.push(['_setSessionCookieTimeout', 1800000]);
parameters
Number cookieTimeoutMillis
New session timeout in milliseconds or 0 to delete the cookie when the browser is closed.
_setSiteSpeedSampleRate()
_setSiteSpeedSampleRate(sampleRate)
Defines a new sample set size for Site Speed data collection. By default, a fixed 1% sampling of your site visitors make up the data pool from which the Site Speed metrics are derived. If you have a relatively small number of daily visitors to your site, such as 100,000 or fewer, you might want to adjust the sampling to a larger rate. This will provide increased granularity for page load time and other Site Speed metrics. (See Site Speed in the Help Center for details about the Site Speed reports.)
The _setSiteSpeedSampleRate()
method must be called
prior to _trackPageview()
in order to be effective.
Analytics restricts Site Speed collection hits for a single property to the greater of 1% of users or 10K hits per day in order to ensure an equitable distribution of system resources for this feature.
Note: We strongly encourage sites with greater than 1 million hits per day to keep their sample selection set to the default 1% rate. Adjusting the sample size to a larger number will not increase your sample size.
Async Snippet (recommended)
_gaq.push(['_setSiteSpeedSampleRate', 5]); _gaq.push(['_trackPageview']);
parameters
Number sampleRate
Value between 0 - 100 to
define the percentage of visitors to your site that will be
measured for Site Speed purposes. For example, a value
of 5
sets the Site Speed collection sample to 5%.
_setVisitorCookieTimeout()
_setVisitorCookieTimeout(cookieTimeoutMillis)
Sets the Google Analytics visitor cookie expiration in milliseconds. By default, the visitor cookie is set to expire in 2 years. If you prefer, you can change the expiration date of the visitor cookie using this method. You can change the expiration timeout to 0 to indicate that this cookie should be deleted when the browser is closed.
Async Snippet (recommended)
_gaq.push(['_setVisitorCookieTimeout', 63072000000]);
parameters
Number cookieTimeoutMillis
New visitor cookie expiration time in milliseconds or 0 to delete the cookie when the browser is closed.
_setVar()
_setVar(newVal)
This method is deprecated. Please use _setCustomVar()
instead.
Sets or defines a custom visitor segment with the supplied string. You can use this value to provide additional segmentation on users to your website. For example, you could have a login page or a form that triggers a value based on a visitor's input, such as a preference the visitor chooses, or a privacy option. This variable is then updated in the cookie for that visitor. When implemented on your site and data is collected via this method, the newly defined segment appears in the User Defined reports in the Visitors section of the Analytics Reports. Additionally, you can access the User Defined Value segment in the Content Detail report to see which percentage of visitors to a page belong to a particular segment that you define.
parameters
String newVal
New user defined value to set.
_trackPageLoadTime()
_trackPageLoadTime()
This method is deprecated because Site Speed reporting is enabled
automatically for all users. Please
use _setSiteSpeedSampleRate()
to adjust the sampling rate for Site Speed reporting.
Enables Site Speed reports for this page. Insert this method for every page on your site for which you want Site Speed reporting. For more information on the Site Speed reports in Analytics, see the Site Speed article in our Help Center.
Verifying Your Changes
If you see data appearing in the reports, your setup is correct. If you see no data after 24 hours, check your customization to make sure you made no errors. See Troubleshooting the Tracking Code for a list of common tracking code errors. This troubleshooting guide also contains Basic Debugging Steps and more information on debugging.
Be aware that enabling Site Speed tracking for your site results in an extra request made to the Google Analytics servers, separate from the page tracking GIF request made for pageviews. To minimize impact on overall site latency, this request is sent only on a sampling of pageviews to your site. For this reason, you might not always see the site speed GIF request when attempting to debug this feature in Firebug or other tools. This is normal and expected.
Async Snippet (recommended)
_gaq.push(['_setAccount', 'UA-12345-1']); _gaq.push(['_trackPageview']); _gaq.push(['_trackPageLoadTime']);
_trackPageview()
_trackPageview(opt_pageURL)
Main logic for GATC (Google Analytic Tracker Code). If linker functionalities are enabled, it
attempts to extract cookie values from the URL. Otherwise, it tries to extract cookie values from
document.cookie
. It also updates or creates cookies as necessary, then writes them back
to the document object. Gathers all the appropriate metrics to send to the UCFE (Urchin Collector
Front-end).
Async Snippet (recommended)
_gaq.push(['_setAccount', 'UA-12345-1']); _gaq.push(['_trackPageview', '/home/landingPage']);
parameters
String opt_pageURL
Optional parameter to indicate what page URL to track metrics under. When using this option, use a beginning slash (/) to indicate the page URL.