Skip to content

Server-render the GTM container snippet - #935

Closed
thostetler wants to merge 1 commit into
adsabs:masterfrom
thostetler:fix/gtm-tag-placement
Closed

thostetler wants to merge 1 commit into
adsabs:masterfrom
thostetler:fix/gtm-tag-placement

Conversation

@thostetler

Copy link
Copy Markdown
Member

GoogleTagManager was rendered inside using next/script at the default
afterInteractive strategy, which is client-only in the pages router. The
container snippet was missing from server HTML and only appeared at the end of
body after hydration, which GTM's console flagged as "tag not placed correctly"
across / and /abs/* URLs.

  • Inlines Google's canonical snippet as the first child of in _document
  • Gates the snippet on NEXT_PUBLIC_GTM_ID being set, avoiding a gtm.js?id=undefined request
  • Removes the client-side GoogleTagManager render from _app and the test provider
  • Keeps sendGTMEvent, which only touches window.dataLayer

GoogleTagManager rendered inside <Layout> using next/script at the
default afterInteractive strategy, which only runs client-side in the
pages router — it wasn't in server HTML and loaded at the end of body
after hydration. GTM flagged this as "tag not placed correctly" on /
and /abs/* pages.

Inlines Google's snippet as the first child of <Head> in _document,
gated on NEXT_PUBLIC_GTM_ID being set. sendGTMEvent is unaffected;
it only touches window.dataLayer.
@github-actions

Copy link
Copy Markdown

Bundle size

Shared by all pages: 624.6 kB (-0.0 kB) ⚪

No route changed by more than 1 kB. ✅

First load = polyfills + shared _app chunks + route chunks, gzipped.

@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.9%. Comparing base (d6e8ab2) to head (7b589a1).

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #935     +/-   ##
========================================
+ Coverage    67.9%   67.9%   +0.1%     
========================================
  Files         379     378      -1     
  Lines       44188   44081    -107     
  Branches     2380    2378      -2     
========================================
- Hits        29974   29930     -44     
+ Misses      14165   14103     -62     
+ Partials       49      48      -1     
Files with missing lines Coverage Δ
src/test-utils.tsx 88.1% <100.0%> (-0.3%) ⬇️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@thostetler

Copy link
Copy Markdown
Member Author

Folded into #934 — the GTM placement fix and the user id hook are being tested and shipped together.

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.

1 participant