sitruc/keenio

Keen.io API 的 Laravel 包装器,方便使用

1.0.11 2019-12-02 23:23 UTC

This package is not auto-updated.

Last update: 2024-09-17 21:41:50 UTC


README

Latest Version Software License Total Downloads

Keen.io PHP SDK 的 Laravel 包装器,方便使用

使用此包,您可以在 Laravel 中优雅地发送事件到 keen.io。以下是一些从基本到优雅的示例。

最简单的示例,使用门面,接受 KeenEvent 或名称和数据直接。

use KeenIO;

KeenIO::addEvent('New Event', ['key' => 'value']);

或传递 KeenEvent

use KeenIO;
use Sitruc\KeenIO\KeenEvent

$event = new KeenEvent('New Event', ['key' => 'value']);

KeenIO::addEvent($event);

如果您愿意,可以直接发送事件。

use Sitruc\KeenIO\KeenEvent;

$event = new KeenEvent('New Event', ['key' => 'value']);

$event->send();

事件排队。

use Sitruc\KeenIO\KeenEvent;

$event = new KeenEvent('New Queued Event', ['key' => 'value']);

$event->queued()->send();

使用 keen.io 的数据丰富功能。

use Sitruc\KeenIO\KeenEvent;

$event = new KeenEvent('New Event', ['key' => 'value']);

//Enriches keen's default keen.timestamp value into enriched_timestamp
$event->enrichDatetime();

$event->enrichDatetime('some.other.timestamp.source', 'new.enriched.location');

$event->send();

方法流畅,所以上述示例可以变为。

use Sitruc\KeenIO\KeenEvent;

$event = new KeenEvent('Keen Event', ['key' => 'value']);

$event->enrichDatetime()
      ->enrichDatetime('some.other.timestamp.source', 'new.enriched.location')
      ->send();

以下 enrichments 值被接受。您可以在此处了解更多关于数据丰富功能(也称为“附加功能”)的信息 在 keen.io 文档中

public function enrichDatetime($source = 'keen.timestamp', $destination = 'enriched_timestamp')

public function enrichIPAddress($source, $destination = 'ip_geo_info')

public function enrichUserAgent($source, $destination = 'parsed_user_agent')

public function enrichURL($source, $destination)

public function enrichReferrer($referrer_url_input, $page_url_input, $destination)

继承 KeenEvent 可以更强大。

幕后,此包使用 Laravel 的调度处理程序。这意味着您可以实现 ShouldQueue 接口,并将任务调度到特定的队列中,就像您调度任何其他工作一样。

以下是一个在 keenio 队列中报告注册事件并丰富 created_at 和 upgrade_date 的示例。简单而优雅!

<?php

namespace App\Http\Controllers;

use App\KeenEvents\NewRegistration;

class UserController
{
    public function registerUser()
    {
        //Register the user.
        NewRegistration::from($user)->send();
    }
}
<?php

namespace App\KeenEvents;

use Sitruc\KeenIO\KeenEvent;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class NewRegistration extends KeenEvent implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;
    
    protected $keenTitle = 'New Registration';
    
    protected $user;
  
    public function __construct($user)
    {
        $this->user = $user;

        $this->onQueue('keenio')
             ->enrichDatetime()
             ->enrichDatetime('upgrade_date', 'enriched_upgrade_date');
    }

    public static function from($user)
    {
        return new static($user);
    }

    public function keenData()
    {
        return [
            'account_type' => $this->user->accountType,
            'upgrade_date' => $this->user->upgradeDate->toAtomString(),
        ];
    }
}

安装

您可以通过 Composer 安装此包。

composer require sitruc/keenio

您必须安装此服务提供者。

// config/app.php
'providers' => [
    ...
    Sitruc\KeenIO\KeenServiceProvider::class,
    ...
];

此包还包含一个门面,它提供了一种简单的方法来调用该类。

// config/app.php
'aliases' => [
    ...
    'KeenIO' => Sitruc\KeenIO\Facades\KeenIO::class,
    ...
];

此包要求您将 project_idwrite_keyread_keyenabled 添加到 services 配置文件中。

// config/services.php
<?php
return [
    ...
    'keenio' => [
        'project_id' => env('KEENIO_PROJECT_ID'),
        'write_key'  => env('KEENIO_WRITE_KEY'),
        'read_key'   => env('KEENIO_READ_KEY'),
        'enabled'    => env('KEENIO_ENABLED'),
    ],
    ...
]

enabled 标志在您想在某些环境中关闭 keen 报告时很有用。

测试

使用以下命令运行测试

vendor/bin/phpunit

composer test

安全

如果您发现任何与安全相关的问题,请通过电子邮件 cthorne@me.com 而不是使用问题跟踪器来报告。

鸣谢

许可

MIT 许可证(MIT)。请参阅 许可文件 以获取更多信息。