Thursday, 14 September 2017 11:30

Monitor Drupal Webform Activity using Google Analytics "OnClick" Tracking

Written by

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.  

Last modified on Thursday, 21 April 2022 17:19
Comments (0)
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Suggested Locations

Latest Comments

Got a similar email that seemed suspicious. Ignored it and they even followed up today.
My organization received one of these emails from "Linda," but uses https://www.bestprosintown.com/p...
Hi Nate, I got the same email template from the same email address today and found you through a ...
Just received one today (16 Aug 2022) from "Mailchimp". Thanks for sharing!
Thanks for posting this. I just got one today. I was 99% sure it was a scam, and your post confirmed...


Design & Development

Wordpress, Drupal, Joomla
New custom websites
Bespoke themes and extensions
Redesigns, upgrades, migrations

Web Design & Development


Optimization & SEO

Let us optimize and manage your overall online presence. We offer full service monthly SEO as well as one-time projects.  

Optimization Plans & Pricing


Maintenance, Patching

White glove monthly backups, security updates, maintenance and testing for your Wordpress, Drupal, or Joomla site.

Maintenance Plans & Pricing


Email Newsletter

Bring your web & marketing performance to the next level: monthly blog post roundup via email.  

Stay in Touch!