:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from collections import defaultdict
from collections.abc import Iterator
from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from products.warehouse_sources.sources.appfigures.settings import APPFIGURES_ENDPOINTS, AppfiguresEndpointConfig
from products.warehouse_sources.sources.sdk import (
ResumableSourceManager,
SourceResponse,
frozen,
make_tracked_session,
retry_with_deadline,
)
APPFIGURES_BASE_URL = "https://api.appfigures.com/v2"
# Report endpoints (group_by=dates) have no "fetch everything" mode that's safe to stream — omitting
# the window returns the entire account history in one un-paginated response, and daily granularity is
# capped at 30 days per request. So the first sync backfills this many days, then incremental syncs
# advance from the stored date watermark.
REPORT_BACKFILL_DAYS = 365
# /ranks needs product ids in its path, so the fan-out reads the account catalog first. In-app
# purchases never hold a store category rank, so they are left out of the request.
PRODUCTS_PATH = "/products/mine"
NON_RANKABLE_PRODUCT_TYPES = frozenset({"inapp"})
# /aso takes one product and one country per request and Appfigures publishes no way to list the
# countries an account tracks keywords in, so the countries come from the source form. Fall back to
# the US storefront, which is the one nearly every account tracks.
DEFAULT_ASO_COUNTRIES = ("US",)
class AppfiguresRetryableError(Exception):
pass
class AppfiguresPageLimitError(Exception):
"""Appfigures caps offset pagination at page*count <= 10000 on list endpoints (e.g. reviews).
Past the cap it returns 400 with a stable "must be less than or equal to 10000" message. `_fetch`
raises this so the caller stops paginating and keeps the rows gathered so far, rather than letting
a benign end-of-traversable-data response fail the whole sync via `raise_for_status`."""
pass
@frozen
class AppfiguresResumeConfig:
# "paged" endpoints (reviews): the next page number to fetch (1-based).
next_page: int | None = None
# "report" endpoints: the next date window's start (yyyy-mm-dd) to fetch.
window_start: str | None = None
# "aso" endpoints: the "<product_id>:<country>" fan-out target to pick back up at.
aso_target: str | None = None
def _headers(token: str) -> dict[str, str]:
# Personal Access Tokens and OAuth 2.0 tokens both ride the standard bearer header; the PAT also
# carries the API client identity, so no separate client-key header is needed.
return {"Authorization": f"Bearer {token}", "Accept": "application/json"}
def _to_date_str(value: Any) -> str | None:
"""Format a datetime/date/ISO-string incremental value as the yyyy-mm-dd Appfigures expects."""
if value is None:
return None
if isinstance(value, datetime):
return value.astimezone(UTC).date().isoformat() if value.tzinfo else value.date().isoformat()
if isinstance(value, date):
return value.isoformat()
# Already a string — take the date portion (handles "2024-01-02T03:04:05" and "2024-01-02").
text = str(value)[:10]
try:
date.fromisoformat(text)
except ValueError as e:
# Surface a descriptive error here rather than letting the partial fragment blow up deep in
# _iter_report's date.fromisoformat call.
raise ValueError(f"Could not derive a yyyy-mm-dd date from incremental value {value!r}") from e
return text
def _is_page_limit_response(response: requests.Response) -> bool:
"""Detect the page*count>10000 offset-pagination cap. Appfigures surfaces the message via the
HTTP reason phrase (picked up by `raise_for_status`), but check the body too in case it's there
instead."""
if response.status_code != 400:
return False
text = f"{response.reason or ''} {response.text or ''}"
return "must be less than or equal to 10000" in text.lower()
@retry_with_deadline(
retry=retry_if_exception_type((AppfiguresRetryableError, requests.ReadTimeout, requests.ConnectionError)),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=1, max=30),
reraise=True,
)
def _fetch(
session: requests.Session,
url: str,
params: dict[str, Any],
logger: FilteringBoundLogger,
) -> dict[str, Any]:
response = session.get(url, params=params, timeout=60)
# Quota is a monthly credit model rather than per-second, so 429 is rare, but treat it and
# transient 5xx as retryable. Everything else (including 401/403) raises for the caller.
if response.status_code == 429 or response.status_code >= 500:
raise AppfiguresRetryableError(f"Appfigures API error (retryable): status={response.status_code}, url={url}")
if _is_page_limit_response(response):
raise AppfiguresPageLimitError()
if not response.ok:
logger.error(f"Appfigures API error: status={response.status_code}, body={response.text}, url={url}")
response.raise_for_status()
return response.json()
def check_credentials(token: str, path: str = "/products/mine") -> int | None:
"""Probe an endpoint and return its HTTP status, or None on a network failure.
The source layer maps the status: 200 = valid, 401 = bad token, 403 = valid token missing the
scope for that endpoint.
"""
try:
response = make_tracked_session(headers=_headers(token), redact_values=(token,)).get(
f"{APPFIGURES_BASE_URL}{path}", params={"count": 1}, timeout=10
)
return response.status_code
except Exception:
return None
def _iter_object(
session: requests.Session,
config: AppfiguresEndpointConfig,
logger: FilteringBoundLogger,
) -> Iterator[list[dict[str, Any]]]:
"""Single-request endpoints whose body is a JSON object keyed by id (e.g. /products/mine)."""
data = _fetch(session, f"{APPFIGURES_BASE_URL}{config.path}", {}, logger)
rows = list(data.values()) if isinstance(data, dict) else data
if rows:
yield rows
def _iter_paged(
session: requests.Session,
config: AppfiguresEndpointConfig,
logger: FilteringBoundLogger,
manager: ResumableSourceManager[AppfiguresResumeConfig],
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
"""Page/count paginated endpoints with a flat list under `data_key` (e.g. /reviews)."""
resume = manager.load_state() if manager.can_resume() else None
page = resume.next_page if resume and resume.next_page else 1
params: dict[str, Any] = {"count": config.page_size, "page": page}
if config.sort:
params["sort"] = config.sort
if should_use_incremental_field and config.start_param:
start = _to_date_str(db_incremental_field_last_value)
if start:
params[config.start_param] = start
url = f"{APPFIGURES_BASE_URL}{config.path}"
while True:
try:
data = _fetch(session, url, params, logger)
except AppfiguresPageLimitError:
logger.info(f"Appfigures: reached page-depth cap, stopping at rows gathered so far: url={url}")
break
rows = data.get(config.data_key, []) if config.data_key else []
total_pages = data.get("pages", 1) or 1
this_page = data.get("this_page", page) or page
if rows:
yield rows
if not rows or this_page >= total_pages:
break
page += 1
params["page"] = page
# Save AFTER yielding so a crash re-fetches the page we were on rather than skipping it;
# merge dedupes the re-pulled rows on the primary key.
manager.save_state(AppfiguresResumeConfig(next_page=page))
def _initial_window_start(
manager: ResumableSourceManager[AppfiguresResumeConfig],
today: date,
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
) -> date:
"""Where a date-windowed walk picks up: saved resume state, then the incremental watermark, then
the backfill horizon."""
resume = manager.load_state() if manager.can_resume() else None
if resume and resume.window_start:
return date.fromisoformat(resume.window_start)
if should_use_incremental_field and db_incremental_field_last_value:
last = _to_date_str(db_incremental_field_last_value)
if last:
return date.fromisoformat(last)
return today - timedelta(days=REPORT_BACKFILL_DAYS)
def _flatten_report(data: Any) -> list[dict[str, Any]]:
"""Turn a group_by=dates report body ({"2024-01-01": {..metrics..}, ...}) into dated rows.
Only dict-valued entries are kept (defensive against any non-date envelope keys), and `date` is
injected so the row is uniquely keyed and partitionable. Sorted ascending so the watermark advances.
"""
if not isinstance(data, dict):
return []
rows: list[dict[str, Any]] = []
for day in sorted(data.keys()):
metrics = data[day]
if isinstance(metrics, dict):
rows.append({"date": day, **metrics})
return rows
def _iter_report(
session: requests.Session,
config: AppfiguresEndpointConfig,
logger: FilteringBoundLogger,
manager: ResumableSourceManager[AppfiguresResumeConfig],
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
"""Reports/* endpoints walked in fixed date windows (daily granularity caps at 30 days/request)."""
today = datetime.now(UTC).date()
window_days = config.window_days or 30
window_start = _initial_window_start(manager, today, should_use_incremental_field, db_incremental_field_last_value)
url = f"{APPFIGURES_BASE_URL}{config.path}"
while window_start <= today:
window_end = min(window_start + timedelta(days=window_days - 1), today)
params: dict[str, Any] = {
"group_by": config.group_by,
"granularity": config.granularity,
"start_date": window_start.isoformat(),
"end_date": window_end.isoformat(),
}
rows = _flatten_report(_fetch(session, url, params, logger))
if rows:
yield rows
if window_end >= today:
break
window_start = window_end + timedelta(days=1)
# Save AFTER yielding the window so a crash re-fetches it rather than skipping; merge dedupes
# the re-pulled dates on the `date` primary key.
manager.save_state(AppfiguresResumeConfig(window_start=window_start.isoformat()))
def _flatten_ranks(data: Any) -> list[dict[str, Any]]:
"""Turn a columnar /ranks body into one row per product, country, category, and date.
The body holds a `dates` array plus a series per product/country/category whose `positions` and
`deltas` arrays line up with it by index.
"""
if not isinstance(data, dict):
return []
dates = data.get("dates") or []
rows: list[dict[str, Any]] = []
for series in data.get("data") or []:
if not isinstance(series, dict):
continue
category = series.get("category") or {}
positions = series.get("positions") or []
deltas = series.get("deltas") or []
for index, day in enumerate(dates):
position = positions[index] if index < len(positions) else None
# A null position means the product held no rank in that chart that day. The default rank
# depth makes that the common case, so skipping keeps the table to real observations.
if position is None:
continue
rows.append(
{
"date": day,
"product_id": series.get("product_id"),
"country": series.get("country"),
"category_id": category.get("id"),
"category_name": category.get("name"),
"category_subtype": category.get("subtype"),
"category_parent_id": category.get("parent_id"),
"category_device": category.get("device"),
"store": category.get("store"),
"position": position,
# Appfigures documents this delta as the change from the prior hour even at daily
# granularity, so it is not a day-over-day move.
"delta": deltas[index] if index < len(deltas) else None,
}
)
return rows
def _rankable_product_ids(
session: requests.Session,
logger: FilteringBoundLogger,
) -> list[str]:
data = _fetch(session, f"{APPFIGURES_BASE_URL}{PRODUCTS_PATH}", {}, logger)
products = data.values() if isinstance(data, dict) else data or []
ids = {
str(product["id"])
for product in products
if isinstance(product, dict)
and product.get("id") is not None
and product.get("type") not in NON_RANKABLE_PRODUCT_TYPES
}
# Sorted so the request chunks are stable across syncs and resumes.
return sorted(ids)
def _iter_ranks(
session: requests.Session,
config: AppfiguresEndpointConfig,
logger: FilteringBoundLogger,
manager: ResumableSourceManager[AppfiguresResumeConfig],
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
"""/ranks, fanned out over the account's products in chunks and walked in date windows."""
product_ids = _rankable_product_ids(session, logger)
if not product_ids:
logger.info("Appfigures: account has no rankable products, skipping ranks")
return
chunk_size = config.products_per_request
chunks = [product_ids[index : index + chunk_size] for index in range(0, len(product_ids), chunk_size)]
today = datetime.now(UTC).date()
window_days = config.window_days or 30
window_start = _initial_window_start(manager, today, should_use_incremental_field, db_incremental_field_last_value)
while window_start <= today:
window_end = min(window_start + timedelta(days=window_days - 1), today)
# Collect the whole window across product chunks before yielding, so rows still leave this
# iterator in ascending date order and the incremental watermark checkpoints correctly.
rows_by_date: dict[str, list[dict[str, Any]]] = defaultdict(list)
for chunk in chunks:
url = (
f"{APPFIGURES_BASE_URL}{config.path}/{','.join(chunk)}/{config.granularity}"
f"/{window_start.isoformat()}/{window_end.isoformat()}"
)
for row in _flatten_ranks(_fetch(session, url, {}, logger)):
rows_by_date[row["date"]].append(row)
for day in sorted(rows_by_date):
yield rows_by_date[day]
if window_end >= today:
break
window_start = window_end + timedelta(days=1)
# Save AFTER yielding the window so a crash re-fetches it rather than skipping; merge dedupes
# the re-pulled rows on the primary key.
manager.save_state(AppfiguresResumeConfig(window_start=window_start.isoformat()))
def _parse_aso_countries(raw: str | None) -> tuple[str, ...]:
"""Read the comma-separated ISO country codes from the source form.
Anything that is not a two-letter code is dropped, which keeps a typo from becoming a request
Appfigures rejects and bounds the fan-out to the size of the ISO country list.
"""
codes = [code.strip().upper() for code in (raw or "").split(",")]
unique = list(dict.fromkeys(code for code in codes if len(code) == 2 and code.isalpha()))
return tuple(unique) or DEFAULT_ASO_COUNTRIES
@frozen
class AsoTarget:
"""One /aso request's scope, because Appfigures takes a single product and country per call."""
product_id: str
country: str
@property
def key(self) -> str:
"""Identifies the target in saved resume state."""
return f"{self.product_id}:{self.country}"
def _aso_targets(
session: requests.Session,
logger: FilteringBoundLogger,
aso_countries: str | None,
) -> list[AsoTarget]:
countries = _parse_aso_countries(aso_countries)
return [
AsoTarget(product_id=product_id, country=country)
for product_id in _rankable_product_ids(session, logger)
for country in countries
]
def _resume_aso_slice(
manager: ResumableSourceManager[AppfiguresResumeConfig],
targets: list[AsoTarget],
) -> tuple[int, int | None]:
"""Where a fan-out picks up: the saved target's index and, for paged endpoints, its page."""
resume = manager.load_state() if manager.can_resume() else None
if not resume or not resume.aso_target:
return 0, None
keys = [target.key for target in targets]
if resume.aso_target not in keys:
# The catalog or the country list changed since the state was saved, so start over.
return 0, None
return keys.index(resume.aso_target), resume.next_page
def _aso_window_params(config: AppfiguresEndpointConfig, today: date) -> dict[str, Any]:
"""The trend window /aso computes its deltas over. Bounded explicitly because Appfigures does not
document what range it picks when the window is left off."""
params: dict[str, Any] = {}
if config.granularity:
params["granularity"] = config.granularity
if config.start_param and config.end_param:
params[config.start_param] = (today - timedelta(days=(config.window_days or 30) - 1)).isoformat()
params[config.end_param] = today.isoformat()
return params
def _iter_aso(
session: requests.Session,
config: AppfiguresEndpointConfig,
logger: FilteringBoundLogger,
manager: ResumableSourceManager[AppfiguresResumeConfig],
aso_countries: str | None,
) -> Iterator[list[dict[str, Any]]]:
"""/aso, fanned out over one product and one country per request and paged per target."""
targets = _aso_targets(session, logger, aso_countries)
if not targets:
logger.info("Appfigures: account has no products to track keywords for, skipping aso_keywords")
return
start_index, resume_page = _resume_aso_slice(manager, targets)
today = datetime.now(UTC).date()
window = _aso_window_params(config, today)
today_str = today.isoformat()
url = f"{APPFIGURES_BASE_URL}{config.path}"
for index in range(start_index, len(targets)):
target = targets[index]
page = resume_page if index == start_index and resume_page else 1
while True:
params: dict[str, Any] = {
# Appfigures supports no other pivot on this endpoint today.
"group_by": "keyword",
"products": target.product_id,
"countries": target.country,
"page": page,
**window,
}
try:
data = _fetch(session, url, params, logger)
except AppfiguresPageLimitError:
logger.info(f"Appfigures: reached page-depth cap for keywords of target={target.key}")
break
rows = data.get(config.data_key) or [] if config.data_key else []
resultset = (data.get("metadata") or {}).get("resultset") or {}
total_pages = resultset.get("total_pages") or 1
this_page = resultset.get("page") or page
if rows:
for row in rows:
# The response carries the keyword only, so the request's own identifiers and the
# day it ran are what make the row unique.
row.setdefault("date", today_str)
row.setdefault("product_id", target.product_id)
row.setdefault("country", target.country)
yield rows
if not rows or this_page >= total_pages:
break
page += 1
# Save AFTER yielding so a crash re-fetches the page we were on rather than skipping it.
manager.save_state(AppfiguresResumeConfig(aso_target=target.key, next_page=page))
if index + 1 < len(targets):
manager.save_state(AppfiguresResumeConfig(aso_target=targets[index + 1].key))
def _iter_aso_stats(
session: requests.Session,
config: AppfiguresEndpointConfig,
logger: FilteringBoundLogger,
manager: ResumableSourceManager[AppfiguresResumeConfig],
aso_countries: str | None,
) -> Iterator[list[dict[str, Any]]]:
"""/aso/stats, one flat object of keyword aggregates per product and country."""
targets = _aso_targets(session, logger, aso_countries)
if not targets:
logger.info("Appfigures: account has no products to track keywords for, skipping aso_stats")
return
start_index, _ = _resume_aso_slice(manager, targets)
today = datetime.now(UTC).date()
window = _aso_window_params(config, today)
today_str = today.isoformat()
url = f"{APPFIGURES_BASE_URL}{config.path}"
for index in range(start_index, len(targets)):
target = targets[index]
params: dict[str, Any] = {"products": target.product_id, "countries": target.country, **window}
data = _fetch(session, url, params, logger)
if isinstance(data, dict) and data:
yield [{"date": today_str, "product_id": target.product_id, "country": target.country, **data}]
if index + 1 < len(targets):
manager.save_state(AppfiguresResumeConfig(aso_target=targets[index + 1].key))
def get_rows(
token: str,
endpoint: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[AppfiguresResumeConfig],
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Any = None,
aso_countries: str | None = None,
) -> Iterator[list[dict[str, Any]]]:
config = APPFIGURES_ENDPOINTS[endpoint]
session = make_tracked_session(headers=_headers(token), redact_values=(token,))
if config.kind == "object":
yield from _iter_object(session, config, logger)
elif config.kind == "paged":
yield from _iter_paged(
session,
config,
logger,
resumable_source_manager,
should_use_incremental_field,
db_incremental_field_last_value,
)
elif config.kind == "aso":
yield from _iter_aso(session, config, logger, resumable_source_manager, aso_countries)
elif config.kind == "aso_stats":
yield from _iter_aso_stats(session, config, logger, resumable_source_manager, aso_countries)
elif config.kind == "ranks":
yield from _iter_ranks(
session,
config,
logger,
resumable_source_manager,
should_use_incremental_field,
db_incremental_field_last_value,
)
else: # "report"
yield from _iter_report(
session,
config,
logger,
resumable_source_manager,
should_use_incremental_field,
db_incremental_field_last_value,
)
def appfigures_source(
token: str,
endpoint: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[AppfiguresResumeConfig],
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Optional[Any] = None,
aso_countries: Optional[str] = None,
) -> SourceResponse:
config = APPFIGURES_ENDPOINTS[endpoint]
return SourceResponse(
name=endpoint,
items=lambda: get_rows(
token=token,
endpoint=endpoint,
logger=logger,
resumable_source_manager=resumable_source_manager,
should_use_incremental_field=should_use_incremental_field,
db_incremental_field_last_value=db_incremental_field_last_value,
aso_countries=aso_countries,
),
primary_keys=config.primary_keys,
partition_count=1,
partition_size=1,
partition_mode="datetime" if config.partition_key else None,
partition_format="month" if config.partition_key else None,
partition_keys=[config.partition_key] if config.partition_key else None,
# Reviews are paged ascending by `date`; reports and ranks are emitted oldest-window-first,
# ranks buffering each window so its product chunks merge back into date order. All arrive
# ascending so the incremental watermark checkpoints correctly. The ASO tables are snapshots
# of a single day, so ordering is moot for them.
sort_mode="asc",
)