Reputation: 6631
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 call.
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.
Upvotes: 1
Views: 196
Reputation: 17319
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; }) });
Upvotes: 2