perf: clean up GTM init listeners after first successful initialization
Description
This PR fixes #7590
This change improves GTM lazy-init behavior in gatsby-browser.js by ensuring all interaction listeners are cleaned up immediately after the first successful GTM initialization.
What changed
- Added
GTM_DELAY_MSconstant for clear delay units. - Added centralized listener management:
addInitListeners()removeInitListeners()
- Replaced per-event removal logic with full listener cleanup after successful
initGTM(). - Kept one-time init guard (
window.gtmDidInit) and existingdataLayerpageview push behavior unchanged. - Updated delay comment to reflect actual behavior in milliseconds.
Notes for Reviewers
Behavioral outcome:
- GTM still initializes exactly once.
- If GTM initializes via timeout first, interaction listeners are now removed immediately (no stale listeners left behind).
- If GTM initializes via user interaction first, same immediate cleanup occurs.
Validation performed:
npx eslint gatsby-browser.js(passes; only environment-level browserslist update notices)- Manual code-path verification for timeout and interaction-triggered init flows.
- [x] Yes, I signed my commits.
Preview deployment for PR #7660 removed.
This PR preview was automatically pruned because we keep only the 3 most recently updated previews on GitHub Pages to stay within deployment size limits.
If needed, push a new commit to this PR to generate a fresh preview.
@Nsanjayboruds Thank you for your contribution! Let's discuss this during the website call tomorrow at 5:30 PM IST | 7 AM CST Add it as an agenda item to the meeting minutes, if you would :slightly_smiling_face:
Hey @CodexRaunak, could you please review this PR when you get a chance? Thank you!
π Walkthrough
Walkthrough
GTM initialization now uses shared timing and event constants, centralized listener registration and removal, and a guarded bootstrap that cleans up all interaction listeners before loading the script.
Changes
GTM listener lifecycle
| Layer / File(s) | Summary |
|---|---|
Centralized GTM bootstrap and cleanup gatsby-browser.js |
Adds shared GTM delay and interaction-event constants, centralizes listener registration and removal, and removes all initialization listeners before loading GTM while preserving the existing data layer payload. |
Estimated code review effort: 2 (Simple) | ~10 minutes
π₯ Pre-merge checks | β 5
β Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title clearly and concisely describes the core GTM listener cleanup change. |
| Linked Issues check | β Passed | The changes satisfy #7590 by removing all GTM init listeners after first init, preserving one-time init and updating the delay comment. |
| Out of Scope Changes check | β Passed | The PR stays focused on GTM initialization cleanup in gatsby-browser.js and adds no unrelated changes. |
| Docstring Coverage | β Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
β¨ Finishing Touches π‘ 1
π οΈ Fix failing CI checks π‘
- [ ] Fix failing CI checks
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
[!WARNING] There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.
π§ ESLint
If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.
ESLint install timed out. The project may have too many dependencies for the sandbox.
Comment @coderabbitai help to get the list of available commands.