From d888e380e8829dcec6848ae26957042d691047b9 Mon Sep 17 00:00:00 2001 From: Jethro Nederhof Date: Thu, 10 Oct 2024 09:17:22 +1100 Subject: [PATCH] Use capture-phase listeners for button click tracking (#1348) In v4 we switched the Form Tracking (#1329) and Link Click Tracking (#1325) plugins to using capture-phase event listeners instead of bubble-phase. This makes the same change for the Button Click Tracking plugin to make it consistent. This has caused issues for some clients historically, e.g. BCPF-1421 where pages stopping propagation of event bubbling prevented buttons being tracked. --- .../button-plugin-capture_2024-10-04-03-17.json | 10 ++++++++++ .../browser-plugin-button-click-tracking/src/api.ts | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 common/changes/@snowplow/browser-plugin-button-click-tracking/button-plugin-capture_2024-10-04-03-17.json diff --git a/common/changes/@snowplow/browser-plugin-button-click-tracking/button-plugin-capture_2024-10-04-03-17.json b/common/changes/@snowplow/browser-plugin-button-click-tracking/button-plugin-capture_2024-10-04-03-17.json new file mode 100644 index 000000000..c77cf0c80 --- /dev/null +++ b/common/changes/@snowplow/browser-plugin-button-click-tracking/button-plugin-capture_2024-10-04-03-17.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@snowplow/browser-plugin-button-click-tracking", + "comment": "Use capture-phase event listeners", + "type": "none" + } + ], + "packageName": "@snowplow/browser-plugin-button-click-tracking" +} \ No newline at end of file diff --git a/plugins/browser-plugin-button-click-tracking/src/api.ts b/plugins/browser-plugin-button-click-tracking/src/api.ts index d3979ae9c..04a1ac109 100644 --- a/plugins/browser-plugin-button-click-tracking/src/api.ts +++ b/plugins/browser-plugin-button-click-tracking/src/api.ts @@ -62,7 +62,7 @@ export function enableButtonClickTracking( }; const addClickListener = () => { - document.addEventListener('click', _listeners[trackerId]); + document.addEventListener('click', _listeners[trackerId], true); }; if (_trackers[trackerId]?.sharedState.hasLoaded) { @@ -83,7 +83,7 @@ export function enableButtonClickTracking( export function disableButtonClickTracking() { for (const trackerId in _trackers) { if (_listeners[trackerId]) { - document.removeEventListener('click', _listeners[trackerId]); + document.removeEventListener('click', _listeners[trackerId], true); } } }