Background: GTM's Closure Compiler and ES5 Constraints
When writing <script> blocks inside Google Tag Manager (GTM) Custom HTML tags, LLMs default to ES6+ syntax (const, arrow functions, template literals). Publishing the container then fails with a Google Closure Compiler error:
Error: This language feature is only supported for ECMASCRIPT_2015 mode or better: const declaration.
GTM validates Custom HTML scripts against ECMAScript 5 (ES5) by default. To avoid manually rewriting const and arrow functions into var and function expressions every time, I packaged GTM's syntax constraints and GA4 event schemas as a Claude Code Skill.
Repository and Syntax Rules
GitHub - ekusiadadus/claude-skill-gtm-javascript
When loaded, the skill enforces the following ES5 rules whenever generating GTM scripts:
| Feature | ES6+ (Rejected by GTM) | ES5 (Accepted by GTM) |
|---|---|---|
| Variables | const, let | var |
| Functions | () => {} | function() {} |
| Strings | `${var}` | 'str' + var |
| Destructuring | const { a, b } = obj | var a = obj.a; var b = obj.b; |
| Loops | for (const x of arr) | for (var i = 0; i < arr.length; i++) |
Repository Layout
The skill consists of four reference files:
SKILL.md— Core ES5 syntax rules, IIFE scope isolation, and error-handling conventionsreference.md— ES6+ to ES5 conversion tableexamples.md— Templates fordataLayer.push, GA4 Ecommerce, and Consent Mode v2checklist.md— Verification steps in GTM Preview Mode (Tag Assistant)
Coverage for 2024–2025 GTM / GA4 Spec Changes
- Consent Mode v2 (March 2024+): Explicit
ad_user_dataandad_personalizationparameters - GA4 Ecommerce Schema: Standard
itemsarray with prior{ ecommerce: null }reset instead of legacy Universal Analyticsecommerce.purchase.products - IE11 EOL (July 2024): ES5 syntax remains required for the GTM compiler, while IE11-specific DOM polyfills are omitted
Installation
Global installation:
git clone https://github.com/ekusiadadus/claude-skill-gtm-javascript.git ~/.claude/skills/gtm-javascriptProject-local installation:
mkdir -p .claude/skills
cp -r skills/gtm-javascript .claude/skills/Generated Code Examples
GA4 purchase Event
Wraps the tag in an IIFE to avoid polluting window, clears the previous ecommerce object, and pushes the GA4 payload:
(function() {
'use strict';
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: 'T_12345',
value: 99.99,
currency: 'USD',
items: [{
item_id: 'SKU_001',
item_name: 'Product Name',
price: 99.99,
quantity: 1
}]
}
});
})();Consent Mode v2 Default State
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied'
});Source and templates: github.com/ekusiadadus/claude-skill-gtm-javascript