Ajax google analytics event tracking on html page not working

The following is the Google Analytics code added just before closing / head on the html page.

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-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); })(); 

Next, I raise an event ... on success or failure ...

 $.ajax({ url: "/xxx/xx/", type: "POST", data: { name: q, seme: o, profile: x } datatype: "html", success: function (f) { _gaq && _gaq.push(["_trackEvent", "contactform", "success", i, p]) }, error: function (f) { _gaq && _gaq.push(["_trackEvent", "contactform", "crash", i, p]) } }); 

But the event was not recorded ... I was waiting to see something in the last 4 days .. I have performed several tests, but it does not appear in the analytics ... Am I doing something wrong? Do I need to add an extra line to the main Google Analytics snippet added to the title tag?

GA tracks normal data, but not these events.

+4
source share
1 answer

I do not understand why you have a line:

 _gaq && _gaq.push(["_trackEvent", "contactform", "success", i, p]) 

not only

 _gaq.push(["_trackEvent", "contactform", "success", i, p]); 

or even

 if (typeof _gaq != "undefined") _gaq.push(["_trackEvent", "contactform", "success", i, p]); 

Another trick to know when the data was sent correctly is a link to ga_debug.js on Google Analytics servers, and not to the standard ga.js - this will give you console output and show "Sent tracking beacon!". as well as all data that was sent when they were successful. I suggest you try and see if this helps!

+6
source

Source: https://habr.com/ru/post/1385860/


All Articles