fingoweb/laravel-amplitude

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

4.1.0 2024-06-05 14:04 UTC

This package is not auto-updated.

Last update: 2024-09-25 15:25:49 UTC


README

Latest Version on Packagist Build Status Scrutinizer Code Quality Total Downloads

如果您需要在Amplitude中跟踪Laravel应用程序的事件,则此包将成为您的最佳伴侣。

此包与Laravel 5.8版本兼容。

安装

您可以通过composer安装此包

composer require francescomalatesta/laravel-amplitude

别忘了使用Artisan发布配置文件

artisan vendor:publish

要开始运行,只需在.env文件中添加您项目的Amplitude API密钥,使用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

安全

如果您发现任何安全问题,请通过francesco@ahia.store发送电子邮件,而不是使用问题跟踪器。

致谢

许可

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