Forums

Adding Google Analytics Event tracking to submit

petersen 24 May, 2012
If you have a form, you can add

onclick ="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);" to the submit tag, but this will fire every time the button is clicked. This is a problem if the form fails validation. Where is it best to load the tracking code so that it's only when successful?
GreyHead 26 May, 2012
Hi Petersen.

You can hook into the Validation on success event like this: Drag a Custom Code action into the form On Load event and add this code:
<?php
$form->form_params->set('jsvalidation_onValidateSuccess', 'trackEvent');
?>

Then add a Load JS action and define the function:
function trackEvent() {
  _gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);
}

The load JS action accepts PHP so you can add variables to the code here if needed.

Bob
petersen 28 May, 2012
Thanks Bob. Popped them in the View. Just waiting for the results to hit analytics
Bromm 13 Sep, 2012
Hey there,

I'm using Chrono Contact V 3.2 09 Nov 2010 and would like to add Google Analytics tracking to each form I'm using. Somehow I can't get it work :-(

Could you explain me how I can achieve that?
I have currently pasted the following tracking code:

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

in "Form" "On Submit code - after sending email:"

Looking forward to your reply.
Kind regards,

Bromm
GreyHead 13 Sep, 2012
Hi Bromm,

I just ran a test and the code you have appears to load the Google Analytics library correctly. Do you see any JavaScript errors on the page?

Bob
petersen 05 Aug, 2013
Digging up an old thread here but could you explain how to implement this using the Auto Serverside validation. Is there a way to attach the js to the show message event?
GreyHead 05 Aug, 2013
Hi petersen,

how to implement this using the Auto Serverside validation.

I've no idea - what exactly are you asking?

To load the code in the On Submit event I'd drag a Load JS action into it; I think that will work OK. I'd put it after any Validation or Captcha actions and before the Show Thanks Message action.

Bob
petersen 05 Aug, 2013
Sorry, I'm not being clear.

The initial thread was about attaching the event to the javascript validation event. I'd like to fire the Google event when the 'Thank you' message shows.
GreyHead 05 Aug, 2013
Hi Petersen,

I think that the method I wrote about in my last post will do that though I'm not sure if loading the code is enough or if it has to be specifically triggered?

Bob
petersen 05 Aug, 2013
That's the crux. Not sure if anything will specifically fire it unless I wrap it in a onload-type function.
GreyHead 07 Aug, 2013
Hi petersen,

I'm sorry, I've lost track of the question here. You can, of course, wrap the code in domready or load function - but that has nothing to do with attaching it to a submit button??

Bob
petersen 07 Aug, 2013
My fault for mixing questions. Sorry Bob.

To clarify, I want to fire the event on the page that is rendered when we use 'Show thank you message'.
GreyHead 07 Aug, 2013
Hi petersen,

Yes, and what is the problem?

Bob
This topic is locked and no more replies can be posted.