Sub topic 5
Install the Google tag directly in your site's HTML
Assume you already understand users, sessions, and events, and you have access to a Google account and your site's HTML. You also need permission to edit the page template or the shared header used by your pages.
Manual installation is a good choice when you control the site's source code and do not already use a website platform integration or Google Tag Manager. You place the Google tag in the HTML head so it loads on every page.
Get the web stream's Measurement ID
A GA4 property can contain multiple data streams. A web data stream represents data collected from a website. Each web stream has a Measurement ID in the format G-XXXXXXXXXX.
To find it:
- Open Google Analytics and select the correct property.
- Open Admin.
- Under Data collection and modification, select Data streams.
- Select the web stream for your site.
- Copy the Measurement ID.
Check the site's hostname before copying the ID. Using an ID from another property sends data to the wrong destination.
| Item | Purpose |
|---|---|
| GA4 property | Stores and reports your analytics data |
| Web data stream | Defines the website that sends data |
| Measurement ID | Tells the Google tag which web stream receives the data |
| Google tag | JavaScript code that sends page and event data |
Add the Google tag to the shared page template
The Google tag uses the gtag.js library. The library loads from Google's tag service, and the gtag('config', ...) call connects it to your web stream.
Paste this code as high in the head element as possible. Replace both instances of G-XXXXXXXXXX with your actual Measurement ID.
<!-- 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>Place the snippet in a shared layout, header template, or server-side page template. This is better than pasting it separately into individual pages because new pages inherit the tag automatically.
Do not put the snippet in the page body unless your platform requires that location. Do not add it only to the home page if you want data from the full site.
Deploy and check the installation
Save the template, publish the change, and open the live site in a new browser tab. Check the rendered page source or browser developer tools to confirm that:
- The
gtag/jsscript loads. - The Measurement ID is correct in both places.
- The
gtag('config', ...)call runs. - The tag appears on representative page types, not just one URL.
You can temporarily add debug_mode to the configuration while testing:
gtag('config', 'G-XXXXXXXXXX', {
debug_mode: true
});Remove or disable this setting after testing if you do not want all traffic from that installation marked for debugging.
Account for consent and existing tags
If privacy rules require consent, set the default consent state before the configuration call. The tag can then update its behavior after the visitor makes a choice.
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied'
});In a real implementation, place this call after defining gtag but before gtag('config', ...). Your consent banner should send an update when the visitor grants or rejects the relevant purposes.
Before publishing, search the template and platform settings for other GA4 installations. A plugin, hard-coded snippet, or Tag Manager container can already send data. Two configuration tags commonly create duplicate page_view events and inflated user or session counts.
Exercise
You find a GA4 snippet in the shared HTML template and a GA4 integration enabled in the site's platform settings. What should you do before publishing another manual snippet?
Show answer
Identify which installation is intended, disable the duplicate route, and verify that only one Google tag configuration sends the automatic page_view event.