Embed code that tracks clicks on certain DOM elements (issue is on links/buttons that redirect)
Let's say I have an embed code with a script that adds events to certain DOM elements. These would normally be links or buttons. Once an event is called I do an ajax call to record that click event.
The problems comes in with links that redirect. So, adding an onclick event to an
<a href="http://google.com">link</a>
might not work since the page will be redirected before the event is recorded via the ajax c开发者_StackOverflowall.
Obviously sites like Google Analytics, Omniture do this when they track conversions. Are there any good ways to solve this problem?
Also, assume this all works with an embed code (just like Google Analytics) - so we can't modify the html code too much.
And in case the ajax call fails, the page still has to get redirected.
hijack the click, prevent the default then trigger the redirect on the callback.
var link = ''; $("a").click(function(e){ e.preventDefault(); link = $(this).attr("href"); $.ajax( .... function () { window.location = link; }) });
精彩评论