viezel / laravel-amplitude
一个用于与 Amplitude 交互的 Laravel 扩展包。
1.1.2
2024-03-08 15:52 UTC
Requires
- php: ^8.2|^8.3
- illuminate/support: ^9.0|^10.0|^11.0
- viezel/amplitude-php-sdk: ^1.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^8.0|^9.0|^10.0|^11.0
README
安装
composer require viezel/laravel-amplitude
AMPLITUDE_API_URL
是基于 US 还是 EU。请注意。
AMPLITUDE_API_KEY= AMPLITUDE_API_URL=https://api.eu.amplitude.com/2/httpapi
'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();