darvis / livewire-google-analytics
Google Analytics 4 event tracking for Laravel Livewire: a trait that dispatches a browser event and a small listener that forwards it to gtag().
Package info
github.com/ArvidDeJong/livewire-google-analytics
pkg:composer/darvis/livewire-google-analytics
Requires
- php: ^8.2
- illuminate/support: ^11.0|^12.0|^13.0
- livewire/livewire: ^3.0|^4.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.0|^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-21 11:31:57 UTC
README
Send Google Analytics 4 events from Laravel Livewire components. You call a PHP method such as $this->trackLead() in a component, and a listener script in your layout passes the event to gtag(). The package does not load Google Analytics, has no measurement id setting and does no consent handling: your own Google tag stays where it is.
Features
- One line per event -
trackLead(),trackNewsletterSignup(),trackCustomEvent(), andtrackEvent()for everything else - Parameters as data - the event name and parameters reach the browser as JSON, never as JavaScript source, so form input cannot break out of a script
- Waits for the Google tag - an event tracked before
window.gtagexists waits (at most 50 events, at most 30 minutes) and is sent in order once it does; opt out with['queue' => false] - Survives a redirect -
trackEventAfterRedirect()carries the event to the next page through the session, and it is sent once - Fails quietly - without
gtagfor the whole visit nothing is sent, nothing throws and the Livewire action carries on - No double events - the listener registers once per window, also with
wire:navigate - Nothing to configure - no config file, no environment variable, no migration
Requirements
- PHP 8.2+
- Laravel 11, 12 or 13
- Livewire 3 or 4
- A Google tag (
gtag.js) on the page; the package does not load it
Installation
composer require darvis/livewire-google-analytics
Include the listener once in your layout, before </body>:
@include('livewire-google-analytics::script')
Quick start
app/Livewire/ContactForm.php:
<?php namespace App\Livewire; use Darvis\LivewireGoogleAnalytics\Traits\TracksAnalytics; use Livewire\Component; class ContactForm extends Component { use TracksAnalytics; public string $email = ''; public function submit(): void { $this->validate(['email' => 'required|email']); // Save or send the message here. $this->trackLead(['form_name' => 'contact_form']); } public function render() { return view('livewire.contact-form'); } }
After a valid submit the browser calls gtag('event', 'generate_lead', {form_name: 'contact_form'}). Track in an action, after the work succeeded, and not in render(), which runs on every request.
In an action that ends in a redirect, use the …AfterRedirect() variant; the page after the redirect has to include the Blade view:
$this->trackEventAfterRedirect('purchase', ['transaction_id' => 'T12345', 'value' => 25.99, 'currency' => 'EUR']); $this->redirectRoute('orders.thanks');
Documentation
The full documentation lives on the documentation site:
- Installation: your Google tag, the listener, and a check that an event arrives
- Quick start: one complete contact form with every file
- Tracking events: every method and where to call it
- How it works: the browser event, the queue, the redirect
- Examples: newsletter, purchase, download
- Testing:
assertDispatched('ga:event', ...)in your own test suite - Troubleshooting: no event, double events, error messages
- FAQ
Laravel Boost
The package ships a guideline and a skill for Laravel Boost, so an AI assistant in your app knows the API and the pitfalls. Run php artisan boost:install, or php artisan boost:update --discover in a project that already uses Boost.
Testing
composer test # Pest composer lint # Pint, check only; composer format fixes composer analyse # Larastan
Changelog
See CHANGELOG.
Contributing
See CONTRIBUTING.
Security
Please report a vulnerability privately, as described in SECURITY, not in the issue tracker.
License
The MIT License (MIT). See LICENSE.