Basic GA4 through Google Tag Manager
Last updated: October 8, 2026
This publisher example keeps one GA4 destination blocked until CYBEXO has validated a completed Analytics choice. GTM and the CMP still load to present privacy choices; the Google measurement tag waits. Denied defaults alone do not provide this blocking.
Use the current CYBEXO Gallery template and Web CMP 1.5.38 or later, with Consent Mode enabled. This example covers a standard dataLayer, one G- measurement ID and a normal document navigation. Google Ads, Floodlight, extra connected destinations, server-side forwarding, independent plugins and custom SPA tracking need their own blocking and verification. Do not combine this with the direct GA4 helper.
1. Prepare the page
Section titled “1. Prepare the page”Remove other installations of the same Google measurement tag, including plugins, inline gtag snippets and tags loaded outside this container. Check the Google tag’s destinations: use only the intended GA4 property for this example.
Download the GTM Basic Analytics gate, review it, and host it on your own site as /assets/cybexo-gtm-basic-analytics.js. Place the following synchronous code before your GTM container snippet. Replace G-YOUR_MEASUREMENT_ID with your actual GA4 measurement ID. Apply your site’s CSP nonce or hash where needed; do not add async or defer.
<!-- Keep the early synchronous TCF bootstrap here when using TCF. --><script src="/assets/cybexo-gtm-basic-analytics.js"></script><script> window.cybexoGtmBasicAnalytics = window.cybexoCreateGtmBasicAnalytics({ measurementId: 'G-YOUR_MEASUREMENT_ID' });</script><!-- Your normal GTM container snippet follows. -->The gate does not load Google or write consent values. It reads the public consent-state API and emits cybexo_analytics_ready once per document after a completed or restored Analytics grant. Missing, invalid, unready, pending or unchosen consent stays blocked. A regional default or the banner closing does not count as a choice. Event details and raw browser storage are not used to grant permission.
If the gate file fails to load, the trigger below remains blocked. Correct the delivery or CSP error and reload the page.
2. Configure GTM
Section titled “2. Configure GTM”- Keep Cybexo CMP on Consent Initialization – All Pages, with all four global defaults denied and no granted regional override for this example. Do not add an Analytics consent requirement to the CMP tag itself: visitors need it to make their choice.
- Under Variables → New → Custom JavaScript, create CYBEXO – Analytics permitted with the function below.
- Under Triggers → New → Custom Event, set the event name to
cybexo_analytics_ready, with regular-expression matching off. Select Some Custom Events and require CYBEXO – Analytics permitted equalstrue. - Edit the Google tag for the same
G-ID. Use only this new trigger; remove its Initialization, All Pages and other startup triggers. Under Advanced Settings → Tag firing options, select Once per page. - Under Advanced Settings → Consent Settings → Additional Consent Checks, select Require additional consent for tag to fire, and add
analytics_storage. Retain the tag’s built-in checks. - Under the Google tag’s Configuration settings, set
allow_google_signalsandallow_ad_personalization_signalsto the boolean valuefalsefor this Analytics-only example. Leave the normal initial page view enabled; do not add another page-view tag on the same ready event.
function () { try { return !!window.cybexoGtmBasicAnalytics && window.cybexoGtmBasicAnalytics.canSend() === true; } catch (_) { return false; }}If your CSP prevents GTM Custom JavaScript variables from evaluating, this recipe will not start Analytics. Have your developer adapt the gate to your permitted GTM setup and verify it before publication; do not weaken CSP simply to copy this example.
The ready event supplies a new trigger after consent is available. Adding an additional consent check to an All Pages trigger alone does not supply a later trigger for a tag that was blocked at page load.
Additional Analytics events
Section titled “Additional Analytics events”Start with the single Google tag above. For each additional GA4 event tag, require both CYBEXO – Analytics permitted equals true on its trigger and the analytics_storage additional consent check. Use the same destination. Ensure the Google tag is configured first, using GTM tag sequencing where necessary and retaining its once-per-page setting. Audit every tag that can load measurement code; a permitted Google tag does not automatically gate another tag.
Discard events attempted before permission. Do not save or replay a pre-consent event backlog. Do not attach purchase, conversion or page-view tags indiscriminately to every CMP update; reopening preferences and restoring consent are not new purchases or page views.
3. Handle withdrawal
Section titled “3. Handle withdrawal”After Analytics has started, the gate immediately sets Google’s destination-specific ga-disable- flag and blocks future gated events when Analytics permission is withdrawn or becomes unavailable. It waits while a consent save is pending, then reloads the document. The following page validates the saved choice before it can start Google again. This reload is intentional; account for it in forms and other unsaved page work.
Changing a GTM consent check does not unload an already-running tag. Requests already in flight or associated with a previously loaded tag’s consent transition cannot be recalled. Verify the transition and the following denied page separately. On a restored back-forward cache document, the gate also requires a fresh page load.
Opening settings or cancelling an unsaved draft must preserve the completed choice. The template remains the sole Google consent owner: do not add a separate advertiser-TCF override or gtag('consent', ...) writer.
4. Preview and verify before publishing
Section titled “4. Preview and verify before publishing”Use GTM Preview / Tag Assistant and the browser Network panel on your own domain. The GTM container and CMP requests are expected before a choice; distinguish them from the GA4 Google tag and collection requests. Check your gateway URLs too if you use a Google tag gateway.
| Journey | Expected result |
|---|---|
| Fresh visit, no choice | Four native defaults denied. Google tag does not fire; no GA4 tag load or collection request. |
| Reject All | No ready event or Google tag load. Reload remains blocked. |
| Accept Analytics | Native Analytics consent granted before the ready event. Google tag fires once and the intended page view is delivered. |
| Restore saved Analytics grant | Ready event and Google tag occur once after the CMP validates the saved decision. |
| Refuse Analytics while allowing other purposes | Google Analytics remains blocked; an advertising choice alone cannot open this gate. |
| Reopen settings, then cancel | No second ready event, no duplicate initial page view and no change to saved consent. |
| Withdraw Analytics | Later gated events blocked immediately. Reload follows completed saving; the next denied document sends no GA4 requests. |
| Block CMP configuration or gate delivery | Google tag remains blocked. Correct the failure and reload to recover. |
| Restore a back-forward cache page | A fresh document validates consent before measurement restarts. |
An empty direct gtag consent queue is normal for native GTM; inspect Tag Assistant’s Consent tab. A tag marked fired does not by itself prove delivery. Retain the published container version and the network/Tag Assistant results for your installation. This example is an implementation starting point, not proof that an untested container or every destination is correctly blocked.
Change modes deliberately
Section titled “Change modes deliberately”- To Advanced mode: remove this publisher gate and its ready-event trigger, restore the intended Google tag initialization trigger, and review/remove the Basic-only additional firing restrictions. Keep the CYBEXO template on Consent Initialization. Verify denied-state behavior and subsequent updates using the Consent Mode validation guide. Avoid keeping both startup triggers, which can duplicate measurement.
- To fully off: a dashboard toggle cannot undo native defaults already set by a GTM template. Remove the CYBEXO native consent tag and any separate default/update writer from the published container. Replace the CMP installation with the direct full-off snippet, preserving the early TCF bootstrap where required. Keep Google tags removed or separately blocked; disabling Consent Mode is not a tag blocker. Remove this GTM example if migrating to the direct GA4 Basic/full-off example so there is only one publisher gate. Test a fresh page after publication.