enmaboya/laravel-amplitude

用于与 Amplitude 一起工作的 Laravel 扩展包。

3.1 2021-08-25 09:23 UTC

This package is auto-updated.

Last update: 2022-07-10 08:43:03 UTC


README

支持的分支,设置已修正;

如果您需要跟踪 Laravel 应用程序的事件到 Amplitude,此扩展包将成为您最好的朋友。

此扩展包与 Laravel 6.0 及更高版本兼容。

安装

您可以通过 composer 安装此扩展包。

composer require enmaboya/laravel-amplitude

不要忘记使用 Artisan 发布配置文件。

php artisan vendor:publish --provider="LaravelAmplitude\Providers\LaravelAmplitudeServiceProvider"

要启动,只需在 .env 文件中添加您的项目 Amplitude API 密钥,使用 AMPLITUDE_API_KEY 作为键。

如果您想使用 Amplitude 门面,请记住在 config/app.phpaliases 项目中添加以下行。

'aliases' => [
    ...

    'Amplitude' => LaravelAmplitude\Facades\Amplitude::class
]

使用方法

Laravel Amplitude 使用简单的语法轻松跟踪您的产品事件。

设置用户 ID

首先,在发送任何内容之前,您需要设置用户 ID。

Amplitude::setUserId('user_id');

注意:设置用户 ID 是必需的。否则,当尝试向 Amplitude 发送数据时,您将收到错误。

发送事件

一旦设置了用户 ID,您就可以准备向您的 Amplitude 项目发送事件。

// simple sending...
Amplitude::sendEvent('app_opened');

// sending with properties...
Amplitude::sendEvent('subscription_paid', ['was_trial' => true]);

此外,您还可以使用专用方法 setUserProperties 更改用户属性

// properties new values are set here
Amplitude::setUserProperties([
    'trial' => false,
    'plan' => 'professional'
]);

// data is sent to Amplitude here
Amplitude::sendEvent('subscription_paid', ['was_trial' => true]);

重要:属性将在下一次 sendEvent 调用时发送到 Amplitude。如果没有其他 sendEvent 调用,则新的用户属性将不会被保存。

队列事件

如果您要发送大量事件并且希望保持良好的性能,您可以选择事件队列而不是您刚刚看到的基本发送。

使用事件队列,您将在请求结束后发送所有事件,而不是在请求生命周期中多次进行 API 调用。

要使用它,只需将您的 sendEvent 调用切换到 queueEvent 方法。

// simple sending...
Amplitude::queueEvent('app_opened');

// sending with properties...
Amplitude::queueEvent('subscription_paid', ['was_trial' => true]);

无需做更多!当请求结束时,Laravel Amplitude 将自动触发您的数据发送操作。

然而,如果您想要更多的控制,并且希望在您的代码中发送队列中的事件,您可以使用 sendQueuedEvents 方法的调用手动完成。

// queueing an event...
Amplitude::queueEvent('app_opened');

// queueing another event...
Amplitude::queueEvent('subscription_paid', ['was_trial' => true]);

// send them!
Amplitude::sendQueuedEvents();

安全

如果您发现任何安全相关的问题,请通过电子邮件 vita-dubov@yandex.ru 联系,而不是使用问题跟踪器。

许可证

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