简体   繁体   中英

_gaq.push Not Showing Up In Google Analytics

I am trying to track a random image that is loaded on the site, and have the following JavaScript firing on my page after the document is ready. For whatever reason, the event is never tracked in Google Analytics. I have added comments below on what sections appear to fire and what ones don't.

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

I should note that this site unfortunately still uses the non-Universal Google Analytics tag. The business--for whatever reason, does not want to update it yet. I unfortunately, do not have permission to change the site configuration to use the new format.

Any feedback as to why this might not be working, would be greatly appreciated.

You're pushing in the code as a string instead of a JS array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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