Modx Analytics Integrations: Setup Guide | OpsBlu Docs

Modx Analytics Integrations: Setup Guide

Available integrations for MODX including analytics platforms, tag managers, and marketing pixels.

MODX provides flexible methods to integrate analytics platforms, tag managers, and marketing pixels into your website. This section covers the most common integrations and MODX-specific implementation details.

Available Integrations

Analytics Platforms

Google Analytics 4

  • Template-based implementation (full control)
  • Plugin/Extra implementation (easier management)
  • GTM implementation (flexible, recommended)
  • Works with both MODX Revolution and Evolution

Tag Management

Google Tag Manager

  • Template-based installation (recommended)
  • Plugin installation via Extras
  • Custom chunk implementation
  • Full control over all pages including checkout/forms

Marketing Pixels

Meta Pixel

  • Template-based implementation
  • Plugin-based installation
  • Event tracking with MODX resources
  • Enhanced measurement with custom TVs

MODX-Specific Integration Considerations

Template Architecture

MODX Revolution uses a flexible template system:

  • Scripts can be added to base templates for global availability
  • Individual templates can have their own scripts
  • Chunks allow reusable code snippets across templates
  • Template Variables (TVs) enable custom tracking parameters
  • Plugins can inject code at specific system events

Template vs Plugin Implementation

Template-Based:

  • Direct control over code placement
  • Easier to debug and modify
  • Must update each template individually
  • Best for custom implementations

Plugin-Based (Extras):

  • Centralized management
  • Automatic updates via package manager
  • Less flexible than template-level code for custom event tracking
  • Best for standard implementations

Resource Structure

MODX organizes content as Resources:

  • Each resource has a unique ID
  • Template determines page structure
  • Context separates different sections (web, mgr, etc.)
  • Resources can have parent-child relationships for hierarchy

System Events for Tracking

MODX provides system events useful for analytics:

  • OnLoadWebDocument - Before page render
  • OnWebPagePrerender - Before content output
  • OnWebPageComplete - After page fully rendered
  • OnBeforeDocFormSave - Before resource save
  • OnDocFormSave - After resource save

See MODX System Events for full documentation.

Template Variables (TVs) for Enhanced Tracking

Use TVs to add custom tracking parameters:

  • Product information (price, category, SKU)
  • Content metadata (author, publish date, topic)
  • Custom dimensions for GA4
  • Event parameters for pixels

Example TV implementation:

[[*product_price]]     // Product price TV
[[*product_category]]  // Category TV
[[*page_type]]         // Custom page type

Performance Impact

Template-Based Integrations:

  • Scripts in base template load on every page
  • Use conditional tags to load only when needed
  • Leverage MODX caching for faster delivery
  • Consider async/defer attributes

Plugin-Based Integrations:

  • Plugins execute on system events
  • Can impact page generation time
  • Test performance impact before deployment
  • Use caching plugins when appropriate

Caching Considerations:

  • MODX caches parsed templates
  • Tracking scripts should be cache-safe
  • Use [[!uncached]] syntax if dynamic data needed
  • Clear cache after tracking updates

Integration Best Practices

1. Consolidate Through GTM

Instead of adding multiple pixels directly to templates:

  • Install GTM once in your base template
  • Add all tracking pixels through GTM
  • Easier to manage and update
  • Better performance (single container load)
  • Non-technical updates possible via GTM interface

2. Use Chunks for Reusability

Create reusable chunks for tracking code:

Create chunk: analytics_head

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>

Use in template:

<head>
  [[$analytics_head]]
</head>

3. Leverage Context for Multi-Site Tracking

If running multiple sites in one MODX installation:

[[++site_name]]          // Current site name
[[!++http_host]]         // Current domain
[[++base_url]]           // Base URL

Use these to conditionally load different tracking codes:

[[if? &subject=`[[++site_name]]` &operator=`EQ` &operand=`Site A` &then=`GTM-AAAAAAA` &else=`GTM-BBBBBBB`]]

4. Test Across Resource Types

Always test integrations across:

  • Homepage - General page view tracking
  • Content pages - Article/blog tracking
  • Contact forms - Form submission tracking
  • Custom templates - Special page types
  • Dynamic resources - Resources with custom output

5. Monitor Data Quality

See Events Not Firing for debugging steps.

Common data quality issues:

  • Duplicate events from multiple implementations
  • Missing resource data (template variables not set)
  • Caching interfering with dynamic values
  • Plugin conflicts affecting tracking

MODX-Specific Tools

Package Manager (Extras)

Access popular analytics extras:

  • Analytics - Google Analytics integration
  • GoogleAnalytics4 - GA4 specific implementation
  • Piwik - Self-hosted analytics
  • TagManager - GTM integration extras

Install via:

Extras → Installer → Search for package

Useful Snippets

Get Resource Data:

[[*id]]              // Current resource ID
[[*pagetitle]]       // Page title
[[*parent]]          // Parent resource ID
[[*template]]        // Template ID
[[*published]]       // Published status
[[*publishedon]]     // Publish date

Get User Data:

[[!+modx.user.id]]           // User ID (if logged in)
[[!+modx.user.username]]     // Username

System Settings:

[[++site_name]]              // Site name
[[++site_url]]               // Site URL
[[++manager_language]]       // Manager language

Development Tools

MODX Console Plugin:

  • Debug output
  • Performance monitoring
  • Error logging

Logging:

$modx->log(modX::LOG_LEVEL_ERROR, 'Tracking event fired');

Next Steps

Choose your integration to get started:

For general integration concepts, see the global integrations hub.