来自 file:// url 的 Google Analytics
我们有一个基于 ajaxy 的 html 应用程序框架,并希望谷歌分析能够使用它。我相信我们已经正确设置,可以在需要时手动调用 _trackPageview
。
然而事情似乎没有被报道。现在,要么我没有让它正常工作,要么通过 url 上的 file://
协议从 javascript 进行 GA 跟踪,默默地违反了一些我不知道的跨域策略。
那么 GA 是否可以通过 file://
处理本地 html?还是我的GA使用有问题?
请注意,我们使用的域实际上并不存在。我们希望使用移动应用跟踪之类的功能,但来自 JavaScript 而不是原生库。为了做到这一点,您似乎设置了一个假域名,并告诉跟踪器它应该报告为哪个域名。
在我的 的末尾:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXACCOUNTID-XX']);
_gaq.push(['_setDomainName', 'myfake.domain.com']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'http://www.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
在我们的 JS 框架中,我们调用:
_gaq.push(['_trackPageview', '/some/path/here']);
We have an ajaxy sort of html based app framework thing and want google analytics to work with it. And I believe we have set things up properly to manually call _trackPageview
where needed.
However things don't seem to be getting reported. Now either I don't have it working right, or GA tracking from javascript with a file://
protocol on the url silently violates some cross domain policy I'm not aware of.
So does GA work with local html via file://
? Or is there something wrong with my GA usage?
Note that the domain we are using doesn't actually exist. We want to use something like the mobile app tracking but from JavaScript rather than a native library. And in order to do this, it looks you setup a fake domain, and tell the tracker what domain it should be reporting as.
At the end of my <head>
:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXACCOUNTID-XX']);
_gaq.push(['_setDomainName', 'myfake.domain.com']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'http://www.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
And in our JS framework we call:
_gaq.push(['_trackPageview', '/some/path/here']);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Google 现在支持禁用协议检查任务,只需将其设置为null,允许您从
file://
url 跟踪分析:Google now supports disabling the protocol check task by setting it to null, allowing you to track analytics from a
file://
url:需要进行一些调整:
禁用 cookie 存储
Cookie 无法使用,因为没有正在运行的域,因此我们需要阻止 GA 尝试使用它们。
这是通过在创建配置中设置
'storage': 'none'
来完成的 (文档)。禁用文件协议检查
默认情况下,如果协议(在我们的示例中为
file
)不是http
或https
,GA 就会中止。使用相应的任务禁用此检查:
ga('set ', 'checkProtocolTask', null)
手动设置活动页面
由于没有域,GA 无法导出表示活动页面的路径。
可以使用页面网址修改功能手动配置:
ga('set', 'page', 'foobar')
随后的
ga('send', 'pageview')
将在数据中显示为访问<代码>/foobar。使用 localStorage 跟踪用户身份(可选)
禁用 cookie 后,不会跨页面加载跟踪用户,因此每次刷新都会触发对另一个唯一访问者的检测。
不过,我们可以在通过设置
'clientId': localStorage.getItem(someKey)
来创建,它会查找以前存储的客户端 ID。存储 ids 是通过
Everything linked
来完成的结合上述所有步骤,我们最终得到如下结果:
A couple of tweaks are necessary:
Disable cookie storage
Cookies cannot be used as there's no domain in action, so we need to prevent GA from trying to use them.
This is done by setting
'storage': 'none'
in creation config (documentation).Disable file protocol check
By default, GA aborts if the protocol (in our case
file
) is nothttp
orhttps
.Disable this check using the corresponding task:
ga('set', 'checkProtocolTask', null)
Set active page manually
Since there is no domain, GA fails to derive a path denoting the active page.
It can be configured manually by using the page URL modification feature:
ga('set', 'page', 'foobar')
A subsequent
ga('send', 'pageview')
will then show up in data as a visit on/foobar
.Track user identity using localStorage (optional)
With cookies disabled, users are not tracked across page loads, so each refresh will trigger detection of another unique visitor.
However, we can provide custom client ids on create by setting
'clientId': localStorage.getItem(someKey)
, which looks for previously stored client ids.Storing ids is done by
Everything combined
Combining all above steps, we end up with something like the following:
好的,我想我已经解决了这个问题。它已经困扰我好几天了。
根据 Google Analytics(分析)帮助中心 ,
这是我的理论:在 Mac OS X Snow Leopard 上的测试中,从 file:// 运行的文档无法设置 cookie。这是因为 cookie 是 HTTP 专有的,当您从 file:// 运行某些内容时,您并没有使用 HTTP 协议。
由于您无法设置 cookie,ga.js 拒绝将 _utm.gif 请求发送到 Google 服务器。没有设置 cookie;没有向 google 发送任何请求,因此 GA 中没有记录任何内容。
解决方案:使用可以将域设置为 http://localhost 的开发环境(类似于 MAMP,如果您使用的是Mac 并需要 LAMP 堆栈)
(奇怪的脚注:我观察到一些奇怪的行为,其中 GA cookie 将设置为来自第三方非 CDN 的不相关导入脚本的域的第三方 cookie这可能是因为服务器随文件发送 HTTP cookie,ga.js 将自身附加到该域,但是,这不会充当后门,因为它仍然不会将 _utm.gif 发送到。谷歌的服务器)。
========
编辑:
您可以尝试人们为无 cookie GA 跟踪创建的各种解决方法之一。
您可能会通过此工具获得一些成功:http://code. google.com/p/google-analytics-js/downloads/list,解释如下:http://remysharp.com/2009/02/27/analytics-for-bookmarklets-injected-scripts/
您可以包含以下内容,而不是所有 GA 代码脚本,然后使用以下代码调用它:
它是为书签/注入脚本跟踪而设计的,但是如果我放入 file:// 类型设置,它能够成功发送 __utm.gif 命中,这意味着它应该在GA。
缺点是无 cookie 意味着它无法准确跟踪访问,只能跟踪页面视图级别的数据。
OK, I think I have this one solved. It's been dogging me for a few days.
According to Google Analytics Help Center,
Here's my theory: In my tests on Mac OS X Snow Leopard, documents run from file:// are not able to set cookies. This is because cookies are proprietary to HTTP, and when you run something from file://, you're not using the HTTP protocol.
Since you're not able to set cookies, ga.js refuses to send the _utm.gif request to Google's servers. No cookies get set; no request is sent to google, so nothing is logged in GA.
Solution: Use a development environment where you can set your domain as http://localhost (something like MAMP, if you're on a Mac and need a LAMP stack)
(Weird footnote: I observed some weird behavior where the GA cookies would set as third-party cookies of the domain of an unrelated imported script from a third party non-CDN domain. This could be because since the server sends HTTP cookies with the file, ga.js is attaching itself to that domain. However, this won't serve as a backdoor, since it still won't send the _utm.gif hit to Google's servers ).
========
EDIT:
You could try one of the various work arounds people have created for cookie-less GA tracking.
You might get some success out of this tool: http://code.google.com/p/google-analytics-js/downloads/list, explained here: http://remysharp.com/2009/02/27/analytics-for-bookmarklets-injected-scripts/
Instead of all of that GA code, you would include the script, and then call it using the following code:
Its designed for bookmarklet/injected script tracking, but if I put in a file:// type setup, its able to successfully send the __utm.gif hit, meaning it SHOULD track successfully in GA.
The drawback is that cookieless means that it won't be able to track visits accurately, just page-view level data.
最终通过调整大小黑客消息传递机制通过 iframe 进行复杂的反弹。
本地文件包括我们服务器上的 iframe。当我们想要跟踪 GA 调用时,我们使用我们需要的信息更改它的 url 哈希
#_trackEvent,foo,bar
,然后更改 iframe 的宽度。在 iframe 中,onresize()
函数被触发,并允许我们通过检查哈希来提交 GA 调用。尽管我非常讨厌这个黑客,但它的工作却完美无缺!
Ended up with a complex bounce through an iframe via the resize hack message passing mechanism.
Local file include an iframe on our server. When we want to track a GA call we changes it's url hash with the info we need
#_trackEvent,foo,bar
, and then change the width of the iframe. In the iframe theonresize()
function gets triggered and allows us to submit GA calls by inspecting the hash.As much as I hate this hack, it works flawlessly!
这是我的代码,它有效
here is my code, it works