php - Trying to track number of clicks in user's emails -
my issue follow:
i'm trying count number of clicks on ads in our newsletter. thing can't include js in emails - it's understandable. found way around writing small piece of code:
<script type="text/javascript" src="http://www.factmag.com/wp-content/themes/modularity_/js/jquery-1.3.2.min.js"></script> <script type="text/javascript"> function geturlvars() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexof('?') + 1).split('&'); for(var = 0; < hashes.length; i++) { hash = hashes[i].split('='); if($.inarray(hash[0], vars)>-1) { vars[hash[0]]+=","+hash[1]; } else { vars.push(hash[0]); vars[hash[0]] = hash[1]; } } return vars; } function redirect() { var link = geturlvars()["page_url"]; settimeout('document.location = "' + link + '"', 100) } </script> <body onload="javascript:redirect();"></body> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setaccount', 'ua-4340871-1']); _gaq.push(['_trackpageview']); (function() { var ga = document.createelement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(ga, s); })(); </script>
it's hosted on our servers , in newsletter ad code has following format:
<a href="http://www.example.com/example_ad_counter/?utm_source=banner6&utm_medium=banner&utm_campaign=banner&page_url=ad_url"><img src="ad_img_url" style="border:1px solid #000;"></a>
so want here:
- user clicks on ad in email.
- he goes page script.
- google analytics counts number needs in order track it.
- script redirects user advertiser's page.
now here's deal - google analytics not doing count here. guess need add in google js in order have no clue what. me one? thanks.
you want make sure redirect called after google's code called. currently, have running onload. yc correct in comment question it's race condition, pushing settimeout
won't resolve race condition.
you try calling redirect function after google inserts page.
the code here:
(function() { var ga = document.createelement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getelementsbytagname('script')[0]; s.parentnode.insertbefore(ga, s); })();
...is literally google adding new script
tag page, , loading analytics url it. brendon right suggest turning off async, i'd suggest adding redirect call after var s = document
line (inside function), , hoping give google enough time dirty work , before redirect.
if doesn't work, stick yc's suggestion , push timeout out further. know redirect delay racing time takes google's script load , run, isn't predictable. put settimeout in same place recommended calling redirect function directly. means wont start counting delay until google's analytics script has been added page.
Comments
Post a Comment