从 jQuery 文档加载 Google Analytics 准备好了吗? [英] Load Google Analytics from jQuery document ready?

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

问题描述

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

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.

我的问题是:1) 移动 GA 代码会以任何方式破坏他们的统计跟踪吗?和,2) 我是否需要模拟他们使用两个脚本标签(一个生成外部脚本标签,一个调用函数)?如果是这样,为什么?在 jQuery 函数中执行此操作的最佳方法是什么?

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?

为了解释 #2,这里是当前位于结束正文标记之前的 GA 代码:

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:

这里接受的答案:

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

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