Skip to content

feat: fix the ga tag data not being logged in due to lazyload#387

Open
amaan-bhati wants to merge 2 commits into
mainfrom
ga-tag-fix
Open

feat: fix the ga tag data not being logged in due to lazyload#387
amaan-bhati wants to merge 2 commits into
mainfrom
ga-tag-fix

Conversation

@amaan-bhati
Copy link
Copy Markdown
Member

@amaan-bhati amaan-bhati commented May 7, 2026

Fixes a major drop in recorded blog traffic caused by delayed Google Analytics script execution.

RCA & Cause

The Next.js script strategy="lazyOnload" forced the browser to wait for a completely idle state before loading Google Analytics. If a user navigated before the browser became idle, the script never fired, resulting in dropped sessions.

Fix

Changed the script strategy to afterInteractive in components/layout.tsx so the tracking script executes as soon as the page is interactive.

Source

Next.js Documentation explicitly recommends afterInteractive for Google Analytics to prevent dropped data from early bounces.

Screenshot 2026-05-07 at 1 47 54 PM

Verification

  • Build: npm run build completed successfully.

Signed-off-by: amaan-bhati <amaanbhati49@gmail.com>
Copilot AI review requested due to automatic review settings May 7, 2026 08:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adjusts the Google Analytics (gtag) loading strategy in the shared Layout to ensure GA events/dataLayer activity is available earlier in the page lifecycle, addressing missed logging when scripts were deferred too late.

Changes:

  • Switched GA loader and inline init scripts from lazyOnload to afterInteractive so GA initializes immediately after hydration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread components/layout.tsx
Signed-off-by: amaan-bhati <amaanbhati49@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread components/layout.tsx
Comment on lines 53 to +57
All non-essential scripts use lazyOnload to keep TBT/TTI low.
They fire after the page is fully interactive. */}
{/* update: changing this to afterInteractive since this is not a non essential script, due to lazyload on this we
are losing on the important data becasue the script for analytics never gets loaded until the page is fully interavtive
and if the user navigates before the page is fully interactive, the script is never triggerred and hence we are losing data */}
Comment thread components/layout.tsx
Comment on lines +55 to +57
{/* update: changing this to afterInteractive since this is not a non essential script, due to lazyload on this we
are losing on the important data becasue the script for analytics never gets loaded until the page is fully interavtive
and if the user navigates before the page is fully interactive, the script is never triggerred and hence we are losing data */}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants