[{"data":1,"prerenderedAt":456},["ShallowReactive",2],{"rule-global-variable-pollution":3},{"id":4,"title":5,"body":6,"breadcrumb":420,"coderail":427,"dateModified":446,"datePublished":446,"description":447,"extension":448,"meta":449,"navigation":183,"next":446,"path":450,"prev":446,"schemaType":451,"seo":452,"stem":453,"wide":454,"__hash__":455},"docs\u002Frules\u002Fglobal-variable-pollution.md","Excessive global variable declarations",{"type":7,"value":8,"toc":412},"minimark",[9,16,58,63,91,95,101,105,285,289,318,322,380,384,408],[10,11,12],"p",{},[13,14,15],"strong",{},"Summary",[17,18,19,30,36,46,52],"ul",{},[20,21,22,25,26],"li",{},[13,23,24],{},"rule_id"," — ",[27,28,29],"code",{},"global-variable-pollution",[20,31,32,35],{},[13,33,34],{},"purpose"," — Detect Custom HTML tags that declare more than two globals outside the common\u002Fexpected set",[20,37,38,41,42,45],{},[13,39,40],{},"effect"," — Unscoped variables persist on ",[27,43,44],{},"window"," and can conflict with site code or other tags",[20,47,48,51],{},[13,49,50],{},"layer"," — container",[20,53,54,57],{},[13,55,56],{},"reversibility"," — non-destructive",[59,60,62],"h2",{"id":61},"what-fires-this","What fires this",[10,64,65,66,69,70,73,74,77,78,77,81,77,84,77,87,90],{},"The rule inspects active Custom HTML tags that have a ",[27,67,68],{},"codeAnalysis"," object. It reads ",[27,71,72],{},"codeAnalysis.globalVars"," and filters out five expected tag-ecosystem globals (",[27,75,76],{},"dataLayer",", ",[27,79,80],{},"gtag",[27,82,83],{},"ga",[27,85,86],{},"fbq",[27,88,89],{},"_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.",[59,92,94],{"id":93},"why-it-matters","Why it matters",[10,96,97,98,100],{},"Every variable declared at the top level of a Custom HTML tag lands on ",[27,99,44],{}," 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.",[59,102,104],{"id":103},"example","Before \u002F after",[106,107,108,204],"compare",{},[109,110,112],"compare-col",{"label":111},"Before — flagged",[113,114,119],"pre",{"className":115,"code":116,"language":117,"meta":118,"style":118},"language-js shiki shiki-themes github-light github-dark","\u002F\u002F Three globals declared at top level — flagged\nvar trackingReady = false\nvar sessionToken = getSessionId()\nvar pageCategory = document.body.dataset.category\n\ndataLayer.push({ event: 'page_meta', pageCategory: pageCategory })\n","js","",[27,120,121,130,148,165,178,185],{"__ignoreMap":118},[122,123,126],"span",{"class":124,"line":125},"line",1,[122,127,129],{"class":128},"sJ8bj","\u002F\u002F Three globals declared at top level — flagged\n",[122,131,133,137,141,144],{"class":124,"line":132},2,[122,134,136],{"class":135},"szBVR","var",[122,138,140],{"class":139},"sVt8B"," trackingReady ",[122,142,143],{"class":135},"=",[122,145,147],{"class":146},"sj4cs"," false\n",[122,149,151,153,156,158,162],{"class":124,"line":150},3,[122,152,136],{"class":135},[122,154,155],{"class":139}," sessionToken ",[122,157,143],{"class":135},[122,159,161],{"class":160},"sScJk"," getSessionId",[122,163,164],{"class":139},"()\n",[122,166,168,170,173,175],{"class":124,"line":167},4,[122,169,136],{"class":135},[122,171,172],{"class":139}," pageCategory ",[122,174,143],{"class":135},[122,176,177],{"class":139}," document.body.dataset.category\n",[122,179,181],{"class":124,"line":180},5,[122,182,184],{"emptyLinePlaceholder":183},true,"\n",[122,186,188,191,194,197,201],{"class":124,"line":187},6,[122,189,190],{"class":139},"dataLayer.",[122,192,193],{"class":160},"push",[122,195,196],{"class":139},"({ event: ",[122,198,200],{"class":199},"sZZnC","'page_meta'",[122,202,203],{"class":139},", pageCategory: pageCategory })\n",[109,205,207],{"label":206},"After — fixed",[113,208,210],{"className":115,"code":209,"language":117,"meta":118,"style":118},"\u002F\u002F All vars scoped inside an IIFE — no globals\n;(function () {\n  var trackingReady = false\n  var sessionToken = getSessionId()\n  var pageCategory = document.body.dataset.category\n\n  dataLayer.push({ event: 'page_meta', pageCategory: pageCategory })\n})()\n",[27,211,212,217,228,239,251,261,265,279],{"__ignoreMap":118},[122,213,214],{"class":124,"line":125},[122,215,216],{"class":128},"\u002F\u002F All vars scoped inside an IIFE — no globals\n",[122,218,219,222,225],{"class":124,"line":132},[122,220,221],{"class":139},";(",[122,223,224],{"class":135},"function",[122,226,227],{"class":139}," () {\n",[122,229,230,233,235,237],{"class":124,"line":150},[122,231,232],{"class":135},"  var",[122,234,140],{"class":139},[122,236,143],{"class":135},[122,238,147],{"class":146},[122,240,241,243,245,247,249],{"class":124,"line":167},[122,242,232],{"class":135},[122,244,155],{"class":139},[122,246,143],{"class":135},[122,248,161],{"class":160},[122,250,164],{"class":139},[122,252,253,255,257,259],{"class":124,"line":180},[122,254,232],{"class":135},[122,256,172],{"class":139},[122,258,143],{"class":135},[122,260,177],{"class":139},[122,262,263],{"class":124,"line":187},[122,264,184],{"emptyLinePlaceholder":183},[122,266,268,271,273,275,277],{"class":124,"line":267},7,[122,269,270],{"class":139},"  dataLayer.",[122,272,193],{"class":160},[122,274,196],{"class":139},[122,276,200],{"class":199},[122,278,203],{"class":139},[122,280,282],{"class":124,"line":281},8,[122,283,284],{"class":139},"})()\n",[59,286,288],{"id":287},"remediation","Remediation",[290,291,292,295,298,305,312,315],"ol",{},[20,293,294],{},"Open each flagged tag in the GTM (Google Tag Manager) UI.",[20,296,297],{},"Identify which variables are declared outside any function scope.",[20,299,300,301,304],{},"Wrap the entire tag body in an IIFE: ",[27,302,303],{},";(function () { \u002F* tag code *\u002F })()",".",[20,306,307,308,311],{},"Confirm that variables shared intentionally across tags use an explicit ",[27,309,310],{},"window.myVar"," prefix with a code comment explaining the cross-tag dependency.",[20,313,314],{},"Test in GTM Preview mode to confirm the tag fires correctly after wrapping.",[20,316,317],{},"Publish a versioned container.",[59,319,321],{"id":320},"metadata","Metadata",[323,324,327,328,333,327,337,341,327,344,348,327,351,354,327,357,361,327,364,368,327,374,377],"dl",{"className":325},[326],"spec-list","\n  ",[329,330,332],"dt",{"id":331},"severity","Severity",[334,335,336],"dd",{},"medium",[329,338,340],{"id":339},"tier","Tier",[334,342,343],{},"info",[329,345,347],{"id":346},"category","Category",[334,349,350],{},"organization",[329,352,353],{"id":50},"Layer",[334,355,356],{},"container",[329,358,360],{"id":359},"version","Version",[334,362,363],{},"1",[329,365,367],{"id":366},"effort","Effort",[334,369,370,373],{},[27,371,372],{},"2hr"," (about 2 hours)",[329,375,376],{"id":56},"Reversibility",[334,378,379],{},"non-destructive",[59,381,383],{"id":382},"see-also","See also",[17,385,386,394,401],{},[20,387,388,393],{},[389,390,392],"a",{"href":391},"\u002Fcategories\u002Forganization\u002F","Organization rules"," — other rules in this category",[20,395,396,400],{},[389,397,399],{"href":398},"\u002Fhealth-score\u002F","Health score"," — how this rule contributes to the score",[20,402,403,407],{},[389,404,406],{"href":405},"\u002Frules\u002F","Rules catalog"," — browse all rules",[409,410,411],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":118,"searchDepth":132,"depth":132,"links":413},[414,415,416,417,418,419],{"id":61,"depth":132,"text":62},{"id":93,"depth":132,"text":94},{"id":103,"depth":132,"text":104},{"id":287,"depth":132,"text":288},{"id":320,"depth":132,"text":321},{"id":382,"depth":132,"text":383},[421,424,426],{"label":422,"href":423},"API Reference","\u002F",{"label":425,"href":405},"Rules",{"label":29},{"cards":428,"onpage":440},[429],{"title":430,"tabs":431},"Evaluate",[432,436],{"label":433,"language":434,"code":435},"cURL","bash","curl https:\u002F\u002Fdeveloper.tagmanifest.com\u002Fv1\u002Fevaluate-rule \\\n  -H \"Authorization: Bearer $TM_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\"rule_id\":\"global-variable-pollution\",\"url\":\"https:\u002F\u002Fexample.com\"}'\n",{"label":437,"language":438,"code":439},"MCP","text","User: Run global-variable-pollution on https:\u002F\u002Fexample.com\n\n# Tool call\n{\n  \"tool\": \"evaluate_rule\",\n  \"arguments\": {\n    \"rule_id\": \"global-variable-pollution\",\n    \"url\": \"https:\u002F\u002Fexample.com\"\n  }\n}\n",[441,442,443,444,445],{"id":61,"label":62},{"id":93,"label":94},{"id":103,"label":104},{"id":287,"label":288},{"id":320,"label":321},null,"Custom HTML tags declaring 3+ globals beyond common\u002Fexpected ones (dataLayer, gtag, ga, fbq, _linkedin_data_partner_id). Globals can conflict with site code or other GTM tags.","md",{},"\u002Frules\u002Fglobal-variable-pollution","TechArticle",{"title":5,"description":447},"rules\u002Fglobal-variable-pollution",false,"keGx8aqymlh1OWaekp_fcbRl2Z7Z-r3oryEC7ChoSY8",1796069484002]