18

Google Analytics embeds a one pixel GIF with a URL like this:

http://www.google-analytics.com/__utm.gif?utmwv=5.1.5&utms=5&utmn=1532897343&utmhn=www.douban.com&utmcs=UTF-8&utmsr=1440x900&utmsc=24-bit&utmul=en-us&utmje=1&utmfl=10.3%20r181&utmdt=%E8%B1%86%E7%93%A3&utmhid=571356425&utmr=-&utmp=%2F&utmac=UA-7019765-1&utmcc=__utma%3D30149280.1785629903.1314674330.1315290610.1315452707.10%3B%2B__utmz%3D30149280.1315452707.10.7.utmcsr%3Dbiaodianfu.com%7Cutmccn%3D(referral)%7Cutmcmd%3Dreferral%7Cutmcct%3D%2Fgoogle-analytics-architecture.html%3B%2B__utmv%3D30149280.162%3B&utmu=qBM~

Why not use an AJAX call? What's the benefit of using a one-pixel GIF?

icktoofay
  • 126,289
  • 21
  • 250
  • 231
Jeffrey Hsu
  • 305
  • 2
  • 9
  • @Joseph: I reverted your edit because Markdown interprets some characters in the URL as italic or other formatting if a block quote is used. – icktoofay Sep 08 '11 at 04:41
  • Sometimes it can use a POST if the gif request is too big. In these cases it will put all variables inside the POST body. It does so because some browsers limit the size of a request url. – Eduardo Sep 08 '11 at 07:45
  • 1
    possible duplicate of [Why serve 1x1 pixel GIF (web bugs) data at all?](http://stackoverflow.com/questions/6638504/why-serve-1x1-pixel-gif-web-bugs-data-at-all) – Yahel Sep 08 '11 at 12:29

2 Answers2

20

Because you can't really do cross domain AJAX (with the exception of CORS, but that's a different story, and a recent phenomenon with less than universal support.) AJAX is for same origin requests. Also, Google Analytics forks from Urchin, which actually predates AJAX technology's adoption.

Requesting an image is pretty standard practice for analytics services "requesting" something as a means of sending something to a third party server. The reason AJAX/CORS doesn't really make sense is that you're not actually requesting an important resource for use on the page, so you want the request itself to be as quick and overhead-less as possible.

The other two ways analytics services occasionally handle sending data from the client is:

  1. Including an invisible iframe, with the query string on the iframe src passing the analytics data

  2. Requesting an image, and instead of returning an image, returning an empty response with a HTTP 204 header.

Community
  • 1
  • 1
Yahel
  • 37,023
  • 22
  • 103
  • 153
  • Why can't Google provide an API which uses Ajax and CORS, not this inferior image method? Are there any other Browser Analytics services that use CORS? I have a particular interest because of the problem in [this thread](http://stackoverflow.com/questions/14413271/if-google-analytics-goes-down-how-do-i-keep-my-site-working/14413293#comment20058553_14413293). – zuallauz Jan 19 '13 at 11:42
  • @zuallauz Using AJAX or CORS wouldn't solve your problem. (It would just introduce browser incompatibilities for older browsers.) For your problem, just wrap the whole GA code in a DOMReady/WindowOnLoad/SetTimeout type delay that will just defer when it tries to load ga.js (which is generally the problem anyway, not the 1 pixel gif) – Yahel Jan 19 '13 at 16:42
  • Answered your question there. – Yahel Jan 19 '13 at 16:57
-2

To maximize compatability. A cell phone browser may not support AJAX, for example, and thus may provide inadequate results. But hey, Google does a lot of funky stuff that nobody can explain.

U4iK_HaZe
  • 171
  • 7
  • 1
    Okay. Perhaps emailing support@google.com will yield a better response. Just try not to get too old waiting for a response. – U4iK_HaZe Sep 08 '11 at 04:42
  • You can't (really) do cross domain AJAX (except for CORS, which has spotty support). This isn't "funky stuff"; this is very standard practice in web analytics. – Yahel Sep 08 '11 at 14:06
  • I was referring to "funky stuff" as to Google and their methods as a company, not specifically to pixel tracking. – U4iK_HaZe Sep 08 '11 at 20:09