Why does Google Analytic request a GIF file?

后端 未结 4 1968
无人共我
无人共我 2020-12-31 04:28

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?

相关标签:
4条回答
  • 2020-12-31 04:57

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 05:06
    1. you can use the __utm.gif tracker without javascript (w some server help)

    2. you can use it in an email message (w some programmatic help before sending the email)

    3. Urchin was developed before AJAX was popular (2005)

    It has nothing to do w cross-domain. They could have used JSONP for that.

    0 讨论(0)
  • 2020-12-31 05:16

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

    The smallest transparent GIF is 43 bytes.

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

    The smallest transparent PNG-24 (which can't be shown by older browsers too) is 67 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.

    0 讨论(0)
提交回复
热议问题