_gaq.push未在Google Analytics中显示

时间:2014-10-09 03:15:28

标签: javascript google-analytics

我正在尝试跟踪网站上加载的随机图片,并在文档准备好后在我的页面上触发以下JavaScript。无论出于何种原因,Google Analytics中都不会跟踪该事件。我在下面添加了一些评论,其中有哪些部分可以解雇,哪些部分没有。

try {
    var loadedImage = $('.banner-container img').attr('src');
    var filename = loadedImage.substring(loadedImage.lastIndexOf('/') + 1);
    var analyticsVariable = "['_setCustomVar', 4, 'Banner Test', '" + filename + "', 1]";
    var analyticsEvent = "['_trackEvent', 'Banner Test', '" + filename + "', '" + filename + "', 1]";

    if (typeof _gaq !== "undefined") {
        _gaq.push(['_set', 'hitCallback' , function(){
            /* THE FOLLOWING LINE -NEVER- FIRES */
            console.log('Google Analytics: A/B testing success');
        }]);


        /* THE FOLLOWING IS WRITTEN TO THE CONSOLE: Google Analytics: Setting account number */
        console.log('Google Analytics: Setting account number');
        _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']); /* REMOVED GA ID FROM EXAMPLE */


        /* THE FOLLOWING IS WRITTEN TO THE CONSOLE: Google Analytics: Pushing A/B testing custom variable: ['_setCustomVar', 4, 'Banner Test', '00004.jpg', 1] */
        console.log('Google Analytics: Pushing A/B testing custom variable: ' + analyticsVariable);
        _gaq.push(analyticsVariable);


        /* THE FOLLOWING IS WRITTEN TO THE CONSOLE: Google Analytics: Pushing A/B testing event: ['_trackEvent', 'Banner Test', '00004.jpg', '00004.jpg', 1] */
        console.log('Google Analytics: Pushing A/B testing event: ' + analyticsEvent);
        _gaq.push(analyticsEvent);
    }
    else
    {
        /* THE FOLLOWING LINE (THANKFULLY) NEVER FIRES */
        console.log('Google Analytics: A/B testing error: _gaq is not available');
    }
}
catch (e) {
    /* THE FOLLOWING LINE (THANKFULLY) NEVER FIRES */
    Console.log('Google Analytics: A/B testing error: An unspecified error occurred');
}

我应该注意到,该网站仍然使用非通用Google Analytics代码。无论出于何种原因,该公司都不想更新它。遗憾的是,我没有权限更改站点配置以使用新格式。

对于为什么这可能不起作用的任何反馈,将不胜感激。

1 个答案:

答案 0 :(得分:2)

您将代码作为字符串而不是JS数组推送。

try {
    var loadedImage = $('.banner-container img').attr('src');
    var filename = loadedImage.substring(loadedImage.lastIndexOf('/') + 1);
    var analyticsVariable = ['_setCustomVar', 4, 'Banner Test', filename, 1];
    var analyticsEvent = ['_trackEvent', 'Banner Test', filename, filename, 1];

    if (typeof _gaq !== "undefined") {
        _gaq.push(['_set', 'hitCallback' , function(){
            /* THE FOLLOWING LINE -NEVER- FIRES */
            console.log('Google Analytics: A/B testing success');
        }]);


        /* THE FOLLOWING IS WRITTEN TO THE CONSOLE: Google Analytics: Setting account number */
        console.log('Google Analytics: Setting account number');
        _gaq.push(['_setAccount', 'UA-XXXXXXXX-1']); /* REMOVED GA ID FROM EXAMPLE */


        /* THE FOLLOWING IS WRITTEN TO THE CONSOLE: Google Analytics: Pushing A/B testing custom variable: ['_setCustomVar', 4, 'Banner Test', '00004.jpg', 1] */
        console.log('Google Analytics: Pushing A/B testing custom variable: ' + analyticsVariable);
        _gaq.push(analyticsVariable);


        /* THE FOLLOWING IS WRITTEN TO THE CONSOLE: Google Analytics: Pushing A/B testing event: ['_trackEvent', 'Banner Test', '00004.jpg', '00004.jpg', 1] */
        console.log('Google Analytics: Pushing A/B testing event: ' + analyticsEvent);
        _gaq.push(analyticsEvent);
    }
    else
    {
        /* THE FOLLOWING LINE (THANKFULLY) NEVER FIRES */
        console.log('Google Analytics: A/B testing error: _gaq is not available');
    }
}
catch (e) {
    /* THE FOLLOWING LINE (THANKFULLY) NEVER FIRES */
    Console.log('Google Analytics: A/B testing error: An unspecified error occurred');
}