klaviyo/php-sdk

此包已被废弃且不再维护。未建议替代包。

Klaviyo PHP SDK

2.3.6 2022-04-22 13:53 UTC

README

弃用通知

此 SDK 及其关联的 pip 包将于 2024-01-01 弃用,将不再接收更新。

我们建议迁移到我们最新的 SDK

您可以在 这里 了解更多关于我们 SDK 版本历史和支持信息。

要比较新旧 API,请查看 此指南

迁移说明

注意:此更改与旧版本不兼容;迁移到新 SDK 需要完成以下步骤

安装新 SDK

composer require klaviyo/sdk

更新导入

use Klaviyo\Klaviyo as Klaviyo;

use Klaviyo\Client;

更新客户端实例化

$client = new Klaviyo( 'PRIVATE_API_KEY', 'PUBLIC_API_KEY' );

$client = new Client('YOUR_API_KEY');

更新 SDK 调用

新 SDK 在命名空间(资源和方法名)、参数(名称、类型和格式)和错误处理方面有很多更改。此外,新 SDK 严格使用标准/内置 PHP 类型(int、string、array 等),而不使用像旧版那样自定义模型。请参考新 SDK 仓库中的 此部分 了解如何更新每个操作。

多店铺限制

新 SDK 当前将 API 密钥设置为全局环境级别。这意味着如果您管理多个店铺,每个店铺的客户端必须在不同的环境中运行。我们计划更新此行为,以更好地支持多店铺应用程序。

什么是 Klaviyo?

Klaviyo 是一个实时服务,通过聚合所有客户数据、识别重要的客户群体并采取行动来理解您的客户。 http://www.klaviyo.com/

此包做什么?

  • 从您的后端直接跟踪客户和事件。

如何安装?

composer require klaviyo/php-sdk

API 示例

安装 Klaviyo 包后,您可以使用公共令牌启动它,该令牌用于跟踪事件或识别配置文件,以及您的私有 API 密钥来利用指标和列表 API。

use Klaviyo\Klaviyo as Klaviyo;

$client = new Klaviyo( 'PRIVATE_API_KEY', 'PUBLIC_API_KEY' );

您可以使用 Klaviyo 容易地跟踪事件或识别人员。注意,跟踪和识别请求需要您的公开令牌。

跟踪一个事件

use Klaviyo\Model\EventModel as KlaviyoEvent;

$event = new KlaviyoEvent(
    array(
        'event' => 'Filled out Profile',
        'customer_properties' => array(
            '$email' => 'someone@mailinator.com'
        ),
        'properties' => array(
            'Added Social Accounts' => False
        )
    )
);

$client->publicAPI->track( $event, true );

您还可以在事件模型中“客户属性”属性中添加配置文件属性

use Klaviyo\Model\EventModel as KlaviyoEvent;

$event = new KlaviyoEvent(
    array(
        'event' => 'Filled out Profile',
        'customer_properties' => array(
            '$email' => 'someone@mailinator.com',
            '$first_name' => 'Thomas',
            '$last_name' => 'Jefferson'
        ),
        'properties' => array(
            'Added Social Accounts' => False
        )
    )
);

$client->publicAPI->track( $event, true );

或者仅为某人添加属性

use Klaviyo\Model\ProfileModel as KlaviyoProfile;

$profile = new KlaviyoProfile(
    array(
        '$email' => 'thomas.jefferson@mailinator.com',
        '$first_name' => 'Thomas',
        '$last_name' => 'Jefferson',
        'Plan' => 'Premium'
    )
);

$client->publicAPI->identify( $profile, true );

您可以得到指标、事件时间轴以及指标的导出分析。更多信息请见此处 https://www.klaviyo.com/docs/api/metrics

#return a list of all metrics in your Klaviyo account
$client->metrics->getMetrics();

#return a timeline of all metrics
$client->metrics->getMetricsTimeline();

#return a specific metric timeline using its metric ID
$client->metrics->getMetricTimelineById( 'METRICID' );

#export metric specific values
$client->metrics->getMetricExport( 'METRICID' );

您可以创建、更新、读取和删除列表。更多信息请见此处 https://www.klaviyo.com/docs/api/v2/lists

#create a list
$client->lists->createList( 'List Name' );

#Get all lists in your Klaviyo account
$client->lists->getLists();

#Get information about a list
$client->lists->getListById( 'ListId' );

#update a lists properties
$client->lists->updateListNameById( 'ListId', 'ListName' );

#Delete a list from account
$client->lists->deleteList( 'ListId' );

#Subscribe or re-subscribe profiles to a list
$client->lists->addSubscribersToList( 'ListId', array $arrayOfProfiles );

#Check if profiles are on a list and not suppressed
$client->lists->checkListSubscriptions( 'ListId', array $emails, array $phoneNumbers, array $pushTokens );

#Unsubscribe and remove profiles from a list
$client->lists->deleteSubscribersFromList( 'ListId', array $emails );

#Add members to list without affecting consent status
$client->lists->addMembersToList( 'ListId', array $arrayOfProfiles );

#Check if profiles are on a list
$client->lists->checkListMembership( 'ListId', array $emails, array $phoneNumbers, array $pushTokens );

#Remove members from a list without changing their consent status
$client->lists->removeMembersFromList( 'ListId', array $emails );

#Get all exclusions on a list
$client->lists->getListExclusions( 'ListId' );

#Get all of the emails, phone numbers and push tokens for profiles in a given list or segment
$client->lists->getAllMembers( 'GroupId', $marker = $marker_value );

您可以根据配置文件ID获取配置文件信息,更多信息请见此处 https://www.klaviyo.com/docs/api/people

#Get profile by profileId
$client->profiles->getProfile( 'ProfileId' );

#Update a profile
$client->profiles->updateProfile( 'ProfileId', array $properties );

#Get all metrics for a profile
$client->profiles->getAllProfileMetricsTimeline( 'ProfileId' );

#Get a specific metric for a profile
$client->profiles->getProfileMetricTimeline( 'ProfileId', 'MetricId' );

#Get a profile's ID by its email address
$client->profiles->getProfileIdByEmail('someone@mailinator.com');

您可以根据一个标识属性请求符合隐私要求的配置文件删除

#Request profile deletion by email
$client->dataPrivacy->requestProfileDeletion('someone@mailinator.com');

#Request profile deletion by phone number
$client->dataPrivacy->requestProfileDeletion('1-234-567-8910', 'phone_number');

#Request profile deletion by person ID
$client->dataPrivacy->requestProfileDeletion('abc123', 'person_id');

异常

Klaviyo\Exception\KlaviyoApiException

当在创建 API 请求时出现问题时抛出。在捕获此异常后,您可以使用 getMessage(),它将返回一个包含有关发生问题的更多详细信息的字符串。

Klaviyo\Exception\KlaviyoRateLimitException

如果发生速率限制,它将抛出 Klaviyo\Exception\KlaviyoRateLimitException。在捕获此异常后,您可以使用 getMessage(),它将返回一个 JSON 编码的数组:{"detail":"请求被限制。预计26.0秒后可用。","retryAfter":26}

Klaviyo\Exception\KlaviyoAuthenticationException

当创建 API 请求时发生认证错误时抛出,通常由无效的 API 密钥引起。

Klaviyo\Exception\KlaviyoResourceNotFoundException

当系统尝试更新一个不存在的属性时抛出。例如,尝试更新账户上不存在的一个列表。