| Install | |
|---|---|
composer require darvis/livewire-google-analytics |
|
| Latest Version: | v1.0.0 |
| PHP: | ^8.1 |
Clean and secure Google Analytics 4 event tracking for Laravel Livewire applications.
// Before: Complex and error-prone ❌
$this->js("gtag('event', 'generate_lead', {...})");
// After: Simple and safe ✅
$this->trackLead(['form_name' => 'contact_form']);
gtag() calls in your componentscomposer require darvis/livewire-google-analytics
Add this once in your main layout, after @livewireScripts:
@livewireScripts
@include('livewire-google-analytics::script')
use Darvis\LivewireGoogleAnalytics\Traits\TracksAnalytics;
class ContactForm extends Component
{
use TracksAnalytics;
public function submit()
{
$this->validate();
Contact::create($this->all());
// Track the conversion
$this->trackLead([
'form_name' => 'contact_form',
'lead_type' => 'contact',
]);
$this->success = true;
}
}
That's it! 🎉
trackLead() - Lead GenerationFor contact forms, quote requests, demo requests:
$this->trackLead([
'form_name' => 'contact_form',
'lead_type' => 'contact',
]);
trackEvent() - Any GA4 EventFor standard GA4 events like purchases, logins:
$this->trackEvent('purchase', [
'transaction_id' => 'T12345',
'value' => 25.99,
'currency' => 'EUR',
]);
trackNewsletterSignup() - Newsletter Subscriptions$this->trackNewsletterSignup([
'source' => 'footer_widget',
]);
trackCustomEvent() - Custom EventsAutomatically adds ga_ prefix:
$this->trackCustomEvent('download_brochure', [
'brochure_name' => 'Product Catalog 2024',
]);
📖 Quick Start Guide - 5-minute beginner-friendly guide
PHP Component → Livewire Event → JavaScript Listener → Google Analytics
$this->trackLead([...]) in your Livewire componentgtag()Benefits:
Browser Console:
// You should see:
[GA4] Event tracked: generate_lead {form_name: "contact_form"}
GA4 Realtime: Events appear in Google Analytics within seconds.
DebugView: See detailed event information in GA4 Admin → DebugView.
class ContactForm extends Component
{
use TracksAnalytics;
public function submit()
{
$validated = $this->validate();
Contact::create($validated);
$this->trackLead([
'form_name' => 'contact_form',
'lead_type' => 'contact',
]);
$this->success = true;
}
}
class CheckoutForm extends Component
{
use TracksAnalytics;
public function completePurchase()
{
$order = Order::create([...]);
$this->trackEvent('purchase', [
'transaction_id' => $order->id,
'value' => $order->total,
'currency' => 'EUR',
]);
return redirect()->route('order.success');
}
}
✅ Track after success - Only track after validation and processing
✅ Use standard events - Prefer trackLead() over custom events
✅ Include context - Add meaningful parameters for analysis
✅ Validate first - Don't track bot submissions
Events not firing?
@livewireScriptsEvents fire multiple times?
render() or mount()See all troubleshooting solutions →
Contributions are welcome! Please see CONTRIBUTING.md for details.
If you discover any security issues, please email info@arvid.nl instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.