javascript - How to wait until tracking scripts have fired before redirecting the user? -


i run typical price comparison website, user browses products, clicks on link go merchant's website.

before being redirected merchant's website, user presented "we redirecting you..." page.

this page there allow tracking codes (google analytics, adwords, bing ads...) track event.

i've placed tracking codes right before closing </body> tag, avoid blocking rendering of page while scripts loaded.

i'm redirecting user meta refresh tag:

<meta http-equiv="refresh" content="0; url=..."> 

it seems work alright, i'm worried that, depending on browser / speed of internet connection, redirect can happen before tracking scripts have fired.

i delay redirect few seconds on safe side, want keep experience smooth user.

i include scripts in <head>, but:

  • this delay display on "redirecting..." page while scripts loaded
  • this not guarantee tracking scripts have done job before user redirected: tracking script first loaded, triggers action asynchronously track event.

how can guarantee tracking scripts have done jobs, while still redirecting user asap?

any feedback on similar experience appreciated.

for google analytics there official way tracking outbound links 'events'. need set onclick function below, , google call callback when has received analytics message.

js

(function(i,s,o,g,r,a,m){i['googleanalyticsobject']=r;i[r]=i[r]||function(){     (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new date();a=s.createelement(o),     m=s.getelementsbytagname(o)[0];a.async=1;a.src=g;m.parentnode.insertbefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');  ga('create', 'ua-0000000-5', 'auto'); ga('send', 'pageview');  var trackoutboundlink = function(url) {     ga('send', 'event', 'outbound_link', url,         {             'hitcallback': function () {                 document.location = url;             }         }     ); }; 

html

<a href='<url>' onclick="trackoutboundlink('<url>'); return false;'>buy me now</a>" 

return false prevents link being followed such, , leaves trackoutboundlinks redirect new url when called ga.

let me know if need more. example use more granularity in event calls distinguish between links websites of product , websites can purchase.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -