How to add Google Analytics 4 to an Astro site
Add GA4 to Astro with inline scripts in your base layout, keep the ID in an environment variable, and make sure view transitions still count page views.
To add Google Analytics 4 to Astro, put the Google tag in the <head> of your base layout with is:inline scripts, so Astro leaves them as they are. Read the Measurement ID from an environment variable, and GA4's enhanced measurement counts page changes, including view transitions.
1. Add the ID to your environment
PUBLIC_GA_ID=G-XXXXXXXXXX2. Add the tag to your base layout
---
const gaId = import.meta.env.PUBLIC_GA_ID;
---
<html lang="en">
<head>
{gaId && (
<>
<script is:inline async src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}></script>
<script is:inline define:vars={{ gaId }}>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', gaId);
</script>
</>
)}
</head>
<body>
<slot />
</body>
</html>is:inline stops Astro from bundling the scripts, and define:vars passes the ID in. Make sure every page uses this layout.
View transitions
With Astro's view transitions (<ClientRouter />), pages change without a full reload. GA4's enhanced measurement counts these through browser history changes; check it's on in GA4: Admin → Data streams → your stream → Enhanced measurement → Page views → Page changes based on browser history events. Then click between pages and watch GA4's Realtime report.
Optional: Partytown
To move the tag off the main thread, add @astrojs/partytown (npx astro add partytown), set the scripts' type to text/partytown, and forward dataLayer.push in the integration's config. Test carefully afterwards: some setups need extra configuration.
Check it works
Build and open your site, then GA4's Realtime overview. More ways to check.
Related
Sources
- Astro: Partytown integration
Last updated
Carrd
Add GA4 to your Carrd site with Carrd's Site Analytics setting: choose Google Analytics as the provider, paste your G- Measurement ID and enable it.
Plain HTML
Add GA4 to a static HTML site by pasting the Google tag into the head of every page. Includes the exact code, where it goes, and how to check it works.