getGaMeasurementIds — GTM Variable Template for GTM
getGaMeasurementIds CORE GTM
Extracts all GA4 Measurement IDs from analytics storage. Returns an array of measurement IDs for all active GA4 properties on the page.
When to Use This
GTM Utilities
Access GTM-specific APIs: dataLayer, debug mode, container settings.
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Extraction
Pull specific values, segments, or patterns from complex data structures.
Browser Storage
Read from cookies, localStorage, and sessionStorage.
Examples
Example 3
OUTPUT
["G-SSCHVBCL1B"]
Example 6
OUTPUT
["G-SSCHVBCL1B", "G-ABCD123456"]
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
Read-only Preview
getGaMeasurementIds
Custom Cookie Configuration
Cookie Prefix (optional)
💾 Custom cookie prefix if GA uses non-standard cookie names.
Supported formats:
✓ String: "xyz"
Supported formats:
✓ String: "xyz"
Cookie Domain (optional)
💾 Custom cookie domain for reading GA cookies.
Supported formats:
✓ Domain: "example.com", ".example.com"
Supported formats:
✓ Domain: "example.com", ".example.com"
Cookie Path (optional)
💾 Custom cookie path for reading GA cookies.
Supported formats:
✓ Path: "/", "/app"
Supported formats:
✓ Path: "/", "/app"
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g.,
val => val.split('.')[0] for first part only, val => 'CID:' + val for formatting). Useful for transformations or formatting.Related Variables
Same category: GTM
Under the Hood
📜 View Implementation Code
/**
* Extracts available GA4 Measurement IDs from analytics storage
*
* @param {string} [data.pfx] - Optional custom cookie prefix (e.g., "xyz")
* @param {string} [data.dom] - Optional custom cookie domain (e.g., "google.com")
* @param {string} [data.pth] - Optional custom cookie path (e.g., "/")
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* @returns {Array|undefined} Array of Measurement IDs found (e.g., ["G-ABC123", "G-XYZ789"])
*
* @framework ggLowCodeGTMKit
*/
const readAnalyticsStorage = require('readAnalyticsStorage');
const getGaMeasurementIds = function(cookiePrefix, cookieDomain, cookiePath) {
let cookieOptions;
if (cookiePrefix || cookieDomain || cookiePath) {
cookieOptions = {};
if (cookiePrefix) cookieOptions.cookie_prefix = cookiePrefix;
if (cookieDomain) cookieOptions.cookie_domain = cookieDomain;
if (cookiePath) cookieOptions.cookie_path = cookiePath;
}
const analyticsData = readAnalyticsStorage(cookieOptions);
if (!analyticsData || !analyticsData.sessions || analyticsData.sessions.length === 0) {
return undefined;
}
const measurementIds = [];
for (let i = 0; i < analyticsData.sessions.length; i++) {
const session = analyticsData.sessions[i];
if (session.measurement_id) {
measurementIds.push(session.measurement_id);
}
}
return measurementIds.length > 0 ? measurementIds : undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
return out(getGaMeasurementIds(data.pfx, data.dom, data.pth));
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_analytics_storage",
"versionId": "1"
},
"param": []
},
"isRequired": true
}
]