jespanag/customerio

支持v2版本的Customer.io PHP API

3.5.9 2024-01-09 10:37 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文件。