从jQuery文档准备好加载Google Analytics(分析)? [英] Load Google Analytics from jQuery document ready?

查看:138
本文介绍了从jQuery文档准备好加载Google Analytics(分析)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于GA有时需要一些时间来加载,而我的jQuery ready()函数在完成之前不会运行。我想将GA代码本身移到ready()函数的末尾。我不想寻找额外的点击跟踪集成 - 我只想让我的ready()脚本先运行。



我的问题是:
1) GA代码以任何方式打破其统计跟踪?而且,
2)是否需要模拟使用两个脚本标记(一个生成外部脚本标记,一个调用该函数)?如果是这样,为什么,以及在jQuery函数中做到这一点的最好方法是什么?



为了解释#2,这里是正好在关闭主体之前的GA代码标记:

 < script type =text / javascript> 
var gaJsHost =((https:== document.location.protocol)?https:// ssl。:http:// www。);
document.write(unescape(%3Cscript src ='+ gaJsHost +google-analytics.com/ga.js'type ='text / javascript'%3E%3C / script%3E));
< / script>
< script type =text / javascript>
尝试{
var pageTracker = _gat._getTracker(UA-8704511-1);
pageTracker._trackPageview();
} catch(err){}< / script>


解决方案

类似的问题在这里:





此处接受的答案为:


The problem is that GA sometimes takes a little while to load, and my jQuery ready() functions don't run until it's done. I'd like to move the GA code itself to the end of the ready() function. I'm not looking for extra click tracking integration - I just want my ready() scripts to run first.

My questions are: 1) Will moving the GA code break their stat tracking in any way? And, 2) Do I need to emulate their use of two script tags (one that generates the external script tag, and one that calls the function)? If so, why, and what's the best way to do that in a jQuery function?

To explain #2, here's the GA code that currently goes just before the closing body tag:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-8704511-1");
pageTracker._trackPageview();
} catch(err) {}</script>

解决方案

A similar question here:

And the accepted answer here:

这篇关于从jQuery文档准备好加载Google Analytics(分析)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆