Excessive global variable declarations
Custom HTML tags declaring 3+ globals beyond common/expected ones (dataLayer, gtag, ga, fbq, _linkedin_data_partner_id). Globals can conflict with site code or other GTM tags.
Summary
- rule_id —
global-variable-pollution - purpose — Detect Custom HTML tags that declare more than two globals outside the common/expected set
- effect — Unscoped variables persist on
windowand can conflict with site code or other tags - layer — container
- reversibility — non-destructive
What fires this
The rule inspects active Custom HTML tags that have a codeAnalysis object. It reads codeAnalysis.globalVars and filters out five expected tag-ecosystem globals (dataLayer, gtag, ga, fbq, _linkedin_data_partner_id) plus a vendor-pattern allowlist covering FullStory, HubSpot, Google Analytics legacy, OneTrust, Cookiebot, Usercentrics, Intercom, Klaviyo, Matomo, Heap, Mixpanel, Pendo, Drift, and Help Scout Beacon. Any tag with more than two globals remaining after filtering is added to the affected list. The rule emits one issue listing all affected tags. Paused tags are excluded.
Why it matters
Every variable declared at the top level of a Custom HTML tag lands on window unless the tag uses a scoping wrapper. Variables from different tags and from the host page share the same namespace. A name collision silently overwrites the earlier value — the earlier tag's data is lost, or the site's own variable is clobbered. The failure mode is often intermittent because it depends on tag firing order. Wrapping tag code in an IIFE (immediately invoked function expression) costs one line and eliminates the risk entirely.
Before / after
// Three globals declared at top level — flagged
var trackingReady = false
var sessionToken = getSessionId()
var pageCategory = document.body.dataset.category
dataLayer.push({ event: 'page_meta', pageCategory: pageCategory })
// All vars scoped inside an IIFE — no globals
;(function () {
var trackingReady = false
var sessionToken = getSessionId()
var pageCategory = document.body.dataset.category
dataLayer.push({ event: 'page_meta', pageCategory: pageCategory })
})()
Remediation
- Open each flagged tag in the GTM (Google Tag Manager) UI.
- Identify which variables are declared outside any function scope.
- Wrap the entire tag body in an IIFE:
;(function () { /* tag code */ })(). - Confirm that variables shared intentionally across tags use an explicit
window.myVarprefix with a code comment explaining the cross-tag dependency. - Test in GTM Preview mode to confirm the tag fires correctly after wrapping.
- Publish a versioned container.
Metadata
- Severity
- medium
- Tier
- info
- Category
- organization
- Layer
- container
- Version
- 1
- Effort
2hr(about 2 hours)- Reversibility
- non-destructive
See also
- Organization rules — other rules in this category
- Health score — how this rule contributes to the score
- Rules catalog — browse all rules
Examples
curl https://developer.tagmanifest.com/v1/evaluate-rule \
-H "Authorization: Bearer $TM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"rule_id":"global-variable-pollution","url":"https://example.com"}'
User: Run global-variable-pollution on https://example.com
# Tool call
{
"tool": "evaluate_rule",
"arguments": {
"rule_id": "global-variable-pollution",
"url": "https://example.com"
}
}