Frequently asked questions
What is darvis/livewire-google-analytics and what does it replace?
A Laravel package that sends Google Analytics 4 events from Livewire components. It replaces gtag() calls that are built as a string in PHP with $this->js(...): a trait dispatches a browser event with the event name and parameters as data, and a listener script in the layout passes it to gtag().
How do I send a Google Analytics 4 event from a Laravel Livewire component?
Install darvis/livewire-google-analytics, put @include('livewire-google-analytics::script') once in your layout, add the TracksAnalytics trait to the component and call a method in an action. trackEvent('purchase', [...]) sends any event, trackLead() sends generate_lead, trackNewsletterSignup() sends sign_up with method set to newsletter, and trackCustomEvent('download') sends ga_download.
Does darvis/livewire-google-analytics load Google Analytics or handle cookie consent?
No. The package has no measurement id, no config file and no environment variable, it does not load gtag.js, and it does no consent handling. You put Google’s own tag in your layout or let your consent tool load it. The listener only calls window.gtag when that function exists.
What happens to an event when Google Analytics is blocked or not loaded yet by a cookie banner?
Until window.gtag exists an event waits in memory, at most 50 events and at most 30 minutes, and is sent in order as soon as gtag appears, for example after the visitor gave consent. If gtag never appears the events disappear with the page. Nothing throws, and ['queue' => false] on the include turns the waiting off.
How do I track a GA4 event in a Livewire action that redirects?
Use trackEventAfterRedirect(), trackLeadAfterRedirect(), trackNewsletterSignupAfterRedirect() or trackCustomEventAfterRedirect(). Livewire fires a dispatched browser event on the page that is going away, so these methods keep the event in the session instead and the listener view on the next page sends it once. That page has to include the Blade view; the published JavaScript file cannot read the session.
Why is a Google Analytics event counted twice in my Livewire app?
The usual causes are a tracking call in render(), which runs on every request, the same event tracked with both trackEvent() and trackEventAfterRedirect(), or a published copy of the view or JavaScript file from an older version. Up to 1.1.0 the listener registered again on every wire:navigate visit, and up to 1.3.0 a page from the browser cache sent its carried events again; publish the view again with --force after an update.
Is it safe to put form input in the event parameters, and what does the package store in the browser?
Parameters reach the browser as JSON, and events carried over a redirect are written into the view with every tag character and quote escaped, so input cannot break out of the script. The package sets no cookie and stores one sessionStorage key, livewire-google-analytics.sent, with at most 100 random ids of carried events and no event data. Everything in the parameters does end up in your Google Analytics property, so keep personal data out of them.
How do I test that a Livewire component tracks an event?
Use Livewire’s own assertion, no browser needed: Livewire::test(ContactForm::class)->call('submit')->assertDispatched('ga:event', name: 'generate_lead', params: [...]). For an AfterRedirect method call session()->start() first and assert on session('livewire-google-analytics.events.0.name').
Which Laravel, Livewire and PHP versions does darvis/livewire-google-analytics support, and what does it cost?
PHP 8.2 and higher with Laravel 11, 12 or 13 and Livewire 3 or 4. The package is free and MIT licensed and needs a Google Analytics 4 property with its Google tag on the page. Sites on Laravel 10 or PHP 8.1 stay on version 1.1.x.