2015-08-27 80 views
2

我试图找到最详尽/兼容的解决方案来跟踪我的网站内的一些链接。正确的方法来跟踪链接的点击

其实,我有这个代码:

$(".article-content a").each(function() { 
    $(this).click(function() { 
     // Tracking code here 
    }); 
}); 

是否有关于真实的用户重定向处理任何recommandations? 我认为我们必须首先排除右键点击? 并确保Ctrl-Click,MouseWheel-Click,Touch事件,通过键盘导航等正确处理以触发,例如GA事件?

+1

您可以检查键码! –

+0

您的问题太广泛了,有很多选择和可能的解决方案。你应该指定你需要具体跟踪,你遇到什么问题,并显示你已经尝试过。 –

+1

这可能会帮助你http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery – Farhan

回答

1

制造类似这样

$('.asdfasdf').mousedown(function(e) { 
    switch (e.which) { 
     case 1: 
      //Left Mouse button pressed 
      break; 
     case 2: 
      //Middle Mouse button pressed 
      break; 
     case 3: 
      //Right Mouse button pressed 
      break; 
     default: 
      //asdfasdf 
    } 
}); 

这里的一些文档:jQuery-Doc

0

尝试jQuery的event.which结合.mousedown。喜欢的东西:用参数来处理

$('.article-content a').mousedown(function(event){ 
    var message = 'click'; 
    if (event.ctrlKey) message += ' ctrl'; 
    if (event.shiftKey) message += ' shift'; 

    switch (event.which) { 
     case 1: 
      message = 'left ' + message; 
      break; 
     case 2: 
      message = 'middle ' + message; 
      break; 
     case 3: 
      message = 'right ' + message; 
      break; 
    } 

    alert(message); 
}); 
0

使用功能单击

$(".article-content a").each(function() { 
    $(this).click(function(e) { 
     if(e.ctrlKey) { 
     //Ctrl+Click 
     } 
     if(e.altKey) { 
     //Alt+Click 
     } 
     ... 
    }); 
}); 

日志e到控制台,以获取更多信息

你可以听其他活动移动:tap, taphold, swipe, swipeleft...

$(".article-content a").on("tap",function(){ 
    #element is tapped 
}); 
0

我建议你采用以下方法。

  1. 类添加到您想要跟踪的元素:

    < a class="trackMouseClick" >I want to be tracked onclick</a > 
    
  2. 定义事件处理程序为每个类:

    //the actual event handler 
    //here you can implement the logic for each kind of event 
    function mousedownHandler(e){ 
        console.log('target element: ' + e.target + '\tbutton clicked: ' + e.which); 
    } 
    
    //the event binder 
    //remark: the event is bound only for the elements feature the proper class 
    $('.trackMouseClick').on('mousedown',function(e){ 
        mousedownHandler(e); 
    }); 
    
  3. 添加尽可能多的类和事件处理程序的你想跟踪的许多事件:

    function mousedownHandler(e){ 
        console.log('target element: ' + e.target + '\tbutton clicked: ' + e.which); 
    } 
    
    function tapHandler(e){ 
        console.log('target element: ' + e.target); 
    } 
    
    
    $('.trackMouseClick').on('mousedown',function(e){ 
        mousedownHandler(e); 
    }).on('tap',function(e){ 
        tapHandler(e); 
    }); 
    

主要优点是:

  • 模块化:你可以添加和删除事件处理程序简单地添加,并从DOM元素删除类

  • 脱钩:使用类从分离DOM结构您要实现的跟踪逻辑

+0

好吧,但不是真的,我想要的,如何确保我可以处理重定向用户在(不正确)点击/触摸/键盘选择的链接上的所有事件? – bigben3333

+0

只需更新我的答案 –