问题
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.
回答1:
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, thenga('send', 'item', {...})once for each item
If you have to wait for your ecommerce:send to complete you could do one of the following:
- Hack a custom
ecommerce.jsthat allows you to pass in ahitCallbackfunction, or - 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 thesrcattribute 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.
回答2:
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.
来源:https://stackoverflow.com/questions/18812345/js-analytics-ecommerce-callback