Skip to content

fix: refactor GA/GTM initialization to avoid duplicate analytics events#5017

Open
Deepanshu1230 wants to merge 1 commit into
asyncapi:masterfrom
Deepanshu1230:master
Open

fix: refactor GA/GTM initialization to avoid duplicate analytics events#5017
Deepanshu1230 wants to merge 1 commit into
asyncapi:masterfrom
Deepanshu1230:master

Conversation

@Deepanshu1230

@Deepanshu1230 Deepanshu1230 commented Jan 26, 2026

Copy link
Copy Markdown

Description

This PR refactors how Google Analytics (ReactGA) and Google Tag Manager (GTM) are handled in HeadComponent.

What changed

  • Moved GA and GTM initialization out of the render phase and into useEffect.
  • Ensured analytics initialization runs only once to prevent duplicate setup during re-renders.
  • Updated pageview tracking to trigger only when the route changes instead of on every render.
  • Preserved existing hostname checks to ensure analytics only runs on asyncapi.com.

Related issue(s)

Resolves #5016

Summary by CodeRabbit

  • Refactor
    • Optimized Google Analytics initialization to execute once per page lifecycle with host-based security validation, improving efficiency and tracking reliability.

✏️ Tip: You can customize this high-level summary in your review settings.

@netlify

netlify Bot commented Jan 26, 2026

Copy link
Copy Markdown

Deploy Preview for asyncapi-website ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 3a3217a
🔍 Latest deploy log https://app.netlify.com/projects/asyncapi-website/deploys/69774d7eaaccfd0008307878
😎 Deploy Preview https://deploy-preview-5017--asyncapi-website.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@coderabbitai

coderabbitai Bot commented Jan 26, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The change refactors GA and GTM initialization in Head.tsx from a direct initialization block to a guarded useEffect hook with useRef, ensuring single initialization per page lifecycle and preventing duplicate pageview events on re-renders.

Changes

Cohort / File(s) Change Summary
GA/GTM Initialization Restructure
components/Head.tsx
Moved GA and GTM initialization into useEffect with useRef guard for single-execution guarantee; added useEffect and useRef imports; pageview dispatch now triggers within effect dependent on path changes; hostname check retained for asyncapi.com-only execution

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

Poem

🐰 Hops through the code with gleeful cheer,
No more tracking events multiplied here!
useEffect guards the initialization gate,
One shot per mount—analytics are great! 🎯✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #5016: moves GA/GTM initialization to useEffect with useRef guard, implements separate useEffect for pageview tracking on path changes, and preserves hostname checks for asyncapi.com only.
Out of Scope Changes check ✅ Passed All changes in components/Head.tsx are directly scoped to the GA/GTM initialization refactoring objectives; no unrelated modifications or scope creep detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly summarizes the main change: refactoring GA/GTM initialization to avoid duplicate analytics events, which directly aligns with the changeset's primary objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Deepanshu1230 Deepanshu1230 changed the title [Fixes]:Refactor GA/GTM initialization to avoid duplicate analytics events [Fix]:Refactor GA/GTM initialization to avoid duplicate analytics events Jan 26, 2026
@Deepanshu1230 Deepanshu1230 changed the title [Fix]:Refactor GA/GTM initialization to avoid duplicate analytics events [fix]:Refactor GA/GTM initialization to avoid duplicate analytics events Jan 26, 2026
@codecov

codecov Bot commented Jan 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (911f62e) to head (3a3217a).
⚠️ Report is 252 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #5017   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines          796       796           
  Branches       146       146           
=========================================
  Hits           796       796           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asyncapi-bot

Copy link
Copy Markdown
Contributor

⚡️ Lighthouse report for the changes in this PR:

Category Score
🔴 Performance 38
🟢 Accessibility 98
🟢 Best practices 92
🟢 SEO 100
🔴 PWA 33

Lighthouse ran on https://deploy-preview-5017--asyncapi-website.netlify.app/

@Deepanshu1230 Deepanshu1230 changed the title [fix]:Refactor GA/GTM initialization to avoid duplicate analytics events fix:Refactor GA/GTM initialization to avoid duplicate analytics events Jan 26, 2026
@Deepanshu1230 Deepanshu1230 changed the title fix:Refactor GA/GTM initialization to avoid duplicate analytics events fix: Refactor GA/GTM initialization to avoid duplicate analytics events Jan 26, 2026
@Deepanshu1230 Deepanshu1230 changed the title fix: Refactor GA/GTM initialization to avoid duplicate analytics events fix: refactor GA/GTM initialization to avoid duplicate analytics events Jan 26, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation.

There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@github-actions github-actions Bot added the stale label May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: To Be Triaged

Development

Successfully merging this pull request may close these issues.

[BUG] Improve Google Analytics & GTM Initialization in Head

2 participants