Google Analytics
Google Analytics 4 (GA4) and Google Tag Manager (GTM) plugin for Contensio
About this plugin
Google Analytics
Adds Google Analytics 4 (GA4) or Google Tag Manager (GTM) tracking to every public page in Contensio. Configured entirely from the admin - no theme editing required.
Features:
- GA4 (
G-XXXXXXXXXX) or GTM (GTM-XXXXXXX) - enter one, not both - GTM takes priority when both are set
- Optional Cookie Consent integration - waits for visitor consent before loading GA4
- Optional IP anonymization for GA4
- Admin settings card in the Settings hub
Requirements
- Contensio 2.0 or later
Installation
Composer
composer require contensio/plugin-gtag
Manual
Copy the plugin directory and register the service provider via the admin plugin manager.
No migrations required.
Configuration
Go to Admin > Settings > Google Analytics.
| Setting | Default | Description |
|---|---|---|
| GA4 Measurement ID | (empty) | Your G-XXXXXXXXXX ID from Google Analytics |
| GTM Container ID | (empty) | Your GTM-XXXXXXX ID from Google Tag Manager |
| Wait for Cookie Consent | on | Delay GA4 load until visitor accepts via the Cookie Consent plugin |
| Anonymize IP | on | Adds {'anonymize_ip': true} to the GA4 config call |
Settings are stored in the core settings table (module = plugin_gtag, setting_key = config).
How it works
GTM mode
When a GTM ID is configured, the plugin injects the standard GTM snippet:
contensio/frontend/head- the<script>loadercontensio/frontend/body-start- the<noscript>iframe fallback
GTM does not interact with the Cookie Consent plugin - manage consent inside GTM itself using built-in consent mode.
GA4 mode
When only a GA4 ID is configured:
- Without consent gating - standard
gtag.jsasync script + config call in<head> - With consent gating (default) - a deferred loader that fires when consent is given:
// Loads immediately if already accepted
if (localStorage.getItem('contensio_consent') === 'accepted') {
loadGtag();
} else {
// Waits for the Cookie Consent plugin's accept event
window.addEventListener('contensio:consent:accepted', loadGtag);
}
The Cookie Consent plugin dispatches contensio:consent:accepted when the visitor clicks Accept. If the Cookie Consent plugin is not installed, consent gating has no effect (GA4 never loads).
Cookie Consent integration
The Cookie Consent plugin dispatches a custom DOM event on accept:
window.dispatchEvent(new Event('contensio:consent:accepted'));
This plugin listens for that event. When respect_consent is true and the event fires, loadGtag() initialises the tracker. For visitors who already accepted in a previous session, localStorage.getItem('contensio_consent') === 'accepted' is true and tracking starts immediately.
Hook reference
| Hook | Description |
|---|---|
contensio/frontend/head |
Injects GTM loader or GA4 <script> |
contensio/frontend/body-start |
Injects GTM <noscript> fallback (GTM mode only) |
contensio/admin/settings-cards |
Settings hub card |