Systembolaget
Systembolaget

Reputation: 2514

Google analytics click event tracking firing twice

I have an Isotope #container div with many .item divs, which can be maximised and minimised by a click in their nested .header div. They also minimise automatically when a viewer directly clicks another content div without minimising (clicking the header) the currently maximised one.

Therefore, the Google analytics event tracking is sometimes fired twice: when the viewer does not directly click in the .header div of another .item div - but decides to minimise the currently maximised one by clicking it first, before clicking another.

How should I modify the logic so ever only one click event is registered in my analytics?

$('.header').click(function () { // instead of registering the entire .item div (default use), only its .header div (child div) receives clicks

    var $previousSelected = $('.selected'); // necessary for maximising and minimising

    if ($(this).parent().hasClass('selected')) { // use $(this).parent() - not $(this) - because the .header div is a child of the .item div

        $(this).parent().removeClass('selected');
        $(this).parent().children('.maximised').hide();
        $(this).parent().children('.minimised').show();

        $items.find('.minimised').removeClass('overlay'); // returns all .minimised divs to previous state after the .item is closed again

    } else {

        $previousSelected.removeClass('selected');
        $previousSelected.children('.minimised').show();
        $previousSelected.children('.maximised').hide();

        $(this).parent().addClass('selected');
        $(this).parent().children('.minimised').hide();
        $(this).parent().children('.maximised').show();

        $items.not('.selected').find('.minimised').addClass('overlay'); // adds .overlay on each .item which is not currently .selected

    }

    $container.isotope('reLayout');

    <!-- ga project interest tracking -->

    var clicked = $(this).parent().data('item');
    _gaq.push(['_trackEvent', 'Items viewed', 'Clicked', clicked, null, true]);

});

Upvotes: 2

Views: 2705

Answers (1)

jk.
jk.

Reputation: 14435

So, if I understand correctly, you only want to fire the GA tracking when something is maximized:

$(this).parent().children('.maximised').show(function(){
    _gaq.push(['_trackEvent', 'Items viewed', 'Clicked', clicked, null, true]);
});

You could also try at the bottom of your .click function:

if( $(this).parent().children('.maximised').is(':visible') ){
   _gaq.push(['_trackEvent', 'Items viewed', 'Clicked', clicked, null, true]); 
}

Upvotes: 1

Related Questions