Click here to see your recently viewed pages and most viewed pages.
Hide

Tracking Code: The _gaq Global Object

The _gaq global object can be used directly for asynchronous page tracking via the push(...) method. It also has methods for creating tracker objects, but these should only be used in rare cases.

For traditional page tracking, use the _gat global object instead.

_gaq Object Methods

Method Details

_createAsyncTracker()

    _createAsyncTracker(accountId, opt_name)
    Creates a tracker object that can be referred to by name when making asynchronous tracking calls. If no name is given, it will be the empty string '', which represents the default tracker in the asynchronous syntax. If an asynchronous tracker already exists for the given name, it will be overwritten with the new tracker.

    Note: This method is generally only safe to use inside a function that has been pushed onto _gaq. It should only be used in rare cases.
    _gaq._createAsyncTracker('UA-65432-1');

    parameters

      String accountId The full web property ID (e.g. UA-65432-1) for the tracker object.
      String opt_name The name to store the tracker object under. Use this name to refer to the tracker object in asynchronous tracking calls later.

    returns

      Tracker The created tracking object.

_getAsyncTracker()

    _getAsyncTracker(opt_name)
    Returns the tracker object previously created under the given name. If no name is given, the default tracker name (the empty string '') will be used. If no tracker object exists with the given name, a new tracker is created, assigned to that name and returned.

    Note: This method is generally only safe to use inside a function that has been pushed onto _gaq. It should only be used in rare cases.
    _gaq._getAsyncTracker();

    parameters

      String opt_name The name of the tracker object to retrieve.

    returns

      Tracker The retrieved or created tracking object.

push

    push(commandArray)
    Executes the given command array, which is simply a JavaScript array that conforms to the following format. The first element of the array must be the name of a tracker object method passed as a string. The rest of the array elements are the values to be passed in as arguments to the function.

    The following usage is typical:
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-65432-1']);
    _gaq.push(['_trackPageview']);

    This function is named push so that an array can be used in the place of _gaq before Analytics has completely loaded. While Analytics is loading, commands will be pushed/queued onto the array. When Analytics finishes loading, it replaces the array with the _gaq object and executes all the queued commands. Subsequent calls to _gaq.push resolve to this function, which executes commands as they are pushed.

    Trackers can be named by prepending the tracker name (along with a dot) in front of the method name. If a function name is qualified with the name of a tracker, it is executed on that tracker.

    The following example creates and uses a tracker arbitrarily called myTracker.
    var _gaq = _gaq || [];
    _gaq.push(['myTracker._setAccount', 'UA-65432-2']);
    _gaq.push(['myTracker._setDomainName', 'foo.com']);
    _gaq.push(['myTracker._trackPageview']);

    Note: Trackers get created the first time a command references them. The default tracker is created via the first command that doesn't explicitly name its tracker. The default tracker's name is the empty string ''.

    In addition to pushing command arrays, you can also push function objects. This can be especially useful for tracker methods that return values. These functions can reference both _gat and _gaq.
    var _gaq = _gaq || [];
    _gaq.push(function() {
      var tracker = _gat._getTracker('UA-65432-1');
      tracker._trackPageview();
    });

    You can also use the push syntax directly in the HTML of your page.
    onclick="_gaq.push(['_trackEvent', 'name', value]);"

    As long as the asynchronous Analytics snippet exists above the link, clicks will be captured without errors; even if Analytics hasn't fully loaded at the time of the click. This is one of the many benefits of asynchronous tracking.

    Lastly, instead of typing _gaq.push(...) for each command array, you can push all of your commands at once. The following code demonstrates this technique. See the Async Snippet Usage Guide for more details.

    _gaq.push(
      ['_setAccount', 'UA-XXXXX-X'],
      ['_setDomainName', 'example.com'],
      ['_setCustomVar', 1, 'Section', 'Life & Style', 3],
      ['_trackPageview']
    );

      parameters

        Array commandArray The command to be executed. One or more of these can be pushed in the same invocation.

      returns

        Number The number of commands that failed to execute.