开发者

loading google analytics parametrized async page tracking script

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

<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:

loading google analytics parametrized async page tracking script

loading google analytics parametrized async page tracking script

For some reason I need the script to be parametrized, so I wrap it开发者_运维百科 within custom googleAnalytics function which gets 2 parameters:

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:

<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:

loading google analytics parametrized async page tracking script

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 ?

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

Regards


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.)

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

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

This will resolve your problem.


I've made this working.

The GoogleAnalytics.js looks following:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜