New GTM custom template API to read Google Analytics client and session cookies

August 2, 2025

If you’ve ever tried to extract client or session IDs from Google Analytics cookies in Google Tag Manager, you know it hasn’t been easy. Developers were forced to reverse-engineer the _ga cookie format, write fragile code, and hope nothing changed overnight. Spoiler alert: it often did.

Introducing readAnalyticsStorage

Google Tag Manager now supports a new sandbox API in custom templates: readAnalyticsStorage. This API provides direct, safe access to Google Analytics' client and session data.

Why it matters

  • Future-proof: The API abstracts away cookie format changes.
  • Reliable: You get guaranteed structure and values.

Try the Demo Template

I’ve built a simple demo GTM custom template that uses this API. It reads and logs GA identifiers. Here's a quick look at the code:

const readAnalyticsStorage = require('readAnalyticsStorage');
const log = require('logToConsole');

const analyticsStorageData = readAnalyticsStorage();
log('Analytics storage data:', analyticsStorageData);

if (!analyticsStorageData) {
  log('Data unavailable or user declined analytics storage.');
  return undefined;
}

return analyticsStorageData;

GTM Preview: Output of Variable Using readAnalyticsStorage

readAnalyticsStorage GTM preview

Conclusion

If you’re using GTM to power analytics or marketing tags, this API is a must-use for cleaner, safer, and more maintainable setups. Try the demo template, explore the API, and future-proof your data collection today. Get your hands dirty with the new readAnalyticsStorage API check out the release notes or reach out to me if you need help getting started!