faladu/laravel-amplitude-v2

这是我的包 laravel-amplitude-v2

dev-master 2023-09-23 09:59 UTC

This package is auto-updated.

Last update: 2024-09-23 12:05:15 UTC


README

Latest Version on Packagist Build Status Scrutinizer Code Quality Total Downloads

如果您需要使用Amplitude版本2跟踪Laravel应用程序的事件,则此包将成为您的最佳拍档

此包与Laravel 5.8及更高版本兼容。

安装

您可以通过composer安装此包

composer require Parthfaladu/laravel-amplitude-v2

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

artisan vendor:publish

为了正常运行,只需在.env文件中将您项目的Amplitude API Key添加到其中,使用AMPLITUDE_API_KEY作为键。

如果您想使用Amplitude外观,请记住在config/app.php中的aliases项中添加以下行。

'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();

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何安全问题,请通过电子邮件parth.fadadu@hotmail.com报告,而不是使用问题跟踪器。

鸣谢

许可

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