On this page
What this issue is
Next.js and React render your page on the server first, as plain HTML. In the browser, React then "hydrates" that HTML: it attaches click handlers and state so the page becomes interactive. Hydration expects the browser render to match the server HTML exactly.
When they differ, React logs a hydration error. Typical messages are Hydration failed because the initial UI does not match what was rendered on the server and Text content does not match server-rendered HTML. In the worst case React throws away the server HTML and re-renders the page on the client, or part of the page stays dead.
Why it costs you leads or sales
Ad platforms optimise on the conversions you report. If a hydration error blocks the purchase or lead event on some pages or browsers, those conversions go missing. Smart bidding learns from less data, reports look worse than reality, and you may cut campaigns that actually work.
- Server sends HTMLPage looks fine
- React hydratesBrowser render differs from server HTML
- Handlers and tags mountListeners and tag code never attach
- Conversion hit sentNo event reaches GA4, Ads or Meta
The server HTML loads, but hydration fails and later steps never run.
Example: illustrative numbers for a page where hydration fails for part of its visitors.
How to check if your site has it
Open the page in development mode
Run
next devand open the page. Next.js shows an error overlay that names the mismatch and often shows a diff of the server and client HTML.Check the production console
Open your live page, press F12 and look at the Console tab. Production builds give shorter errors such as
Minified React error #418,#423or#425. Reload with the cache cleared and test on mobile width too.Watch the Network tab
Filter for
collect,g/collect,googleads,facebook.com/troranalytics. Load the page and trigger the conversion. If the console shows an error and no hit appears, the two are likely linked.Test the key journeys
Do this for the form submit, add to cart and checkout pages, not only the home page. Errors often appear only on pages with dynamic content.
How to fix it
Most hydration errors come from a small set of causes. Find which one applies, then fix it.
| Cause | Example | Fix |
|---|---|---|
| Values that change per render | Date.now(), Math.random(), new Date().toLocaleString() in JSX | Compute in useEffect or pass a fixed value from the server |
| Browser-only checks in render | typeof window !== 'undefined' choosing different markup | Render the same markup first, then update in useEffect |
| Invalid HTML nesting | <div> inside <p>, <a> inside <a> | Correct the nesting |
| Browser extensions or scripts changing the DOM | Tag or consent scripts editing the body before hydration | Load them with next/script and avoid editing React-owned nodes |
| Manually pasted tag snippets | Raw <script> in a component or in <head> edited by hand | Use the Script component |
import { useEffect, useState } from 'react';
export default function Greeting() {
const [time, setTime] = useState(null);
useEffect(() => {
setTime(new Date().toLocaleTimeString());
}, []);
// Same output on server and first client render
return <p>{time ? `Local time: ${time}` : 'Loading...'}</p>;
}Use next/script for tags
In the App Router, add the GA4 or GTM script in
app/layout.jswith<Script src="..." strategy="afterInteractive" />. In the Pages Router, put it inpages/_app.js. Do not paste raw script tags inside page components.Fire events after mount
Send conversion events from
onClick, form handlers or auseEffect, never during render. Check thatwindow.gtagorwindow.dataLayerexists before calling it.Fix every error in the console
Work through each hydration message. One error can hide others, so reload and recheck after each fix.
Load the GTM container once
Add the container with
next/scriptin the root layout rather than editing the HTML by hand. Add thenoscriptiframe in the body only if you need it.Push events from handlers
Use
window.dataLayer = window.dataLayer || []; window.dataLayer.push({event: 'generate_lead'})inside the submit handler, not during render.Test in Preview mode
Open Tag Manager Preview, load the page and confirm your tags show as Fired for the event.
Check the headless storefront code
This applies only if you run a Hydrogen or custom Next.js storefront. Standard Shopify themes do not use React hydration. Apply the Custom code steps to your storefront.
Check headless setups only
This applies if WordPress is the back end and the front end is Next.js. Tags added by WordPress plugins will not load on the Next.js front end. Add them in the Next.js app instead.
import Script from 'next/script';
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"
strategy="afterInteractive"
/>
<Script id="ga4-init" strategy="afterInteractive">
{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');`}
</Script>
</body>
</html>
);
}How to confirm it's fixed
- The browser console shows no hydration errors on the key pages
- Reload with cache cleared and test on desktop and mobile width
- The Network tab shows the page_view hit on load
- The conversion hit appears when you submit the form or reach the thank-you page
- GA4 DebugView or the Meta Pixel Helper shows the event
- Test again on the production build, not only
next dev
How to prevent it happening again
- Run
next buildand test the production site before each release - Never use random or time-based values directly in render
- Load all third-party tags with
next/script - Treat console errors in code review as release blockers
- Recheck the key journeys after every deploy, theme change or consent tool update
Day 1
A developer adds a date or a banner that renders differently on the server and the client.
Day 1
The page looks fine, so the release goes out.
Day 3
Reported conversions in ad platforms drop. Nobody links it to the deploy.
Day 10
The console error is found and fixed, and hits return.
How FunnelGuard handles this
FunnelGuard detects this today. A robot visitor runs your real journeys on a schedule, from the EU or the US, and watches for console errors and missing tracking hits at each step. When a page logs errors and an expected GA4, Google Ads, Meta or other hit does not arrive, that is flagged.
Questions people ask
Why are my tags not firing in Next.js?
A common cause is a hydration error, where the server HTML and the browser render do not match. Other causes are tag scripts pasted in the wrong place, events fired during render, or consent tools blocking them. Check the browser console and the Network tab first.
What is a hydration error in Next.js?
It is an error logged when React cannot attach to the server-rendered HTML because the browser render differs from it. Messages include Hydration failed because the initial UI does not match and Text content does not match server-rendered HTML. React may then re-render the page or leave parts non-interactive.
Can a hydration error stop Google Analytics or Meta Pixel from working?
Yes, it can. If React discards parts of the page or fails to attach handlers, tag code and event listeners that depend on them may never run. The page can look normal while conversion hits are missing.
How do I fix Text content does not match server-rendered HTML?
Find what renders differently on the server and client, such as Date.now(), Math.random(), locale formatting or a window check. Render the same output first, then set the client-only value in useEffect. Also check for invalid HTML nesting.
Should I use suppressHydrationWarning?
Only for small, harmless differences like a timestamp on a single element. It hides the warning but does not fix a mismatch. Using it widely can leave real tracking problems undetected.
How do I add Google Analytics to Next.js correctly?
Use the next/script component in your root layout (app/layout.js) or pages/_app.js with strategy="afterInteractive". Load the gtag script and run the config call there. Avoid pasting raw script tags inside page components.
How can I tell if a hydration error is hurting my conversions?
Open the console on your key pages and look for hydration errors, then compare with the Network tab for missing hits. A monitoring service that runs your journeys on a schedule can also catch console errors alongside missing hits after a deploy.
Related issues
- TRK-389SPA route change sends no page_viewFix guide
- TRK-390SPA sends page_view twice per routeFix guide
- TRK-392Headless checkout loses tracking from main siteFix guide
- TRK-404Service worker caches old page with old tagsFix guide
- TRK-398Page builder (Elementor/Webflow) custom code not publishedFix guide
- TRK-393Shopify checkout extensibility removed checkout.liquid scriptsFix guide