antwerpes / laravel-event-store
Store for tracking events in laravel applications
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- chiiya/laravel-code-style: ^3.0
- nunomaduro/collision: ^7.8|^8.0
- orchestra/testbench: ^8.8|^9.0
This package is auto-updated.
Last update: 2024-12-07 13:22:53 UTC
README
Simple store for tracking events (e.g. for Google Tag Manager) in Laravel. Fire events from anywhere in your application and later retrieve them in your frontend.
Installation
You can install the package via composer:
composer require antwerpes/laravel-event-store
You must also add the middleware to your web
group, at the end of the stack:
protected $middlewareGroups = [ 'web' => [ ... \Antwerpes\LaravelEventStore\Middleware\FlashEventStore::class, ], ];
You can optionally publish the config file with:
php artisan vendor:publish --tag="laravel-event-store-config"
This is the contents of the published config file:
return [ 'session_key' => '_eventStore', ];
Usage
From anywhere in your application, you can fire events like this:
use Antwerpes\LaravelEventStore\Facades\EventStore; EventStore::push('event-name'); // Or with additional data EventStore::push('event-name', ['foo' => 'bar']);
Events that you don't retrieve during the current request cycle will be flashed to the session and made available to the next request. That way, you can also fire events and retrieve them after a redirect.
// This will work EventStore::push('event-name'); return view('some-view'); // This will also work EventStore::push('event-name'); return redirect()->route('some-route');
In your frontend, you can dump the events like this:
{!! EventStore::dumpForGTM() !!}
which will output something like this:
<script> window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event': 'event-name', 'foo': 'bar' }); </script>
In case you want to use a different variable name, you can pass it as a parameter:
{!! EventStore::dumpForGTM('myDataLayer') !!}
You can also pull the events as an array and use them however you like:
EventStore::pullEvents();
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Leave an issue on GitHub, or create a Pull Request.
Credits
License
The MIT License (MIT). Please see License File for more information.