antwerpes/laravel-event-store

用于跟踪 Laravel 应用程序中的事件存储

1.1.0 2024-06-07 12:12 UTC

README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

Laravel 中跟踪事件(例如 Google Tag Manager)的简单存储。您可以从应用程序的任何位置触发事件,并在稍后在前端检索它们。

安装

您可以通过 composer 安装此包

composer require antwerpes/laravel-event-store

您还必须在 web 组中添加中间件,并将其放在栈的末尾

protected $middlewareGroups = [
    'web' => [
        ...
        \Antwerpes\LaravelEventStore\Middleware\FlashEventStore::class,
    ],
];

您可以选择使用以下命令发布配置文件:

php artisan vendor:publish --tag="laravel-event-store-config"

这是发布配置文件的内容

return [
    'session_key' => '_eventStore',
];

使用方法

您可以从应用程序的任何位置触发事件,例如这样:

use Antwerpes\LaravelEventStore\Facades\EventStore;

EventStore::push('event-name');
// Or with additional data
EventStore::push('event-name', ['foo' => 'bar']);

在当前请求周期内未检索的事件将被闪存到会话中,并可供下一次请求使用。这样,您也可以在重定向后触发事件并检索它们。

// This will work
EventStore::push('event-name');
return view('some-view');

// This will also work
EventStore::push('event-name');
return redirect()->route('some-route');

在您的客户端,您可以这样输出事件:

{!! EventStore::dumpForGTM() !!}

这将输出类似以下内容:

<script>
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
        'event': 'event-name',
        'foo': 'bar'
    });
</script>

如果您想使用不同的变量名,您可以将其作为参数传递

{!! EventStore::dumpForGTM('myDataLayer') !!}

您还可以将事件作为数组检索,并按需使用它们

EventStore::pullEvents();

更新日志

请参阅 更新日志 了解最近的变化。

贡献

欢迎贡献!在 GitHub 上留下一个问题,或创建一个拉取请求。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件