Sub topic 4
Installing GA4 on your site
This chapter assumes you know basic HTML, how a browser loads scripts, what a query string is, and basic JavaScript syntax. By the end you will have Google Analytics 4 (GA4) collecting page views on your site, and you will know how to check that it works and what commonly goes wrong.
What GA4 installation actually does
GA4 is Google's web analytics product. Installing it means adding a small piece of JavaScript to your pages. That script does three things:
- It loads the GA4 library (
gtag.js) from Google's servers. - It creates or reads a first-party cookie that holds a browser identifier called the
client_id. - It sends hits to Google, each describing one event, such as a page view.
GA4 is event-based. A page view is only one kind of event, named page_view. Every later chapter on events and key events builds on this one, so the installation has to be correct first.
The pieces you need
| Piece | What it is | Example |
|---|---|---|
| Account | The top-level container, usually one per company | "Example Co" |
| Property | One set of data, usually one per site or app | "example.com" |
| Web data stream | The connection between your site and a property | `https://example.com` |
| Measurement ID | The identifier that starts with `G-` | `G-XXXXXXXXXX` |
The measurement ID is the value you paste into the snippet. The data stream is where you find it.
Create the property and data stream
- Sign in to Google Analytics and open Admin.
- Create an account if you do not have one, then create a property. Set the reporting time zone and currency now, because they affect every report.
- Under the property, open Data streams and choose Web.
- Enter your site URL and a stream name.
- Leave Enhanced measurement on for now. It automatically records scrolls, outbound clicks, site search, file downloads, and some video engagement, with no code from you.
- Copy the measurement ID shown at the top of the stream details.
Install the gtag.js snippet
The standard installation is the Google tag. Place it as high as you can in the <head> of every page you want measured. Replace G-XXXXXXXXXX with your own measurement ID.
<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>Read it line by line:
- The first
<script>tag loads the library. Theasyncattribute means the browser keeps parsing the page while the file downloads, so tracking does not block rendering. window.dataLayer = window.dataLayer || [];creates a global array if one does not already exist. Commands are queued here.function gtag(){dataLayer.push(arguments);}defines a helper that pushes its arguments onto that array. The library reads the queue when it loads, so commands you run before the library arrives are not lost.gtag('js', new Date());records when the tag started.gtag('config', 'G-XXXXXXXXXX');attaches the tag to your property. By default it also sends the firstpage_view.
Note that gtag pushes the arguments object, not an array of values. This detail matters later when you push your own data into the same queue. The data layer chapter covers the difference.
Where the snippet goes
- Static sites: paste it into a shared layout or header file, not into each page by hand.
- CMS platforms (WordPress, Shopify, Wix, Squarespace): use the built-in Google Analytics field or the official Google integration, and enter only the measurement ID. Pasting a second copy of the snippet on top of a plugin is a common cause of duplicate counts.
- Single-page applications: the page does not reload when the route changes, so you may need to send a
page_viewyourself on each route change. GA4's enhanced measurement can detect browser history changes in many cases, but you should confirm that in testing instead of assuming it.
Confirm that data arrives
Do not wait a day to see whether reports fill in. Standard reports can lag by hours. Use the real-time tools instead.
- Open your site in a browser tab.
- In GA4, open Reports, then Realtime. Your visit should appear within a minute or so.
- For finer detail, open Admin, then DebugView. DebugView shows each event as it arrives, with its parameters. It lists only devices in debug mode, such as a browser with the Google Analytics Debugger extension enabled, or a hit sent with the
debug_modeparameter.
You can also open the browser's developer tools, switch to the Network tab, and filter for collect. A successful hit appears as a request to a Google Analytics host, and the query string contains values such as tid (your measurement ID) and en (the event name). A status of 200 or 204 means the request was accepted.
What the default installation gives you
After the snippet runs, GA4 records these events with no extra code:
page_viewon each page load.session_startwhen a new session begins.first_visitthe first time a browser is seen.user_engagementwhen the page has been in the foreground and active.
With enhanced measurement on, you also get scroll, click (outbound links), file_download, view_search_results, and some video events.
Each event carries automatic parameters such as page_location, page_referrer, and page_title. Traffic source information is derived from the landing URL and the referrer. That is why the UTM tagging chapter comes before this one: GA4 reads utm_source, utm_medium, and utm_campaign from the landing page URL when the session starts. If the snippet is missing from the landing page, the tags are never read.
A worked check with a tagged social link
Suppose you installed the snippet on example.com. You want to confirm that GA4 reads a social campaign link correctly. Open this URL in a new tab:
https://example.com/spring-sale?utm_source=instagram&utm_medium=paid_social&utm_campaign=spring_sale_2026&utm_content=carousel_aThen:
- Look at Realtime and find the traffic source card. You should see
instagramas the source. - Look in the Network tab. The
collectrequest should include the full URL, with the UTM parameters, in itsdlvalue. - After processing finishes (often within 24 to 48 hours), open Reports, then Acquisition, then Traffic acquisition, and look for the session source and medium
instagram / paid_social.
If Realtime shows the visit as (direct) / (none), the UTM parameters were probably lost. Common causes are a redirect that drops the query string, or a link shortener that rewrites the URL.
Common installation errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No data at all | Wrong measurement ID, or the snippet is not on the page | Compare the `G-` ID with the data stream, and view the page source |
| Page views counted twice | The snippet is installed twice, for example in the theme and in a plugin or tag manager | Keep a single installation per measurement ID |
| Traffic from your own team in reports | Internal traffic is not filtered | Define internal traffic rules in the data stream settings, then activate the data filter |
| Everything shows as direct | Redirects strip the query string, or the snippet loads late | Preserve parameters across redirects and place the snippet in the `<head>` |
| Self-referrals such as your own payment domain | Checkout on another domain starts a new session | Add the domain to the unwanted referrals list |
| Missing data for many visitors | Ad blockers, browser protections, or a consent banner blocking the script | See the consent and limits chapters, and expect a gap |
| Numbers differ from the ad platform | Different definitions, windows, and attribution | Expected, and covered in a later chapter |
Edge cases to know about
- Multiple domains or subdomains. GA4 stores the
client_idin a cookie scoped to your domain. Subdomains normally share it. Separate domains do not, so a visitor who moves fromexample.comtoshop.example.netlooks like two different people unless you configure cross-domain measurement in the data stream settings. - Delayed standard reports. Realtime and DebugView show data at once. Standard reports are processed on a delay, and some dimensions, such as the default channel group, may be unavailable for recent hours.
- Data retention. Event-level data retention for explorations defaults to two months, and you can extend it to fourteen months in the property settings. Change it early, because it does not apply backwards to data already expired.
- Sampling and thresholds. Some reports can apply thresholds or sampling, especially when Google signals data is enabled. If a number seems oddly low, check for a threshold notice.
- Loading order and consent. If a consent banner must run first, the snippet needs consent defaults set before the
configcall. The consent chapter covers this exactly.
Summary of the install process
- Create a property and a web data stream, and copy the
G-measurement ID. - Add the Google tag once, high in the
<head>, on every page. - Load your site and confirm the hit in Realtime, DebugView, and the Network tab.
- Test a UTM-tagged URL and confirm the source and medium.
- Set up internal traffic filtering, cross-domain measurement if you need it, and data retention.
Exercise
You installed the GA4 snippet in your site's header, and a WordPress plugin you enabled last month also has your measurement ID configured. Reports show roughly twice as many page views as your server logs suggest. What is the most likely cause, and how would you confirm it using the Network tab?
Show answer
The same measurement ID is installed twice, so each page load sends two page_view hits. To confirm, load a page with the Network tab filtered on collect and "Preserve log" on. If two requests with the same tid and en=page_view appear for one page load, the tag is duplicated. Fix it by removing one installation, either the plugin setting or the hand-pasted snippet.
Exercise
You open a link ending in ?utm_source=instagram&utm_medium=paid_social&utm_campaign=spring_sale_2026, but Realtime shows the visit as (direct) / (none). Name two things you would check.
Show answer
First, check whether a redirect on the way to the landing page removes the query string, by comparing the address bar after the page loads with the URL you opened. Second, check the dl value of the collect request in the Network tab to see whether the UTM parameters reached GA4. Other valid checks are whether a link shortener rewrote the URL, and whether the snippet sits on the landing page and loads before the visitor leaves.