将 Google Analytics 与 Require.js 结合使用时出现问题
我使用 require.js (http://requirejs.org/) 来实现我网站上的许多功能,到目前为止,它似乎运作良好。不过,我在尝试包含 Google Analytics 代码时遇到了问题。该代码似乎拒绝添加 utm.gif 并且没有向 Google 发送信标。我想知道这是否是一个范围问题。
define(function() {
var Analytics = {};
Analytics.Apply = function() {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}
return Analytics;
});
ga.debug 不会引发任何错误,并且 utm.gif 不会出现。如果我将代码移到 require.js 之外(我的意思是使用 require.js 的模块化 javascript,因此只需将其内联添加到页面中),utm.gif 就会成功添加到页面,并且 ga.debug 会发送其信标。
我发现这个网站似乎成功使用了它,但我不确定该网站做了什么不同的事情: http://paceyourself.net/2011/05/14/managing-client-side-javascript-with-requirejs/
其他人遇到组合问题require.js 和 GA?
I'm using require.js (http://requirejs.org/) for a number of functions on my site and so far it seems to be working well. I've run into an issue when trying to include Google Analytics code though. The code seems to refuse to add a utm.gif and is not sending off a beacon to Google. I'm wondering if it's a scope thing.
define(function() {
var Analytics = {};
Analytics.Apply = function() {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}
return Analytics;
});
ga.debug throws no errors and utm.gif does not appear. If I move the code outside require.js (by that I mean the modular javascript using require.js, so just adding it inline to the page), utm.gif is added to the page successfully and ga.debug sends off its beacon.
I found this site that seems to be using it successfully, but I'm not sure what that site is doing different: http://paceyourself.net/2011/05/14/managing-client-side-javascript-with-requirejs/
Anyone else run into issues combining require.js and GA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
其他答案都不适合我,但在阅读 Google Analytics 后,我设法找出了一些可行的方法文档。
在您的主
app.js
自己的文件
analytics.js
中:这是有效的,因为 requirejs 保证在函数执行时,
analytics.js
> 将完成加载。这意味着window.ga
函数已准备好接受命令。None of the other answers worked for me, but I managed to figure out something that does work, after reading the Google Analytics documentation.
in your main
app.js
in its own file
analytics.js
:This works because requirejs guarantees that by the time the function executes,
analytics.js
will have finished loading. This means that thewindow.ga
function is ready to accept commands.请参阅此 requirejs 群组帖子,了解有关该问题的讨论。
See this requirejs group thread for a discussion of the issue.
对于最新版本的 Google Analytics,我与 RequireJS 一起使用的代码片段是 -
For the latest version of Google Analytics, the code snippet I use with RequireJS is -
开始吧:
这是我当前正在使用的模块,向@user2305274 致敬
Here we go:
That's the module I am currently using, hat tip to @user2305274
使用较新的
analytics.js
时,其他解决方案对我不起作用。直接将 URL 作为依赖项放入是行不通的,因为 requirejs 无法确定脚本何时完成加载。 requirejs 的异步插件似乎也不适合我(尽管我将它用于谷歌地图 api)。以下方法对我有用:
The other solutions didn't work for me when using the newer
analytics.js
. Putting the URL in directly as a dependency didn't work, because requirejs wasn't able to determine when the script finished loading. The async plugin for requirejs didn't seem to work for me either (although I am using it for the google maps api).The following approach worked for me: