Thursday, 14 September 2017 06:30

Monitor Drupal Webform Activity using Google Analytics "OnClick" Tracking

Written by 
Monitor Drupal Webform Activity using Google Analytics "OnClick" Tracking

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.  

Read 566 times Last modified on Thursday, 14 September 2017 06:36

Leave a comment

Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.

Copyright © 2005-2016 Covington Creations, LLC | Hosting by InMotion | Legal