Hoppe
Hoppe

Reputation: 6815

Google analytics is not calling __utm.gif, but throws no error messages

I'm having a problem with google analytics. I know there's a lot out there, but couldn't find a solution.

I want to log the user's email address as a custom variable in Google analytics. I have the google analytics snippet in a function, and pass in the user email address.

The GA plugin doesn't output anything to the console window.

There are no javascript errors.

I stepped through the code with breakpoints, and the function below runs through to completion. It goes into the if statements, etc.

The file ga.js loads successfully, but the __utm.gif file does not get called.

    <script type="text/javascript">
    //<![CDATA[        
    function googleTrack(email) {
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'xyz']);
        _gaq.push(['_trackPageview']);

        if ((parent == null) || (!parent.IsCMSDesk)) {
            //track user name
            _gaq.push(['_setCustomVar', //1, \"Email address\", \"{0}\", 3)
                1,                   // This custom var is set to slot #1.  Required parameter.
                'email_address',     // The name acts as a kind of category for the user activity.  Required parameter.
                email,               // This value of the custom variable.  Required parameter.
                3                    // Sets the scope to page-level.  Optional parameter.
                ]);

            //if googleCustomVar is defined, it will track it. if not, won't
            if (!((typeof (window.googleCustomVar) === "undefined") && (typeof (googleCustomVar) === "undefined"))) {
                _gaq.push(['_setCustomVar', //1, \"Email address\", \"{0}\", 3)
                2,                   // This custom var is set to slot #1.  Required parameter.
                'section',     // The name acts as a kind of category for the user activity.  Required parameter.
                googleCustomVar,               // This value of the custom variable.  Required parameter.
                3                    // Sets the scope to page-level.  Optional parameter.
                ]);
            }
        }

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

    googleTrack('[email protected]');
    //]]>
</script>

If I simply it to just this code, all things work again:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xyz']);
_gaq.push(['_trackPageview']);
(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);
})();

What's not working right here? The end goal is to be able to call a function that will track the user's email address as a custom variable in Google analytics.

Upvotes: 2

Views: 1713

Answers (1)

Rob W
Rob W

Reputation: 349232

You're defining _gaq as a local variable, inside a function. Because of this, the Google Analytics code cannot read the variable. The original code does work using var, because it's executed in the local scope. To declare a variable in the global scope from within a function, omit var, or use window._gaq = ...:

Your broken code:

<script type="text/javascript">
//<![CDATA[        
function googleTrack(email) {
    var _gaq = _gaq || [];      //<--- `var` inside a function = local variable

Fixed, working code:

    _gaq = _gaq || [];

Upvotes: 4

Related Questions