thebearcod/yandex-metrika-sdk

Yandex Metrika SDK

0.4.0.0 2023-10-11 14:25 UTC

This package is auto-updated.

Last update: 2024-09-12 05:56:08 UTC


README

Yandex Metrika API管理的方便交互的SDK

Latest Stable Version Total Downloads License

安装

使用Composer

$ composer require thebearcod/yandex-metrika-sdk

获取令牌

  1. 转到 https://oauth.yandex.ru/
  2. 点击 "注册新应用"
  3. 填写必要的字段。
  4. 在 "平台" 部分,选择 "Web服务" 并点击 "替换开发URL"
  5. 在 "权限" 部分,为 "Yandex. Metrika" 标记 "获取统计信息" 和 "创建计数器"
  6. 保存后,需要复制应用程序ID并将其放入获取令牌的URL中
https://oauth.yandex.ru/authorize?response_type=token&client_id=ИД ПРИЛОЖЕНИЯ

初始化

$counterId = '';
$token = '';

// CDP API
$cdp = new \WebCrea\YandexMetrikaSdk\Api\CdpApi($token);

// Management API
$management = new \WebCrea\YandexMetrikaSdk\Api\ManagementApi($token);

使用示例

$counterId = '00000000';
$token = '00000000000000000000';

$cdp = new \WebCrea\YandexMetrikaSdk\Api\CdpApi($token);

// Сопоставление статусов заказов
$map['order_statuses'] = [
    [
        'id' => 'NO',
        'humanized' => 'Не оплачен',
        'type' => 'IN_PROGRESS',
    ],
    [
        'id' => 'N',
        'humanized' => 'Новый заказ',
        'type' => 'IN_PROGRESS',
    ],
];

$result = $cdp->mapOrderStatuses($counterId, $map);

// Загрузка заказов (JSON)
$orders['orders'] = [
    [
        "id" => "32152",
        "client_uniq_id" => "sertw345fgdsg",
        "client_type" => "CONTACT",
        "create_date_time" => "2020-04-14 13:17:00",
        "update_date_time" => "2020-04-17 16:12:21",
        "finish_date_time" => "2020-04-17 11:59:00",
        "revenue" => 2000,
        "order_status" => "N",
        "cost" => 100500,
        "products" => ["Товар А" => 173, "Товар Б" => 146],
    ],
];

$result = $cdp->uploadOrdersJson($counterId, $orders, ['merge_mode' => 'SAVE']);

// Информация о последних загрузках 
$result = $cdp->getLastUploadings($counterId);

CDP API说明

订单状态映射 https://yandex.ru/dev/metrika/doc/api2/crm/schema/maporderstatuses.html

public function mapOrderStatuses(int $counterId, array $content): array

客户数据(JSON)加载 https://yandex.ru/dev/metrika/doc/api2/crm/data/uploadcontactjson.html

public function uploadContactsJson(int $counterId, array $content, array $requestParams): array

订单(JSON)加载 https://yandex.ru/dev/metrika/doc/api2/crm/data/uploadordersjson.html

public function uploadOrdersJson(int $counterId, array $content, array $requestParams): array

创建属性 https://yandex.ru/dev/metrika/doc/api2/crm/schema/createattributes.html

public function createAttributes(int $counterId, array $content, array $requestParams = []): array

创建商品列表 https://yandex.ru/dev/metrika/doc/api2/crm/schema/createproducts.html

public function createProducts(int $counterId, array $content): array

所有属性信息 https://yandex.ru/dev/metrika/doc/api2/crm/schema/createproducts.html

public function getAttributes(int $counterId, array $requestParams = []): array

用户自定义属性类型信息 https://yandex.ru/dev/metrika/doc/api2/crm/schema/getpredefinedtypes.html

public function getPredefinedTypes(int $counterId): array

系统属性类型信息 https://yandex.ru/dev/metrika/doc/api2/crm/schema/gettypes.html

public function getTypes(int $counterId): array

最后上传信息 https://yandex.ru/dev/metrika/doc/api2/crm/uploadings/getlastuploadings.html

public function getLastUploadings(int $counterId, array $requestParams = []): array