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(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.


      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.


      Tracker The created tracking object.


    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.


      String opt_name The name of the tracker object to retrieve.


      Tracker The retrieved or created tracking object.


    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']);

    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', '']);

    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');

    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.

      ['_setAccount', 'UA-XXXXX-X'],
      ['_setDomainName', ''],
      ['_setCustomVar', 1, 'Section', 'Life & Style', 3],


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


        Number The number of commands that failed to execute.