Advertising sustains the DA. Ads are hidden for members. Join today

Third-party Integration Modules

Integrate your module with Google Analytics

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

This page is made for module developers.

The code example below is an example of how to integrate your module with Google Analytics module version 2.x or below. You should also see Advanced Google Analytics JavaScript Snippets about links to the Google Tracking API documentation.

function mymodule_preprocess_page(&$variables) {

  // Example code. Make sure your module runs all values through drupal_to_js() to be protected against XSS.
  $before = 'pageTracker._setDomainName(".example.com");';

  $after  = 'pageTracker._addTrans("1234", "My Partner Store", "84.99", "7.66", "15.99", "Boston", "MA", "USA");';
  $after .= 'pageTracker._addItem("343212", "DD4444", "Lava Lamp", "Decor", "34.99", "1");';
  $after .= 'pageTracker._trackTrans();';
 
  $variables['closure'] = preg_replace('/(.*)(<script type="text\/javascript">var pageTracker = _gat._getTracker\("(UA-\d{4,}-\d+)"\);)(.*)(pageTracker._trackPageview\((.*)?\);)(.*)(<\/?script>)(.*)/i', "$2$before$4$5$7$after$8", $variables['closure']);

}

Help improve this page

Page status: Not set

You can: