script-artist/laravel-amplitude-analytics

一个用于与Amplitude协同工作的Laravel包。

dev-master 2023-06-07 17:45 UTC

This package is auto-updated.

Last update: 2024-09-07 20:37:12 UTC


README

Latest Version on Packagist Build Status Scrutinizer Code Quality Total Downloads

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

此包与Laravel 5.8版本兼容。

安装

您可以通过composer安装此包。

composer require hashcode7/laravel-amplitude-analytics

别忘了使用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)。有关更多信息,请参阅许可证文件