javascript 如何使用 Google Analytics 跟踪 IFrame 中的点击次数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18966960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:53:12  来源:igfitidea点击:

How to Track Clicks in an IFrame with Google Analytics?

javascriptwordpressiframegoogle-analyticstracking

提问by Colin Rock

I'd like to track outbound clicks within an iframe on my self-hosted Wordpress site. I already use Google Analytics to track inbound visitors (GA code in the header), but I'd like to know which/how many of those visitors click on links within the iframe on each of my pages. For the record, the iframe does come from an external advertiser.

我想在我自托管的 Wordpress 网站上的 iframe 中跟踪出站点击。我已经使用 Google Analytics 来跟踪入站访问者(标题中的 GA 代码),但我想知道哪些/有多少访问者点击了我每个页面上 iframe 内的链接。作为记录,iframe 确实来自外部广告商。

This is the iframe code I was given (using sample names in place of real ones):

这是我得到的 iframe 代码(使用示例名称代替真实名称):

<iframe src="http://www.advertisersite123.com//widget.html?width=510&height=950&product=2" frameborder="0" scrolling="no" width="510" height="950"></iframe>

This line of code is currently pasted on single.php, making a table of links appear on every single post at the bottom of the page.

这行代码目前粘贴在 single.php 上,使链接表出现在页面底部的每个帖子上。

I'd like to know how many clicks (just clicks in general is fine, don't need to know where they go) are occurring in this iframe. Ideally broken down by each individual URL on my website so I can determine which URLs are more successful in generating clicks within the iframe.

我想知道在这个 iframe 中发生了多少次点击(一般点击就可以了,不需要知道它们去了哪里)。理想情况下,按我网站上的每个单独 URL 进行细分,这样我就可以确定哪些 URL 在 iframe 内产生点击的效果更好。

And if possible, I'd like to create a funnel that tells me which visitors to my website (by source) exit via clicks in the iframe.

如果可能的话,我想创建一个漏斗,告诉我网站的哪些访问者(按来源)通过 iframe 中的点击退出。

Is any of this possible? And if so, what code do I add to the page?

这有可能吗?如果是这样,我应该向页面添加什么代码?

Thank you for any and all assistance, it is appreciated!

感谢您的任何帮助,不胜感激!

回答by Crayon Violent

What you are asking for is not easily done. In general, you cannot have javascript on the parent page that can hook into events on the iframed page if it is not hosted on the same domain. Same thing for the iframe page trying to tap into the parent page's DOM. This is called cross-site scripting (XSS), which goes against the same domain origin policy.

你所要求的并不容易做到。一般来说,如果 iframe 页面不是托管在同一个域上,则父页面上不能有 javascript 可以挂钩到 iframe 页面上的事件。尝试访问父页面的 DOM 的 iframe 页面也是如此。这称为跨站点脚本 (XSS),它违反了相同的域源策略。

At a minimum, the easiest thing to do would be to put the GA code on the iframed page. But that's probably not something you can do (though it couldn't hurt to ask the vendor).

至少,最简单的方法是将 GA 代码放在 iframe 页面上。但这可能不是您可以做的事情(尽管询问供应商并没有什么坏处)。

But some vendors (usually social media vendors such as Facebook, Google and Twitter) will have their widgets that output an iframe w/ info. They will set their server to allow for cross-domain scripting, which would allow you to tap into the iframe's DOM. Usually though they provide an API along with the widget that makes it easier to hook into relevant events (like share events), so that you don't have to do (much) coding yourself.

但是一些供应商(通常是 Facebook、Google 和 Twitter 等社交媒体供应商)的小部件会输出带有信息的 iframe。他们会将他们的服务器设置为允许跨域脚本,这将允许您访问 iframe 的 DOM。通常,尽管它们随小部件一起提供 API,可以更轻松地连接到相关事件(如共享事件),因此您不必自己进行(很多)编码。

Basically long story short, there's no way for you to track it without the person in charge of the iframe domain getting involved.

基本上长话短说,如果没有 iframe 域负责人的参与,您就无法跟踪它。

回答by Lokesh Suthar

I was searching for something similar and came across this. This jQuery Pluginlets you track an Iframe.

我正在寻找类似的东西并遇到了这个。这个 jQuery插件可让您跟踪 Iframe。

回答by Liko

Tried events?

尝试事件?

If the click is out of iframe:

如果点击超出 iframe:

_gaq.push(['_trackEvent', eventCategory, eventAtion, eventLabel]);

If the click is inside the iframe you should use:

如果点击在 iframe 内,您应该使用:

window.parent._gaq.push(['_trackEvent', eventCategory, eventAtion, eventLabel]);