Freeframe

How to Add Analytics to Your Website (Free and Paid)

Add Google Analytics, Cloudflare Web Analytics, Plausible, Fathom or Vercel Analytics to an HTML, Next.js, Astro or WordPress site, and check it works.

By
· 7 min read

Adding analytics takes about 15 minutes: pick a tool, copy one snippet (or one ID) from its dashboard, paste it into the <head> of every page, deploy, and open a page to see your own visit arrive. Google Analytics is free and detailed but sets cookies. Cloudflare Web Analytics is free and cookie-free. Plausible and Fathom are paid, cookie-free and simpler to read. If your site is on Vercel, its built-in Web Analytics is the least work.

Which analytics tool to pick#

Prices and limits at the time of writing, from each provider's own page:

Tool Cost Cookies Good for
Google Analytics Free Yes: _ga and _ga_<ID>, 2 years each Detailed reports, Google Ads, form and scroll data
Cloudflare Web Analytics Free; 10 sites if they aren't proxied through Cloudflare None A free, private visitor count
Plausible Starter from $9 a month for 1 site and up to 10k pageviews; 30-day free trial None One simple dashboard you'll actually read
Fathom From $15 a month for up to 100,000 pageviews across 50 sites; 7-day trial None Several small sites on one bill
Vercel Web Analytics Free on Hobby up to 50,000 events a month; Pro pays $0.03 per 1,000 events None needed for page views Sites already hosted on Vercel

A few things worth knowing before you choose:

  • Cookies affect consent. Google Analytics' cookies identify returning visitors, which is what triggers cookie-consent rules in places like the EU and the UK. A cookie-free tool usually avoids that question. See Do You Need a Cookie Banner? for how those rules work.
  • Google Analytics requires a privacy policy that mentions it, and so do most US state privacy laws once you collect visitor data. Do You Need a Privacy Policy on Your Website? covers what to write.
  • Ad blockers hide some visits from every script-based tool. Cloudflare's own FAQ says its JavaScript beacon can be blocked by ad blockers; only its edge analytics, for sites proxied through Cloudflare, count every request. Treat any tool's numbers as a trend, not an exact headcount.
  • Hobby limits pause, they don't bill. On Vercel's free Hobby plan, collection stops for the rest of the cycle once you pass 50,000 events, after a three-day grace period.

For most small business sites, either Google Analytics (if you run Google Ads or want form and scroll reports) or Cloudflare Web Analytics (if you just want visitors, pages and referrers for free) is enough.

Step 1: Get your snippet or ID#

Google Analytics (Google's setup guide):

  1. Go to analytics.google.com and click Start measuring (or, in Admin, click Create > Property).
  2. Enter a property name, a US reporting time zone and US dollars as the currency, then your industry and business size, and click Create. Accept the terms.
  3. In Admin, under Data collection and modification, click Data streams > Add stream > Web.
  4. Enter your domain and a stream name, leave Enhanced measurement on, and click Create stream.
  5. On the stream's page, copy the Measurement ID (it starts with G-). For a hand-written site, also click View tag instructions > Install manually and copy the whole Google tag snippet. Copy it from that page rather than from a tutorial: Google changes the snippet from time to time.

Enhanced measurement records page views, scrolls to 90% of a page, outbound clicks, site searches, file downloads, YouTube plays and form starts and submissions without any extra code.

Cloudflare Web Analytics (getting started): in the Cloudflare dashboard, open Web Analytics, select Add a site, enter your hostname under Set up hostname, select Done, then copy the JS snippet from Manage site. If your domain already runs through Cloudflare's proxy, it can turn analytics on for you without a snippet.

Plausible: the snippet appears when you add a site. For an existing site, open the site's Settings > General, find Tracking, and click Review next to Site installation (Plausible's docs).

Fathom: add the site in your Fathom dashboard and copy the embed code it shows you.

Vercel: in the dashboard, click Analytics in the sidebar, select your project and click Enable (Vercel's quickstart). The tracking routes go live with your next deployment.

Step 2: Add it to your site#

Where the snippet goes depends on how the site is built. Put it in one shared place where you can, so every page gets it.

HTML sites#

An HTML site has no shared layout, so the snippet goes in every .html file, including 404.html. Google says to paste its tag immediately after the opening <head> tag; Plausible goes inside <head> too, while Cloudflare asks for its snippet before the closing </body> tag:

html
<head>
  <!-- Paste the Google tag or Plausible snippet here -->
  <meta charset="utf-8" />
  ...
</head>
<body>
  ...
  <!-- Or paste the Cloudflare Web Analytics snippet here -->
</body>

With a dozen pages, your editor's find-and-replace across files does it in one go: replace <head> with <head> plus the snippet. Search afterwards to check every file has it once, not twice.

On Vercel, a plain HTML site uses two short script tags from Vercel's quickstart instead, with no package to install.

Next.js sites#

Add it once to the root layout (src/app/layout.tsx in Freeframe's Next.js templates). For Google Analytics, the Next.js docs recommend the @next/third-parties package, which loads the tag after the page has hydrated:

sh
pnpm add @next/third-parties@latest
tsx
import { GoogleAnalytics } from "@next/third-parties/google";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
      <GoogleAnalytics gaId="G-XXXXXXXXXX" />
    </html>
  );
}

Next.js moves between pages without full reloads. Google Analytics still counts those page views as long as Page changes based on browser history events is checked under Enhanced measurement.

For Vercel Web Analytics, run pnpm i @vercel/analytics, import Analytics from @vercel/analytics/next and put <Analytics /> just before </body>. For Plausible, Fathom or Cloudflare, paste their snippet into the layout the same way.

Astro sites#

Add it once to the base layout (src/layouts/Layout.astro in Freeframe's Astro templates). Astro bundles and rewrites any <script> that has no attributes other than src, which breaks third-party snippets, so add is:inline to each of the snippet's script tags:

<head>
  <script is:inline async src="https://www.googletagmanager.com/..."></script>
  <script is:inline>
    // the rest of the snippet, exactly as copied
  </script>
</head>

On Vercel, import Analytics from "@vercel/analytics/astro" in the layout's front matter and put <Analytics /> inside <head>.

WordPress sites#

Don't edit the theme's files: a theme update would remove your change. For Google Analytics, install Site Kit by Google (free, from Google, 5+ million active installs at the time of writing) from Plugins > Add New Plugin, activate it, and follow its setup to connect Analytics and Search Console with your Google account. Plausible has an official WordPress plugin too. For other tools, use a plugin that adds code to the site header, paste the snippet, and save.

Step 3: Check it's working#

  1. Deploy the change and open your live site in a normal browser window (not one with an ad blocker).
  2. Google Analytics: open Reports > Realtime. Google says data can take up to 30 minutes to start, and standard reports fill in over the following day.
  3. Other tools: open the dashboard; Plausible, Fathom and Cloudflare show recent visits on the main page.
  4. If nothing appears, open your browser's developer tools, go to Network, reload, and filter by the tool's name (googletagmanager, plausible, cloudflareinsights, or /view for Vercel). No request means the snippet isn't on that page; a blocked request usually means an ad blocker or a Content Security Policy that doesn't allow the tool's domain.

Step 4: Track the actions that matter#

Page views tell you which pages people read. What a small business usually wants to know is how many people called, asked for a quote or booked. In Google Analytics, form submissions are already counted by Enhanced measurement as form_submit. To treat it as a goal, in Admin, under Data display, click Events, find the event on the Recent events tab and click its star to mark it as a key event, so it shows up in your reports as a result that counts. In Next.js, sendGAEvent from @next/third-parties/google sends your own events, such as a click on a phone number. Vercel supports custom events on Pro; for the other tools, check their docs for how to send one.

Then make a habit of it: once a month, look at the top pages, where visitors came from, and how many key events happened. That's the same monthly check the website launch checklist suggests, next to Search Console.

Where this fits in a template#

Every Freeframe template ships without analytics, so nothing tracks visitors until you choose a tool. A trades site like Handy gets most of its value from counting quote requests; a restaurant site like Bistro mostly wants to know which pages people read before they book. The deploying guide covers putting the site online first.

About the author

Founder of Freeframe

Paul Peery runs Freeframe, which publishes complete multi-page website templates in HTML, Next.js, Astro, Nuxt and WordPress, and writes about getting small websites built, launched and kept up to date.

Get new templates by email

An email when new templates come out. Confirm by email, unsubscribe any time. How we use your address

Templates for this

Handy website template preview

Handy

Business

A high-trust home services site with an instant quote calculator and service-area checker.

HTML + TailwindNext.js (paid)Astro (paid)Nuxt (paid)WordPress (paid)

HTML free · Next.js, Astro, Nuxt & WordPress $19

10 pages · 147 KB

Documentation
Bistro website template preview

Bistro

Business

A warm, high-end website for a restaurant, café or wine bar.

HTML + TailwindNext.js (paid)Astro (paid)Nuxt (paid)WordPress (paid)

HTML free · Next.js, Astro, Nuxt & WordPress $19

7 pages · 390 KB

Documentation
Launch website template preview

Launch

Business

A polished SaaS product website with pricing, sign-in and dark mode.

HTML + TailwindNext.js (paid)Astro (paid)Nuxt (paid)WordPress (paid)

HTML free · Next.js, Astro, Nuxt & WordPress $19

7 pages · 518 KB

Documentation

Keep reading