2013-07-16 17:36

如何在iFrame中使用Google Analytics进行访问者推介?


Please let me know if this should go on serverfault.. I'd like to know if it is possible to get a visitor referral, to know where he/she comes from from within an iFrame. The trouble I have is if someone say clicks a link on twitter (or any other site) that goes to a given page - the referral Google Analytics captures within the iframe is that of the parent window and not the referral itself.

what's the trick ? Thanks !

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dqhr76378 dqhr76378 8年前

    It is clearly documented in their help center:

    Installing the Analytics tracking code on a site that uses frames requires some careful consideration to ensure that your traffic is tracked accurately, and that the referral information is retained.

    We recommend that a site using frames includes the tracking code in the section of the frameset (parent) page, as well as directly before the tag of each child frame that you want to track. If you place your tracking code within a tag thinking that this is equivalent to the body, the code will not run and no data will be recorded.

    If all frames on the site reside on the same domain, then referral information can be properly recorded as long as you've installed the tracking code on the first page users visit on the site.

    The Solution:

    • Load the iframe using _getLinkerUrl to link the visit inside the iframe with the visit on the top frame
    • Use P3P headers on the iframed page to work around stupid internet explorer.

    Additional Notes:

    • Even if you don't care about the top level page you should add a tag into it, if you don't you can't use _getLinkerUrl and you lose the traffic source, etc. If you don't use _getLinkerUrl GA inside the iframe will think it's a brand new visit referral from the top level page.
    • Setting cookies inside an iframe, in a third-party domain is the definition of a third-party cookie. Because of that any browser that is set to block third-party cookies will block the GA cookies and GA won't work. This include Safari (both Desktop and Mobile) that are set to block third-party cookies by default. So if visits using Safari or iDevices are important for you (likely these days) this tracking won't probably give you good results. The only solution is to eliminate the iframed page, either put it in your domain or open it in a new window/tab.

    Source: #11724605

    Hope this helps!

    点赞 评论 复制链接分享