How to Fix MODX Tracking Events Not Firing | OpsBlu Docs

How to Fix MODX Tracking Events Not Firing

Fix GA4, GTM, and pixel events not firing on MODX — template chunk placement, snippet output conflicts, and full-page static cache debugging

Common causes and solutions for tracking events that don't fire correctly on MODX sites.

For general tracking troubleshooting, see the global tracking troubleshooting guide.

Quick Diagnosis Checklist

Before diving deep, check these common issues:

  • Ad blocker disabled (for testing)
  • Incognito/private mode (clear cache)
  • Browser console has no errors (F12)
  • MODX cache cleared (Manage → Clear Cache)
  • Template saved (if recently edited)
  • Plugin enabled (if using plugin method)
  • GTM container published (if using GTM)
  • MODX placeholders parsing (not showing as literal text)
  • No plugin conflicts (disable plugins one by one)

MODX-Specific Tracking Issues

Cache Interfering with Tracking

Problem: Tracking events showing cached data instead of current resource.

Diagnosis:

// Check if tracking shows wrong resource ID
// View page source, search for resourceId or similar

Common scenarios:

  • Resource ID always shows "1" (homepage)
  • Page title always the same across pages
  • TVs showing values from different resources

Fix:

Option 1: Use Uncached Tags

<!-- Before: Cached (shows cached data) -->
<script>
  dataLayer.push({
    'resourceId': '[[*id]]',
    'pageTitle': '[[*pagetitle]]'
  });
</script>

<!-- After: Uncached (shows current data) -->
<script>
  dataLayer.push({
    'resourceId': '[[!*id]]',
    'pageTitle': '[[!*pagetitle]]'
  });
</script>

Option 2: Use Plugin (Always Dynamic)

<?php
// Plugin automatically uses current resource
$resource = $modx->resource;
$dataLayer = [
    'resourceId' => $resource->get('id'),
    'pageTitle' => $resource->get('pagetitle')
];

Option 3: Clear Cache After Changes

  1. ManageClear Cache
  2. Test in incognito mode
  3. Verify correct data appears

MODX Placeholders Not Parsing

Problem: MODX tags appear as literal text in tracking code or browser.

Example in page source:

<script>
  gtag('event', 'page_view', {
    'page_title': '[[*pagetitle]]',  // Should be actual title
    'resource_id': '[[*id]]'         // Should be number
  });
</script>

Causes:

1. Incorrect Tag Syntax

// Wrong syntax
{*pagetitle}      // Wrong delimiters
[*pagetitle]      // Missing brackets
[[pagetitle]]     // Missing asterisk
[[*pagetitle]     // Missing closing bracket

// Correct syntax
[[*pagetitle]]    // Resource field
[[+placeholder]]  // Placeholder
[[++setting]]     // System setting
[[!*uncached]]    // Uncached resource field

2. Tags in Cached Chunk

If tracking code is in a cached chunk:

// In chunk that's cached elsewhere
<script>
  gtag('event', 'page_view', {
    'page_title': '[[!*pagetitle]]'  // Use uncached !
  });
</script>

Or move tracking to template instead of chunk.

3. Parser Not Processing

Check template:

  • Template is saved
  • Resource is published
  • Cache is cleared

Verify in browser:

// Console check
console.log('Resource ID:', '[[*id]]');
// If shows [[*id]], parser didn't run

Template Variable (TV) Values Not Available

Problem: TV placeholders show empty or undefined in tracking.

Diagnosis:

// Check TV value in template
[[*tv_name]] - Empty or placeholder shows?

Causes & Fixes:

1. TV Not Assigned to Template

Fix:

  1. ElementsTemplates → Your template
  2. Template Variables tab
  3. Ensure TV is in "Assigned" list
  4. Save template

2. TV Not Set on Resource

Fix:

  1. Edit resource
  2. Template Variables tab
  3. Set TV value
  4. Save resource

3. TV Name Misspelled

// Wrong
[[*product_pric]]   // Typo

// Correct
[[*product_price]]

4. TV Output Filter Needed

// May return empty if TV not set
[[*product_price]]

// Better: Provide default
[[*product_price:default=`0`]]

// Or check if exists
[[*product_price:notempty=`[[*product_price]]`:default=`0`]]

Plugin Execution Issues

Problem: Plugin-based tracking not appearing on pages.

Diagnosis:

  1. Check plugin is enabled:

    • ElementsPlugins
    • Find your tracking plugin
    • Ensure checkbox is checked
  2. Check system events:

    • Open plugin
    • System Events tab
    • Ensure correct event checked (e.g., OnWebPagePrerender)
  3. Check for errors:

    • ReportsError Log
    • Look for plugin-related errors

Common issues:

1. Wrong System Event

// Plugin should be on OnWebPagePrerender for output injection
// Not OnLoadWebDocument (too early) or OnWebPageComplete (too late)

Fix: Check System Events tab, select OnWebPagePrerender.

2. Plugin Disabled

Fix: Enable plugin checkbox and save.

3. Plugin Code Error

// Check error log for PHP errors
// Common issues:
- Undefined variables
- Syntax errors
- Missing semicolons

Debug:

<?php
// Add to plugin to verify it's running
$modx->log(modX::LOG_LEVEL_ERROR, 'Tracking plugin executed');

Check error log to confirm message appears.

4. Plugin Execution Order

Problem: Another plugin interfering.

Fix:

  1. ElementsPlugins
  2. Adjust plugin Priority (lower number = higher priority)
  3. Or disable other plugins temporarily to test

Google Analytics 4 (GA4) Issues

GA4 Events Not Appearing

1. Check GA4 DebugView

Enable debug mode:

gtag('config', 'G-XXXXXXXXXX', {
  'debug_mode': true
});

Check DebugView:

  • GA4AdminDebugView
  • Should see events in real-time
  • If events appear here but not in Reports, wait 24-48 hours for processing

2. Verify Measurement ID

<!-- Wrong: Universal Analytics (old) -->
gtag('config', 'UA-XXXXXXXXX');

<!-- Correct: GA4 -->
gtag('config', 'G-XXXXXXXXXX');

Check MODX implementation:

// In template or plugin
G-XXXXXXXXXX  // Should start with G-

3. gtag Not Loaded

Console check:

console.log(typeof gtag);
// Should be "function"
// If "undefined", gtag.js didn't load

Causes:

Fix:

<!-- Ensure gtag.js loads -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXXXXX');
</script>

4. MODX Data Not Populating

Issue: Events fire but MODX data shows as placeholders.

Diagnosis:

// In GA4 DebugView, check event parameters
// If see [[*id]] instead of number, placeholders not parsed

Fix:

// Ensure MODX tags parse before GA4 code
<script>
  gtag('event', 'page_view', {
    'page_title': '[[!*pagetitle]]',      // Use uncached
    'resource_id': '[[!*id]]',
    'template': '[[!*template]]'
  });
</script>

Or use plugin to avoid parsing issues:

<?php
$resource = $modx->resource;

$gaCode = <<<HTML
<script>
  gtag('event', 'page_view', {
    'page_title': '{$resource->get('pagetitle')}',
    'resource_id': '{$resource->get('id')}',
    'template': '{$resource->get('template')}'
  });
</script>
HTML;

GA4 via GTM Not Working

Check GTM is loading:

console.log(window.google_tag_manager);
// Should show object with your container

If undefined:

  • GTM code not in template
  • GTM container ID wrong
  • JavaScript error blocking GTM

See GTM Issues below.

Meta Pixel Issues

Meta Pixel Not Loading

1. Verify Pixel Helper

Install Meta Pixel Helper:

  • Green icon = Working
  • Yellow = Warnings
  • Red = Error
  • No icon = Not loaded

2. Check Pixel ID

// Correct format: 16-digit number
fbq('init', '1234567890123456');

// Wrong: Letters or wrong format
fbq('init', 'YOUR_PIXEL_ID');  // Placeholder not replaced

3. fbq Not Defined

Console check:

console.log(typeof fbq);
// Should be "function"

If undefined:

<!-- Ensure base pixel code loads -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'YOUR_PIXEL_ID');
fbq('track', 'PageView');
</script>

Meta Pixel Events Not Firing

1. Check Events Manager

Real-time testing:

  1. Meta Events Manager → Test Events
  2. Enter MODX site URL
  3. Perform actions
  4. Verify events appear within seconds

2. Event Parameters Missing MODX Data

Issue: Events fire but missing resource data.

Fix:

<!-- Ensure MODX placeholders parse -->
<script>
  fbq('track', 'ViewContent', {
    'content_name': '[[!*pagetitle]]',
    'content_ids': ['[[!*id]]'],
    'content_category': '[[!*parent:select=`pagetitle`]]'
  });
</script>

Or use plugin for reliability:

<?php
$resource = $modx->resource;

$pixelCode = <<<HTML
<script>
  fbq('track', 'ViewContent', {
    'content_name': '{$resource->get('pagetitle')}',
    'content_ids': ['{$resource->get('id')}'],
    'value': 0
  });
</script>
HTML;

3. FormIt Form Events

Issue: Lead event not firing on form submission.

Diagnosis:

// Check if success message shows
[[!+fi.successMessage:notempty=`Form submitted!`]]

If form submits but event doesn't fire:

<!-- Ensure event code is in success condition -->
[[!+fi.successMessage:notempty=`
<script>
  fbq('track', 'Lead', {
    'content_name': 'Contact Form',
    'content_category': '[[*pagetitle]]'
  });
</script>
<p>Thank you! Form submitted.</p>
`]]

Google Tag Manager (GTM) Issues

GTM Container Not Loading

1. Verify Installation

Check template:

// Should be in <head>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});...})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>

// Should be in <body>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"...></iframe></noscript>

Console check:

console.log(window.google_tag_manager);
// Should show GTM object with container ID

2. Container Not Published

Most common GTM issue: Changes made but not published.

Fix:

  1. Go to GTM
  2. Look for "Workspace Changes" banner
  3. Click Submit
  4. Click Publish

Verify: Container version should show as "Published"

3. Container ID Wrong

// Check template/plugin for correct ID
GTM-XXXXXXX  // Should match your actual container

Data Layer Issues

1. Data Layer Empty or Missing

Console check:

console.log(window.dataLayer);
// Should show array with MODX data

If undefined or empty:

Cause: Data layer not initialized before GTM.

Fix - Template:

<!-- Data layer MUST come BEFORE GTM code -->
<script>
  window.dataLayer = window.dataLayer || [];
  dataLayer.push({
    'resourceId': '[[!*id]]',
    'pageTitle': '[[!*pagetitle]]'
  });
</script>

<!-- THEN GTM code -->
<script>(function(w,d,s,l,i){...GTM code...})</script>

Fix - Plugin:

<?php
// Ensure data layer injects before GTM
$dataLayerCode = '...';
$gtmCode = '...';

$output = $modx->resource->_output;
$output = str_replace('</head>', $dataLayerCode . $gtmCode . '</head>', $output);

2. MODX Data Not in Data Layer

Issue: Data layer exists but MODX values missing.

Check:

window.dataLayer.filter(obj => obj.resourceId);
// Should show objects with resourceId

If empty or shows placeholders:

// Use uncached tags
dataLayer.push({
  'resourceId': '[[!*id]]',           // Uncached
  'pageTitle': '[[!*pagetitle]]'
});

// Or use plugin
<?php
$dataLayer = [
    'resourceId' => $modx->resource->get('id'),
    'pageTitle' => $modx->resource->get('pagetitle')
];
$json = json_encode($dataLayer);
// Push $json to page

GTM Variables Not Populating

Issue: GTM variable shows undefined in Preview mode.

Diagnosis:

  1. GTM Preview mode
  2. Variables tab
  3. Check variable value

Causes:

1. Wrong Data Layer Path

// Wrong
ecommerce.products.0.id

// Correct (check your actual data layer structure)
resourceId

Fix: Match exact data layer structure in GTM variable.

2. Data Doesn't Exist on Page

Example: Trying to get product price on non-product page.

Fix: Use GTM triggers to ensure variable only accessed when data exists.

GTM Tags Not Firing

Check in Preview mode:

  1. Enable GTM Preview
  2. Navigate to MODX page
  3. Check Tags tab
  4. Look for "Tags Not Fired"

Common causes:

1. Trigger Not Met

Fix: Review trigger conditions, ensure MODX data populates variables correctly.

2. Tag Paused

Fix: In GTM, ensure tag is not paused (unpause in tag settings).

3. Blocking Trigger

Fix: Check for trigger exceptions blocking the tag.

Form Tracking Issues

FormIt Events Not Tracking

Problem: Form submits but tracking event doesn't fire.

Diagnosis:

  1. Confirm form submits:

    • Success message shows
    • Email received
    • Form data saved
  2. Check success condition:

// Does this show after submission?
[[!+fi.successMessage:notempty=`Success!`]]

If form submits but no event:

Fix 1: Ensure Event in Success Block

[[!+fi.successMessage:notempty=`
<!-- Event code here -->
<script>
  gtag('event', 'form_submit', {...});
  fbq('track', 'Lead', {...});
</script>
<p>Thank you for your submission!</p>
`]]

Fix 2: Use Custom FormIt Hook

Create plugin:

<?php
/**
 * Custom FormIt Hook for Tracking
 */

$formName = $hook->getValue('form_name') ?: 'contact';

// Store in session to fire on next page load
$_SESSION['form_submitted'] = [
    'formName' => $formName,
    'resourceId' => $modx->resource->get('id')
];

return true;

In FormIt:

[[!FormIt?
  &hooks=`email,FormItSaveForm,trackFormHook`
]]

Debugging Workflow

Step-by-Step Process

  1. Clear All Caches

    • MODX cache: ManageClear Cache
    • Browser cache: Ctrl+Shift+Delete
    • Test in incognito mode
  2. Verify Basic Setup

    • Tracking code in template/plugin
    • IDs/Container IDs correct
    • No JavaScript errors (console)
    • Template saved
    • Plugin enabled (if using)
  3. Test MODX Placeholders

    <!-- Add to template temporarily -->
    Resource ID: [[*id]]<br/>
    Page Title: [[*pagetitle]]<br/>
    <!-- Should show actual values, not placeholders -->
    
  4. Test Tracking Code

    // Console checks
    console.log(typeof gtag);     // GA4
    console.log(typeof fbq);      // Meta Pixel
    console.log(window.google_tag_manager);  // GTM
    console.log(window.dataLayer);  // Data layer
    
  5. Use Platform Tools

    • GA4 DebugView
    • Meta Events Manager Test Events
    • GTM Preview Mode
  6. Check MODX Error Log

    • ReportsError Log
    • Look for JavaScript or plugin errors
  7. Test Across Resources

    • Test on multiple pages
    • Different templates
    • Different resource types

Common Error Messages

"gtag is not defined"

Cause: GA4 script not loaded.

Fix:

  • Check script src is correct
  • Disable ad blocker
  • Check for JavaScript errors before gtag

"fbq is not defined"

Cause: Meta Pixel script not loaded.

Fix:

  • Verify Pixel base code in template
  • Check Pixel ID is correct
  • Disable ad blocker

"dataLayer is not defined"

Cause: Data layer not initialized.

Fix:

window.dataLayer = window.dataLayer || [];
// Initialize BEFORE pushing data

MODX placeholder shows as literal text

Cause: Parser not running or wrong syntax.

Fix:

  • Use correct syntax: [[*id]]
  • Clear MODX cache
  • Use uncached: [[!*id]]
  • Move to plugin instead of template

When to Get Help

Hire a MODX Developer

  • Events still not firing after troubleshooting
  • Complex custom tracking requirements
  • Plugin development needed
  • Template architecture issues
  • Multiple integrations conflicting

Find developers: MODX Professional Directory

Platform Support

GA4: Google Analytics Help Meta: Meta Business Help GTM: Tag Manager Help

MODX Community

Forums: forums.modx.com Slack: MODX Slack community Documentation: docs.modx.com

Next Steps

For general troubleshooting strategies, see Tracking Troubleshooting Guide.