Advanced Topics Guide for Lean GA4 Tracker

Advanced Topics Guide for Lean GA4 Tracker

Lean GA4 Tracker is designed to be simple yet flexible.

This guide covers advanced features for developers and power users who want more control over event tracking and integrations.


Using Google Tag Manager (GTM)

Instead of sending data directly to GA4, you can route events through Google Tag Manager.

  1. In plugin settings, switch Tracking Method to GTM.
  2. Enter your GTM Container ID (format: GTM-XXXXXX).
  3. Configure GA4 tags and triggers inside your GTM workspace.

πŸ‘‰ Advantage: You gain more flexibility to manage tags, pixels, and scripts in one place.


Manually Firing Events

Developers can manually fire GA4 events using the gtag() function.

Example: Track a custom login event in your theme or plugin:

<script>
gtag('event', 'login', {
  method: 'Google'
});
</script>

πŸ’‘ Place this in your theme’s template, custom JS file, or fire it via a WordPress hook.


Custom Event Selectors

Lean GA4 Tracker supports CSS selectors for triggering custom events.

Example: Track video play on an element with ID #promo-video:

[
  {
    "event": "video_play",
    "selector": "#promo-video"
  }
]

When the element is clicked, a video_play event will be sent to GA4.


Debugging & Validation

Debug Mode

  • Enable Debug Mode in plugin settings.
  • Open browser Developer Tools β†’ Console.
  • Watch for logs of events being fired.

DebugView in GA4

  • Go to Google Analytics 4 β†’ Admin β†’ DebugView.
  • Interact with your site and confirm that events appear in real time.

Excluding Certain Pages

If you want to disable tracking on specific pages, use the built-in filter hook:

add_filter('lean_ga4_should_track', function ($should_track) {
    if (is_page('privacy-policy')) {
        return false;
    }
    return $should_track;
});

πŸ‘‰ This example disables tracking on the Privacy Policy page.


Multisite Advanced Options

  • Network Enforced Settings β†’ Super admins can lock GA4 settings across all sites.
  • Per-Site Overrides β†’ Allow each site to set its own GA4 ID and custom events.

Developer Hooks

Lean GA4 Tracker provides WordPress hooks for extensibility:

/**
 * Filter GA4 measurement ID
 */
add_filter('lean_ga4_measurement_id', function ($id) {
    return 'G-NEWID12345';
});

/**
 * Action after event fired
 */
add_action('lean_ga4_event_fired', function ($event_name, $params) {
    error_log("Event fired: $event_name");
}, 10, 2);

Performance Considerations

  • Async/Defer Mode β†’ Always enable for better Core Web Vitals scores.
  • Minimal Payload β†’ The plugin only loads GA4 script, no extra bloat.
  • Role Exclusions β†’ Ensure admin/editor traffic is excluded to keep reports clean.

Next Steps

Let Us Work Together

Let us Plan Your Website or Digital Strategy Together

Start My Proposal Now β†’
Advanced Topics Guide for Lean GA4 Tracker – Lean GA4 Tracker Documentation