You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用GA追踪鼠标滚轮/右键打开外部URL的出站点击?

Absolutely, you can close this tracking gap for your outbound links! The problem arises because Google Analytics' default click-based triggers miss middle-wheel clicks and right-click "open in new tab/window" actions—since those don't fire the standard click event before the page unloads. Here are practical, tested solutions to capture all these outbound interactions:

1. Use mousedown Events Instead of click

The mousedown event fires as soon as any mouse button is pressed (left, middle, or right), which makes it perfect for capturing all initial link interactions—even if the user opens the link in a new tab/window.

Add this JavaScript snippet to your site (place it before the closing </body> tag or in a DOM-ready handler):

document.addEventListener('mousedown', function(e) {
  // Find the closest <a> tag from the clicked element
  const targetLink = e.target.closest('a');
  
  // Skip if it's not an external link
  if (!targetLink || !targetLink.href.startsWith('http') || targetLink.host === window.location.host) {
    return;
  }

  // Capture left (0), middle (1), and right (2) button clicks
  if ([0, 1, 2].includes(e.button)) {
    // Send the event to GA (adjust category/action/label to match your setup)
    gtag('event', 'outbound_link_interaction', {
      'event_category': 'Outbound Links',
      'event_action': e.button === 1 ? 'middle_wheel_click' : e.button === 2 ? 'right_click' : 'left_click',
      'event_label': targetLink.href,
      'transport_type': 'beacon' // Ensures the event is sent even if the page unloads quickly
    });
  }
});

This code:

  • Targets only external links (skips internal site links)
  • Differentiates between left, middle, and right clicks in the event action
  • Uses transport_type: 'beacon' to prevent event loss when the user opens the link in a new tab (since the original page might unload before the event sends)

2. Add Context Menu Specific Tracking (Optional)

If you want to explicitly track right-clicks that open the context menu (even if the user doesn't ultimately open the link), add a contextmenu event listener as a supplement:

document.addEventListener('contextmenu', function(e) {
  const targetLink = e.target.closest('a');
  if (targetLink && targetLink.href.startsWith('http') && targetLink.host !== window.location.host) {
    gtag('event', 'outbound_link_contextmenu', {
      'event_category': 'Outbound Links',
      'event_action': 'right_click_contextmenu',
      'event_label': targetLink.href,
      'transport_type': 'beacon'
    });
  }
});

This helps you understand how often users right-click your outbound links, regardless of whether they open them.

3. Test Thoroughly

After implementing, verify all scenarios work:

  • Left-click the link to open in the same tab
  • Middle-wheel click to open in a new tab
  • Right-click > "Open in new tab/window"
  • Check GA's Real-Time report to confirm events are firing for each case. For GA4, you can use the DebugView tool to see events in real time.

Note: If you're using Google Tag Manager instead of directly adding gtag code, you can replicate this logic by creating triggers for mousedown and contextmenu events, then setting up tags to send the outbound link data to GA.

内容的提问来源于stack exchange,提问作者user8937713

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:19:40