Fix: Google Analytics and GTM initialize on every render causing duplicate events

Open Beginner friendly
#4,644 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Domain
frontend

Research direction

Start in Head.tsx at HeadComponent and inspect how analytics initialization currently runs during rendering. Move the initialization and pageview behavior to mount-time handling, then verify that rerendering the component does not produce duplicate analytics initialization or pageview events.

Written by the indexing model from the issue text.

Description

stale

I was exploring the codebase and noticed a potential issue in Head.tsx regarding how we handle analytics initialization.

Currently, the ReactGA.initialize TagManager.initialize, and ReactGA.pageview calls are placed directly inside the component body of HeadComponent

  if (typeof window !== 'undefined' && window.location.hostname.includes('asyncapi.com')) {
    TagManager.initialize({ gtmId: 'GTM-T58BTVQ' });
    ReactGA.initialize('UA-109278936-1');
    ReactGA.pageview(window.location.pathname + window.location.search);
  }

Because this logic isn't wrapped in a useEffect or componentDidMount, it executes every time the HeadComponent re-renders.

We should wrap this logic in a useEffect hook with an empty dependency array. This ensures it only runs once when the component mounts, which is the intended behavior for a page view.

@princerajpoot20 do you guys think this needs to be done , would like to know your thoughts on this!! , i could raise a pr for the same

Dominant language
TypeScript
Stars
717
Forks
1.2k
Avg merge
14h 59m
Merged PRs (30d)
40

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.

More from asyncapi/website

All issues in asyncapi/website

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.