Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Sets up, audits, and improves analytics tracking with GA4, GTM, Mixpanel, Segment, and UTM parameters.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/gtm-implementation.md
1# Google Tag Manager Implementation Reference23Detailed guide for implementing tracking via Google Tag Manager.45## Contents6- Container Structure (tags, triggers, variables)7- Naming Conventions8- Data Layer Patterns9- Common Tag Configurations (GA4 configuration tag, GA4 event tag, Facebook pixel)10- Preview and Debug11- Workspaces and Versioning12- Consent Management13- Advanced Patterns (tag sequencing, exception handling, custom JavaScript variables)1415## Container Structure1617### Tags1819Tags are code snippets that execute when triggered.2021**Common tag types:**22- GA4 Configuration (base setup)23- GA4 Event (custom events)24- Google Ads Conversion25- Facebook Pixel26- LinkedIn Insight Tag27- Custom HTML (for other pixels)2829### Triggers3031Triggers define when tags fire.3233**Built-in triggers:**34- Page View: All Pages, DOM Ready, Window Loaded35- Click: All Elements, Just Links36- Form Submission37- Scroll Depth38- Timer39- Element Visibility4041**Custom triggers:**42- Custom Event (from dataLayer)43- Trigger Groups (multiple conditions)4445### Variables4647Variables capture dynamic values.4849**Built-in (enable as needed):**50- Click Text, Click URL, Click ID, Click Classes51- Page Path, Page URL, Page Hostname52- Referrer53- Form Element, Form ID5455**User-defined:**56- Data Layer variables57- JavaScript variables58- Lookup tables59- RegEx tables60- Constants6162---6364## Naming Conventions6566### Recommended Format6768```69[Type] - [Description] - [Detail]7071Tags:72GA4 - Event - Signup Completed73GA4 - Config - Base Configuration74FB - Pixel - Page View75HTML - LiveChat Widget7677Triggers:78Click - CTA Button79Submit - Contact Form80View - Pricing Page81Custom - signup_completed8283Variables:84DL - user_id85JS - Current Timestamp86LT - Campaign Source Map87```8889---9091## Data Layer Patterns9293### Basic Structure9495```javascript96// Initialize (in <head> before GTM)97window.dataLayer = window.dataLayer || [];9899// Push event100dataLayer.push({101'event': 'event_name',102'property1': 'value1',103'property2': 'value2'104});105```106107### Page Load Data108109```javascript110// Set on page load (before GTM container)111window.dataLayer = window.dataLayer || [];112dataLayer.push({113'pageType': 'product',114'contentGroup': 'products',115'user': {116'loggedIn': true,117'userId': '12345',118'userType': 'premium'119}120});121```122123### Form Submission124125```javascript126document.querySelector('#contact-form').addEventListener('submit', function() {127dataLayer.push({128'event': 'form_submitted',129'formName': 'contact',130'formLocation': 'footer'131});132});133```134135### Button Click136137```javascript138document.querySelector('.cta-button').addEventListener('click', function() {139dataLayer.push({140'event': 'cta_clicked',141'ctaText': this.innerText,142'ctaLocation': 'hero'143});144});145```146147### E-commerce Events148149```javascript150// Product view151dataLayer.push({ ecommerce: null }); // Clear previous152dataLayer.push({153'event': 'view_item',154'ecommerce': {155'items': [{156'item_id': 'SKU123',157'item_name': 'Product Name',158'price': 99.99,159'item_category': 'Category',160'quantity': 1161}]162}163});164165// Add to cart166dataLayer.push({ ecommerce: null });167dataLayer.push({168'event': 'add_to_cart',169'ecommerce': {170'items': [{171'item_id': 'SKU123',172'item_name': 'Product Name',173'price': 99.99,174'quantity': 1175}]176}177});178179// Purchase180dataLayer.push({ ecommerce: null });181dataLayer.push({182'event': 'purchase',183'ecommerce': {184'transaction_id': 'T12345',185'value': 99.99,186'currency': 'USD',187'tax': 5.00,188'shipping': 10.00,189'items': [{190'item_id': 'SKU123',191'item_name': 'Product Name',192'price': 99.99,193'quantity': 1194}]195}196});197```198199---200201## Common Tag Configurations202203### GA4 Configuration Tag204205**Tag Type:** Google Analytics: GA4 Configuration206207**Settings:**208- Measurement ID: G-XXXXXXXX209- Send page view: Checked (for pageviews)210- User Properties: Add any user-level dimensions211212**Trigger:** All Pages213214### GA4 Event Tag215216**Tag Type:** Google Analytics: GA4 Event217218**Settings:**219- Configuration Tag: Select your config tag220- Event Name: {{DL - event_name}} or hardcode221- Event Parameters: Add parameters from dataLayer222223**Trigger:** Custom Event with event name match224225### Facebook Pixel - Base226227**Tag Type:** Custom HTML228229```html230<script>231!function(f,b,e,v,n,t,s)232{if(f.fbq)return;n=f.fbq=function(){n.callMethod?233n.callMethod.apply(n,arguments):n.queue.push(arguments)};234if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';235n.queue=[];t=b.createElement(e);t.async=!0;236t.src=v;s=b.getElementsByTagName(e)[0];237s.parentNode.insertBefore(t,s)}(window, document,'script',238'https://connect.facebook.net/en_US/fbevents.js');239fbq('init', 'YOUR_PIXEL_ID');240fbq('track', 'PageView');241</script>242```243244**Trigger:** All Pages245246### Facebook Pixel - Event247248**Tag Type:** Custom HTML249250```html251<script>252fbq('track', 'Lead', {253content_name: '{{DL - form_name}}'254});255</script>256```257258**Trigger:** Custom Event - form_submitted259260---261262## Preview and Debug263264### Preview Mode2652661. Click "Preview" in GTM2672. Enter site URL2683. GTM debug panel opens at bottom269270**What to check:**271- Tags fired on this event272- Tags not fired (and why)273- Variables and their values274- Data layer contents275276### Debug Tips277278**Tag not firing:**279- Check trigger conditions280- Verify data layer push281- Check tag sequencing282283**Wrong variable value:**284- Check data layer structure285- Verify variable path (nested objects)286- Check timing (data may not exist yet)287288**Multiple firings:**289- Check trigger uniqueness290- Look for duplicate tags291- Check tag firing options292293---294295## Workspaces and Versioning296297### Workspaces298299Use workspaces for team collaboration:300- Default workspace for production301- Separate workspaces for large changes302- Merge when ready303304### Version Management305306**Best practices:**307- Name every version descriptively308- Add notes explaining changes309- Review changes before publish310- Keep production version noted311312**Version notes example:**313```314v15: Added purchase conversion tracking315- New tag: GA4 - Event - Purchase316- New trigger: Custom Event - purchase317- New variables: DL - transaction_id, DL - value318- Tested: Chrome, Safari, Mobile319```320321---322323## Consent Management324325### Consent Mode Integration326327```javascript328// Default state (before consent)329gtag('consent', 'default', {330'analytics_storage': 'denied',331'ad_storage': 'denied'332});333334// Update on consent335function grantConsent() {336gtag('consent', 'update', {337'analytics_storage': 'granted',338'ad_storage': 'granted'339});340}341```342343### GTM Consent Overview3443451. Enable Consent Overview in Admin3462. Configure consent for each tag3473. Tags respect consent state automatically348349---350351## Advanced Patterns352353### Tag Sequencing354355**Setup tags to fire in order:**356Tag Configuration > Advanced Settings > Tag Sequencing357358**Use cases:**359- Config tag before event tags360- Pixel initialization before tracking361- Cleanup after conversion362363### Exception Handling364365**Trigger exceptions** - Prevent tag from firing:366- Exclude certain pages367- Exclude internal traffic368- Exclude during testing369370### Custom JavaScript Variables371372```javascript373// Get URL parameter374function() {375var params = new URLSearchParams(window.location.search);376return params.get('campaign') || '(not set)';377}378379// Get cookie value380function() {381var match = document.cookie.match('(^|;) ?user_id=([^;]*)(;|$)');382return match ? match[2] : null;383}384385// Get data from page386function() {387var el = document.querySelector('.product-price');388return el ? parseFloat(el.textContent.replace('$', '')) : 0;389}390```391