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.
- In plugin settings, switch Tracking Method to GTM.
- Enter your GTM Container ID (format: GTM-XXXXXX).
- 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
- Troubleshooting Guide β Fix common issues such as missing data or invalid JSON.
- Roadmap & Changelog β Stay updated on upcoming features like OAuth authentication and built-in reporting.