How to add Google Analytics 4 to a plain HTML site
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.
For a plain HTML site, paste the Google tag into the <head> of every HTML page, right after the opening <head> tag. If your pages share a template or include file, put it there once.
The code
Copy your own tag from GA4 (Admin → Data streams → your stream → View tag instructions → Install manually). It looks like this:
<!doctype html>
<html lang="en">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-XXXXXXXXXX");
</script>
<meta charset="utf-8" />
<title>My site</title>
</head>
<body>
…
</body>
</html>Every page, not just the homepage
A page without the tag isn't counted. On a site with many files, use your editor's find-in-files to check every .html file contains gtag/js?id=. Static site generators (Hugo, Jekyll, Eleventy) have a base layout or head partial: put it there.
Check it works
Upload the pages, open your site in a private window and look at GA4's Realtime overview. More ways to check.
Related
Last updated
Astro
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.
Google Tag Manager
Install GA4 through Google Tag Manager: add a Google tag with your G- ID, fire it on all pages, preview, publish, and send signup events from the data layer.