有效负载转换器
本指南将引导您使用事件 API 的有效负载转换器。
有效负载转换器提供了一种能够在 TikTok 的服务器端自动识别有效负载的外部平台渠道(例如 Meta),然后将其转换为 TikTok 的等效格式的解决方案。广告主可以借此直接通过事件 API 向 TikTok 发送预先构建的事件。
使用前需知
请遵循这些步骤使用 TikTok 开始建立网站数据连接,并使用有效负载转换器向 TikTok 发送事件。
发送请求
若要发送新事件,需向事件 API 接口发送 POST 请求:https://business-api.tiktok.com/open_api/v1.3/event/track/。
| Header 字段{30%} | 数据类型 {20%}| 描述{50%} |
|---|---|---|
| Access-Token {必填}| string | 授权访问令牌。<br><br>若想获取访问令牌,请查看[事件 API 2.0 - 获取访问令牌](https://ads.tiktok.com/marketing_api/docs?id=1771101130925058)。 |
| X-Event-Source-ID {必填}| string | Pixel ID。 |
| Content-Type{必填} | string | 请求信息类型。<br><br> 允许格式: `application/json`。|
| X-Ttp-Cookie | string | 用于建立 TikTok Pixel 和事件 API 之间的事件去重。<br><br> 详情请查看<a href="#item-link-使用有效负载转换器进行事件去重">使用有效负载转换器进行事件去重</a>。 |
以下有效负载转换器的示例展示了如何通过 TikTok 事件 API 发送 Meta 事件。事件到达 TikTok 侧后,将触发有效负载转换器,从而将 Meta 事件转换为 TikTok 事件。
cURL
(code curl http)
curl --location --request POST 'https://business-api.tiktok.com/open_api/v1.3/event/track/' \
--header 'Access-Token: <TIKTOK_PIXEL_ACCESS_TOKEN>' \
--header 'X-Event-Source-ID: <TIKTOK_PIXEL_ID>' \
--header 'Content-Type: application/json' \
--data-raw '
{
"data": [
{
"event_name": "ViewContent",
"event_time": 1705657064,
"user_data": {
"em": [
"8830eedd6c6b5ea97d181563a349476ca1bb25ace1f94b5c5e48d9cad727941b"
],
"ph": [
"254aa248acb47dd654ca3ea53f48c2c26d641d23d7e2e93a1ec56258df7674c4"
],
"client_ip_address": "123.123.123.123"
},
"custom_data": {
"currency": "USD",
"value": 123.45,
"contents": [
{
"id": "product123",
"quantity": 1
}
]
},
"event_source_url": "http://tiktok-market.com/product/123",
"action_source": "website"
}
]
}
'
(/code)
Java SDK
If advertisers are already using the Meta Conversion API SDK, you can reuse the existing Meta code. By merely adding the "Access-Token" and "X-Event-Source-ID", you can effortlessly send Meta events to TikTok (for specific operations, refer to the following Java SDK example).
(code curl http)
import com.facebook.ads.sdk.APIContext;
import com.facebook.ads.sdk.APIException;
import com.facebook.ads.sdk.serverside.*;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import okhttp3.*;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static final String ACCESS_TOKEN = "<META_CAPI_ACCESS_TOKEN>";
public static final String PIXEL_ID = "<META_PIXEL_ID>";
public static void main(String[] args) {
//Example of Sending events using Meta Conversion API SDK.
APIContext context = new APIContext(ACCESS_TOKEN).enableDebug(true);
context.setLogger(System.out);
UserData userData = new UserData()
.emails(Arrays.asList("joe@eg.com"))
.phones(Arrays.asList("12345678901"))
.clientIpAddress("123.123.123.123");
Content content = new Content()
.productId("product123")
.quantity(1L);
CustomData customData = new CustomData()
.addContent(content)
.currency("USD")
.value(123.45F);
Event event = new Event();
event.eventName("ViewContent")
.eventTime(System.currentTimeMillis() / 1000L)
.userData(userData)
.customData(customData)
.eventSourceUrl("http://tiktok-market.com/product/123")
.actionSource(ActionSource.website);
EventRequest eventRequest = new EventRequest(PIXEL_ID, context);
eventRequest.addDataItem(event);
try {
EventResponse response = eventRequest.execute();
System.out.println(String.format("Standard API response : %s ", response));
} catch (APIException e) {
e.printStackTrace();
}
//Example of Payload Converter: Sending Meta Events via the TikTok Events API.
JsonObject metaPayload = new JsonObject();
metaPayload.add("data", new Gson().fromJson(eventRequest.getSerializedPayload(), JsonElement.class));
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://business-api.tiktok.com/open_api/v1.3/event/track/")
.addHeader("Access-Token", "<TIKTOK_PIXEL_ACCESS_TOKEN>")
.addHeader("X-Event-Source-ID", "<TIKTOK_PIXEL_ID>")
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), metaPayload.toString()))
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(String.format("Tiktok Standard API response : %s ", response));
} catch (IOException e) {
e.printStackTrace();
}
}
}
(/code)
Java 非 SDK
(code curl http)
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import okhttp3.*;
public class Main {
public static final String ACCESS_TOKEN = "<META_CAPI_ACCESS_TOKEN>";
public static final String PIXEL_ID = "<META_PIXEL_ID>";
public static void main(String[] args) {
//Example of Sending Meta events using http post
JsonObject userData = new JsonObject();
userData.addProperty("em", "8830eedd6c6b5ea97d181563a349476ca1bb25ace1f94b5c5e48d9cad727941b");
userData.addProperty("ph", "254aa248acb47dd654ca3ea53f48c2c26d641d23d7e2e93a1ec56258df7674c4");
JsonObject contentsData = new JsonObject();
customData.addProperty("product_id", "product123");
customData.addProperty("quantity", 1);
List<JsonObject> contentsArr = new ArrayList<>();
contentsArr.add(contentsData);
JsonObject customData = new JsonObject();
customData.addProperty("contents", contentsArr);
customData.addProperty("value", 123.45);
customData.addProperty("currency", "USD");
JsonObject event = new JsonObject();
event.addProperty("event_name", "ViewContent");
event.addProperty("event_time", System.currentTimeMillis() / 1000L);
event.add("user_data", userData);
event.add("custom_data", customData);
event.addProperty("action_source", "website");
event.addProperty("event_source_url", "http://tiktok-market.com/product/123");
JsonObject metaPayload = new JsonObject();
JsonArray data = new JsonArray();
data.add(event);
metaPayload.add("data", data);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://graph.facebook.com/v20.0/" + PIXEL_ID + "/events?access_token=" + ACCESS_TOKEN)
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), metaPayload.toString()))
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(String.format("Meta Standard API response : %s ", response));
} catch (Throwable t) {
System.out.println(t.getMessage());
}
//Example of Payload Converter: Sending Meta Events via the TikTok Events API.
OkHttpClient tiktokClient = new OkHttpClient();
Request tiktokRequest = new Request.Builder()
.url("https://business-api.tiktok.com/open_api/v1.3/event/track/")
.addHeader("Access-Token", "<TIKTOK_PIXEL_ACCESS_TOKEN")
.addHeader("X-Event-Source-ID", "<TIKTOK_PIXEL_ID>")
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), metaPayload.toString()))
.build();
try {
Response response = tiktokClient.newCall(tiktokRequest).execute();
System.out.println(String.format("Tiktok Standard API response : %s ", response));
} catch (Throwable t) {
System.out.println(t.getMessage());
}
}
}
(/code)
Python SDK
(code curl http)
import json
import time
import requests
from facebook_business.adobjects.serverside.action_source import ActionSource
from facebook_business.adobjects.serverside.content import Content
from facebook_business.adobjects.serverside.custom_data import CustomData
from facebook_business.adobjects.serverside.event import Event
from facebook_business.adobjects.serverside.event_request import EventRequest
from facebook_business.adobjects.serverside.user_data import UserData
from facebook_business.api import FacebookAdsApi
# Example of Sending events using Meta Conversion API SDK.
access_token = '<META_CAPI_ACCESS_TOKEN>'
pixel_id = '<META_PIXEL_ID>'
FacebookAdsApi.init(access_token=access_token)
user_data = UserData(
emails=['joe@eg.com'],
phones=['12345678901'],
client_ip_address='123.123.123.123',
)
content = Content(
product_id='product123',
quantity=1,
)
custom_data = CustomData(
contents=[content],
currency='USD',
value='123.45',
)
event = Event(
event_name='ViewContent',
event_time=int(time.time()),
user_data=user_data,
custom_data=custom_data,
event_source_url='http://tiktok-market.com/product/123',
action_source=ActionSource.WEBSITE,
)
events = [event]
event_request = EventRequest(
events=events,
pixel_id=pixel_id,
)
event_response = event_request.execute()
print(event_response)
# Example of Payload Converter: Sending Meta Events via the TikTok Events API.
meta_payload = {
'data': [event.normalize() for event in events]
}
headers = {
'Access-Token': '<TIKTOK_PIXEL_ACCESS_TOKEN>',
'X-Event-Source-ID': '<TIKTOK_PIXEL_ID>',
'Content-Type': 'application/json',
}
response = requests.post('https://business-api.tiktok.com/open_api/v1.3/event/track/', headers=headers,
data=json.dumps(meta_payload))
print(response.text)
(/code)
Python 非 SDK
(code curl http)
import json
import time
import requests
META_ACCESS_TOKEN = '<META_CAPI_ACCESS_TOKEN>'
META_PIXEL_ID = '<META_PIXEL_ID>'
def convert():
# example Meta CAPI event
event = {
"event_name": "ViewContent",
"event_time": int(time.time()),
"action_source": "website",
"event_source_url": "http://tiktok-market.com/product/123",
"user_data": {
"em": [
# hash for joe@eg.com
"8830eedd6c6b5ea97d181563a349476ca1bb25ace1f94b5c5e48d9cad727941b"
],
"ph": [
# hash for 12345678901
"254aa248acb47dd654ca3ea53f48c2c26d641d23d7e2e93a1ec56258df7674c4"
]
},
"custom_data": {
"contents": [
{
"product_id": "product123",
"quantity": 1
}
],
"currency": "USD",
"value": 123.45
}
}
meta_payload = {
'data': [event]
}
# post event to Meta CAPI
meta_url = "https://graph.facebook.com/v20.0/" + META_PIXEL_ID + "/events?access_token=" + META_ACCESS_TOKEN
meta_headers = {'Content-Type': 'application/json'}
meta_response = requests.post(url=meta_url, headers=meta_headers, data=json.dumps(meta_payload))
print(meta_response.text)
tiktok_headers = {
'Access-Token': '<TIKTOK_PIXEL_ACCESS_TOKEN>', # tiktok eAPI access token
'X-Event-Source-ID': '<TIKTOK_PIXEL_ID>', # tiktok pixel ID
'Content-Type': 'application/json',
}
# post same meta payload to TikTok Events API
tiktok_response = requests.post(url='https://business-api.tiktok.com/open_api/v1.3/event/track/',
headers=tiktok_headers, data=json.dumps(meta_payload))
# example success response {"code": 0, "message": "OK", "request_id": "xxxxxxx", "data": {}}
print(tiktok_response.text)
(/code)
验证事件设置
在设置完有效负载转换器集成且发送生产环境的事件给 TikTok 之前,您可以发送测试事件以验证数据连接是否正常工作。请按照这些步骤生成用于放入有效负载并发送给 TikTok 的测试事件代码。一旦您获取到测试事件代码,请参考以下示例将测试事件代码放入有效负载。
cURL
(code http)
curl --location --request POST 'https://business-api.tiktok.com/open_api/v1.3/event/track/' \
--header 'Access-Token: <TIKTOK_PIXEL_ACCESS_TOKEN>' \
--header 'X-Event-Source-ID: <TIKTOK_PIXEL_ID>' \
--header 'Content-Type: application/json' \
--data-raw '
{
"data": [
{
"event_name": "ViewContent",
"event_time": 1705657064,
"user_data": {
"em": [
"8830eedd6c6b5ea97d181563a349476ca1bb25ace1f94b5c5e48d9cad727941b"
],
"ph": [
"254aa248acb47dd654ca3ea53f48c2c26d641d23d7e2e93a1ec56258df7674c4"
],
"client_ip_address": "123.123.123.123"
},
"custom_data": {
"currency": "USD",
"value": 123.45,
"contents": [
{
"id": "product123",
"quantity": 1
}
]
},
"event_source_url": "http://tiktok-market.com/product/123",
"action_source": "website"
}
],
<span style="background-color: green;">"test_event_code": "<TIKTOK_TEST_EVENT_CODE>"</span>
}
'
(/code)
Java SDK
(code http)
import com.facebook.ads.sdk.APIContext;
import com.facebook.ads.sdk.APIException;
import com.facebook.ads.sdk.serverside.*;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import okhttp3.*;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static final String ACCESS_TOKEN = "<META_CAPI_ACCESS_TOKEN>";
public static final String PIXEL_ID = "<META_PIXEL_ID>";
public static void main(String[] args) {
//Example of Sending events using Meta Conversion API SDK.
APIContext context = new APIContext(ACCESS_TOKEN).enableDebug(true);
context.setLogger(System.out);
UserData userData = new UserData()
.emails(Arrays.asList("joe@eg.com"))
.phones(Arrays.asList("12345678901"))
.clientIpAddress("123.123.123.123");
Content content = new Content()
.productId("product123")
.quantity(1L);
CustomData customData = new CustomData()
.addContent(content)
.currency("USD")
.value(123.45F);
Event event = new Event();
event.eventName("ViewContent")
.eventTime(System.currentTimeMillis() / 1000L)
.userData(userData)
.customData(customData)
.eventSourceUrl("http://tiktok-market.com/product/123")
.actionSource(ActionSource.website);
EventRequest eventRequest = new EventRequest(PIXEL_ID, context);
eventRequest.addDataItem(event);
<span style="background-color: green;">eventRequest.setTestEventCode("<TIKTOK_TEST_EVENT_CODE>");</span>
try {
EventResponse response = eventRequest.execute();
System.out.println(String.format("Standard API response : %s ", response));
} catch (APIException e) {
e.printStackTrace();
}
//Example of Payload Converter: Sending Meta Events via the TikTok Events API.
JsonObject metaPayload = new JsonObject();
metaPayload.add("data", new Gson().fromJson(eventRequest.getSerializedPayload(), JsonElement.class));
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://business-api.tiktok.com/open_api/v1.3/event/track/")
.addHeader("Access-Token", "<TIKTOK_PIXEL_ACCESS_TOKEN>")
.addHeader("X-Event-Source-ID", "<TIKTOK_PIXEL_ID>")
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), metaPayload.toString()))
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(String.format("Tiktok Standard API response : %s ", response));
} catch (IOException e) {
e.printStackTrace();
}
}
}
(/code)
Java 非 SDK
(code http)
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import okhttp3.*;
public class Main {
public static final String ACCESS_TOKEN = "<META_CAPI_ACCESS_TOKEN>";
public static final String PIXEL_ID = "<META_PIXEL_ID>";
public static void main(String[] args) {
//Example of Sending Meta events using http post
JsonObject userData = new JsonObject();
userData.addProperty("em", "8830eedd6c6b5ea97d181563a349476ca1bb25ace1f94b5c5e48d9cad727941b");
userData.addProperty("ph", "254aa248acb47dd654ca3ea53f48c2c26d641d23d7e2e93a1ec56258df7674c4");
JsonObject contentsData = new JsonObject();
customData.addProperty("product_id", "product123");
customData.addProperty("quantity", 1);
List<JsonObject> contentsArr = new ArrayList<>();
contentsArr.add(contentsData);
JsonObject customData = new JsonObject();
customData.addProperty("contents", contentsArr);
customData.addProperty("value", 123.45);
customData.addProperty("currency", "USD");
JsonObject event = new JsonObject();
event.addProperty("event_name", "ViewContent");
event.addProperty("event_time", System.currentTimeMillis() / 1000L);
event.add("user_data", userData);
event.add("custom_data", customData);
event.addProperty("action_source", "website");
event.addProperty("event_source_url", "http://tiktok-market.com/product/123");
JsonObject metaPayload = new JsonObject();
JsonArray data = new JsonArray();
data.add(event);
metaPayload.add("data", data);
<span style="background-color: green;">metaPayload.add("test_event_code", "<TIKTOK_TEST_EVENT_CODE>")</span>
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://graph.facebook.com/v20.0/" + PIXEL_ID + "/events?access_token=" + ACCESS_TOKEN)
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), metaPayload.toString()))
.build();
try {
Response response = client.newCall(request).execute();
System.out.println(String.format("Meta Standard API response : %s ", response));
} catch (Throwable t) {
System.out.println(t.getMessage());
}
//Example of Payload Converter: Sending Meta Events via the TikTok Events API.
OkHttpClient tiktokClient = new OkHttpClient();
Request tiktokRequest = new Request.Builder()
.url("https://business-api.tiktok.com/open_api/v1.3/event/track/")
.addHeader("Access-Token", "<TIKTOK_PIXEL_ACCESS_TOKEN")
.addHeader("X-Event-Source-ID", "<TIKTOK_PIXEL_ID>")
.addHeader("Content-Type", "application/json")
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), metaPayload.toString()))
.build();
try {
Response response = tiktokClient.newCall(tiktokRequest).execute();
System.out.println(String.format("Tiktok Standard API response : %s ", response));
} catch (Throwable t) {
System.out.println(t.getMessage());
}
}
}
(/code)
Python SDK
(code http)
import json
import time
import requests
from facebook_business.adobjects.serverside.action_source import ActionSource
from facebook_business.adobjects.serverside.content import Content
from facebook_business.adobjects.serverside.custom_data import CustomData
from facebook_business.adobjects.serverside.event import Event
from facebook_business.adobjects.serverside.event_request import EventRequest
from facebook_business.adobjects.serverside.user_data import UserData
from facebook_business.api import FacebookAdsApi
# Example of Sending events using Meta Conversion API SDK.
access_token = '<META_CAPI_ACCESS_TOKEN>'
pixel_id = '<META_PIXEL_ID>'
FacebookAdsApi.init(access_token=access_token)
user_data = UserData(
emails=['joe@eg.com'],
phones=['12345678901'],
client_ip_address='123.123.123.123',
)
content = Content(
product_id='product123',
quantity=1,
)
custom_data = CustomData(
contents=[content],
currency='USD',
value='123.45',
)
event = Event(
event_name='ViewContent',
event_time=int(time.time()),
user_data=user_data,
custom_data=custom_data,
event_source_url='http://tiktok-market.com/product/123',
action_source=ActionSource.WEBSITE,
)
events = [event]
event_request = EventRequest(
events=events,
<span style="background-color: green;">test_event_code='<TIKTOK_TEST_EVENT_CODE>',</span>
pixel_id=pixel_id,
)
event_response = event_request.execute()
print(event_response)
# Example of Payload Converter: Sending Meta Events via the TikTok Events API.
meta_payload = {
'data': [event.normalize() for event in events]
}
headers = {
'Access-Token': '<TIKTOK_PIXEL_ACCESS_TOKEN>',
'X-Event-Source-ID': '<TIKTOK_PIXEL_ID>',
'Content-Type': 'application/json',
}
response = requests.post('https://business-api.tiktok.com/open_api/v1.3/event/track/', headers=headers,
data=json.dumps(meta_payload))
print(response.text)
(/code)
Python 非 SDK
(code http)
import json
import time
import requests
META_ACCESS_TOKEN = '<META_CAPI_ACCESS_TOKEN>'
META_PIXEL_ID = '<META_PIXEL_ID>'
def convert():
# example Meta CAPI event
event = {
"event_name": "ViewContent",
"event_time": int(time.time()),
"action_source": "website",
"event_source_url": "http://tiktok-market.com/product/123",
"user_data": {
"em": [
# hash for joe@eg.com
"8830eedd6c6b5ea97d181563a349476ca1bb25ace1f94b5c5e48d9cad727941b"
],
"ph": [
# hash for 12345678901
"254aa248acb47dd654ca3ea53f48c2c26d641d23d7e2e93a1ec56258df7674c4"
]
},
"custom_data": {
"contents": [
{
"product_id": "product123",
"quantity": 1
}
],
"currency": "USD",
"value": 123.45
}
}
meta_payload = {
'data': [event],
<span style="background-color: green;">'test_event_code': '<TIKTOK_TEST_EVENT_CODE>'</span>
}
# post event to Meta CAPI
meta_url = "https://graph.facebook.com/v20.0/" + META_PIXEL_ID + "/events?access_token=" + META_ACCESS_TOKEN
meta_headers = {'Content-Type': 'application/json'}
meta_response = requests.post(url=meta_url, headers=meta_headers, data=json.dumps(meta_payload))
print(meta_response.text)
tiktok_headers = {
'Access-Token': '<TIKTOK_PIXEL_ACCESS_TOKEN>', # tiktok eAPI access token
'X-Event-Source-ID': '<TIKTOK_PIXEL_ID>', # tiktok pixel ID
'Content-Type': 'application/json',
}
# post same meta payload to TikTok Events API
tiktok_response = requests.post(url='https://business-api.tiktok.com/open_api/v1.3/event/track/',
headers=tiktok_headers, data=json.dumps(meta_payload))
# example success response {"code": 0, "message": "OK", "request_id": "xxxxxxx", "data": {}}
print(tiktok_response.text)
(/code)
请前往事件管理平台中 Pixel 的测试事件选项卡,在事件活动部分查找测试事件。任何与有效负载有关的问题都将显示出来,以帮助您解决问题。
在切换到生产模式之前,需删除 test_event_code。
使用有效负载转换器进行事件去重
对于同时使用 TikTok Pixel SDK 和事件 API 发送相同事件的广告主,进行事件去重是必要的。事件去重可以使 TikTok 更好地报告或优化您的推广系列表现。详情参见事件去重。
当向事件 API 发送其他平台的有效负载,即使用有效负载转换器功能时,您可以设置 Cookie 去重,从而对 TikTok Pixel 和事件 API 之间进行事件去重。
您需要在您的 TikTok Pixel 设置中启用第一方 Cookie,并在您的事件 API 请求中发送额外的 X-Ttp-Cookie HTTP 请求 Header。该 Header X-Ttp-Cookie 的值应为 TikTok 第一方 Cookie(_ttp)的 Cookie 值。以下是一个 cURL 请求的示例:
(code curl http)
curl --location --request POST 'https://business-api.tiktok.com/open_api/v1.3/event/track/' \
--header 'Access-Token: <TIKTOK_PIXEL_ACCESS_TOKEN>' \
--header 'X-Event-Source-ID: <TIKTOK_PIXEL_ID>' \
--header 'X-Ttp-Cookie: <TIKTOK_TTP_COOKIE_VALUE>' \
--header 'Content-Type: application/json' \
--data-raw '{
<SOME_PAYLOAD>
}'
(/code)
返回与报错
若想查看 API 报错处理的详情,请查看此文档。