Tracking registration conversions with Drupal and Google Analytics Goals

Submitted by Janak on Tue, 06/23/2009 - 09:59

For one of the website (at work), we needed to track registration conversations using Google Analytics Goals. As with all websites I work on, it is Drupal powered..

Scenario

  1. User vists a page of interest, download or action requires the user to register.
  2. User visits the registration page (log the page user came from)
  3. After registration display a welcome page (Google Analytics Goal page)
  4. Redirect the user back to the page in Step 1

By default you can use the "destination=foobar" method to redirect user after the registartion step. In our situation this would not be suitable as we needed to display an intermediate page that we could use for Google Analytics Goals.

 

Here is some rough code (Drupal 5) to illustrate how a custom module was created to do this:

Menu callback for the welcome page (Goal page)

/**
 * Implemention of hook_menu().
 */
function gagoals_register_menu($may_cache) {
 
  if (!$may_cache) {
 
    $access = user_access('access content');
 
    $items[] = array(
    'path' => 'user/register/welcome',
    'title' => t('Welcome..'),
    'callback' => 'gagoals_register_welcome',
    'access' => $access,
    'type' => MENU_CALLBACK,    
    );
 
  }
  return $items;
}

We alter the registration form

function gagoals_register_form_alter($form_id, &$form) {
  if ($form_id == 'user_register') {    
    gagoals_register_destination();
    $form['#redirect'] = 'user/register/welcome';    
  }
}

Funtion to handle the destination urls etc

function gagoals_register_destination(){  
  $query = '';
  $domain = $_SERVER[HTTP_HOST];
  $referer = $_SERVER[HTTP_REFERER];
 
  // if a destination is already defined
  if (!empty($_REQUEST['destination'])) { 
    $query = $_REQUEST['destination'];
  }  
  elseif(stripos($referer, $domain) !== false){ // no destination, refering page was local?
    // yes, registration page was accessed from a local page
    $parsed_url = parse_url($referer);
    $path = trim($parsed_url['path'], '/');
    $path_array = explode('/', $path);
 
    if($path_array[0] != 'user'){ // exclude any redirects from user* pages
      $query = $path;
    }
  }
 
  if($query != ''){
    // maybe the user did not register and drifted off to another page
    // check to make sure the destination url is still valid
    if($_SESSION['destination'] != $query){ // has the url changed?        
      $_SESSION['destination'] = $query;
    }
  }      
}

Finally the welcome page callback

function gagoals_register_welcome(){    
  $timeout = 5;
  $url = 'http://www.example.com';
 
  // if a destination is set
  if(isset($_SESSION['destination'])){
    $destination = $_SESSION['destination'];
    $url .= "/" . $destination;
    unset($_SESSION['destination']);
  }
 
  drupal_set_header('Refresh: ' . $timeout . ';url=' . $url);
  drupal_set_html_head("<noscript><meta http-equiv=\"refresh\" content=\"{$timeout};url={$url}\"></noscript>");
 
  return "
  <p>
    <br />
    Please wait a few seconds and you'll be sent straight back to the page where you came from.    
  </p>
  ";
}

Some after thoughts

As a bi-product of this module, Drupal registration will tracks all redirect users to the page they were originally on (not default Drupal functionality)..

It is also possible (if needed at some stage) to collect these successful referring urls into a database. So, for each successful registration one could log:

  • the page user came from
  • time of registration
  • geo ip
  • browser etc

Combined with Geo-IP data, I guess you could determine if a particular landing page/node works in converting targeted demographic into a lead/registration (goal)...