feat: fix the ga tag data not being logged in due to lazyload#387
Open
amaan-bhati wants to merge 2 commits into
Open
feat: fix the ga tag data not being logged in due to lazyload#387amaan-bhati wants to merge 2 commits into
amaan-bhati wants to merge 2 commits into
Conversation
Signed-off-by: amaan-bhati <amaanbhati49@gmail.com>
Contributor
There was a problem hiding this comment.
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
lazyOnloadtoafterInteractiveso GA initializes immediately after hydration.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: amaan-bhati <amaanbhati49@gmail.com>
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 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 */} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
afterInteractiveincomponents/layout.tsxso 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.
Verification
npm run buildcompleted successfully.