How to set up AJAX tracking in Google Analytics?

I created my Google Analytics account. And I copied and pasted the code specified in my index.php file. It seems to me that it works, as I see calls to www.google-analytics.com from firebug.

Now I want to track how many times the function "functions.php" is called via ajax from the index file.

I tried using _gaq.push(['_trackPageview', 'functions.php']); but he gave me an Uncaught ReferenceError: _gaq is not defined . So I added var _gaq = _gaq || []; var _gaq = _gaq || []; into your code. The error has disappeared, but I do not see the call at www.google-analytics.com after the completion of ajax.

Can someone help me set it up so that analytics track ajax calls?

My code looks like

 <script type='text/javascript'> (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-1234556-1', 'domain.com'); ga('send', 'pageview'); var _gaq = _gaq || []; function submit_data(){ var text_area=$('#textarea').val(); var url ="functions.php"; jQuery.ajax({ type: "get", dataType: "text", url: url, data: { what : "generate", text_area: text_area, t: Math.random() }, success: function(data, textStatus){ $('#textarea').val(data); // _gaq.push(['_setAccount', 'UA-12345-1']); _gaq.push(['_trackPageview', 'functions.php']); } }); } </script> 
+8
javascript ajax google-analytics
source share
4 answers

It sounds like you're mixing Universal Analytics calls ( analytics.js and ga() ) with Async Analytics ( ga.js and _gaq.push() ), but I don't see any code to initialize ga.js

Try to change

 var _gaq = _gaq || []; 

to

 var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-12345-1']); _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); })(); 
+3
source share

I think that when checking in Google Analytics, you select "Universal Analytics" and use a new code counter. Look in the DOM browser, there is no "_gaq" object - and therefore is an error. You tried to fix it with an empty array (_gaq).
Old code:

var _gaq = _gaq | | [];
_gaq.push (['_ setAccount', 'UA-XXXXXX-1']);

Do not use the old code! (And you cannot use the counter of several codes 'UA-XXXXXX-1' - this is an error)
New code:

ga ('create', 'UA-XXXXXXX-1', 'mysite.com');
ga ('send', 'pageview');

The new Google counter has a new syntax.
Event Use Documentation: https://developers.google.com/analytics/devguides/collection/analyticsjs/events
Usage example:
I have a calculator on the page and I want to track events by clicking a button on it.
Category - Using Calculator,
Event - "Cost Calculation".
Old code:

_gaq.push (['_ trackEvent', 'Using the calculator', 'Calculation of cost'),

New code:

ga ('send', 'event', 'Using the calculator', 'Cost calculation'),

Category and event - required!
PS: Sorry. I have bad English and I used google translator :)

Upd:

 <script type='text/javascript'> (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'); //Use once per page ga('create', 'UA-1234556-1', 'domain.com'); ga('send', 'pageview'); // function submit_data(){ var text_area=$('#textarea').val(); var url ="functions.php"; jQuery.ajax({ type: "get", dataType: "text", url: url, data: { what : "generate", text_area: text_area, t: Math.random() }, success: function(data, textStatus){ $('#textarea').val(data); ga('send', 'event', 'MyCategory', 'functions.php'); } }); } </script> 
+13
source share

If you use Universal Analytics ( analytics.js ), switch here:

 _gaq.push(['_trackPageview', 'functions.php']); 

:

 ga('send', 'pageview', 'functions.php'); 
+6
source share

Yes, just add this after your Google Analytics script to define the _gaq array:

 var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-65432-1']); _gaq.push(['_trackPageview']); 
+1
source share

All Articles