Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Analytics Ecommerce callback

I couldn't find anywhere how to use hitCallback parameter with google analytics ecommerce:send. In docs its only example is with send,pageview.

Here is the code I tried:

ga('ecommerce:send',
   {'hitCallback': function() {window.location.href="/test.php";}}
);

but it didn't work although the tracking worked.

like image 896
EralpB Avatar asked Sep 15 '13 12:09

EralpB


2 Answers

As mentioned, hitCallback doesn't work for ecommerce:send. Craziness.

However, what I found (today at least) is that it does work for ecommerce:addTransaction and ecommerce:addItem (for when they are finally sent). Therefor you just have to make sure that all your hitCallbacks from those calls have been hit and then you're safe.

like image 170
logidelic Avatar answered Sep 21 '22 04:09

logidelic


I ran in to the same problem and, having looked at the source code, there's no clean way to do it.

The thing about ga('ecommerce:send') is that internally it calls -

  • ga('send', 'transaction', {...}) once for your transaction, then
  • ga('send', 'item', {...}) once for each item

If you have to wait for your ecommerce:send to complete you could do one of the following:

  1. Hack a custom ecommerce.js that allows you to pass in a hitCallback function, or
  2. Google Analytics uses <img/> tags to send data if it's less than 2KB. So, you could check every <img/> tag that is created by JavaScript, and if the src attribute contains www.google-analytics.com then wait for it to load.

I've actually used #2 successfully (see http://jsfiddle.net/zkQXX/), but it really is a hack.

like image 22
Jez Avatar answered Sep 20 '22 04:09

Jez