Tuesday, September 2, 2008

Enable Google Analytics without javascript


Site counter tracker has been a very important component for us to trace popularity of a web portal. Luckily, a lot of web site counter we can use freely. All these time I count on two site counter to track my pages. StatCounter.com offer a easy to manage and weekly reporting statistic tracker. Simply sign up and add new project to the account, and paste snippet to your HTML template. StatCounter free edition has limitation of storing last 500 records only for analysis.

Google analytics (GA) offer a more complete service, and advanced reporting tools. But so far I searched across the net, found nothing we can do to implement GA in pages (rss or widget) that not support JavaScript. With such limitation, we can't track images loadings, email reads, pdf files, and the most important to me : WML pages.

Got inspired from LinkLove, which is run smoothly on my server but lack of User Agent info, I try to alter some point. Instead of using fopen remote file, I use CUrl function. With CUrl function, we can send set HTTP header to GA to simulate the actual hit by handset to the GA Server. To know more, simply download and try it on your web server. This script is totally standalone, no external 3rd function needed.

On WML/RSS pages, instead of copying GA Urchin JS snippet, simply add a
<img src="http://yourserver.com/ga.php" alt="" /> tag like this :

<img src="http://yourserver.com/ga.php?t=UrchinCode&amp;d=your-original-domain" alt="GA" />

10 comments:

Anonymous said...

Ok, i am no computer guy nor native english speaker. I am trying to use GA on a blog that is sort of WYSIWG blog. I cant put any files on this site. I cannot paste any code just insert image source. What i was trying to do is use ga.php on a different domain, put the image(php file)source adress on my blog and register my blog with GA. Well...it doesnt work. Then i tried to check on the same domain. Still doesnt work. What am i doing wrong? Please tell me the process in more details. My email is newidest@hotmail.com I would apreciate it greatly.

Unknown said...

How do you track the exit page?

Anonymous said...

//will this work for c#

string urchinUrl = "http://www.google-analytics.com/__utm.gif?utmwv=1&";
urchinUrl += "utmn="+(new Random()).Next(1000000000,9999999999);//random request number
urchinUrl += "&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn="+lblDomain.Text;//enter your domain
urchinUrl += "&utmr="+Request.UrlReferrer.AbsoluteUri;
urchinUrl += "&utmac="+lblUrchinCode.Text;//enter the new urchin code
string cookieNo = (new Random()).Next(10000000,99999999);
string today = DateTime.Now.ToString();
urchinUrl += "&utmcc=__utma%3D"+cookieNo+"."+(new Random()).Next(1000000000,2147483647)+"."+today+"."+today+"."+today+".2%3B%2B__utmb%3D"+cookieNo+"%3B%2B__utmc%3D"+cookieNo+"%3B%2B__utmz%3D"+cookieNo+"."+today+".2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D"+cookie+"."+"-"+"%3B";//random cookie number
WebHelper.getResponse(urchinUrl,100,"GET","image/gif");

says said...

the idea of this post is to simulate real GA works on a standard device which has no fancy script supported, i.e basic mobile phones, internet tablet, etc.
but sure your snippet helps C# developer very much.

milez said...

Hey, why is $urchinUrl overwritten in line 318?

Best regards
Jan

milez said...

sorry ... line 17 ;)

Anonymous said...

Yes seems to be overwritten, so parameters given before don't affect at all. What gives?

UzzeR said...

$var_uservar='-'; //enter your own user defined variable

which variable is it?

Is sript based on ga.js or old urchin.js?

What is the php configuration requirements?

Unknown said...

If you want google analytics with an image request (could also work to track pdf files, swf, etc..) you can use http://nojsstats.blogspot.com/

Unknown said...

Andy has witten an great class for this:
http://www.acleon.co.uk/posts/galvanize-google-analytics-without-the-javascript/comment-page-1