Sub topic 2
Install the Google tag directly in your site's HTML
This chapter assumes you have a GA4 property and a web data stream, you can edit your site's HTML or templates, and you know that a script in the page <head> loads before the page body. You need the Measurement ID from your web data stream, which has the format G-XXXXXXXXXX.
Manual installation
The Google tag is the gtag.js script that sends data to GA4. Pasting it into your pages is the most direct route. It works on any site, but you maintain it yourself on every template.
<!-- 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>Replace G-XXXXXXXXXX with your Measurement ID. Put the snippet in a shared header template so every page inherits it. Single-page apps should load it once at startup, not once per route.
> Do not paste the snippet twice.
Platform integrations
Many site builders and content management systems have a GA4 field in their settings. You enter the Measurement ID there, and the platform writes the tag for you. Plugins work the same way. This route needs no code, but the platform controls what gets sent. Check the setting after each platform or plugin update, because an update can change behavior or remove the tag.
Installation through Google Tag Manager
Google Tag Manager (GTM) is a container script that loads other tags. Add the GTM snippet once to your site. Then, inside GTM, create a Google tag with your Measurement ID and publish a version. You can change tags later without editing site code, and preview mode lets you test before publishing. Remove any direct gtag.js snippet, or both will fire.
Exercise
Your site has a GTM container and also a pasted gtag.js snippet. What happens, and what should you do?
Show answer
Both fire, so each page view is counted twice. Remove the direct snippet and keep the tag inside GTM.