Skip to content

getLocalStorage — GTM Variable Template for GTM

VARIABLES › GTM
getLocalStorage CORE GTM

Retrieves a raw string value from localStorage and caches it during the current GTM event for performance optimization with string key.


When to Use This

GTM Utilities

Access GTM-specific APIs: dataLayer, debug mode, container settings.

Browser Storage

Read from cookies, localStorage, and sessionStorage.


Examples

Example 5
CONFIGURATION
Storage key: "user_preferences"
OUTPUT
"dark_mode=true"

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
getLocalStorage
Storage Key
💾 The key to look up in localStorage.

Supported formats:
  ✓ String
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.


Under the Hood

📜 View Implementation Code
/**
* Retrieves a raw string value from localStorage and caches it during the current GTM event.
* 
* @param {string} data.src - The key to look up 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 src before lookup.
* 
* @returns {string|null|undefined} The raw string value from localStorage, or null/undefined if not found.
*
* @framework ggLowCodeGTMKit
*/
const localStorage = require('localStorage');
const templateStorage = require('templateStorage');
const copyFromDataLayer = require('copyFromDataLayer');

const getLocalStorage = function(key) {
   const gtmId = copyFromDataLayer('gtm.uniqueEventId');
   if (gtmId === templateStorage.get(key + gtmId)) {
       return templateStorage.get(key);
   }
   const rawValue = localStorage.get(key);
   templateStorage.set(key, rawValue);
   templateStorage.set(key + gtmId, gtmId);
   return rawValue;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getLocalStorage - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(getLocalStorage(value));
// ===============================================================================
// getLocalStorage() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(getLocalStorage(value));
};
*/


___WEB_PERMISSIONS___

[
  {
    "instance": {
      "key": {
        "publicId": "access_local_storage",
        "versionId": "1"
      },
      "param": []
    },
    "isRequired": true
  },
  {
    "instance": {
      "key": {
        "publicId": "read_data_layer",
        "versionId": "1"
      },
      "param": [
        {
          "key": "allowedKeys",
          "value": {
            "type": 1,
            "string": "specific"
          }
        }
      ]
    },
    "isRequired": true
  },
  {
    "instance": {
      "key": {
        "publicId": "access_template_storage",
        "versionId": "1"
      },
      "param": []
    },
    "isRequired": true
  }
]