getCookieValue — GTM Variable Template for GTM
getCookieValue CORE GTM
Returns the first value of a cookie with the given name, or all values if multiple is enabled.
Examples
Example 5
CONFIGURATION
Cookie name: sessionId
OUTPUT
abc123
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
getCookieValue
Cookie Name
💾 The name of the cookie to retrieve values for.
Supported formats:
✓ String
Supported formats:
✓ String
Advanced Settings
☑️ Whether to decode the cookie values with JavaScript's decodeURIComponent().
☑️ When enabled, returns all cookie values as an array instead of just the first value.
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Related Variables
Same category: GTM
Under the Hood
📜 View Implementation Code
/**
* Returns the first value of a cookie with the given name, or all values if multiple is enabled.
*
* @param {string} data.src - The name of the cookie to retrieve values for.
* @param {boolean} [data.dec] - Optional parameter to specify whether to decode the cookie values.
* @param {boolean} [data.mul] - Optional parameter to return all values instead of just the first one.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before processing.
*
* @returns {string|Array} The first cookie value (string) or all cookie values (array) if multiple is true.
*
* @framework ggLowCodeGTMKit
*/
const getCookieValues = require('getCookieValues');
const getValues = function(cookieName, decode, multiple) {
let result = getCookieValues(cookieName, decode);
if (multiple !== true && !!result) { return result[0]; }
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getCookieValue - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedName = applyCast(data.pre, data.src);
return out(getValues(processedName, data.dec, data.mul));
// ===============================================================================
// getCookieValue(...) – Apply Mode
// ===============================================================================
/*
return function(value, decode, multiple) {
decode = data.rp1 ? data.dec : decode;
multiple = data.rp2 ? data.mul : multiple;
return out(getValues(value, decode, multiple));
};
*/
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "get_cookies",
"versionId": "1"
},
"param": [
{
"key": "cookieAccess",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]