Skip to content

perf: clean up GTM init listeners after first successful initialization#7660

Open
Nsanjayboruds wants to merge 7 commits intolayer5io:masterfrom
Nsanjayboruds:perf/gtm-init-listener-cleanup
Open

perf: clean up GTM init listeners after first successful initialization#7660
Nsanjayboruds wants to merge 7 commits intolayer5io:masterfrom
Nsanjayboruds:perf/gtm-init-listener-cleanup

Conversation

@Nsanjayboruds
Copy link
Copy Markdown
Contributor

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

  • Yes, I signed my commits.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 25, 2026

Preview deployment for PR #7660 removed.

This PR preview was automatically pruned because we keep only the 6 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.

@Bhumikagarggg
Copy link
Copy Markdown
Contributor

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

@Nsanjayboruds
Copy link
Copy Markdown
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: clean up GTM init listeners after first successful initialization

3 participants