Your site is a folder of HTML files on a static host, and you want to know how many people open it, which pages they read and where they came from. There is no backend to add tracking code to.
You do not need one. Either put a small script in every page that reports each view to an analytics service, or use counts that come from the server that already sends your files — your host's built-in analytics, or the access log if you run the server yourself. Google's own instructions for adding its tag by hand are to paste the Google tag immediately after <head> on each page. Netlify and Vercel have their own analytics, Cloudflare offers a free script that works on any host, and on nginx the log you need is already being written.
The 30-second version
| Approach | Where the count comes from | What you add | Misses |
|---|---|---|---|
| A script in every page (Google Analytics, GoatCounter, …) | the visitor's browser runs it | one snippet in your page template | visitors whose ad blocker blocks it |
| Your host's analytics | the host's own logs, or a script the host gives you | a switch in the dashboard, sometimes a snippet | depends which of the two it is |
| Your own access log | every request that reached your server | nothing, then a log reader such as GoAccess | requests answered from a cache in front of it; bots are in there too |
If you only want to know whether anybody is reading, start with whatever your host offers. Pick a script-based tool when you need events, campaigns or funnels.
Why a static site can still count visitors
"Static" describes how your pages are made, not whether a server is involved. A page view still starts as an HTTP request that some server answers, unless the browser already has the page cached. That gives you two places to count from:
- The browser. A script in the page runs after it loads and sends a small request to an analytics service. It sees what JavaScript sees: screen size, which page, the referrer, clicks if you tell it to.
- The server. Whoever served the file can write down that they served it. No script, nothing in your HTML.
Most tools are one or the other. The difference explains almost every surprise later in this article.
Option 1: a script in every page
This works on every host, because it is just part of your HTML.
For Google Analytics, create a property and a web data stream, then open the tag instructions and choose manual installation. Google's help says to paste the whole block, starting at <!-- Google tag (gtag.js) -->, immediately after <head> on each page. Copy it from your own data stream rather than from a tutorial: it already carries your tag ID. Google's developer page for the tag shows the snippet with a TAG_ID placeholder and gives the same placement rule.
"Each page" is the part that bites on a hand-written site. If you use a generator — Hugo, Eleventy, Astro, Jekyll — put the snippet in the shared layout or head partial once, and every generated page gets it. If you write HTML by hand, every file needs it, and a page you forget is a page that never shows up.
Privacy-focused tools such as GoatCounter work the same way: their getting-started page gives one script tag to put in the layout.
Option 2: your host's built-in analytics
Several static hosts count for you. They are not the same thing under the hood.
Netlify reads its own logs. Its documentation says the data comes from its CDN server logs, with no client-side code, so it is not affected when visitors use ad blockers or turn JavaScript off. Its "Unique visitors" chart counts distinct IP addresses within a day, and its page views count only text/html responses with status 200, 201 or 304, so errors and redirects are left out.
Vercel uses a script. You enable Web Analytics in the project dashboard, and for a plain HTML site add two script tags — the second loads /<unique-path>/script.js, a route Vercel adds to your deployment.
Cloudflare Web Analytics comes from a hosting company but is not tied to its hosting: it is free and does not require moving your DNS to Cloudflare. You add a JavaScript beacon to your pages, so it is really a script-based tool, and it works on any host. Cloudflare says it does not collect or use your visitors' personal data.
Drop2Run, the host behind this blog, counts at the edge like Netlify, but stores no IP address, no hash of one and no cookie, so it reports visits — pages opened from outside the site — rather than unique visitors, and the owner's own previews of a draft are not counted. That is read from the router's code (apps/router/src/traffic.ts, commit b669270), not measured on a live site.
Before you trust a host's numbers, find out which kind it is. A log-based count and a script-based count of the same site are measuring different things.
Option 3: read your own access log
If you serve the site from your own nginx, the data is already on disk. nginx's default is access_log logs/access.log combined;, and the combined format records the client address, the time, the request line, the status, the referrer and the user agent for every request.
A log reader turns that into a report. GoAccess's getting-started page gives the command for a static HTML report:
goaccess access.log -o report.html --log-format=COMBINEDAdd --real-time-html and the report keeps updating. Nothing runs in the visitor's browser, so no extension can block it. It only sees requests that reach your server, though: a page served from the browser's cache or from a CDN in front of nginx never gets there. And crawlers, uptime checks and scanners are in the log too, and you will spend some time telling them apart from people.
Single-page apps count one page load
A React, Vue or Svelte app loads one HTML file and then changes the URL with JavaScript. A log only sees the requests the browser actually makes, so it sees the first load and then API calls and assets, not the pages a visitor clicks through.
Script-based tools can follow route changes, but you may have to switch the feature on:
- Google Analytics: in the web stream's Enhanced measurement settings, enable both "Page loads" and "Page changes based on browser history events". If you use Google Tag Manager, Google says not to enable this automatic history tracking, because it can double-count page views.
- Cloudflare: the beacon sends a measurement on every route change in a single-page app.
- Vercel: its quickstart describes route support for the Next.js, Remix, SvelteKit and Astro integrations, and for Vue with
vue-router; the plain HTML snippet, plain React and the genericinjectfunction have none.
Why two tools rarely agree
Run a script-based tool and a log-based one on the same site and the numbers will differ, sometimes by a lot. Three reasons:
- Blocked scripts. Cloudflare says plainly that its analytics beacon is blocked by ad blockers, naming Adblock Plus, Brave and the DuckDuckGo extension. A server log still records those visits.
- Clients that never run JavaScript. A script-based tool only hears from browsers that ran its script. Anything that fetches the HTML without running it — a crawler, a
curl— is in the log and nowhere else. - Different meanings of "visitor". Netlify counts distinct IPs per day. GoatCounter builds a session from site, User-Agent and IP, kept in memory for 8 hours, and never writes the IP or User-Agent to disk. Google Analytics sets a cookie "used to distinguish users". Three definitions, three numbers.
Pick one tool as your source of truth and watch its trend. Comparing it to another tool's absolute number tells you about the tools, not your visitors.
Troubleshooting: the dashboard stays empty
Nothing in Google Analytics. Google says data collection may take up to 30 minutes to begin and suggests checking the Realtime report. Open your site in a private window with extensions off, then look there.
The script never loads. Open DevTools, Network tab, reload. For Vercel, the quickstart says you should see a request to /<unique-path>/view on each page. If nothing analytics-related appears, check that the snippet is in the deployed HTML (view source on the live page, not your local file).
A Content-Security-Policy blocks it. If your site sends a CSP with script-src or default-src, an inline snippet such as Google's or Vercel's will not run until the policy allows it. Allowing the script is not always enough: the policy also has to allow the domain the script reports to, or it loads and then sends nothing. Security headers on a static site covers how to allow it and how to test a policy in report-only mode first.
Only some pages report. On a hand-written site, a file is missing the snippet. On a single-page app, see the section above.
Cookies and consent
Google Analytics sets _ga, "used to distinguish users", and _ga_<container-id>, "used to persist session state", both lasting two years by default. A cookie like that is why some sites show a consent banner.
Whether you need one depends on where you and your visitors are, and the rules are moving. The UK regulator's current guidance says PECR apply to all cookies, and a cookie "only essential for your own purposes" still requires consent — and the same page says a revised version is under consultation. That guidance covers cookies and similar technologies, not cookies alone, so a tool that avoids the cookie but stores something else on the device is not automatically outside it. For anything beyond a plain count, get advice for your jurisdiction rather than relying on a vendor's marketing page.
When not to add analytics
- A page you send to one person. A proposal, a draft, a prototype for a client. You will know whether they opened it when they reply; a tracker adds a script and, possibly, a consent question for nothing.
- When you will not look at it. A tracking script nobody reads the results of still costs every visitor a download and a request to a third party.
- When your host already counts. Adding a second tool to answer "is anyone reading?" gives you two numbers that disagree and no new information.
Questions
Can I add Google Analytics to a static site without a backend?
Yes. The Google tag is HTML: paste it immediately after <head> on every page, or once in your generator's layout. It runs in the visitor's browser and reports to Google's servers.
Do I need a plugin from my host?
Not for a script-based tool: the script is part of your files, so it works on any static host, GitHub Pages included. A host-specific option, such as Netlify's or Vercel's, only exists on that host.
Why does my host report more visitors than Google Analytics?
If your host counts from its logs, as Netlify does, it records requests from visitors whose ad blocker stops the analytics script and from clients that never run JavaScript; a script-based count only records browsers that ran the script. If your host's analytics is itself a script, as Vercel's is, the gap mostly disappears; what remains is which blockers block which script, plus how each tool defines a visitor.
How do I count page views in a single-page app?
Use a script-based tool with route support. In Google Analytics, enable "Page changes based on browser history events" under Enhanced measurement. A log-based count only sees the first load.
Is there analytics for a static site without cookies?
Yes. Log-based counts put nothing in the browser at all. Among scripts, GoatCounter builds sessions from the site, User-Agent and IP held in server memory for eight hours, and Cloudflare says its Web Analytics does not collect visitors' personal data. Check each vendor's own privacy page for what, if anything, it stores on the device.