PHPackages                             eg-mohamed/laravel-ga4-events - 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. eg-mohamed/laravel-ga4-events

ActiveLibrary

eg-mohamed/laravel-ga4-events
=============================

A Laravel package to define and dispatch Google Analytics 4 (GA4) events.

1.0(1mo ago)00MITPHPPHP ^8.2CI passing

Since Mar 29Pushed 1mo agoCompare

[ Source](https://github.com/EG-Mohamed/Laravel-Ga4-Events)[ Packagist](https://packagist.org/packages/eg-mohamed/laravel-ga4-events)[ Docs](https://github.com/eg-mohamed/laravel-ga4-events)[ GitHub Sponsors](https://github.com/MohamedSaid)[ RSS](/packages/eg-mohamed-laravel-ga4-events/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (2)Used By (0)

Laravel GTM Events
==================

[](#laravel-gtm-events)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a110b1df4bd0066fb8a95d9f4338d065ae8c99e63e2ff1c6f9c778e33ca12c44/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65672d6d6f68616d65642f6c61726176656c2d6761342d6576656e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/eg-mohamed/laravel-ga4-events)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b8873cc0925ec87cb362f9506de2e6212877b7c924cd0cf8a6c3e343d5ef2144/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f65672d6d6f68616d65642f6c61726176656c2d6761342d6576656e74732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/eg-mohamed/laravel-ga4-events/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/4e8152e292951126d6a9e673de300e175a6a14e644ae1d9793c4aab09ae457a3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f65672d6d6f68616d65642f6c61726176656c2d6761342d6576656e74732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/eg-mohamed/laravel-ga4-events/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/6ca1bb3d092c7bb7cd845ff8cc5e50b09cad173c01910d3ad9a36dd964c71f4e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65672d6d6f68616d65642f6c61726176656c2d6761342d6576656e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/eg-mohamed/laravel-ga4-events)

Push events to Google Tag Manager's `dataLayer` and Meta Pixel from Laravel, Livewire, and plain JavaScript through one unified bridge. The package injects the tracking snippets, validates payloads, and provides browser-side debug output through `console.log`.

Features
--------

[](#features)

- Push events to `window.dataLayer` for GTM to handle
- Auto-injects the GTM container snippet
- Auto-injects the Meta Pixel base snippet
- Client-side payload validation before pushing
- Works from JavaScript, DOM custom events, and Livewire
- Blade component and directive for easy layout injection
- Console debug output

Compatibility
-------------

[](#compatibility)

- PHP: `^8.3`
- Laravel: `12.x`, `13.x`

Installation
------------

[](#installation)

```
composer require eg-mohamed/laravel-ga4-events
```

Publish the config:

```
php artisan vendor:publish --tag="ga4-events-config"
```

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

[](#quick-start)

1. Set your GTM container ID in `.env`:

```
GTM_CONTAINER_ID=GTM-XXXXXXX
GTM_EVENTS_ENABLED=true
```

Optional Meta Pixel support:

```
META_PIXEL_ID=123456789012345
META_PIXEL_INJECT_SCRIPT=true
```

2. Inject the bridge once in your layout (before ``):

```

```

Legacy directive also works:

```
@ga4Events
```

3. Push events from JavaScript:

```
window.GTMEvents.track('purchase_started', {
    currency: 'USD',
    value: 99.95,
    item_count: 2,
})
```

GTM receives `{event: 'purchase_started', currency: 'USD', value: 99.95, item_count: 2}` in `dataLayer`. If Meta Pixel is configured, the same payload is also sent through `fbq`, using `track` for standard Meta events and `trackCustom` for custom ones.

Livewire Usage
--------------

[](#livewire-usage)

```
$this->dispatch('gtm-event', name: 'profile_updated', params: ['section' => 'security']);
```

The bridge normalizes wrapped Livewire payload shapes automatically.

JavaScript API
--------------

[](#javascript-api)

### `track(name, params = {})`

[](#trackname-params--)

```
window.GTMEvents.track('add_to_cart', { product_id: 15, value: 150 })
```

### `dispatch(payload, source = 'manual')`

[](#dispatchpayload-source--manual)

```
window.GTMEvents.dispatch({ name: 'search', params: { term: 'sneakers' } })
```

### DOM custom event

[](#dom-custom-event)

```
window.dispatchEvent(new CustomEvent('gtm:event', {
    detail: { name: 'checkout_step', params: { step: 2 } },
}))
```

### `config`

[](#config)

```
console.log(window.GTMEvents.config)
```

Payload Structure
-----------------

[](#payload-structure)

```
{
    "name": "event_name",
    "params": {
        "key": "value"
    }
}
```

Validation rules:

- `name` must be a non-empty string matching `allowed_name_pattern`
- `name` must not exceed `max_event_name_length`
- `params` must be an object
- param keys validated for length
- param values support `string`, `number`, `boolean`, `null`, or nested objects
- string values truncated to `max_param_value_length`

Configuration
-------------

[](#configuration)

Published at `config/ga4-events.php`:

```
return [
    'enabled' => (bool) env('GTM_EVENTS_ENABLED', true),
    'container_id' => env('GTM_CONTAINER_ID'),
    'inject_gtm_script' => (bool) env('GTM_EVENTS_INJECT_SCRIPT', true),
    'meta_pixel_id' => env('META_PIXEL_ID'),
    'inject_meta_pixel_script' => (bool) env('META_PIXEL_INJECT_SCRIPT', true),
    'meta_pixel_event_map' => [],
    'meta_pixel_standard_events' => [
        'AddPaymentInfo',
        'AddToCart',
        'AddToWishlist',
        'CompleteRegistration',
        'Contact',
        'CustomizeProduct',
        'Donate',
        'FindLocation',
        'InitiateCheckout',
        'Lead',
        'Purchase',
        'Schedule',
        'Search',
        'StartTrial',
        'SubmitApplication',
        'Subscribe',
        'ViewContent',
    ],
    'event_bus_name' => env('GTM_EVENTS_EVENT_BUS_NAME', 'gtm:event'),
    'livewire_event_name' => env('GTM_EVENTS_LIVEWIRE_EVENT_NAME', 'gtm-event'),
    'global_js_object' => env('GTM_EVENTS_GLOBAL_JS_OBJECT', 'GTMEvents'),
    'debug' => (bool) env('GTM_EVENTS_DEBUG', false),
    'strict_validation' => (bool) env('GTM_EVENTS_STRICT_VALIDATION', false),
    'drop_invalid_events' => (bool) env('GTM_EVENTS_DROP_INVALID_EVENTS', true),
    'max_event_name_length' => (int) env('GTM_EVENTS_MAX_EVENT_NAME_LENGTH', 40),
    'max_params' => (int) env('GTM_EVENTS_MAX_PARAMS', 25),
    'max_param_key_length' => (int) env('GTM_EVENTS_MAX_PARAM_KEY_LENGTH', 40),
    'max_param_value_length' => (int) env('GTM_EVENTS_MAX_PARAM_VALUE_LENGTH', 100),
    'max_param_nesting' => (int) env('GTM_EVENTS_MAX_PARAM_NESTING', 4),
    'allowed_name_pattern' => env('GTM_EVENTS_ALLOWED_NAME_PATTERN', '/^[a-zA-Z][a-zA-Z0-9_]*$/'),
    'console_prefix' => env('GTM_EVENTS_CONSOLE_PREFIX', '[GTM Events]'),
];
```

### Meta Pixel event mapping

[](#meta-pixel-event-mapping)

Map your existing event names to Meta standard events when needed:

```
'meta_pixel_event_map' => [
    'purchase_completed' => 'Purchase',
    'checkout_started' => 'InitiateCheckout',
    'product_viewed' => 'ViewContent',
],
```

When the mapped event name matches one of `meta_pixel_standard_events`, the package sends it with `fbq('track', ...)`; otherwise it falls back to `fbq('trackCustom', ...)`.

Debug Mode
----------

[](#debug-mode)

```
GTM_EVENTS_DEBUG=true
```

Example console output:

```
[GTM Events] [INFO] GTM bridge initialized.
[GTM Events] [INFO] Event pushed to dataLayer from api.
[GTM Events] [ERROR] Invalid GTM payload from livewire.

```

Artisan Command
---------------

[](#artisan-command)

```
php artisan gtm-events:check
```

Validates that the package is enabled and shows the current GTM and Meta Pixel identifiers.

Skip Auto Injection
-------------------

[](#skip-auto-injection)

If you load the GTM snippet yourself:

```
GTM_EVENTS_INJECT_SCRIPT=false
```

The bridge still pushes events to `window.dataLayer`.

PHP-side Validation
-------------------

[](#php-side-validation)

```
use MohamedSaid\LaravelGa4Events\Facades\LaravelGa4Events;

$result = LaravelGa4Events::track('purchase_completed', ['value' => 120]);

// $result['valid']  — bool
// $result['errors'] — array of strings
// $result['name']   — sanitized name
// $result['params'] — sanitized params
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for information about changes.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for contribution details.

Security
--------

[](#security)

Please review [our security policy](../../security/policy) for reporting vulnerabilities.

Credits
-------

[](#credits)

- [Mohamed Said](https://github.com/EG-Mohamed)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance91

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

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

45d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23424932?v=4)[Mohamed Said](/maintainers/EG-Mohamed)[@EG-Mohamed](https://github.com/EG-Mohamed)

---

Top Contributors

[![EG-Mohamed](https://avatars.githubusercontent.com/u/23424932?v=4)](https://github.com/EG-Mohamed "EG-Mohamed (26 commits)")

---

Tags

data-layerga4google-analytics-4google-tag-managerlaravel-packagelaravelMohamedSaidlaravel-ga4-events

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/eg-mohamed-laravel-ga4-events/health.svg)

```
[![Health](https://phpackages.com/badges/eg-mohamed-laravel-ga4-events/health.svg)](https://phpackages.com/packages/eg-mohamed-laravel-ga4-events)
```

###  Alternatives

[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[spatie/laravel-health

Monitor the health of a Laravel application

85810.0M83](/packages/spatie-laravel-health)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[spatie/laravel-prometheus

Export Laravel metrics to Prometheus

2651.3M6](/packages/spatie-laravel-prometheus)[christophrumpel/missing-livewire-assertions

This package adds missing livewire test assertions.

149336.0k9](/packages/christophrumpel-missing-livewire-assertions)

PHPackages © 2026

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