Google Analytics & iFrame

给你一囗甜甜゛ 提交于 2019-12-03 20:06:46

Yes, you have to include the GA boilerplate code inside your framed page

<script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

Browsers regard each and every frame as a separate window with its separate dom and javascript environment, so to be able to access pageTracker in the framed page you must include the ga.js script in this page as well.

Note that you'll also need to add

<script type="text/javascript"> 
 try {
  var pageTracker = _gat._getTracker("UA-XXXXXX-X");
 } catch(err) {}
</script>

So that the pageTracker object is actually defined. Finally, please also note that the two pageTracker javascript objects on both the framed page and the framing page, although they have the same identifier are different JS objects. That's a result of the fact that JS keeps each frame in its own window object, which practically means a JS scope.

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