开发者

where to put google analytics code [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

google analytics at head? or end of body?

The google analytics code has to be pu开发者_如何学Pythont before the HEAD tag. Can anyone tell where do i put the ga code if there is no HEAD tag present in my jsp file?


The correct answer here would be to add head tags to your file. You should be using valid html markup for all your pages, and including head tags (even if empty - though certain other tags within head tag are required, depending on what type of document you are serving. Like..title tag and some meta tags...) is part of a validly marked up document. Not to mention that it hurts your SEO efforts not to have it (with relevant info, like aforementioned title and meta tags..).

However if you insist on not having a head tag in your document, you can place the GA code anywhere; in the head tag is just the optimal place - according to some people.

The logic behind putting it higher up is to give it a chance to track, even if a person tries to leave the page before it is done loading (closing the browser, clicking the redirect button, maybe js on the page redirects, etc...).. However, in my experience, I do not really think these are valid reasons for putting the tracking code high up on the page.

In fact, I would argue against tracking hits such as those. My main question/thought about that is...how can you rightfully say a user viewed the page if they immediately left it, even before it was fully loaded? But that's just IMO.


Found answered http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55574

here is quotation

We generally recommend that you place the Google Analytics tracking code immediately before the closing tag of your pages in order to avoid any issues with page loading delays for visitors with slow connection speeds. However, there are several situations where it might make more sense for you to install the code in the head:

  • When using frames
  • If your pages call _link or _linkByPost
  • If you're using custom JavaScript functions that may conflict with the ga.js


Google Analytics link to Google Webmaster Tool is easily verified if code is placed in the head.

The argument "is it a pageview if the code fires from a visitor and then they immediately leave" seems a non-argument to me - this should be registered as a bounce. If you place it lower down you are in danger of code not firing and no visit / pageview being initiated.

Further to this in-page analytics, which is a useful view in Google Analytics, is dependent on correct placement as advised by Google Analytics immediately before the closing head tag.

Note that the referenced quote posted by Augustus is in fact old - for the latest Asynchronous tracking code Google Analytics advises code to be in the head http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=161379


I feel the adding the google analytics at the bottom will be more effective as it is a java script.

Its one of the best practice to follow and allowing the website to load faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜