I am trying to insert a google tracker to a form submit button/form using the webform module (drupal 7). Here is my code:

Drupal.behaviors.basicWorks = function(context) { // Track submission events. $('#webform-client-form-198', context).submit(function() { ga('send', {'hitType': 'event', 'eventCategory': 'PDTestlinktoDrupal', 'eventAction': 'clickAction', 'eventLabel': 'PDConversionTest' }); });

My question is:

Where do I place this code? Inside the webform.js inside the /js folder? Or do I create my own file inside the /js folder and upload?

Been working on this issue for a few days now and I could really use some insight.

  • Add the above code to a JS file in your module – rfegade Aug 18 '15 at 18:00
  • Please don't use signatures in your questions. Your questions are linked to your account; if you want to add anything about you, your profile is the place where to add it. – kiamlaluno Oct 19 '15 at 16:45
  1. Create the script.js file in to your theme folder
  2. Copy paste the above code into it
  3. Specify the script.js path in the .info file of the theme as scripts[] = script.js
  • I tried this method and I am still not getting any hits in google. Thank you for your suggestion. I will keep troubleshooting. – Jason D'Amico Aug 18 '15 at 18:45
  • I guess it will reflect in your GA statistics after 24 hrs fro the code integration – rfegade Aug 19 '15 at 2:01

you have 2 option: 1- using #attached to the $form element. use hook_form_alter

function YourMODULE_form_webform_client_form_1_alter(&$form, &$form_state) {
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'YourMODULE') . '/js/your_script.js',

2- add it in your custom module with drupal_add_js() function, Also it will be much better if you just load the js on only that page (web form page) so you will avoid loading it on every page.

  • ... and if 2- then best via hook_page_build() – leymannx Aug 18 '15 at 18:21
  • The challenge is that the form is on every single page except for a couple. So I would like to avoid having to place the code on every page. I will give this a shot although I am not familiar with using hooks. (beginner) – Jason D'Amico Aug 18 '15 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.