boo*_*ean 8 javascript analytics google-analytics require
我正在使用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;
});
Run Code Online (Sandbox Code Playgroud)
ga.debug不会抛出任何错误,并且不会出现utm.gif.如果我将代码移到require.js之外(我的意思是模块化的javascript使用require.js,所以只需将其内联添加到页面中),utm.gif就会成功添加到页面中,ga.debug会发送它的信标.
我发现这个网站似乎成功使用它,但我不确定该网站的用途是什么:http://paceyourself.net/2011/05/14/managing-client-side-javascript-with-requirejs /
还有其他人遇到过将require.js和GA结合起来的问题吗?
其他答案都没有对我有用,但在阅读Google Analytics文档后,我设法找出了有效的方法.
在你的主要 app.js
requirejs.config({
paths: {
ga: '//www.google-analytics.com/analytics'
}
});
requirejs(['analytics'], function () {
...
});
Run Code Online (Sandbox Code Playgroud)
在自己的文件中analytics.js:
define(['ga'], function () {
window.ga('create', 'UA-XXXXXX-1');
window.ga('send', 'pageview');
});
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为requirejs保证在函数执行时,analytics.js将完成加载.这意味着该window.ga函数已准备好接受命令.
| 归档时间: |
|
| 查看次数: |
7583 次 |
| 最近记录: |