简体   繁体   English

自执行匿名功能中的Javascript Google Analytics(分析)

[英]Javascript google analytics in self-executing anonymous function

I'm trying to write some custom javascript inside a self-executing anonymous function. 我正在尝试在自执行匿名函数中编写一些自定义javascript。 This custom javascript needs to interact with the global Google Analytics ga function that is created when the Google Analytics library is loaded. 此自定义JavaScript需要与加载Google Analytics(分析)库时创建的全局Google Analytics(分析) ga函数进行交互。 The following code is in the <head> of the page loading the library: 以下代码位于加载库的页面的<head>中:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXXX-X', 'auto');
</script>

At the bottom of the page, my custom javascript is loaded. 在页面底部,加载了我的自定义javascript。 This is the code: 这是代码:

(function($, MutationObserver, shippingMethods, ga) {

    function triggerPageview(path) {
        ga('send', 'pageview', path);
    }

})(jQuery, MutationObserver, shippingMethods, ga);

What I am finding is that the current code does not successfully trigger a pageview. 我发现的是当前代码无法成功触发网页浏览。 The pageview only gets triggered if I remove ga from the arguments, like this: 仅当我从参数中删除ga时,才会触发综合浏览量,如下所示:

(function($, MutationObserver, shippingMethods) {

    function triggerPageview(path) {
        ga('send', 'pageview', path);
    }

})(jQuery, MutationObserver, shippingMethods);

The code above works, the first example doesn't. 上面的代码有效,第一个示例无效。 I'm assuming this is me misunderstanding how these arguments to a self-executing anonymous function are supposed to work. 我以为这是我误解了应该如何使用这些自执行匿名函数的参数。 I'm guessing that when the self-executing function is executed, the value of ga is some value before the external Google Analytics library is loaded. 我猜想在执行自执行功能时, ga的值是在加载外部Google Analytics(分析)库之前的某个值。 However, I thought that after the library loads, and ga is updated, that the ga inside my function would also be updated. 但是,我认为,库加载,并经过ga更新后,在ga我的函数内也将更新。 Is this not true? 这不是真的吗

I logged the value of ga before the pageview is triggered, to compare, first with ga passed in: 我先记录了ga的值,然后才触发网页浏览进行比较,首先与传入的ga进行比较:

(function($, MutationObserver, shippingMethods, ga) {

    function triggerPageview(path) {
        console.log(ga);
        ga('send', 'pageview', path);
    }

})(jQuery, MutationObserver, shippingMethods, ga);

This logs: 记录:

ƒ (){
    (i[r].q=i[r].q||[]).push(arguments)}

Then without the ga argument: 然后没有ga参数:

(function($, MutationObserver, shippingMethods) {

    function triggerPageview(path) {
        console.log(ga);
        ga('send', 'pageview', path);
    }

})(jQuery, MutationObserver, shippingMethods);

This logs: 记录:

ƒ (a){J(1);Z.D.apply(Z,[arguments])}

So, it seems I am definitely misunderstanding something. 所以,看来我肯定是误会了。 Can someone explain what's going on here? 有人可以解释这是怎么回事吗? What is the "proper" way to refer to a global within custom code like I am showing? 在我所显示的自定义代码中引用全局的“正确”方法是什么?

When I pass ga into my function, is it making a "copy" of the current value of ga , rather than referring to the global ga variable? 当我将ga传递给函数时,它是对ga的当前值进行“复制”,而不是引用全局ga变量吗?

I think I answered my own question. 我想我回答了我自己的问题。 Correct me if I'm wrong, but based on the docs here: 如果我错了,请指正我,但请根据此处的文档进行:

https://developers.google.com/analytics/devguides/collection/analyticsjs/how-analyticsjs-works https://developers.google.com/analytics/devguides/collection/analyticsjs/how-analyticsjs-works

I think Google Analytics is redefining the ga command after the library loads. 我认为Google Analytics(分析)在库加载后正在重新定义ga命令。 So my function still references the originally defined ga method, but the global ga has been redefined. 因此,我的函数仍引用最初定义的ga方法,但全局ga已重新定义。 In this case it seems it would be best to not pass the ga argument, and instead just always refer to window.ga . 在这种情况下,最好不要传递ga参数,而应该始终引用window.ga

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

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