The new readAnalyticsStorage sandbox API enables developers to read Google Analytics client and session IDs directly within custom templates using client-side JavaScript. This replaces the unreliable method of reverse-engineering Google-set cookie formats, which often broke when Google Analytics updated its cookies. The API ensures a stable, future-proof way for Google Tag Manager users to access necessary cookie data without disruption.
Custom Template APIs Overview
Google Tag Manager supports several Core APIs for building custom templates in sandboxed JavaScript. These APIs are accessed via require()
statements, for example:
const myAPI = require('myAPI');
Only the listed APIs are compatible with sandboxed JavaScript.
Example: addConsentListener API
- Registers a listener function triggered when a specified consent type's state changes.
- The listener activates when consent changes from denied to granted or vice versa.
- Consent types with no state are treated as granted, so listeners won't fire when an unset consent type becomes granted.
- Listener functions must manage their own execution frequency.
The readAnalyticsStorage API and other custom template APIs provide a safer, more reliable way to interact with Google Analytics data and consent states within Google Tag Manager.