← 返回目录
📁 API Reference › Events 1.0 › Pixel Events

获取Pixel事件数据

您可以使用本接口查看 Pixel 在一段时间内的的事件数据,包括:归因事件数(归因到您的广告上的事件数),预览事件数(广告预览产生的事件数),总事件数(从所有数据源接收到的事件总数)。

重要提示
使用事件 API 接口代表您同意遵守TikTok 商业产品(数据)条款,并且不会与 TikTok 分享敏感数据。

v1.2与v1.3版本对比

以下表格概括了 v1.2与v1.3接口的变化。 ``xtable |变化内容{30%}|v1.2{35%}|v1.3{35%}| |---|---|---| |接口路径| /v1.2/pixel/event/stats/| /v1.3/pixel/event/stats/| |请求参数类型|advertiser_id: number <br>pixel_ids: number[] |advertiser_id: string<br>pixel_ids: string[] | |返回参数类型|pixel_id: number |pixel_id: string | |新增返回参数| /|custom_event_type<br>browser_event_total_count<br>server_event_total_count`|


## 请求

**地址**  https://business-api.tiktok.com/open_api/v1.3/pixel/event/stats/

**方法** GET

**Header**

```xtable
|字段{30%}|类型{15%}|描述{55%}|
|---|---|---|
|Access-Token {必填}|string|授权访问令牌。可以通过[获取访问令牌](https://ads.tiktok.com/marketing_api/docs?id=1738373164380162)接口获取。|

参数

|字段{30%}|类型{15%}|描述{55%}|
|---|---|---|
|advertiser_id {必填}|string| 广告主 ID。 |
|date_range {必填}|object| 时间范围,不得超过 30 天。<br><br><p><span style="color:darkred"><b>注意</b></span>:从此接口获取的事件数据与[事件管理平台](https://ads.tiktok.com/help/article/about-tiktok-events-manager?lang=zh)显示的事件数据可能会存在轻微差异。这是因为此接口获取的事件数据基于 UTC 时间,而事件管理平台显示的事件数据基于广告主的时区。|
#|start_date {必填}|string| 起始时间(UTC 时间)。<br><br>示例: `"2020-02-16"`。|
#|end_date {必填}|string| 结束时间(UTC 时间)。<br><br>示例: `"2020-02-17"`。 |
|pixel_ids {必填}|string[]| Pixel ID 列表。<br><br>最大数量:10。 |

示例

(code curl http)
curl --location --request GET  'https://business-api.tiktok.com/open_api/v1.3/pixel/event/stats/?advertiser_id={{advertiser_id}}&pixel_ids=["{{pixel_id}}"]&date_range={"start_date":"2024-03-31","end_date":"2024-04-06"}' \
--header 'Access-Token: {{Access-Token}}' \
(/code)

(code Java java)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.apache.http.client.utils.URIBuilder;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Demo {
    private static final String ACCESS_TOKEN = "xxx";
    private static final String PATH = "/open_api/v1.3/pixel/event/stats/";
    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * Build request URL
     *
     * @param path Request path
     * @return Request URL
     */
    private static String buildUrl(String path) throws URISyntaxException {
        URI uri = new URI("https", "business-api.tiktok.com", path, "", "");
        return uri.toString();
    }


    /**
     * Send GET request
     *
     * @param jsonStr:Args in JSON format
     * @return Response in JSON format
     */
    private static String get(String jsonStr) throws IOException, URISyntaxException {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        URIBuilder ub = new URIBuilder(buildUrl(PATH));
        Map< String, Object > map = mapper.readValue(jsonStr, Map.class);
        map.forEach((k, v) -> {
            try {
                ub.addParameter(k, v instanceof String ? (String) v : mapper.writeValueAsString(v));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        });
        URL url = ub.build().toURL();

        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .addHeader("Access-Token", ACCESS_TOKEN)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static void main(String[] args) throws IOException, URISyntaxException {
        Long advertiser_id = ADVERTISER_ID;
        String start_date = START_DATE;
        String end_date = END_DATE;
        List < Long > pixel_ids_list = PIXEL_IDS;
        String pixel_ids = mapper.writeValueAsString(pixel_ids_list);

        // Args in JSON format
        String myArgs = String.format("{\"advertiser_id\": \"%s\", \"date_range\": {\"start_date\": \"%s\", \"end_date\": \"%s\"}, \"pixel_ids\": %s}",advertiser_id, start_date, end_date, pixel_ids);
        System.out.println(get(myArgs));
    }
}

(/code)

(code Python python)
# coding=utf-8
import json
import requests

from six import string_types
from six.moves.urllib.parse import urlencode, urlunparse  # noqa

ACCESS_TOKEN = "xxx"
PATH = "/open_api/v1.3/pixel/event/stats/"


def build_url(path, query=""):
    # type: (str, str) -> str
    """
    Build request URL
    :param path: Request path
    :param query: Querystring
    :return: Request URL
    """
    scheme, netloc = "https", "business-api.tiktok.com"
    return urlunparse((scheme, netloc, path, "", query, ""))

def get(json_str):
    # type: (str) -> dict
    """
    Send GET request
    :param json_str: Args in JSON format
    :return: Response in JSON format
    """
    args = json.loads(json_str)
    query_string = urlencode({k: v if isinstance(v, string_types) else json.dumps(v) for k, v in args.items()})
    url = build_url(PATH, query_string)
    headers = {
        "Access-Token": ACCESS_TOKEN,
    }
    rsp = requests.get(url, headers=headers)
    return rsp.json()

if __name__ == '__main__':
    advertiser_id = ADVERTISER_ID
    start_date = START_DATE
    end_date = END_DATE
    pixel_ids_list = PIXEL_IDS
    pixel_ids = json.dumps(pixel_ids_list)

    # Args in JSON format
    my_args = "{\"advertiser_id\": \"%s\", \"date_range\": {\"start_date\": \"%s\", \"end_date\": \"%s\"}, \"pixel_ids\": %s}" % (advertiser_id, start_date, end_date, pixel_ids)
    print(get(my_args))

(/code)

(code PHP php)

$ACCESS_TOKEN = "xxx";
$PATH = "/open_api/v1.3/pixel/event/stats/";

/**
 * Build request URL
 * @param $path : Request path
 * @return string
 */
function build_url($path)
{
    return "https://business-api.tiktok.com" . $path;
}

/**
 * Send GET request
 * @param $json_str : Args in JSON format
 * @return bool|string : Response in JSON format
 */
function get($json_str)
{
    global $ACCESS_TOKEN, $PATH;
    $curl = curl_init();

    $args = json_decode($json_str, true);

    /* Values of querystring is also in JSON format */
    foreach ($args as $key => $value) {
        $args[$key] = is_string($value) ? $value : json_encode($value);
    }

    $url = build_url($PATH) . "?" . http_build_query(
            $args
        );

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "Access-Token: " . $ACCESS_TOKEN,
        ),
    ));

    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}


$advertiser_id = ADVERTISER_ID;
$start_date = START_DATE;
$end_date = END_DATE;
$pixel_ids_list = PIXEL_IDS;
$pixel_ids = json_encode($pixel_ids_list);

/* Args in JSON format */
$my_args = sprintf("{\"advertiser_id\": \"%s\", \"date_range\": {\"start_date\": \"%s\", \"end_date\": \"%s\"}, \"pixel_ids\": %s}", $advertiser_id, $start_date, $end_date, $pixel_ids);
echo get($my_args);
(/code)

返回

|字段{30%}|类型{15%}|描述{55%}|
|---|---|---|
|code |number|返回码,详见[附录-返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097)|
|message |string|返回信息,详见[附录-返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097)|
|data|object|返回数据|
#|list |object[]| 数据列表  |
##|pixel_id |string| Pixel ID |
##|statistics |object[]| 统计信息 |
###|pixel_event_type |string| 仅当事件为标准网站事件(Pixel 事件)时返回。<br><br>标准网站事件类型。<br><br>本字段的枚举值参见[Pixel 事件类型- Pixel 事件](1739585696931842.html)小节中的“<b>用于广告创编的事件名称</b>”列。 |
###| custom_event_type | string | 仅当事件为自定义网站事件时返回。 <br> <br>自定义网站事件类型。 自定义事件是 TikTok 合作伙伴可以在预定义的标准事件列表外自定义的行为。若想了解自定义事件的详情,请查看[标准事件和自定义事件](https://ads.tiktok.com/help/article/standard-events-parameters?lang=zh)。  <br> <br>不支持使用[/pixel/event/create/](1740858807646209.html)创建自定义网站事件,但支持使用事件 1.0 接口[/pixel/track/](1740858531237890.html)或[/pixel/batch/](1740858565852225.html),或事件 2.0 接口[/event/track/](1771101303285761.html)直接回传自定义网站事件。 例如,若回传 `SHOPPING` 事件,则该事件视作自定义事件,因为该事件类型未在[事件 API 1.0 - 报告网站事件和参数 - 支持的网站事件](https://business-api.tiktok.com/portal/docs?id=1741601162187777#item-link-支持的网站事件)或[事件 API 2.0 - 支持的事件 - 网站标准事件](https://business-api.tiktok.com/portal/docs?id=1771101186666498#item-link-网站标准事件)中的“<b>事件</b>”列列出。 |
###|attributed_count |number| 归因事件数(归因到您的广告上的事件数) |
###|preview_count |number| 预览事件数(广告预览产生的事件数) |
###| total_count | number |总事件数,即从所有数据源接收到的该事件类型(`pixel_event_type` 或 `custom_event_type`)的网站事件总数。 <br><br>公式: `total_count`= `browser_event_total_count` + `server_event_total_count`. |
###| browser_event_total_count | number | 从网络浏览器接收到的该事件类型的网站事件数。 |
###| server_event_total_count | number | 从服务器接收到的该事件类型的网站事件数。 |
|request_id |string|请求的日志 ID,唯一标识一个请求。|

示例

(code Success-Response http)
HTTPS/1.1 200 OK
{
    "code": 0,
    "message": "OK",
    "request_id": "{{request_id}}",
    "data": {
        "list": [
            {
                "statistics": [
                    {
                        "server_event_total_count": 3,
                        "convert_id": null,
                        "total_count": 10,
                        "browser_event_total_count": 7,
                        "attributed_count": 0,
                        "preview_count": 0,
                        "pixel_event_type": "ON_WEB_CART"
                    },
                    {
                        "server_event_total_count": 7803,
                        "convert_id": null,
                        "total_count": 12231,
                        "browser_event_total_count": 4428,
                        "attributed_count": 0,
                        "preview_count": 0,
                        "pixel_event_type": "ON_WEB_DETAIL"
                    },
                    {
                        "server_event_total_count": 0,
                        "convert_id": null,
                        "total_count": 16622,
                        "custom_event_type": "shopping",
                        "browser_event_total_count": 16622,
                        "attributed_count": 0,
                        "preview_count": 0
                    }
                ],
                "pixel_id": "{{pixel_id}}"
            }
        ]
    }
}
(/code);

内容来源:TikTok API for Business 官方文档 · 本地归档

在接这个接口时卡住了?
Stuck wiring this API into production? We build ad-delivery tooling on these platforms — campaign automation, reporting pipelines, error-handling and migration between API versions.
联系我们 · Talk to us 每日平台更新简报 →