Thursday, 23 May 2013

Making sure Google Analytics is done before submitting a form

Want to track form submissions with Google Analytics, but losing the data because the form submits before the asynchronous GA code is done? Fear not, Google has added a callback event.

This StackOverflow post contains an answer that outlines (in a single onclick statement if that floats your boat) how to do it. In summary:

onclick="var _this=this;_gaq.push(['_set','hitCallback',function(){$(_this).parents('form').first().submit();}]);_gaq.push(['_trackEvent','My category','My action']);return !window._gat;"

No comments:

Post a Comment