Google Analytics module provides an advanced textarea on the settings page for including additional JavaScript to every page.

The official documentation about Google Analytics Tracking Code should be read first to understand how Google tracking works in general. On the Google Analytics Tracking API page you will find all available methods that can be used in this textarea. Also, you'll find JavaScript snippets dotted around in the Google Analytics Help Center.

6.x-3.x and 7.x-1.x

Don't forget to prefix all used methods in the latest (asynchronous) version with _gaq.. See Upgrading from 2.x to 3.x of Google Analytics module for the differences between the tracker customizations. Google's Asynchronous Migration Examples page has more details.

5.x-1.x and 6.x-2.x

Don't forget to prefix all used methods in the traditional (ga.js) version with pageTracker.

Add your Google Analytics advanced JavaScript snippets as comments to this page.

Comments

budda’s picture

GA allows for tracking across multiple subdomains using a single line of script.

pageTracker._setDomainName("example.com");

See Further information for details.

maku520’s picture

no, it's this:

pageTracker._setDomainName(".example.com");

you missed the first period before the domain name.

hass’s picture

By default, Google Analytics tracks referrals from a growing list of search engines. You can also configure Analytics to identify referrals from additional search engines by adding the following line to your tracking code:

pageTracker._addOrganic("name_of_searchengine","q_var");

See here for more information.

pcambra’s picture

setTimeout('_gaq.push([\'_trackEvent\', \'NoBounce\', \'Over 10 seconds\'])',10000);

http://padicode.com/blog/analytics/the-real-bounce-rate/

igrok’s picture

The new version of Google Analytics allows you to track page load times, but it's disabled by default. To enable it, add this snippet in the 'after' code box in the Google Analytics module's settings page:

_gaq.push(['_trackPageLoadTime']);
shark’s picture

Currently I have the Google Analytics Drupal module set to record outbound links as events. For goal tracking, I'd like to record just a small handful of outbound links as fake goal URLs. For example, a user clicks on a link to example.com/join and I want to record it as a page hit to mysite.com/goals/join.

The easiest solution, if you can use a text input mode like PHP and make use of the onClick link attribute, is to write:

<a href="example.com/join" onClick="_gaq.push(['_trackPageview', '/goals/join']);">Join via our partner site</a>

However, I also wanted this to work for users who a) aren't familiar with JavaScript and b) can't use the onClick attribute. So I have them write:

<a href="example.com/join" data-ga-page="/goals/join">Join via our partner site</a>

and in my theme's .info file I say:

; Scripts
scripts[] = script.js

and finally in the script.js file, I have some jQuery which sets the onClick behavior using the data-ga-page attribute as the page name variable:

Drupal.behaviors.my_theme = function (context) {
  // Special support for Google Analytics goals.
  // On any (normally untracked) external link, we can set a goal-related fake page to track. Example:
  // <a href="example.com/join" data-ga-page="/goals/join">Join via our partner site</a>

  $('a[data-ga-page]').click(function() {
    var analyticsPage = $(this).attr('data-ga-page');
    _gaq.push(['_trackPageview', analyticsPage]);
  });

Note the javascript examples are using the Google Analytics asynchronous syntax. For the older syntax, check out this similar code:
http://sealedabstract.com/code/tracking-an-outbound-link-as-a-goal-with-...