The JSON Cookie Reader is a Google Tag Manager (GTM) Custom Variable Template that reads a specified cookie, parses it as JSON, and returns the value from a given key path (e.g. utm_source, click_id.value).
It is especially useful when paired with a tag like the Attribution Cookie Writer, which stores attribution data in a JSON-formatted cookie.
Built by Daniel Perry-Reed – marketing analytics specialist at Data to Value.
- Reads a cookie by name (e.g.
gtm_attr) - Decodes it (if URL-encoded)
- Parses the value as a JSON object
- Retrieves the value at a dot-notated key path (e.g.
click_id.value)
If you want a ready-to-go setup, use the provided GTM container that includes the JSON Cookie Reader template and an example variable.
-
Download the container file 📥 Download
gtm_json_cookie_reader_container.jsonhere -
Import into GTM
- Go to your GTM workspace > Admin > Import Container
- Upload the downloaded JSON file
- Choose your workspace
- Select Merge and resolve any conflicts
-
Review and publish
- The variable template and example variables should be ready to use
- Update field values as needed and publish your container
- In GTM, go to Templates > New Variable Template
- Paste in the JavaScript from
json_cookie_reader.jshere - Configure the permissions (see below)
- Save and name your variable template (e.g. "JSON Cookie Reader")
{
"permissions": {
"read_cookies": ["*"],
"apiAccess": [
"getCookieValues",
"decodeUriComponent",
"JSON"
]
}
}-
Go to Variables > New
-
Choose your custom variable template
-
Fill in the fields:
- Cookie name: the cookie to read from (e.g.
gtm_attr) - Key path: the JSON key to extract (e.g.
utm_sourceorclick_id.type)
- Cookie name: the cookie to read from (e.g.
For a cookie named gtm_attr containing:
{
"utm_source": "google",
"click_id": {
"type": "gclid",
"value": "abc123"
}
}| Key Path | Returned Value |
|---|---|
utm_source |
google |
click_id.type |
gclid |
click_id.value |
abc123 |
If the cookie is missing or the key doesn’t exist, the variable will return an empty string.
Built and maintained by the team at Data to Value — your data activation partner helping marketing teams transform data into predictable revenue and growth.
MIT