Thursday, August 29, 2013

Drupal 7: How to use Google analytics custom variables in Drupal

This article describes how I use the power of Google analytics custom variables for my Drupal websites. I make use of the Google analytics module and the Token module on drupal.org. You can find more information about installing these modules onhttp://drupal.org/project/google_analytics and http://drupal.org/project/token. If you successfully installed these modules, you can continue reading this article.
On the settings page of the google analytics module (admin/config/system/googleanalytics) you can find an area to fill in custom variables. You could fill in a static string but that wouldn't be useful to analyze. Making use of tokens is way more powerful. You could chose one from the default tokens but in our example we are going to make our own. Doing so will give us more flexibility.
To make your own token in Drupal, we have to let Drupal know that we want our own custom token by implementing the hook "hook_token_info".
We start by adding a now type for all our custom google analytics variables. As you can see in the code below, we add our custom tokens to the new type.
/**
* Implements hook_token_info().
*/
function my_module_token_info() {
  
// new type
  
$info['types']['google-analytics'] = array(
    
'name' => t('Google analytics variables'),
    
'description' => t("Google analytics variables."),
  );
  
// new token
  
$info['tokens']['google-analytics']['user-roles'] = array(
    
'name' => t('User roles'),
    
'description' => t("All the roles that the user has."),
  );
  return 
$info;
}
?>
Now that Drupal knows our token. We can add the hook "hook_tokens" that replaces the token with its actual value. In our example, we are returning the user roles. By doing so we can track the page-views separated by roles. You could also use a yet existing token to return the roles of the user but for an example purpose, we are using a custom token.
/**
* Implements hook_tokens().
*/
function my_module_tokens($type$tokens, array $data = array(), array $options = array()) {
  
$replacements = array();

  if (
$type == 'google-analytics') {
    foreach (
$tokens as $name => $original) {
      switch (
$name) {
        case 
'user-roles':
          global 
$user;
          
$replacements[$original] = implode(', '$user->roles);
          break;

      }
    }
  }

  return 
$replacements;
}
?>
After adding the token, you have to clear the cache so that Drupal searches for the new token. After doing so, you have to add it on the Google analytics settings page (admin/config/system/googleanalytics). On the screenshot below, you can see that I have added the new token to the first line of custom variables. I named it "Roles". This name appears in the top-level Custom Variables report of the Analytics reports.
After saving the settings page, we get this extra code on every page (inside the Google analytics script)
_gaq.push(['_setCustomVar', 1, "Roles", "authenticated user, administrator", 3]);
This is exactly what we wanted. Now start thinking what would be useful to track on you website and make an extra token for it. This gives you a powerful way of tracking states/elements on you website.