PHPackages                             darvis/livewire-google-analytics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. darvis/livewire-google-analytics

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

darvis/livewire-google-analytics
================================

Clean Google Analytics 4 event tracking for Laravel Livewire applications

v1.0.0(3mo ago)02MITPHPPHP ^8.1CI passing

Since Jan 27Pushed 3mo agoCompare

[ Source](https://github.com/ArvidDeJong/livewire-google-analytics)[ Packagist](https://packagist.org/packages/darvis/livewire-google-analytics)[ RSS](/packages/darvis-livewire-google-analytics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Livewire Google Analytics
=========================

[](#livewire-google-analytics)

[![Latest Version on Packagist](https://camo.githubusercontent.com/cf666d90c8c3fb6954ddb864894cf994ec6044d32405e30c8c2c67d5ee41be5c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6461727669732f6c697665776972652d676f6f676c652d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/darvis/livewire-google-analytics)[![Total Downloads](https://camo.githubusercontent.com/bd2092d248f61dcbc566943f2f0e30d8aff7b3c29fc82ef8928dbb3296de567c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6461727669732f6c697665776972652d676f6f676c652d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/darvis/livewire-google-analytics)

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']);
```

Features
--------

[](#features)

- ✅ **Clean API** - No more manual `gtag()` calls in your components
- ✅ **Type-safe** - Full PHP type hints and IDE autocomplete
- ✅ **Secure** - No JavaScript injection vulnerabilities
- ✅ **Zero configuration** - Works out of the box
- ✅ **Livewire 3 &amp; 4** - Full support for both versions
- ✅ **Laravel 10, 11, 12** - Compatible with all modern Laravel versions

Quick Start
-----------

[](#quick-start)

### 1. Install

[](#1-install)

```
composer require darvis/livewire-google-analytics
```

### 2. Add Script to Layout

[](#2-add-script-to-layout)

Add this **once** in your main layout, after `@livewireScripts`:

```
@livewireScripts
@include('livewire-google-analytics::script')
```

### 3. Use in Components

[](#3-use-in-components)

```
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! 🎉

Available Methods
-----------------

[](#available-methods)

### `trackLead()` - Lead Generation

[](#tracklead---lead-generation)

For contact forms, quote requests, demo requests:

```
$this->trackLead([
    'form_name' => 'contact_form',
    'lead_type' => 'contact',
]);
```

### `trackEvent()` - Any GA4 Event

[](#trackevent---any-ga4-event)

For standard GA4 events like purchases, logins:

```
$this->trackEvent('purchase', [
    'transaction_id' => 'T12345',
    'value' => 25.99,
    'currency' => 'EUR',
]);
```

### `trackNewsletterSignup()` - Newsletter Subscriptions

[](#tracknewslettersignup---newsletter-subscriptions)

```
$this->trackNewsletterSignup([
    'source' => 'footer_widget',
]);
```

### `trackCustomEvent()` - Custom Events

[](#trackcustomevent---custom-events)

Automatically adds `ga_` prefix:

```
$this->trackCustomEvent('download_brochure', [
    'brochure_name' => 'Product Catalog 2024',
]);
```

Documentation
-------------

[](#documentation)

📚 **[Complete Documentation](docs/README.md)**

- **[What is this?](docs/01-what-is-this.md)** - Learn what the package does and why
- **[Installation Guide](docs/02-installation.md)** - Detailed installation instructions
- **[Basic Usage](docs/03-basic-usage.md)** - Learn all the methods and best practices
- **[Examples](docs/04-examples.md)** - 7 complete real-world examples
- **[Testing](docs/05-testing.md)** - How to verify your tracking works
- **[Troubleshooting](docs/06-troubleshooting.md)** - Common issues and solutions

📖 **[Quick Start Guide](QUICK_START.md)** - 5-minute beginner-friendly guide

How It Works
------------

[](#how-it-works)

```
PHP Component → Livewire Event → JavaScript Listener → Google Analytics

```

1. You call `$this->trackLead([...])` in your Livewire component
2. The trait dispatches a browser event with the data
3. The JavaScript listener forwards it to `gtag()`
4. Google Analytics receives and processes the event

**Benefits:**

- ✅ Clean separation of PHP and JavaScript
- ✅ No JavaScript injection vulnerabilities
- ✅ Works with async GA4 loading
- ✅ Fails silently if GA4 is blocked

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- Laravel 10, 11, or 12
- Livewire 3 or 4
- Google Analytics 4 property

Testing
-------

[](#testing)

**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.

[Learn more about testing →](docs/05-testing.md)

Examples
--------

[](#examples)

### Contact Form

[](#contact-form)

```
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;
    }
}
```

### E-commerce Purchase

[](#e-commerce-purchase)

```
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');
    }
}
```

[See 7 complete examples →](docs/04-examples.md)

Best Practices
--------------

[](#best-practices)

✅ **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

[Learn all best practices →](docs/03-basic-usage.md#best-practices)

Troubleshooting
---------------

[](#troubleshooting)

**Events not firing?**

- Check if script is after `@livewireScripts`
- Verify trait is added to component
- Look for JavaScript errors in console

**Events fire multiple times?**

- Don't call tracking in `render()` or `mount()`
- Only track in action methods

[See all troubleshooting solutions →](docs/06-troubleshooting.md)

Contributing
------------

[](#contributing)

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- **[Arvid de Jong](https://github.com/darvis)** - Creator and maintainer
- **[All Contributors](../../contributors)**

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

Support
-------

[](#support)

- 📧 **Email:**
- 🐛 **Issues:** [GitHub Issues](../../issues)
- 📖 **Documentation:** [docs/README.md](docs/README.md)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance79

Regular maintenance activity

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

111d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/24c445b7580e09ff72b8340d1423886148c4c8a249d0a828c98285109e7e5663?d=identicon)[darvis](/maintainers/darvis)

---

Top Contributors

[![ArvidDeJong](https://avatars.githubusercontent.com/u/7394837?v=4)](https://github.com/ArvidDeJong "ArvidDeJong (1 commits)")

---

Tags

laraveleventstrackinglivewireanalyticsgoogle-analyticsga4

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/darvis-livewire-google-analytics/health.svg)

```
[![Health](https://phpackages.com/badges/darvis-livewire-google-analytics/health.svg)](https://phpackages.com/packages/darvis-livewire-google-analytics)
```

###  Alternatives

[livewire/flux

The official UI component library for Livewire.

9385.0M86](/packages/livewire-flux)[bezhansalleh/filament-google-analytics

Google Analytics integration for FilamentPHP

205144.8k5](/packages/bezhansalleh-filament-google-analytics)[mediconesystems/livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS

1.2k711.3k8](/packages/mediconesystems-livewire-datatables)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[cornford/googlitics

An easy way to integrate Google Analytics with Laravel.

3310.2k](/packages/cornford-googlitics)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
