printu / customerio

Customer.io 的 PHP API

3.5.3 2023-06-15 07:36 UTC

README

Customer.io API 的 PHP 绑定。

API 文档

Actions Status Code Climate Test Coverage

提供两个主要的 API 主机用于集成:

行为跟踪

https://track.customer.io/api/v1/
行为跟踪 API 用于通过 Customer.io 识别和跟踪客户数据。

API

https://api.customer.io/v1/api/
API 允许您从您的 Customer.io 账户中读取数据,用于在您的后端系统中的自定义工作流程或用于报告目的。

安装

API 客户端可以通过 Composer 安装。

在您的 composer.json 文件中

{
    "require": {
        "printu/customerio": "~3.0"
    }
}

一旦创建 composer.json 文件,您就可以运行 composer install 来进行初始包安装,并运行 composer update 来更新到 API 客户端的最新版本。

该客户端使用 Guzzle

基本用法

请记住在您的应用程序中包含 Composer 自动加载器

<?php
require_once 'vendor/autoload.php';

// Application code...
?>

在创建客户端时配置您的访问凭证

<?php
use Customerio\Client;

$client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID');

/*
 * To authenticate, provide your key as a Bearer token in a HTTP Authorization header.
 * You can create and manage your API keys by visiting your App API Keys page directly or by clicking the Integrations
 *  link in the left-hand menu of your Customer.io account and choosing Customer.io API > Manage API Credentials > App API Keys.
 */
$client->setAppAPIKey('APP_KEY');

?>

更改区域到 EU

<?php
use Customerio\Client;

$client = new Client('YOUR_API_KEY', 'YOUR_SITE_ID', ['region' => 'eu']);

?>

本地测试

从项目根目录运行 phpunit 以启动所有测试。

示例

客户

<?php
// Create customer
try {
    $client->customers->add(
        [
            'id' => 1,
            'email' => 'user@example.com',
            'plan' => 'free',
            'created_at' => time()
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

// Get customer
try {
    $client->customers->get(
        [
            'email' => 'user@example.com',        
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

// Update customer
try {
    $client->customers->update(
        [
            'id' => 1,
            'email' => 'user@example.com',
            'plan' => 'premium'
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error   
}

// Delete customer
try {
    $client->customers->delete(
        [
            'id' => 1,
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error   
}

事件

<?php
// Add customer event
try {
    $client->customers->event(
        [
            'id' => 1,
            'name' => 'test-event',
            'data' => [
                'event-metadata-1' => 'test',
                'event-metadata-2' => 'test-2'
            ]
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

// Add anonymous event
try {
    $client->events->anonymous(
        [
            'name' => 'invite-friend',
            'data' => [
                'recipient' => 'invitee@example.com'
            ]
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

匿名事件 示例 用法。

分段

<?php
// Get segment data
try {
    $client->segments->get(
        [
            'id' => 1
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

在此 检查其他可用方法

页面查看

<?php
// Add page view
try {
    $result = $client->page->view(
        [
            'id' => 1,
            'url' => 'http://example.com/login',
            'data' => [
                'referrer' => 'http://example.com'
            ]
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

活动

<?php
// Get campaigns data
try {
    $client->campaigns->get(
        [
            'id' => 1
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

在此 检查其他可用方法

<?php
// Trigger broadcast campaign
try {
    $result = $client->campaigns->trigger(
        [
            'id' => 1,
            'data' => [
                'headline' => 'Roadrunner spotted in Albuquerque!',
                'date' => 'January 24, 2018', 
                'text' => 'We\'ve received reports of a roadrunner in your immediate area! Head to your dashboard to view more information!' 
            ],
            'recipients' => [
                'segments' => [
                    'id' => 1
                ]
            ]
        ]
    );
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle the error
}

有关 API 触发广播的更多示例,请参阅 此处

许可证

MIT 许可证。有关更多详细信息,请参阅 LICENSE 文件。