Google Analytics with ecommerce or event tracking

Permalink
Just wanted to share something I figured out today and, AFAIK, isn't /anywhere/ else on the internet.

C5 puts the google analytics code at the bottom of the page. This is best practice for the old google analytics synchronous code but isn't necessary for the new GA async code. Concrete5 doesn't allow you to define the tracking account number at the top and include the javascript at the bottom (without playing with templates, attributes, etc). This usually isn't a problem.

However, if you want to use event or ecommerce tracking, you will be adding to the _gaq array before the bottom of the page, where your account number is set. Surprisingly, google doesn't handle this well, and, when it does send the requests, doesn't reorder appropriately. Without hacking around c5 to set the account number a the top, you can just push that to the front of the array.

So, the solution is to change the .push() to a .unshift.

Instead of this code:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-1']);
_gaq.push(['_trackPageview']);
 .....
  })();


use:
var _gaq = _gaq || [];
_gaq.unshift(['_setAccount', 'UA-XXXXX-2'], ['_trackPageview']);
....


(Notice I combined into one call... that's not necessary, but you'd have to unshift the trackpageview first, then unshift the setaccount in order to get the setaccount to go first.)

James

jshannon