我有一个带有许多.item div的Isotope #container div,可以通过单击它们的嵌套.header div来最大化和最小化。当观看者直接单击另一个内容div而没有最小化(单击标题)当前最大化的div时,它们也会自动最小化。
因此,有时会触发两次Google Analytics(分析)事件跟踪:当查看者没有直接单击另一个.item div的.header div时,而是决定先单击当前最大化的最小化,然后再单击另一个。
我应该如何修改逻辑,以便在我的分析中仅记录一个单击事件?
$('.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]);
});
最佳答案
因此,如果我理解正确,那么您只想在最大化某些值时触发GA跟踪:
$(this).parent().children('.maximised').show(function(){
_gaq.push(['_trackEvent', 'Items viewed', 'Clicked', clicked, null, true]);
});
您也可以在
.click
函数的底部尝试:if( $(this).parent().children('.maximised').is(':visible') ){
_gaq.push(['_trackEvent', 'Items viewed', 'Clicked', clicked, null, true]);
}
关于google-analytics - Google Analytics(分析)点击事件跟踪触发两次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12092066/