drhttp/moesif-laravel

Moesif 集成/Laravel 数据采集中间件


README

Built For Latest Version Total Downloads Software License Source Code

GitHub 上的源代码

为 PHP Laravel (> 5.1) 提供中间件,可自动记录 API 调用并发送至 Moesif 进行 API 分析和日志分析

Laravel 4.2

为 Laravel 4.2 提供了 Moesif SDK。感谢 jonnypickett 创建它。

如何安装

通过 Composer

$ composer require moesif/moesif-laravel

或将 'moesif/moesif-laravel' 添加到您的 composer.json 文件中。

如何使用

添加服务提供者


// In config/app.php

'providers' => [
  /*
   * Application Service Providers...
   */
    Moesif\Middleware\MoesifLaravelServiceProvider::class,
];

添加到中间件

如果网站根是您的 API,则添加到根级别


// In App/Http/Kernel.php

protected $middleware = [
  /*
   * The application's global HTTP middleware stack.
   *
   * These middleware are run during every request to your application.
   */
   \Moesif\Middleware\MoesifLaravel::class,
];

如果 API 在特定的路由组中,则添加到您的路由组

// In App/Http/Kernel.php

protected $middlewareGroups = [
  /**
   * The application's API route middleware group.
   */
   'api' => [
        //
        \Moesif\Middleware\MoesifLaravel::class,
    ],
];

要跟踪特定路由,请使用特定路由的中间件设置。

发布包配置文件

$ php artisan vendor:publish --provider="Moesif\Middleware\MoesifLaravelServiceProvider"

设置配置

编辑 config/moesif.php 文件。


// In config/moesif.php

return [
    //
    'applicationId' => 'Your Moesif Application Id',
    'logBody' => true,
];

您的 Moesif 应用程序 ID 可在 Moesif 门户 中找到。注册 Moesif 账户后,您的 Moesif 应用程序 ID 将在入门步骤中显示。

您可以在任何时间通过登录到 Moesif 门户,点击右上角菜单,然后点击 安装 来找到您的 Moesif 应用程序 ID。

有关其他配置选项,请见下文。

配置选项

您可以在 config/moesif.php 文件中定义 Moesif 配置选项。其中一些字段是函数。

applicationId

类型:String 必须的,一个标识您的应用程序的字符串。

identifyUserId

类型:($request, $response) => String 可选的,一个函数,它接受 $request 和 $response 并返回一个用于 userId 的字符串。Moesif 自动通过 $request->user()['id'] 获取终端 userId,如果您使用非标准方式将用户注入到 $request 或希望覆盖 userId,您可以使用 identifyUserId 来完成。


// In config/moesif.php

$identifyUserId = function($request, $response) {
    // Your custom code that returns a user id string
    $user = $request->user();
    if ($request->user()) {
        return $user->id;
    }
    return NULL;
};
return [
  //
  'identifyUserId' => $identifyUserId
];

identifyCompanyId

类型:($request, $response) => String 可选的,一个函数,它接受 $request 和 $response 并返回一个用于 companyId 的字符串。


// In config/moesif.php

$identifyCompanyId = function($request, $response) {
    # Your custom code that returns a company id string
    return '67890';
};
return [
  //
  'identifyCompanyId' => $identifyCompanyId
];

identifySessionId

类型:($request, $response) => String 可选的,一个函数,它接受 $request 和 $response 并返回一个用于 sessionId 的字符串。Moesif 通过处理您的数据自动会话化,但您可以通过 identifySessionId 覆盖此结果,如果您对结果不满意。

getMetadata

类型:($request, $response) => Associative Array 可选的,一个函数,它接受 $request 和 $response 并返回 $metdata,它是 JSON 的关联数组表示形式。


// In config/moesif.php

$getMetadata = function($request, $response) {
  return array("foo"=>"laravel example", "boo"=>"custom data");
};

return [
  //
  'getMetadata' => $getMetadata
];

apiVersion

类型:String 可选的,一个字符串,用于指定 API 版本,例如 1.0.1,以便更容易进行过滤。

maskRequestHeaders

类型:$headers => $headers 可选的,一个函数,它接受 $headers,它是一个关联数组,并返回一个关联数组,其中已删除/遮蔽了敏感的标题。

// In config/moesif.php

$maskRequestHeaders = function($headers) {
    $headers['password'] = '****';
    return $headers;
};

return [
  //
  'maskRequestHeaders' => $maskRequestHeaders
];

maskRequestBody

类型:$body => $body 可选的,一个函数,它接受 $body,它是 JSON 的关联数组表示形式,并返回一个关联数组,其中已删除任何信息。


// In config/moesif.php

$maskRequestBody = function($body) {
    // remove any sensitive information.
    $body['password'] = '****';
    return $body;
};

return [
  //
  'maskRequestBody' => $maskRequestBody
];

maskResponseHeaders

类型: $headers => $headers 可选,与上面相同,但用于响应。

maskResponseBody

类型: $body => $body 可选,与上面相同,但用于响应。

skip

类型: ($request, $response) => String 可选,一个函数,它接受一个请求和一个响应,如果这个API调用不应该发送到Moesif则返回true。

debug

类型: Boolean 可选,如果为true,将使用 Illuminate\Support\Facades\Log 打印调试信息。

logBody

类型: Boolean 可选,默认为true,设置为false将不会将请求和响应体记录到Moesif。

更新单个用户

在Moesif中创建或更新用户资料。元数据字段可以是任何客户人口统计信息或其他您想要存储的信息。只需要 user_id 字段。

use Moesif\Middleware\MoesifLaravel;

// Only userId is required.
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
// metadata can be any custom object
$user = array(
    "user_id" => "12345",
    "company_id" => "67890", // If set, associate user with a company object
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "email" => "john@acmeinc.com",
        "first_name" => "John",
        "last_name" => "Doe",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "mary@contoso.com"
        )
    )
);

$middleware = new MoesifLaravel();
$middleware->updateUser($user);

元数据字段可以是您想要在用户上设置的任何自定义数据。需要 user_id 字段。

批量更新用户

与updateUser类似,但用于在单个批处理中更新用户列表。只需要 user_id 字段。

use Moesif\Middleware\MoesifLaravel;

$userA = array(
    "user_id" => "12345",
    "company_id" => "67890", // If set, associate user with a company object
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "email" => "john@acmeinc.com",
        "first_name" => "John",
        "last_name" => "Doe",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "mary@contoso.com"
        )
    )
);

$userB = array(
    "user_id" => "12345",
    "company_id" => "67890", // If set, associate user with a company object
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "email" => "john@acmeinc.com",
        "first_name" => "John",
        "last_name" => "Doe",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "mary@contoso.com"
        )
    )
);

$users = array($userA);

$middleware = new MoesifLaravel();
$middleware->updateUsersBatch($users);

元数据字段可以是您想要在用户上设置的任何自定义数据。需要 user_id 字段。

更新单个公司

在Moesif中创建或更新公司资料。元数据字段可以是任何公司人口统计信息或其他您想要存储的信息。只需要 company_id 字段。

use Moesif\Middleware\MoesifLaravel;

// Only companyId is required.
// Campaign object is optional, but useful if you want to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
// metadata can be any custom object
$company = array(
    "company_id" => "67890",
    "company_domain" => "acmeinc.com", // If domain is set, Moesif will enrich your profiles with publicly available info 
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "org_name" => "Acme, Inc",
        "plan_name" => "Free",
        "deal_stage" => "Lead",
        "mrr" => 24000,
        "demographics" => array(
            "alexa_ranking" => 500000,
            "employee_count" => 47
        )
    )
);

$middleware = new MoesifLaravel();
$middleware->updateCompany($company);

元数据字段可以是您想要在公司上设置的任何自定义数据。需要 company_id 字段。

批量更新公司

与update_company类似,但用于在单个批处理中更新公司列表。只需要 company_id 字段。

use Moesif\Middleware\MoesifLaravel;

$companyA = array(
    "company_id" => "67890",
    "company_domain" => "acmeinc.com", // If domain is set, Moesif will enrich your profiles with publicly available info 
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "org_name" => "Acme, Inc",
        "plan_name" => "Free",
        "deal_stage" => "Lead",
        "mrr" => 24000,
        "demographics" => array(
            "alexa_ranking" => 500000,
            "employee_count" => 47
        )
    )
);

$companies = array($companyA);

$middleware = new MoesifLaravel();
$middleware->updateCompaniesBatch($companies);

元数据字段可以是您想要在公司上设置的任何自定义数据。需要 company_id 字段。

Moesif Laravel SDK的积分

  • 队列和通过分叉的非阻塞进程发送数据的部分基于Mixpanel的PHP客户端代码,该代码在Apache License,版本2.0下开源。

其他提示

  • 发送数据的方式是通过使用exec()和cURL命令的派生(即非阻塞方式)。Php exec() 命令可能成功,但cURL本身可能存在401错误。因此,集成后,如果您在Moesif仪表板中看不到事件和数据,请打开调试选项,然后cURL命令本身将被记录。您可以执行该cURL命令并查看问题所在。最常见的问题是检查应用程序ID是否设置正确。

使用Moesif集成的Laravel应用程序进行测试

Moesif Laravel测试

一个集成了Moesif的Laravel应用程序示例

Moesif Laravel示例

其他集成

要查看更多关于集成选项的文档,请访问 集成选项文档.