layer5 icon indicating copy to clipboard operation
layer5 copied to clipboard

perf: clean up GTM init listeners after first successful initialization

Open Nsanjayboruds opened this issue 7 months ago β€’ 3 comments

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_MS constant 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 existing dataLayer pageview 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.

Signed commits

  • [x] Yes, I signed my commits.

Nsanjayboruds avatar Apr 25 '26 06:04 Nsanjayboruds

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.

github-actions[bot] avatar Apr 25 '26 07:04 github-actions[bot]

@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:

Bhumikagarggg avatar Apr 26 '26 11:04 Bhumikagarggg

Hey @CodexRaunak, could you please review this PR when you get a chance? Thank you!

Nsanjayboruds avatar Apr 30 '26 11:04 Nsanjayboruds

Review Change Stack

πŸ“ 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.

coderabbitai[bot] avatar Jul 29 '26 07:07 coderabbitai[bot]