Skip to main content
TagManifest /Docs Dashboard

Direct document.cookie access

Custom JavaScript variable accesses document.cookie directly instead of using GTM's built-in Cookie variable type.

On this page 6

Summary

  • rule_id — variable-cookie-access
  • purpose — Detect Custom JavaScript variables that read document.cookie directly
  • effect — Manual cookie parsing is fragile and bypasses GTM's consent-aware cookie variable type
  • layer — container
  • reversibility — non-destructive

What fires this

The rule filters data.variables for entries where type === 'jsm' (Custom JavaScript variable). For each match, it reads v.parameters.javascript and tests for document.cookie (case-insensitive regex). Any variable containing that pattern is flagged. The rule returns one issue listing all affected variables by ID and name. Variables of other types are not checked.

Why it matters

GTM (Google Tag Manager) provides a built-in First-Party Cookie variable type. It reads a named cookie without requiring manual string parsing. Custom JavaScript variables that call document.cookie must parse the raw cookie string themselves — a pattern prone to edge cases with missing cookies, special characters, and encoding. The built-in type also integrates with GTM's consent framework. When consent blocks cookie access, the built-in variable handles that gracefully. A Custom JavaScript variable must replicate that handling manually or silently misbehave.

Before / after

// Custom JavaScript variable — reads raw cookie string
function() {
  var match = document.cookie.match(/session_id=([^;]+)/)
  return match ? match[1] : undefined
}
GTM variable type: 1st Party Cookie
Cookie Name: session_id

No JavaScript required. GTM parses the cookie
and handles consent-blocked access automatically.

Remediation

  1. Open the flagged Custom JavaScript variable in GTM.
  2. Identify which cookie name(s) the code reads from document.cookie.
  3. Create a new GTM variable using the built-in 1st Party Cookie type, setting the cookie name field to the identified cookie.
  4. Replace every reference to the Custom JavaScript variable — in tags, triggers, and other variables — with the new First-Party Cookie variable.
  5. Verify the replacement in GTM Preview mode and confirm the correct value resolves.
  6. Delete or pause the Custom JavaScript variable once the transition is confirmed.
  7. Publish the container.

Metadata

Severity
low
Tier
info
Category
quality
Layer
container
Version
1
Effort
15min (about 15 minutes)
Reversibility
non-destructive

See also

Examples

Evaluate
curl https://developer.tagmanifest.com/v1/evaluate-rule \
  -H "Authorization: Bearer $TM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"rule_id":"variable-cookie-access","url":"https://example.com"}'