jan
jan

Reputation: 4103

Google Analytics - Event pushing not working

I inserted Google Analytics with the following code snipped.

<script>
  (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-xyz-1', 'xyz.com');
  ga('send', 'pageview');

</script>

Now I want to track a specific page in my ajax application. The following link describes how to push events to google analytics:

https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide

If I try to execute the following command, I just get the error saying "_gaq" is not defined.

_gaq.push(['_trackPageview', 'myPage']);

Am I doing something wrong here or are the docs are outdated?

Edit: So there seems to be a new API. What would be the equivalent to:

_gaq.push(['_trackPageview', 'myPage']);

Would this be the same command in the new API?

ga('send', 'pageview', 'myPage);

Upvotes: 0

Views: 4600

Answers (3)

Petr Havlik
Petr Havlik

Reputation: 3317

Jan, I believe the pageview request is already in the snippet you mentioned:

ga('send', 'pageview');

Just make sure you the line above this -- you need to make sure that domain and Analytics account ID is specified:

ga('create', 'UA-123456-1', 'yourdomain.com');

Looking at your code, it seems that you are using new Universal Analytics library, so _gaq.push commands won't work anymore. When you refer to documentation, always double check you looking at Analytics.js section (link).

Upvotes: 0

Nick Blexrud
Nick Blexrud

Reputation: 9603

You're mixing the new analytics.js with the old async ga.js. Use analytics.js (Universal Analytics) event tracking documentation instead.

Upvotes: 6

gartox
gartox

Reputation: 891

This is actually the code that samples the variable is not defined _gaq, reviewing the Analitycs page I see that the code is as follows:

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

Check the block of code and make sure that all is well implemented

Upvotes: 0

Related Questions