Forums

Adding Google Analytics tracking in CFv4

qmeister 29 Apr, 2015
Hi,

I'm struggling a little with the addition of Google Analytics in CFv4.

I added a custom code action on the onLoad event as seen a separate post:
<?php
$form->form_params->set('jsvalidation_onValidateSuccess', 'trackEvent');
?>

I placed this first, before Show HTML.

Then I added a LoadJS action to the onSubmit event as follows:
function trackEvent() {
  _gaq.push(['_trackEvent', 'Contact', 'Myformname', 'Submit']);
}

I placed this LoadJS action first, before DB Save, Email and Show Thanks message.

It's not working and I'm not sure what else to try.

Any help gratefully accepted.

Thanks in advance.
GreyHead 29 Apr, 2015
Hi qmeister,

When do you want to do the tracking - when the form loads i,e, it's viewed, or after it submits?

The FAQ Prof Calculus linked to won't quite work in CFv4.

Bob
qmeister 30 Apr, 2015
Hi,

Thanks for getting back to me. I want the tracking to occur on submit. I've only found CFv3 and CFv4 posts which deal with that, so grateful if you could point me in the right direction.

Many thanks
Quentin
Max_admin 01 May, 2015
Hi Quentin,

Just add the tracking code into a "Load JavaScript" action inside the "on submit" event.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
qmeister 01 May, 2015
Hi Max,

Thanks for the response. I tried as suggested but still no luck.

Removed the custom code action on the onLoad event as mentioned above. That left me with the LoadJS action on the onSubmit event as follows:

function trackEvent() {

  _gaq.push(['_trackEvent', 'Contact', 'Myformname', 'Submit']);

}


Still not working.
GreyHead 01 May, 2015
Hi qmeister,

I checked the Google Analytics docs and it looks as though this should work, again in a Load JS action in the On Submit event:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');
replacing UA-XXXX-Y with the Property ID for the Google Analytics Property you wish to track.

Bob
qmeister 01 May, 2015
Hi,

Thanks for getting back to me. I tried as suggested but without any luck.

Our form opens in a popup and that may be what is complicating things. I also tried adding the GA tracking code which we have in our template to the LoadJS event and then also added the event function in the same LoadJS event as follows:

  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);
  })();

function trackEvent() {
  _gaq.push(['_trackEvent', 'Contact', 'Myformname', 'Submit']);
}


I really thought this would work but no luck with it either.

Grateful for any additional suggestions!
GreyHead 01 May, 2015
Hi qmeister,

Please post a link to the form so I can take a quick look.

Bob
GreyHead 02 May, 2015
Hi qmeister,

I don't see the code that I suggested on the page?

The code that is there is showing a JavaScript error "Uncaught SyntaxError: Unexpected token ILLEGAL" for this line
   _gaq.push([’_trackEvent’, ’Contact’, ’Request A Sample’, ’Submit’]);
I suspect because there are curly quotes there instead of straight ones.

Bob
qmeister 02 May, 2015
Hi Bob,

I updated with the code you suggested and changed the quotes to straight ones so you can check. I had this before and it did not work. Still doesn't work.
GreyHead 03 May, 2015
Hi qmeister,

In the Chrome Console I added breaks to the two ga() methods and they are both being executed when the thank you page loads. So maybe the code isn't the right version? Is there any way to debug from the GA end?

Bob
qmeister 05 May, 2015
Hi,

The code version I'd say is wrong, because in our template we have the other version. I've updated as follows but still no luck.

 
  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);
  })();

function trackEvent() {
   _gaq.push(['_trackEvent', 'Category', 'Action', 'Label']);
}

I'm not aware of any way to troubleshoot from the GA side.
qmeister 24 Jun, 2015
Hi GreyHead,

I'm afraid I'm still struggling with this - the event tracking is still not working and I'm not sure what else to try.

Any ideas?

Thanks in advance.
This topic is locked and no more replies can be posted.