hashcode7/laravel-amplitude-analytics

Laravel 与 Amplitude 交互的包。

v4.0.1 2022-07-07 22:18 UTC

This package is not auto-updated.

Last update: 2024-09-28 07:16:30 UTC


README

Latest Version on Packagist Build Status Scrutinizer Code Quality Total Downloads

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

此包与 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)。有关更多信息,请参阅 许可文件