gpt4 book ai didi

google-analytics - Google Analytics(分析)无法与 Meteor 配合使用

转载 作者:行者123 更新时间:2023-12-02 01:46:43 24 4
gpt4 key购买 nike

我正在尝试使用这种技术 http://www.bicobic.com/posts/BkFDo4CqcSnGcGtri将谷歌分析添加到 Meteor,但它不起作用(我已经使用谷歌分析调试器检查过)。我正在尝试使用 Universal Analytics 代码。

这是我的代码

<template name="ganalytics"> 
<div id="ganalytics">
</div>
</template>


Meteor.startup(function() {
isGanalyticsLoaded = false;

//a scriptloaderfunction
//if the script jsE1 loaded the callback is executed
myScriptLoader = function funcMyScriptLoader(jsEl, callback) {
if (window.attachEvent) {

// for IE (sometimes it doesn't send loaded event but only complete)
jsEl.onreadystatechange = function funcOnReadyStateChange() {
if (jsEl.readyState === 'complete') {
jsEl.onreadystatechange = "";
} else if (jsEl.readyState === 'loaded') {
jsEl.onreadystatechange = "";
}

if (typeof callback === 'function') {
callback();
}
};
} else {
// most browsers
jsEl.onload = function funcOnLoad () {
if (typeof callback === 'function') {
callback();
}
};
}
};
});


Template.ganalytics.rendered = function() {
if(!isGanalyticsLoaded) {
window['GoogleAnalyticsObject']='ga';
window['ga']=window['ga']||function(){
(window['ga'].q=window['ga'].q||[]).push(arguments)
}, window['ga'].l=1*new Date();

var myGAJs = document.createElement('script'),
s = document.getElementsByTagName('script')[0];
myGAJs.type ='text/javascript';
myGAJs.async = true;
myGAJs.src = '//www.google-analytics.com/analytics.js';

myScriptLoader(myGAJs, function funcEventLoaded() {
isGanalyticsLoaded = true;
ga('create', 'UA-XXXXXX', 'url');
ga('send', 'pageview');
});
s.parentNode.insertBefore(myGAJs, s);
}
};


<template name="layout">
<div>
{{yield}}
</div>
{{yield 'ganalytics'}}
</template>

我的代码可能有什么问题?

提前致谢。

最佳答案

较新的 Google Analytics 代码段似乎不适用于 Meteor,但将这种较旧的优化样式放入 HEAD 中却可以:

var _gaq=[['_setAccount','UA-xxx'],['_setDomainName', 'example.com'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));

您还可以执行此操作将客户端错误记录到 GA:

// log client-side errors to Google Analytics
window.onerror = function(message, file, line) {
_gaq.push(['_trackEvent', 'JS Error', file + ':' + line + '\n\n' + message]);
};

关于google-analytics - Google Analytics(分析)无法与 Meteor 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21224129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com