Installing Google Analytics 4 directly with Google's gtag.js remains a supported option. The useful choice is whether one shared analytics tag is enough for the site you actually operate. For a small website with one GA4 data stream, direct installation can remove a management layer and leave one small piece of code to maintain. That simplicity disappears if a WordPress plugin, theme setting, consent platform or hosting integration already sends the same tag. Before adding anything, decide which system owns analytics.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxxxxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxxxxxxxx');
</script>What the five parts of the snippet do
The first script element loads Google's tag library asynchronously. The browser can continue parsing the page while that file loads, rather than waiting for it before doing anything else. The id in the library URL identifies the destination you intend to configure. Google's current gtag.js guide continues to document this direct installation method. Google: install gtag.js
window.dataLayer = window.dataLayer || []; creates the queue if it does not already exist. The small gtag() function puts commands into that queue. The next line records when the tag started. Finally, the config command connects the page to the Measurement ID and, on a conventional multi-page website, initiates automatic page-view measurement. A single-page application or site with client-side routing needs separate navigation testing and may require explicit page-view handling.
The library comes from googletagmanager.com, even though this method does not create or use a Google Tag Manager container. The domain is part of Google's shared tag infrastructure. The practical sign that you are using GTM would be a container ID such as GTM-XXXXXXX and a container-based installation, not the hostname in this loader.
Replace the placeholder in both locations. Do not paste a numeric GA4 property ID, a stream ID or a Tag Manager container ID into this template. For a GA4 web stream, the value you need is the Measurement ID, which normally begins with G-.
Choose the lightest installation that fits the site
Choose the method whose responsibilities you can actually maintain. Google currently recommends Google Tag Manager as its general deployment method, while still documenting direct gtag.js installation as a supported manual route. A small site does not need to add a container merely to prove that its analytics setup is modern. Google tag installation guidance
| Method | Best when | Main responsibility |
|---|---|---|
| Direct gtag.js | One stable GA4 destination is needed across a small site | Keep one shared template and its consent behavior correct |
| CMS or analytics integration | Non-developers maintain the site or the platform already coordinates consent | Confirm exactly what the integration loads and disable competing routes |
| Google Tag Manager | Several vendors, destinations, triggers or publishing roles need versioned control | Govern the container, permissions, consent settings and releases |
A direct tag is a sensible option when the website has one analytics destination, one technical owner and a stable shared layout. A maintained CMS integration may be safer when editors should never touch templates. GTM starts to earn its extra layer when a team needs several marketing or measurement tags, event rules, preview environments, permissions and rollback.
Whichever route you choose, use one deployment path for the same Google tag. A direct snippet plus a plugin plus GTM creates three possible owners and a real risk of duplicate or conflicting configuration.
Find the correct Measurement ID
In Google Analytics, select the account and property that should receive the website's data. Open Admin, then Data streams, and select the relevant Web stream. The stream details show a Measurement ID that normally starts with G-. Google also provides an installation view where you can choose platform instructions or view the tag for manual installation. Google: find your tag ID
The hierarchy matters. An account can contain properties, and a GA4 property can contain data streams. The numeric property ID is useful for administration and APIs, but it is not the value used in this browser snippet. A web-stream Measurement ID tells the Google tag where to send web events.
If a web stream for the same production site already exists, do not create another one merely because a tutorial begins with a creation screen. Reuse the intended stream unless your measurement design calls for separation. Check the stream URL, property name and reporting time zone before copying the ID. That small review prevents a technically successful tag from populating the wrong property.
Check for an existing tag before adding another
Inventory the site before you paste code. On WordPress, look at Site Kit, analytics plugins, theme options, header-injection plugins, consent-management settings and any hosting dashboard that offers analytics. On another CMS, inspect its native integrations and custom-code fields. Ask whoever manages advertising or conversion tracking whether a GTM container is already present.
Then use Google Tag Assistant on the live site. It can show which Google tags load and which events they send. Page source and browser developer tools can also reveal a Measurement ID, but finding the text is not enough by itself. A consent tool may load it only after a choice, and a single visible loader may configure more than one destination. Google: verify a tag with Tag Assistant
If the target Measurement ID already appears, identify its owner before changing anything. Google warns against adding the same Google tag more than once. Duplicate page views inflate traffic and distort engagement calculations while still making the Realtime report look reassuringly active. Google: conflicting on-page code
Choose one route, document it and disable the others. This ownership note can be one sentence in the site's technical record: for example, GA4 is deployed by the consent platform; the theme and analytics plugins must not add it. That sentence is often more valuable six months later than another installation screenshot.
Place the tag once, where every page can use it
For a hand-coded or statically generated site, put the snippet in the shared head template immediately after the opening head element, then deploy that template to every measured page. Do not paste it individually into articles. A page-level paste creates uneven coverage and makes updates difficult. Google: direct tag placement
For WordPress, prefer a maintained integration or a site-wide custom-code field that survives theme updates and lets you see who owns the tag. A header-injection plugin can provide that shared placement when the site lacks a suitable built-in route. Head, Footer and Post Injections, the plugin shown in the original video, is still available in the WordPress directory, but it is an injection tool. It does not by itself decide whether analytics may load before consent. WordPress plugin directory
Directly editing a parent theme's header.php file is no longer the default recommendation here. WordPress warns that theme updates can overwrite direct file changes, and a syntax error in a live template can break pages. If a manual theme implementation is genuinely required, use a child theme, keep a recoverable backup and make the edit in a controlled development or staging workflow. WordPress: editing files
Do not put the tag in an individual WordPress post block. Analytics belongs in the shared site head so that coverage is consistent and one maintainer can find it again.
Consent changes what installed means
The base snippet is not a consent banner. If it is placed in the head exactly as shown, the browser loads Google's library and the config command normally initiates measurement. A site that needs prior consent must connect deployment to a consent mechanism rather than assuming the analytics code handles the choice on its own.
Google describes basic and advanced consent-mode patterns. In basic consent mode, the site or consent-management platform blocks the relevant Google tags until the visitor interacts with the consent mechanism, and loads them only after the relevant consent is granted. In an advanced implementation, the tag can load before the visitor chooses, but the site must set the relevant consent defaults before commands that send measurement data. While Analytics consent remains denied, Google may receive consent state and cookieless measurements. The consent platform must send and persist later updates. Google: basic and advanced consent mode
Those patterns have different technical and privacy consequences. If your policy is that no Analytics or advertising tag request should occur before acceptance, basic mode and prior blocking are the clearer fit. If you use advanced mode, the default consent command must run before configuration or event commands. Pasting the simple template alone does not create that sequence. Google: implement consent mode
The YouTube player is a separate third-party integration with its own loading and consent behavior. Articleous therefore presents the retained video behind an explicit load button. Consent requirements depend on location, purpose, configuration and applicable law. This guide cannot turn a tag into universal compliance. Decide what should happen before a choice, after acceptance and after rejection, then make the implementation reproduce that decision consistently.
Verify the full consent path, not one green indicator
Open a fresh private browser window with extensions disabled where practical. Clear any stored consent for the site, then run these five checks:
- Before making a choice, inspect which Google requests, cookies and events appear. The result should match the site's chosen basic or advanced consent behavior.
- Accept analytics, then use Tag Assistant to confirm the intended Measurement ID and a page-view event. Check GA4 Realtime to confirm that activity reaches the correct property.
- Navigate to a second page. Confirm that the new view is measured once, not twice, and that the shared tag is present on page types outside the homepage.
- On a clean first visit, reject analytics consent. Reload and navigate again, then confirm that the denied choice persists and the implementation matches the documented denied behavior.
- After a previous acceptance, withdraw analytics consent. Confirm that the interface sends an immediate denial update, preserves the choice for later pages and follows the site's policy for cookies or data already stored.
Google says data can begin appearing within about 10 to 15 minutes and that ordinary reports may take 24 to 48 hours. Its troubleshooting guidance also warns that a first implementation can take up to 24 hours to appear in Realtime. Use Tag Assistant for the immediate technical check. Realtime often confirms the destination within minutes, but a delay does not by itself prove failure. Standard reports are the later check. Google: data collection timing Google: troubleshoot tag setup
Test with and without your normal ad blocker if events seem absent, and clear page, CDN and server caches after changing injected code. Save the date, Measurement ID, deployment owner and five-check result in a short implementation note. That turns future troubleshooting from archaeology into a repeatable check.
Use the symptom to locate the failure
| Symptom | Likely cause | Next check |
|---|---|---|
| Tag Assistant cannot find the tag | The snippet is missing, cached, malformed or absent from this template | Inspect the rendered head, confirm both IDs and purge caches |
| Tag Assistant sees the tag but no event reaches GA4 | Consent remains denied, the wrong destination is configured or a request is blocked | Inspect consent state, network requests and the selected web stream |
| One navigation creates two page views | The tag is configured by more than one plugin, template or container | List every deployment route and leave one owner |
| Realtime works but normal reports look empty | Standard reporting has not finished or report filters differ | Wait up to 48 hours and check filters, identity and date range |
| Tracking disappears after a theme update | Code was added directly to the updated parent theme | Restore from the implementation record and move it to a durable integration or child theme |
Do not respond to every symptom by pasting the snippet again. Repeated installation is precisely how a missing-tag problem becomes a duplicate-tag problem.
When Google Tag Manager becomes the simpler option
Direct gtag.js is easy when one stable GA4 configuration is the whole job. It becomes harder to govern when the site adds advertising destinations, several conversion vendors, custom event rules, separate release permissions or frequent changes by people who do not deploy application code.
At that point, GTM can become the simpler operational system, even though it adds a technical layer. It offers a central container, preview tools, versions and publishing controls. The switch should solve a governance problem. Installing GTM only to reproduce one unchanged page-view tag is optional, not a requirement for a modern GA4 property.
If you do switch, plan a clean cutover. Publish the new container only after the direct route is removed or disabled, then repeat all five checks. The goal is one deliberate implementation, not a period in which both methods compete.
What changed since the original video
The retained 2022 video remains useful because its central point is still correct: a small site can send data to GA4 without a Google Tag Manager container. The code pattern is recognisable, and the distinction between a direct tag and a tag-management layer still matters. Original video by Søren Drimer Pejstrup
Three parts now need stronger qualification. Google Analytics menu labels and setup screens have changed, so readers should follow the current Data streams route rather than match every frame. Direct editing of a WordPress parent theme is too fragile to be the default recommendation. Most importantly, an installation is not complete until its consent behavior has been designed and tested.
Use the video for the direct-install idea and visual orientation. Use the current code box and checks in this article for the production decision. Before closing the job, record one tag owner, one deployment path, the consent behavior and the result of all five tests.
Sources
- Google Tag Platform: set up gtag.js
- Google Analytics: find your Google tag ID
- Google Analytics: conflicting on-page code
- Google Analytics: verify a tag with Tag Assistant
- Google Analytics: data collection timing
- Google Analytics: troubleshoot tag setup
- Google Analytics: basic and advanced consent mode
- Google Tag Platform: implement consent mode
- WordPress: editing files
- WordPress plugin directory: Head, Footer and Post Injections
- Original 2022 article
- Original 2022 video
- Kokul Jose photograph, CC BY-SA 4.0
- Creative Commons Attribution-ShareAlike 4.0 licence
