I recently had to come up with a way to track a client's Drupal site, using the Webform module, from within Google Analytics. Since the Webform module generates the "submit" button automatically, there is no easy way to include the "onClick" tracking code provided by Google into the form:
onClick="ga('send', 'event', { eventCategory: 'Legacy Event Signup', eventAction: 'Sign Up', eventLabel: 'Legacy Signups', eventValue: 1});"
My solution was to write a short "hook" script in the template. To incorporate into your site, you need to modify this file:
/sites/all/themes/YOURTHEME/template.php
<?php
/*
* Implements hook_form_alter().
*/
function YOURTHEMENAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_client_form_144') {
$form['actions']['submit']['#attributes'] = array('onclick' => "ga('send', 'event', { eventCategory: 'Legacy Event Signup', eventAction: 'Sign Up', eventLabel: 'Legacy Signups', eventValue: 1});");
}
}
?>
Note: You can also download a TXT file containing this script in the "attachments" area below.