Sub topic 4
Installing GA4 on Your Site
This chapter assumes you know basic HTML, how a browser loads scripts, what a URL query string is, and basic JavaScript syntax. You also need access to a Google Analytics property and permission to edit your site's HTML or tag-manager configuration.
Create a GA4 web data stream
Google Analytics 4 (GA4) collects data through a web data stream, which is a connection between one website and one GA4 property.
- Open your GA4 property.
- Go to Admin.
- Under Data collection and modification, choose Data streams.
- Select Web.
- Enter your site's HTTPS URL and a stream name.
- Create the stream.
- Copy the Measurement ID, which looks like
G-XXXXXXXXXX.
The property is the reporting container. The data stream is the website source inside that container. Do not create a new property for every campaign or social network. Use one web stream for the site, then distinguish traffic with source, medium, campaign, and event data.
Add the Google tag
For a simple site, place the Google tag in the <head> element of every page. Replace G-XXXXXXXXXX with your real 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>The first line loads Google's analytics library asynchronously, so it does not normally block the page from rendering. The dataLayer array stores messages before the library is ready. The config call initializes GA4 for this page and enables the standard page-view collection.
If your site uses a shared layout, add the tag there rather than copying it into individual pages. A site built with a framework may need the tag in its document template or root layout. For a single-page application, also check that route changes produce page views when users move between views without a full page reload.
Choose between HTML and a tag manager
A tag manager is a tool that loads and controls marketing tags without requiring a code release for every change. It can be useful when several people manage analytics, advertising pixels, and event rules.
| Setup | Best fit | Main risk |
|---|---|---|
| Direct Google tag | Small site or simple measurement plan | Changes require code access |
| Tag manager | Multiple tags and frequent changes | Duplicate or conflicting tags |
Use one primary installation method. If you install GA4 directly and also configure the same Google tag in a tag manager, one visit may send duplicate page views.
Account for consent
If your site requires visitor consent, do not initialize analytics as though consent was already granted. Configure consent defaults before the GA4 configuration call, then update consent after the visitor makes a choice. The exact categories and banner behavior depend on your jurisdiction and consent-management platform.
A denied consent choice can reduce the data GA4 receives. That is expected behavior, not necessarily an installation failure.
Verify the installation
Open your site in a private browser window, then visit Reports > Realtime in GA4. A recent visitor should appear within a short time. For more detailed checks, use Google Tag Assistant or the browser's developer tools.
Check these points:
- The page requests
gtag/jsonly once. - The request contains the correct Measurement ID.
- The page sends one page view, not two.
- A social landing URL keeps its UTM query parameters.
- Consent settings match the visitor's choice.
- Internal test traffic is excluded from business reports when appropriate.
Realtime data is useful for checking collection, but it is not a final reporting source. Standard reports can take longer to process and may apply filters, attribution rules, or thresholding.
Exercise
A site shows two page views in Realtime for one page load. What should you inspect first?
Show answer
Inspect the page source and tag-manager preview for duplicate GA4 installations. Look for both a direct Google tag and a tag-manager configuration, or two tags using the same Measurement ID.