Skip to content

fix(analytics): make autoTrack's cleanup function actually remove the click listener - #2600

Open
Grit03 wants to merge 1 commit into
wxt-dev:mainfrom
Grit03:fix/analytics-auto-track-remove-listener
Open

fix(analytics): make autoTrack's cleanup function actually remove the click listener#2600
Grit03 wants to merge 1 commit into
wxt-dev:mainfrom
Grit03:fix/analytics-auto-track-remove-listener

Conversation

@Grit03

@Grit03 Grit03 commented Aug 26, 2026

Copy link
Copy Markdown

Overview

autoTrack() returns a cleanup function that is documented as removing the listeners it set up, but the returned function never removes anything.

root.addEventListener('click', onClick, { capture: true, passive: true });
return () => {
  root.removeEventListener('click', onClick); // capture defaults to false
};

The listener is registered with capture: true but removal is attempted with the default capture: false, so no listener matches and nothing is removed.

Related spec references

MDN, removeEventListener() Parameters,

options.capture: A boolean value that specifies whether the event listener to be removed is registered as a capturing listener or not. If this parameter is absent, the default value false is assumed.

DOM Standard, removeEventListener() method steps:

  1. Let capture be the result of flattening options.
  2. If this's event listener list contains an event listener whose type is type, callback is callback, and capture is capture, then remove an event listener with this and that event listener.

MDN, "Matching event listeners for removal":

the only option removeEventListener() checks is the capture/useCapture flag

So capture is the only option used for matching, and omitting it means false. passive is discarded by the "flatten options" step, so it does not need to be passed to removeEventListener().

Manual Testing

Paste into any page's DevTools console (verified on Chrome 151):

const btn = document.createElement('button');
document.body.append(btn);
let tracked = 0;
const onClick = () => tracked++;

document.addEventListener('click', onClick, { capture: true, passive: true });
const cleanup = () => document.removeEventListener('click', onClick); // before
// const cleanup = () => document.removeEventListener('click', onClick, { capture: true }); // after

btn.click();
cleanup();
btn.click();
console.log(tracked); // before: 2 (listener still attached), after: 1

Related Issue

No existing issue.

@Grit03
Grit03 requested a review from aklinker1 as a code owner August 26, 2026 05:06
@netlify

netlify Bot commented Aug 26, 2026

Copy link
Copy Markdown

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 055ca43
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6a8e74673af5bb00087f400b
😎 Deploy Preview https://deploy-preview-2600--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the pkg/analytics Includes changes to the `packages/analytics` directory label Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg/analytics Includes changes to the `packages/analytics` directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant