简体   繁体   English

为什么 Google 的分析跟踪代码首先执行?

[英]Why does Google's Analytics tracking code execute first?

I was playing around with custom variables in Google Analytics and wasn't sure why the following actually works.我在 Google Analytics 中玩弄自定义变量,但不确定为什么以下内容确实有效。 Why does Google's tracking code get executed first, especially since it is at the bottom of the page?为什么 Google 的跟踪代码会首先执行,尤其是因为它位于页面底部?

Both scripts are in self-executing functions, so how does javascript determine which one to execute first?两个脚本都在自执行函数中,那么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>  

The first one does execute first, and isn't in a self executing function.第一个确实首先执行,并且不在自执行 function 中。

It consists of a call to the $ function and has one argument: an anonymous function.它包括对$ function 的调用,并有一个参数:匿名 function。

$ is a very badly named function. $是一个非常糟糕的名称 function。 The name itself is meaningless and it has been adopted by half a dozen different libraries to do half a dozen different things.这个名字本身是没有意义的,它已经被六个不同的图书馆采用来做六个不同的事情。

In jQuery, if you pass a function to $ it runs that function when the ready event is triggered (but the end of the HTML document being parsed). In jQuery, if you pass a function to $ it runs that function when the ready event is triggered (but the end of the HTML document being parsed). This is probably what is happening here.这可能就是这里发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM