I encountered an issue with my code while attempting to use the TikTok API to fetch reports similar to those generated in TikTok Ads Manager. I followed the documentation on campaign_name, ad_name, and various metrics, but I’m getting an error indicating that some metrics fields are invalid. Here’s the relevant part of my code:
import requests
import json
import pandas as pd
from datetime import date
# Define the API endpoint and parameters
url = "https://business-api.tiktok.com/open_api/v1.2/reports/integrated/get/"
params = {
"advertiser_id": " ", # Replace with your advertiser ID
"report_type": "BASIC",
"start_date": "2024-06-10", # Replace with your start date
"end_date": date.today(), # Replace with your end date
"data_level": "AUCTION_ADVERTISER",
"dimensions": json.dumps(["advertiser_id", "stat_time_day"]), # Include stat_time_day as a dimension
"page_size": 1000,
"metrics": json.dumps([
"campaign_name",
"ad_name",
"cpc",
"cpm",
"impressions",
"clicks",
"ctr",
"reach",
"conversion",
"cost_per_conversion",
"conversion_rate",
"conversion_rate_v2",
"video_watched_2s",
"video_watched_6s",
"engaged_view",
"engagements",
"page_content_view_events"
])
}
# Define the headers, including the access token
headers = {
"Access-Token": " " # Replace with your access token
}
# Make the GET request
response = requests.get(url, headers=headers, params=params)
# Check the status code and process the response
if response.status_code == 200:
data = response.json()
print(json.dumps(data, indent=4)) # Print the entire response for debugging
# Adjust the DataFrame creation based on the actual response structure
if "data" in data and "list" in data["data"]:
df = pd.DataFrame(data["data"]["list"])
print(df)
else:
print("No data found in response or incorrect response structure.")
else:
print(f"Error: {response.status_code}")
print(response.text)
# Error message received:
# {
# "code": 40002,
# "message": "Please correct the information in invalid metric fields and try again. Invalid metric fields: ['campaign_name', 'page_content_view_events', 'ad_name', 'conversion_rate_v2', 'engagements']. ",
# "request_id": " ",
# "data": {}
# }
# No data found in response or incorrect response structure.
I’ve tried adjusting data_level parameters without success. How can I resolve this issue?
New contributor
Ferdiansyah Ersatiyo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.