getRootDomain! — GTM Variable Template for GTM
getRootDomain! EXTENDED GTM
Determines the root domain of the current page by testing cookie access at each domain level. The result is cached for performance.
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
getRootDomain!
This function takes no parameters.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the root domain before returning it (e.g.,
d => '.' + d to add leading dot for cookies, d => d.toUpperCase()). Useful for formatting.Related Variables
Same category: GTM
Under the Hood
📜 View Implementation Code
/**
* Determines the root domain of the current hostname by attempting to set a test cookie at each level.
* Uses templateStorage to cache the result for performance.
*
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* @returns {string} The root domain where cookies can be set (e.g., "example.com").
*
* @author David Vallejo
* @see https://bit.ly/4jXILvT
* @modified Caching and framework adaptation
*
* @framework ggLowCodeGTMKit
*/
const templateStorage = require('templateStorage');
const setCookie = require('setCookie');
const getCookieValues = require('getCookieValues');
const getUrl = require('getUrl');
const getRootDomain = function() {
const cached = templateStorage.getItem('rd');
if (cached) {
return cached;
}
const testCookie = '_gtm_rd';
let hostname = getUrl('host');
let candidate = null;
if (hostname.substring(0, 4) === 'www.') {
hostname = hostname.substring(4);
}
const parts = hostname.split('.');
for (let i = 1; i <= parts.length; i++) {
candidate = parts.slice(-i).join('.');
setCookie(testCookie, 't', {
'domain': candidate,
'path': '/',
'max-age': 1,
'secure': true
});
if (getCookieValues(testCookie).length > 0) {
setCookie(testCookie, '', {
'domain': candidate,
'path': '/',
'max-age': 0,
'secure': true
});
templateStorage.setItem('rd', candidate);
return candidate;
}
}
templateStorage.setItem('rd', hostname);
return hostname;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getRootDomain - Direct mode
// ===============================================================================
return out(getRootDomain());
// ===============================================================================
// getRootDomain! – Apply Mode
// ===============================================================================
/*
return function() {
return out(getRootDomain());
};
*/
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "get_cookies",
"versionId": "1"
},
"param": [
{
"key": "cookieAccess",
"value": {
"type": 1,
"string": "specific"
}
},
{
"key": "cookieNames",
"value": {
"type": 2,
"listItem": [
{
"type": 1,
"string": "gtmTestCookie"
}
]
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired":