Bravo Net
Bravo Net

Reputation: 805

Unable to fire GA event in jquery

dp("a.download-2").click(function(){
    
    var e=dp(this).attr("id");
    var t=dp(this).attr("name");
    var n=dp(this).attr("rel");
    var r=navigator.platform;
    var i=r.substring(0,3);

    if(i=="Win"){
        jQuery("#fb_pixel").addClass("fb_conversion");

        _gaq.push(["_trackEvent",n,"download",t]);

        window.location.href="http://"+e+".rackcdn.com/abc123.exe";
    }else if(i=="Mac"){
        jQuery("#fb_pixel").addClass("fb_conversion");

        _gaq.push(["_trackEvent",n,"download",t]);

        window.location.href="http://"+e+".rackcdn.com/abc123.dmg";
        
    }else{
        alert("The software only supports Windows and Mac operating system.");return false}});

the tracking in this code will not work.

but if i add in one alert code below the GA tracking event code. the tracking works fine. like the code below,

dp("a.download-2").click(function(){
    
    var e=dp(this).attr("id");
    var t=dp(this).attr("name");
    var n=dp(this).attr("rel");
    var r=navigator.platform;
    var i=r.substring(0,3);

    if(i=="Win"){
        jQuery("#fb_pixel").addClass("fb_conversion");

        _gaq.push(["_trackEvent",n,"download",t]);
        alert(n+" "+t);

        window.location.href="http://"+e+".rackcdn.com/abc123.exe";
    }else if(i=="Mac"){
        jQuery("#fb_pixel").addClass("fb_conversion");

        _gaq.push(["_trackEvent",n,"download",t]);
        alert(n+" "+t);

        window.location.href="http://"+e+".rackcdn.com/abc123.dmg";
        
    }else{
        alert("The software only supports Windows and Mac operating system.");return false}});

I've no idea why this alert is affecting. by right it shouldn't.

Upvotes: 1

Views: 71

Answers (1)

Serendipity
Serendipity

Reputation: 1037

The line window.location.href="http://"+e+".rackcdn.com/abc123.exe"; forces the browser to redirect before your GA code has had a chance to work.

So you can try using a setTimeout within your code after the GA code is written. That way you can ensure that the GA code runs prior to redirecting the page.

The alert works because it pauses execution till you click 'OK', which is enough time in the world for any javascript to work before it.

Upvotes: 1

Related Questions