views:

29

answers:

1

Can anyone explain how javascript trackers (like the code that Google Analytics and Click, etc have you put on your page), work? I'd like to build my own and I'm fine with the PHP side of things, its just getting the data from a remote page with javascript that I'm unsure of.

A: 

They work by building and sending an image request with variables appended to the src="..." for the image, and then server-side scripting is used to parse the variables, and then a 1x1 transparent pixel output as the response. It is done this way so that if the user doesn't have javascript enabled, you can still have a hardcoded image inside noscript tags, passing the account info as a param. If you look at GA or yahoo or omniture code, you will see the img tag in noscript tags. The javascript basically does the exact same thing, only it appends more stuff to the image - extra data that can only be done with javascript (misc browser stuff you can't get from headers from the image request), as well as data from custom tracking.

Crayon Violent
So the image requested would in fact be a PHP or JS script?
Gary
well if the javascript is enabled, the image tag is generated and appended to the document via javascript. But you CAN make a request server-side (it wouldn't be an image request, you'd just be doing like a cURL request with the url you'd normally use for the img src) - but if you wanna make the request server-side, you're gonna have to fake some headers to make GA think it's coming from a browser, because they have code to filter out bots etc...
Crayon Violent