gpt4 book ai didi

javascript - Google Analytics inside Document 准备好不工作了吗?

转载 作者:行者123 更新时间:2023-11-30 08:54:07 24 4
gpt4 key购买 nike

关于为什么有效的原因的快速问题:

<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);

(function() {
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);
})();

</script>

不会:

<script type="text/javascript">
$(function(){
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);

(function() {
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);
})();
});
</script>

是什么阻止分析在文档准备就绪的情况下运行?

最佳答案

你需要将_gaq变量声明到全局作用域

<script type="text/javascript">
var _gaq = _gaq || [];
$(function(){
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
...

或使 window._gaq 属性与谷歌分析一起使用

<script type="text/javascript">
$(function(){
window.gaq = window._gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
...

关于javascript - Google Analytics inside Document 准备好不工作了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15226726/

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