How to Add Google Analytics to a Website

Create a GA4 property, copy its Measurement ID, paste the gtag.js snippet into the head of every page, and confirm visits show in the Realtime report.

You want to know how many people visit the site and which pages they read. Google Analytics 4 is free and needs one script block on every page.

Create a GA4 property

Go to analytics.google.com and sign in with a Google account. Open Admin (the gear icon at the bottom left) and create a new property. Give it a name, choose the reporting time zone and currency, and continue through the business details screens.

Add a web data stream

When asked to choose a platform, pick Web. Enter the site URL and a stream name, then create the stream. The data stream is what receives hits from the site.

Copy the Measurement ID

The stream details page shows a Measurement ID in the form G-XXXXXXXXXX. Copy it. This ID is the only part of the snippet that is specific to your site.

Paste the tag into every page

The same page shows an installation snippet. Put it inside <head> on every page of the site, as high as possible, with your ID in place of the placeholder:

<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>

If the site has a shared layout or header template, add it there once. On WordPress, Squarespace, and similar platforms there is usually a field for the Measurement ID or a header-code box in the site settings instead.

Verify in the Realtime report

Deploy the change, then open the site in a browser. In Analytics, go to Reports then Realtime. Within a minute the active user count should show at least one visitor and the page you opened. If it stays at zero, view the page source and confirm the script is present and the ID matches the one in the data stream.

Consent is required in the EU and UK. GA4 sets cookies and collects identifiers, so a site with visitors there needs a consent banner that keeps the tag from collecting data until the visitor agrees. Most consent tools integrate with Google Consent Mode, which adjusts what the tag does based on the visitor's choice.

More Websites how-tos