Sub topic 8
Installing the GA4 Tag with gtag.js, Tag Manager, or a CMS
Assume you have already created a GA4 web data stream and can access its measurement ID. The measurement ID identifies that stream and has the format G-XXXXXXXXXX. You also need permission to edit the website, its tag manager container, or its CMS settings.
Choose one installation method
All three methods send data to the same web data stream. Choose the method that matches how your site is managed.
| Method | Best for | Main responsibility |
|---|---|---|
| gtag.js | Developers who edit page templates | Add and maintain JavaScript |
| Google Tag Manager | Teams managing several marketing tags | Configure, test, and publish tags |
| CMS integration | Sites built with a supported platform | Enter the measurement ID in the platform settings |
Do not install the same stream through multiple methods. Duplicate installation can send page views twice and make reports inaccurate.
Install with gtag.js
Place the Google tag in the head section of every page, or in the shared template used by every page. Replace the example measurement ID with the ID from your web stream.
<!-- 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>The async attribute lets the browser load the library without blocking the rest of the page. The config call connects the tag to your stream and normally sends a page_view event when a page loads.
For a temporary test, enable debug mode:
<script>
gtag('config', 'G-XXXXXXXXXX', {
'debug_mode': true
});
</script>Remove debug mode after testing, or apply it only for selected test visitors. You can also send a recommended event after a successful action:
gtag('event', 'sign_up', {
method: 'email'
});Install with Google Tag Manager
Google Tag Manager stores tag settings in a container. Add the Google tag to the container, enter the measurement ID, and set its trigger to load on all pages. Use the preview mode to test the container, then publish the container version.
For additional actions, create an event tag and configure its event name and parameters. Tag Manager lets non-developers change these settings, but every change still needs testing and a controlled publishing process.
Install through a CMS
Many CMS platforms provide a Google Analytics field, plugin, or integration screen. Enter the measurement ID exactly as shown in the GA4 web stream. Confirm that the integration adds the tag to all public pages, not only the home page.
CMS integrations can add consent controls or automatically support common events. Check the generated page source and the integration's documentation before adding custom code.
Test the installation
Use this sequence:
- Open the published page in a private browser window.
- Check Realtime for a recent active user.
- Use DebugView when debug mode is enabled.
- Inspect the browser's network requests if no data appears.
- Check that the measurement ID matches the intended stream.
- Wait briefly before concluding that the installation failed.
Exercise
A site already has a Google tag installed through its CMS. The marketing team wants to add Google Tag Manager. What should you check before publishing the container?
Show answer
Check whether the CMS tag will remain active. Keep only one base GA4 installation, or remove the duplicate before publishing, then test Realtime and DebugView.