Why does Google Analytics use a one-pixel picture to transfer data?

☆樱花仙子☆ 提交于 2019-12-02 17:42:49
Yahel

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!