cho*_*obo 2 javascript google-analytics
我正在谷歌分析中使用自定义变量,并且不确定为什么以下实际上有效.为什么Google的跟踪代码会先被执行,特别是因为它位于页面的底部?
这两个脚本都是自动执行的函数,那么javascript如何确定首先执行哪一个?
// top of the page
<script type="text/javascript">
$(function ()
{
_gaq.push(['_setCustomVar', 1, 'Account', 'PartTime', 1]);
});
</script>
// bottom of the page
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxxx']);
_gaq.push(['_setDomainName', '.xxxxxxxx.com']);
_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>
Run Code Online (Sandbox Code Playgroud)
第一个执行首先执行,而不是自执行功能.
它包含对$函数的调用,并且有一个参数:匿名函数.
$是一个非常糟糕的功能.这个名字本身毫无意义,它被六个不同的图书馆采用,可以做六个不同的事情.
在jQuery中,如果你传递一个函数,$它会在ready触发事件时运行该函数(但是正在解析HTML文档的末尾).这可能就是这里发生的事情.
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |