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
Metadata
Release files for TikTokMarketing 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| tiktokmarketing-0.1.5.tar.gz | 5.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| TikTokMarketing-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / tiktokmarketing-0.1.5.tar.gz
| Download URL | tiktokmarketing-0.1.5.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c428b6e88c2738d21b081f987fb34f0f642721e639528bd2976df23470acda68
|
|
BLAKE2b-256 checksum How to use checksums |
ea274ae13716e479d8d2d7ed1a1022c9557f908bed9ec43355495616643b7265
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.0
|
Release files / TikTokMarketing-0.1.5-py3-none-any.whl
| Download URL | TikTokMarketing-0.1.5-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a08617bcdc5139419adf228ee2b9a2b5b09b6c0adde8be1555cd45da5c59cbf6
|
|
BLAKE2b-256 checksum How to use checksums |
f8af143275a924109b4dbcb50822364e680313ea55fcf8e7cab85d33410c7b37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.0
|
Security for TikTokMarketing 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.