views:

178

answers:

4

Why does Google Analytic request a GIF file?

Is it because the GIF allows access to more data than JavaScript alone. Is it to get the IP address of the user?

A: 

I guess it is primarily to get some feedback from Browsers that don't have Javascript enabled.

x4u
But the Analytics markup is just a string building and a `document.write()`, there is no img element fallback.
alex
Well than it's obviously not for this porpose. ;) It was just a guess.
x4u
+6  A: 

Even with JavaScript enabled, analytics requests a GIF file. If you look at the GET params of the image, it contains a lot of information about the browser. Stuff like utmsr=1280x1024 (the screen size). Google Code has a list of parameters.

It uses the image request to send information about the browser without an XMLHttpRequest.

Now, to actually answer the original question, Google is probably doing this to get around cross-domain XMLHttpRequest restrictions.

Brian McKenna
+3  A: 

Google's javascript has to transmit the details of your page view to their servers some how. Ajax cannot be used across domains, so the only way to submit the information is to request a file from a Google server, passing the necessary information in the query string. To facilitate this, some form of content must be requested using a standard HTML tag. The easiest way to do this without impacting the page itself is to load a 1x1 transparent gif. Note that in the case of the Google script (and others), this image isn't actually added to the page. It's merely loaded via a javascript statement

var img1 = new Image();
img1.src = 'http://path/to/file.gif?otherinfohere';

This loads the image without adding it to the page. The information could also be loaded using a script tag like so:

<script src="http://path/to/script.js?otherinfohere" type="text/javascript"><script>

However, users are more likely to have javascript blocked than images, so the safer route is to request an image. As to why they would use a gif instead of say, a jpg, a gif is safer in case a rogue browser actually adds the image to the page. The transparent gif is unlikely to negatively impact the layout, where as a 1x1 jpg would leave a 1 pixel dot somewhere on the page.

Edit: To add to my comment about users having blocked javascript, a gif request containing static information can be added inside a noscript tag to allow basic tracking even in the event that javascript is disabled. To my knowledge, GA doesn't do this, but some other web analytics providers do.

Chris
+1  A: 

http://www.perlmonks.org/?node_id=7974

The smallest transperant GIF is 43 bytes.

http://garethrees.org/2007/11/14/pngcrush/

The smallest transperant PNG-24 (which can't be shown by older browsers too) is 73 bytes.

http://www.techsupportteam.org/forum/digital-imaging-photography/1892-worlds-smallest-valid-jpeg.html

The smallest (opaque) JPEG is 134 bytes.

The math is simple! Bigger size = more costs.

Pindatjuh
+1 for your research :)
alex