hasLocalStorageKey β GTM Variable Template for GTM
hasLocalStorageKey EXTENDED GTM
Check whether a specific key exists in localStorage.
When to Use This
Section titled βWhen to Use ThisβGTM Configuration
Section titled β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
hasLocalStorageKey
Storage Key
πΎ The key to check in localStorage.
Supported formats:
β String
Supported formats:
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the key before internal logic (e.g., normalize case, add prefix). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., val => val ? 'Exists' : 'Missing', val => !val for negation). Useful for chaining transformations on the output.
Related Variables
Section titled βRelated VariablesβSame category: GTM
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Checks whether a given key exists in localStorage. * * @param {string} data.key - The key to check in localStorage. * @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 key before checking. * * @returns {boolean} True if the key exists in localStorage, false otherwise. * * @framework ggLowCodeGTMKit */
const localStorage = require('localStorage');
const hasLocalStorageKey = function(key) { return localStorage.get(key) !== null;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// hasLocalStorageKey - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedKey = applyCast(data.pre, data.key);return out(hasLocalStorageKey(processedKey));// ===============================================================================// hasLocalStorageKey() β Apply Mode// ===============================================================================/*return function(value) { return out(hasLocalStorageKey(value));};*/
___WEB_PERMISSIONS___
[ { "instance": { "key": { "publicId": "access_local_storage", "versionId": "1" }, "param": [] }, "isRequired": true }]