加载谷歌分析参数化异步页面跟踪脚本

时间:2022-03-03 15:17:42

If I use the google analytics async site tracking script, in the end of head section of my page, everything works as expected:

如果我使用谷歌分析异步网站跟踪脚本,在我的页面的head部分的末尾,一切都按预期工作:

<head>   
    <script type="text/javascript">

        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXX-X']);
        _gaq.push(['_setDomainName', 'test.com']);
        _gaq.push(['_trackPageview', '/title=ied&action=fire']);

        (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>
</head>

In the fiddler there can be seen 2 requests:

在小提琴手中可以看到2个请求:

加载谷歌分析参数化异步页面跟踪脚本加载谷歌分析参数化异步页面跟踪脚本

For some reason I need the script to be parametrized, so I wrap it within custom googleAnalytics function which gets 2 parameters:

出于某种原因,我需要对脚本进行参数化,因此我将其包装在自定义googleAnalytics函数中,该函数获取2个参数:

function googleAnalytics(domain, queryString) {
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_setDomainName', domain]);
    _gaq.push(['_trackPageview', queryString]);

    (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);
    })();
}

Abowe code has been saved to GoogleAnalytics.js file. I loaded it to the page in the head section like below:

Abowe代码已保存到GoogleAnalytics.js文件中。我把它加载到头部的页面,如下所示:

<head>
    <script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript">googleAnalytics('test.com', '/title=ied&action=fire');</script>
</head>

But this time fiddler shows only 1 request:

但这次小提琴手只显示了1个请求:

加载谷歌分析参数化异步页面跟踪脚本

The Google Analytics Tracking Code Debugger also shows nothing. Only ga.js script is downloaded but there is no other request which populates data for google analytics report. What is wrong in this approach and how it can be fixed ?

Google Analytics跟踪代码调试程序也不会显示任何内容。仅下载了ga.js脚本,但没有其他请求填充Google Analytics分析报告的数据。这种方法有什么问题以及如何解决?

Btw: I need this 2 parameters and the async version of the tracking script.

顺便说一句:我需要这2个参数和跟踪脚本的异步版本。

Regards

问候

2 个解决方案

#1


2  

Your problem is that the your _gaq declaration is local in scope. ga.js loads, and looks for _gaq in the global scope. (Once the script is injected, it no longer has access to the function's scope.)

您的问题是您的_gaq声明在范围内是本地的。 ga.js加载,并在全局范围内查找_gaq。 (一旦注入脚本,它就不再能够访问该函数的范围。)

To fix it, you can either place var _gaq = _gaq || []; outside of the function, or you can replace it within the function with this:

要修复它,您可以放置​​var _gaq = _gaq || [];在函数外部,或者您可以在函数内替换它:

window._gaq = window._gaq || []; 

This will resolve your problem.

这将解决您的问题。

#2


0  

I've made this working.

我已经做到了这一点。

The GoogleAnalytics.js looks following:

GoogleAnalytics.js看起来如下:

function googleAnalytics(domain, queryString) {        
    _gaq.push(['_setDomainName', domain]);
    _gaq.push(['_trackPageview', queryString]);
}

while the head section is as follows:

而头部如下:

<head>
    <script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXX-X']);

        (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>
    <script type="text/javascript">googleAnalytics('com.dev.abb.com','');</script>
</head>

Regards

问候

#1


2  

Your problem is that the your _gaq declaration is local in scope. ga.js loads, and looks for _gaq in the global scope. (Once the script is injected, it no longer has access to the function's scope.)

您的问题是您的_gaq声明在范围内是本地的。 ga.js加载,并在全局范围内查找_gaq。 (一旦注入脚本,它就不再能够访问该函数的范围。)

To fix it, you can either place var _gaq = _gaq || []; outside of the function, or you can replace it within the function with this:

要修复它,您可以放置​​var _gaq = _gaq || [];在函数外部,或者您可以在函数内替换它:

window._gaq = window._gaq || []; 

This will resolve your problem.

这将解决您的问题。

#2


0  

I've made this working.

我已经做到了这一点。

The GoogleAnalytics.js looks following:

GoogleAnalytics.js看起来如下:

function googleAnalytics(domain, queryString) {        
    _gaq.push(['_setDomainName', domain]);
    _gaq.push(['_trackPageview', queryString]);
}

while the head section is as follows:

而头部如下:

<head>
    <script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-XXXXX-X']);

        (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>
    <script type="text/javascript">googleAnalytics('com.dev.abb.com','');</script>
</head>

Regards

问候