yar1
yar1

Reputation: 1351

Google Anlytics event not firing

I am not getting and event data in GA. I installed Google Analytics Debugger Chrome extension and I see nothing happening (same goes when looking at Network panel in developer tools). I Googled it and read many (many) other answers and it looks like I'm doing things right. Page views, etc. are registering correctly...

I have this code as the last thing before my closing tag:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-MYREALCODE', 'mybna.net');
    ga('send', 'pageview');
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-MYREALCODE']);
    _gaq.push(['_trackPageview']);
</script>

My event handlers are done using jQuery, all inside an external js file, loaded before the closing tag. For example:

$(function () {
    $('#show-less').click(function (e) {
        pbr.showHideMore(e);
        _gaq.push(['_trackEvent', 'ShowMore', 'Hide', 'top button']);
    });
});

Any ideas anyone?

Upvotes: 1

Views: 85

Answers (2)

Eike Pierstorff
Eike Pierstorff

Reputation: 32760

Edit: I keep deleting and undeleting this anwswer because I'm not sure if your code is redundant or if you are actually trying to send data to two different properties.

If it's the first, see original answer below. For the latter make sure that you account/property-Ids are pointing to correctly configured properties (i.e. the first UA, the latter asynchronous), plus I'm pretty sure you need to include the ga.js liberary two if you want the methods pushed in the array to be executed.

You are mixing Universal Analytics and asynchronous code, that's not going to work (i.e. if you have ga send and _gaq.push in the same piece of code you are most probably doing it wrong). Adapt your event tracking code for UA and you should be alright.

Upvotes: 2

Sean Kendle
Sean Kendle

Reputation: 3609

Try wrapping the click listener in a document ready function:

$(document).ready(function(){
    $('#show-less').click(function (e) {
        pbr.showHideMore(e);
        _gaq.push(['_trackEvent', 'ShowMore', 'Hide', 'top button']);
    });
});

I often find that the listener won't link up until the DOM is fully loaded. Sometimes it does, though, but that might have to do with the code being below the object in question in the code.

It always helps me to put an "alert" into the click function while testing to be sure it's actually firing off (and catching) that event.

Upvotes: 0

Related Questions