A package designed to connect to the TikTok Marketing API and return data to report on.
Project description
TikTokMarketing 
This package enables efficient querying and retrieval of performance metrics from the TikTok Marketing API on different levels of granularity.
Build status
Requirements
- Python 3.8+
Installation
pip install TikTokMarketing
Features
- Distributed via PyPI.
- Wrapper around the TikTok Marketing API to assist marketers on a budget with their reporting.
- Under the hood
TikTokMarketingmakes use of therequestsandjsonmodules. - It has no additional dependencies.
TikTokMarketingsupports the latest version of the APIv1.3- Supports both basic and audience reporting
- Includes human-readable interest category mapping
Data Levels
The API returns data on 4 distinct levels:
- AUCTION_ADVERTISER
- AUCTION_CAMPAIGN
- AUCTION_ADGROUP
- AUCTION_AD
For available dimensions and metrics, refer to the official documentation:
Example Usage
Basic Setup
from TikTokMarketing import TikTokAPI, InterestCategories
from datetime import datetime, timedelta
# Initialize the API
api = TikTokAPI('YOUR_ACCESS_TOKEN')
# Set up date range
end_date = datetime.now().strftime('%Y-%m-%d')
start_date = (datetime.now() - timedelta(days=7)).strftime('%Y-%m-%d')
Basic Reporting
# Basic reporting at advertiser level
basic_result = api.get_data(
start_date=start_date,
end_date=end_date,
dimensions=['advertiser_id', 'stat_time_day'],
metrics=['spend', 'impressions', 'clicks'],
advertiser_id='YOUR_ADVERTISER_ID',
data_level='AUCTION_ADVERTISER'
)
Audience Reporting
Age Distribution
age_result = api.get_data(
start_date=start_date,
end_date=end_date,
dimensions=['age'],
metrics=['spend', 'impressions', 'clicks'],
advertiser_id='YOUR_ADVERTISER_ID',
data_level='AUCTION_ADVERTISER',
report_type='AUDIENCE',
audience_dimensions=['age'],
order_field='spend',
order_type='ASC'
)
Gender and Age Combined
gender_age_result = api.get_data(
start_date=start_date,
end_date=end_date,
dimensions=['gender', 'age', 'stat_time_day'],
metrics=['spend', 'impressions', 'reach'],
advertiser_id='YOUR_ADVERTISER_ID',
data_level='AUCTION_ADVERTISER',
report_type='AUDIENCE',
audience_dimensions=['gender', 'age']
)
Geographic Distribution
country_result = api.get_data(
start_date=start_date,
end_date=end_date,
dimensions=['country_code'],
metrics=['impressions', 'reach', 'spend'],
advertiser_id='YOUR_ADVERTISER_ID',
data_level='AUCTION_ADVERTISER',
report_type='AUDIENCE',
audience_dimensions=['country_code'],
order_field='impressions',
order_type='DESC'
)
Audience Network Analysis
network_result = api.get_data(
start_date=start_date,
end_date=end_date,
dimensions=['ac'],
metrics=['impressions', 'reach', 'spend'],
advertiser_id='YOUR_ADVERTISER_ID',
data_level='AUCTION_ADVERTISER',
report_type='AUDIENCE',
audience_dimensions=['ac'],
order_field='impressions',
order_type='DESC'
)
Interest Categories
# Get and map interest categories
interest_result = InterestCategories.map_response(
api.get_data(
start_date=start_date,
end_date=end_date,
dimensions=['interest_category'],
metrics=['impressions', 'reach', 'spend', 'clicks'],
advertiser_id='YOUR_ADVERTISER_ID',
data_level='AUCTION_ADVERTISER',
report_type='AUDIENCE',
audience_dimensions=['interest_category'],
order_field='impressions',
order_type='DESC'
)
)
Converting Results to Pandas DataFrame
import pandas as pd
# Create empty dictionary with column names
dimensions = ['stat_time_day', 'advertiser_id']
metrics = ['spend', 'impressions', 'clicks']
all_fields = dimensions + metrics
TikTok_Data = TikTokAPI.create_dict(input_list=all_fields)
# Populate dictionary from API response
result_list = basic_result.get('data', {}).get('list', [])
for entry in result_list:
for fieldname in TikTok_Data.keys():
if fieldname in entry.get('dimensions', {}):
TikTok_Data[fieldname].append(entry['dimensions'][fieldname])
else:
TikTok_Data[fieldname].append(entry['metrics'][fieldname])
# Convert to DataFrame
df = pd.DataFrame(TikTok_Data)
Note
When running TikTokMarketing in a docker container, the ip address might be blocked with the API response: Client IP address is in banned Country list
Available Parameters
Report Types
- BASIC
- AUDIENCE
Audience Dimensions
- gender
- age
- country_code
- ac (Audience network)
- platform
- interest_category
- language
- device_brand
- device_model
- device_price
- network
Order Types
- ASC
- DESC
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tiktokmarketing-0.1.5.tar.gz.
File metadata
- Download URL: tiktokmarketing-0.1.5.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c428b6e88c2738d21b081f987fb34f0f642721e639528bd2976df23470acda68
|
|
| MD5 |
d4bfa8172203e81c04071883834a5b5d
|
|
| BLAKE2b-256 |
ea274ae13716e479d8d2d7ed1a1022c9557f908bed9ec43355495616643b7265
|
File details
Details for the file TikTokMarketing-0.1.5-py3-none-any.whl.
File metadata
- Download URL: TikTokMarketing-0.1.5-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a08617bcdc5139419adf228ee2b9a2b5b09b6c0adde8be1555cd45da5c59cbf6
|
|
| MD5 |
cde0319920e4cb15c58fcf899bb0631f
|
|
| BLAKE2b-256 |
f8af143275a924109b4dbcb50822364e680313ea55fcf8e7cab85d33410c7b37
|