layer5io / layer5io/layer5

perf: clean up GTM init listeners after first successful initialization

Open
#7,590 1 comment 0 reactions 1 assignee View on GitHub

@Nsanjayboruds is already working on this.

Since Apr 5, 2026.

framework/gatsby help wanted kind/bug type/performance
Dominant language
JavaScript
Stars
1.1k
Forks
1.6k
Avg merge
2d 10h
Merged PRs (30d)
18

Description

Description

gatsby-browser.js defers GTM initialization with both a timeout and interaction listeners (scroll, mousemove, touchstart), but listener cleanup is incomplete.

Current flow:

  • setTimeout(initGTM, 3500) triggers delayed initialization
  • interaction listeners also call initGTMOnEvent
  • cleanup only removes the listener for the single event that fired

If GTM initializes through timeout first, interaction listeners remain attached until each event type occurs, creating unnecessary runtime overhead.

Reference:

Expected Behavior
  • GTM should initialize exactly once (current behavior already does this via window.gtmDidInit).
  • After first successful initialization (timeout or interaction), all GTM init listeners should be removed immediately.
  • Delay documentation/comment should clearly match milliseconds used in code.
Screenshots

Not UI-specific. This is a runtime/event-listener behavior issue in client bootstrap logic.

Environment:
  • Host OS: Ubuntu Linux
  • Browser: Google Chrome (also browser-agnostic due to JS event handling)

Contributor Resources and Handbook

The layer5.io website uses Gatsby, React, and GitHub Pages. Site content is found under the master branch.

Join the Layer5 Community by submitting your community member form.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.