English 繁體中文 简体中文

How to get a Google tag to understand the traffic to your website?

Howard Pong Yuen Lam · AI Notes · November 6, 2026 · 6 min read

A website can sit on the internet and you would never know whether anyone reads it. A Google tag is a short piece of code that you put on your pages. Every time someone opens a page, the tag sends a small, anonymous note to Google Analytics. After a few hours you can log in and see how many times each page was opened, which pages are most popular, which country the visitor was in, or how they reached your site — by typing the address directly (direct), by clicking a link on another website (referral), by clicking your link in a search engine’s results (organic search), or by clicking your link inside an AI assistant.

The tag does not tell you who the people are. It counts traffic — page views and visits — not individuals. The way it does this is: the first time a browser opens one of your pages, Google Analytics 4 (GA4, the fourth and current generation of Google Analytics, released in 2020) generates a random number called a client ID and stores it in a small text file called a cookie on your own website’s domain, as Google explains in its official cookie documentation. That number is not the person’s name, email, or a hardware device ID — it is just an anonymous label for that particular browser on that particular computer or phone. (Unless you separately set up GA4’s User-ID feature for logged-in visitors — which this note does not — Analytics never learns who the visitor is.) On later visits the same cookie is read again, so Analytics can tell that the same browser came back, without ever knowing who is using it. This note walks through getting a tag from Google and pasting it into your website, step by step.

Watch the walkthrough

Open in Google Drive (no account needed).

What you need before you start

Step 1: Open Google Analytics

Go to analytics.google.com and sign in with your Google account. The first time you open it, Google asks you to create an account. An account is just a container for your websites; you can call it anything, for example your own name or the name of your site.

Step 2: Create a GA4 property

Inside the account, Google asks you to create a property. A property represents one website. Give it a name (for example “My Website”) and choose your time zone and currency.

Make sure the property is a GA4 property. You can tell by the ID it shows you later: GA4 IDs start with G-.

Step 3: Add a web data stream

A property needs at least one data stream, which is the connection between your website and Google Analytics.

On the property setup screen, choose Web, then type your website’s address (for example https://example.com) and give the stream a name. Click Create stream.

Step 4: Copy the Google tag

After the stream is created, Google shows you a Measurement ID that looks like G-XXXXXXXXXX, and a block of code called the Google tag. It looks like this (with your own ID in place of G-XXXXXXXXXX):

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

Click the copy button next to the code in Google Analytics to copy your own version.

Step 5: Paste it into your website

Open the HTML of every page you want to measure. Find the <head> section near the top of the file, and paste the Google tag right after the opening <head> tag, before anything else.

<html>
<head>
  <!-- paste the Google tag here -->
  ...
</head>

Putting it at the very top makes sure the tag loads before the rest of the page, so even visitors who leave after a second are counted. If your site uses a template or a content management system, paste the tag into the shared template once and it will appear on every page automatically. If you use an AI-integrated IDE — such as Visual Studio Code with an AI assistant — you can ask the AI to help you open the HTML of every page you want to measure and paste the Google Tag.

Step 6: Check that it works

Save your pages and open your website in a browser. Then go back to Google Analytics and open the Realtime report on the left. You should see your own visit appear within a few seconds. That is the quickest way to confirm the tag is working.

The full reports — how many people visited each page, how they reached your site (direct, referral, organic search, or an AI assistant), which pages they looked at — take a few hours to process, so come back the next day to see the first complete numbers.

What the numbers tell you

Once the tag is running, Google Analytics can show you:

Privacy and limitations

Further reading