Skip to content

: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.

feat(data-warehouse): add applovin user revenue and campaign tables (#116970)

Tom Owers29926d6baba6
diff --git a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
index 639913b96f4f..ff6aad45b55a 100644
--- a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
+++ b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
@@ -250,7 +250,6 @@
1 products/warehouse_sources/backend/temporal/data_imports/sources/appfigures/appfigures.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/appfigures/settings.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/appfollow/settings.py
-1 products/warehouse_sources/backend/temporal/data_imports/sources/applovin/applovin.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/applovin/settings.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/appsflyer/settings.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/appsignal/appsignal.py
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
index f8629272251a..838b9fc7ceaa 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
@@ -747,11 +747,11 @@ Today (7): `advertiser_report`, `max_ad_revenue`, `max_ad_unit_revenue`, `max_co
Diffed against: <https://support.applovin.com/en/growth/promoting-your-apps/api/axon-campaign-management-api>
-- [ ] `User-Level Ad Revenue (GET /max/userAdRevenueReport)` — connects user and impression revenue to product activity for attribution and lifetime value analysis (high)
-- [ ] `Campaigns (GET /manage/v1/campaign/list)` — resolves campaign_id_external through hashed_id and adds campaign status, targeting, and budgets (high)
-- [ ] `Creative sets (GET /manage/v1/creative_set/list)` — describes creative content and targeting for campaign performance analysis (medium)
+- [x] `User-Level Ad Revenue (GET /max/userAdRevenueReport)` — connects user and impression revenue to product activity for attribution and lifetime value analysis (high)
+- [x] `Campaigns (GET /manage/v1/campaign/list)` — resolves campaign_id_external through hashed_id and adds campaign status, targeting, and budgets (high)
+- [x] `Creative sets (GET /manage/v1/creative_set/list)` — describes creative content and targeting for campaign performance analysis (medium)
- [ ] `Assets (GET /manage/v1/asset/list)` — identifies the images, videos, and playable assets used in creative sets (medium)
-- [ ] `Campaign operation logs (GET /manage/v1/operation_logs/campaign)` — explains performance changes after campaign edits, pauses, and budget changes (medium)
+- [x] `Campaign operation logs (GET /manage/v1/operation_logs/campaign)` — explains performance changes after campaign edits, pauses, and budget changes (medium)
Note: Compared collection GET operations in the official Campaign Management API reference with the reporting endpoint map. Also checked https://support.applovin.com/en/max/reporting-apis/user-level-ad-revenue-api for user and impression revenue exports. Campaign reads need a separate key; revenue exports need application and date requests, followed by CSV downloads. Excluded uploads, writes, and report column choices.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/applovin.py b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/applovin.py
index efeef5683459..aef30fa4e688 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/applovin.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/applovin.py
@@ -1,22 +1,40 @@
+import csv
+import codecs
import dataclasses
from collections.abc import Iterator
from datetime import UTC, date, datetime, timedelta
+from decimal import Decimal, InvalidOperation
from typing import Any, Optional
from urllib.parse import urlencode
import requests
from structlog.types import FilteringBoundLogger
+from posthog.dataclasses import frozen
+
from products.warehouse_sources.backend.temporal.data_imports.sources.applovin.settings import (
APPLOVIN_API_BASE_URL,
APPLOVIN_ENDPOINTS,
+ APPLOVIN_MANAGEMENT_API_BASE_URL,
+ MANAGEMENT_ENDPOINTS,
+ MANAGEMENT_PAGE_SIZE,
MAX_REQUEST_WINDOW_DAYS,
+ OPERATION_LOG_WINDOW_DAYS,
+ PRIMARY_KEYS,
REPORT_LOOKBACK_DAYS,
REPORT_PAGE_SIZE,
REPORT_WINDOW_DAYS,
+ USER_AD_REVENUE_APPS,
+ USER_AD_REVENUE_BATCH_SIZE,
+ USER_AD_REVENUE_ENDPOINT,
+ USER_AD_REVENUE_LOOKBACK_DAYS,
+ USER_AD_REVENUE_PATH,
+ USER_AD_REVENUE_PRIMARY_KEYS,
AppLovinEndpointConfig,
+ AppLovinManagementEndpointConfig,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_session
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.http.observer import redact_request_urls
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http.url_utils import redact_literal_values
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
@@ -27,6 +45,8 @@ VALIDATION_TIMEOUT_SECONDS = 30
# Matched by `AppLovinSource.get_non_retryable_errors`, so keep these stable.
AUTH_ERROR_PREFIX = "AppLovin denied the request"
BAD_REQUEST_ERROR_PREFIX = "AppLovin rejected the report request"
+MANAGEMENT_AUTH_ERROR_PREFIX = "AppLovin denied the Campaign Management API request"
+MANAGEMENT_CREDENTIALS_MISSING_PREFIX = "AppLovin Campaign Management API credentials are missing"
# Deliberately NOT registered as non-retryable: the tracked session already retried the request,
# and an upstream outage must not permanently disable the source.
TRANSIENT_ERROR_PREFIX = "AppLovin API error (retryable)"
@@ -64,12 +84,12 @@ def _to_date(value: Any) -> Optional[date]:
return None
-def _body_code(body: Any) -> Optional[int]:
- """AppLovin echoes its own status in the JSON body's `code` key; 200 means success."""
+def _body_code(body: Any, key: str = "code") -> Optional[int]:
+ """AppLovin echoes its own status in the JSON body (`code`, or `status` on some endpoints); 200 means success."""
if not isinstance(body, dict):
return None
try:
- return int(body["code"])
+ return int(body[key])
except (KeyError, TypeError, ValueError):
return None
@@ -87,6 +107,44 @@ def _prefix_for_code(code: int) -> str:
return BAD_REQUEST_ERROR_PREFIX
+def _get(
+ session: requests.Session, url: str, secret: str, timeout: int = REQUEST_TIMEOUT_SECONDS, stream: bool = False
+) -> requests.Response:
+ try:
+ return session.get(url, timeout=timeout, stream=stream)
+ except requests.RequestException as e:
+ # `requests`/`urllib3` transport errors quote the full request URL, which can carry the
+ # key as a query param. Re-raise redacted, and drop the cause so the original
+ # (unredacted) message can't be surfaced through the exception chain.
+ raise AppLovinAPIError(f"AppLovin request failed: {_redacted(str(e), secret)}") from None
+
+
+def _raise_for_status(
+ response: requests.Response, path: str, secret: str, auth_prefix: str = AUTH_ERROR_PREFIX
+) -> None:
+ if response.status_code in _AUTH_STATUS_CODES:
+ raise AppLovinAPIError(f"{auth_prefix}: status={response.status_code}, endpoint={path}")
+
+ if _is_transient_status(response.status_code):
+ raise AppLovinAPIError(f"{TRANSIENT_ERROR_PREFIX}: status={response.status_code}, endpoint={path}")
+
+ if not response.ok:
+ raise AppLovinAPIError(
+ f"{BAD_REQUEST_ERROR_PREFIX}: status={response.status_code}, endpoint={path}, "
+ f"body={_redacted(response.text, secret)[:500]}"
+ )
+
+
+def _json_body(response: requests.Response, path: str, secret: str) -> Any:
+ try:
+ return response.json()
+ except ValueError:
+ raise AppLovinAPIError(
+ f"{BAD_REQUEST_ERROR_PREFIX}: endpoint={path} returned a non-JSON body: "
+ f"{_redacted(response.text, secret)[:500]}"
+ ) from None
+
+
def _build_url(config: AppLovinEndpointConfig, api_key: str, start: date, end: date, offset: int) -> str:
params: dict[str, Any] = {
"api_key": api_key,
@@ -113,35 +171,9 @@ def _fetch_page(
offset: int,
logger: FilteringBoundLogger,
) -> list[dict[str, Any]]:
- url = _build_url(config, api_key, start, end, offset)
-
- try:
- response = session.get(url, timeout=REQUEST_TIMEOUT_SECONDS)
- except requests.RequestException as e:
- # `requests`/`urllib3` transport errors quote the full request URL, which carries the
- # Report Key as a query param. Re-raise redacted, and drop the cause so the original
- # (unredacted) message can't be surfaced through the exception chain.
- raise AppLovinAPIError(f"AppLovin request failed: {_redacted(str(e), api_key)}") from None
-
- if response.status_code in _AUTH_STATUS_CODES:
- raise AppLovinAPIError(f"{AUTH_ERROR_PREFIX}: status={response.status_code}, endpoint={config.path}")
-
- if _is_transient_status(response.status_code):
- raise AppLovinAPIError(f"{TRANSIENT_ERROR_PREFIX}: status={response.status_code}, endpoint={config.path}")
-
- if not response.ok:
- raise AppLovinAPIError(
- f"{BAD_REQUEST_ERROR_PREFIX}: status={response.status_code}, endpoint={config.path}, "
- f"body={_redacted(response.text, api_key)[:500]}"
- )
-
- try:
- body = response.json()
- except ValueError:
- raise AppLovinAPIError(
- f"{BAD_REQUEST_ERROR_PREFIX}: endpoint={config.path} returned a non-JSON body: "
- f"{_redacted(response.text, api_key)[:500]}"
- ) from None
+ response = _get(session, _build_url(config, api_key, start, end, offset), api_key)
+ _raise_for_status(response, config.path, api_key)
+ body = _json_body(response, config.path, api_key)
code = _body_code(body)
if code is not None and code != 200:
@@ -155,12 +187,14 @@ def _fetch_page(
return [row for row in results if isinstance(row, dict)]
[email protected](frozen=True)
class AppLovinResumeConfig:
# First day (YYYY-MM-DD) of the report window still being fetched.
next_window_start: Optional[str] = None
# `offset` of the next unfetched page within that window.
next_offset: int = 0
+ # Page number of the next unfetched Campaign Management list page.
+ next_page: int = 0
def get_rows(
@@ -226,6 +260,308 @@ def get_rows(
resumable_source_manager.clear_state()
+@frozen(order=True)
+class AppLovinApp:
+ package_name: str
+ platform: str
+
+
+def _user_ad_revenue_apps(
+ session: requests.Session, api_key: str, day: date, logger: FilteringBoundLogger
+) -> list[AppLovinApp]:
+ """List the apps with MAX impressions on one day, in a stable order."""
+ apps: set[AppLovinApp] = set()
+ offset = 0
+ while True:
+ rows = _fetch_page(session, USER_AD_REVENUE_APPS, api_key, day, day, offset, logger)
+ for row in rows:
+ package_name, platform = row.get("package_name"), row.get("platform")
+ if package_name and platform:
+ apps.add(AppLovinApp(package_name=str(package_name), platform=str(platform).lower()))
+ if len(rows) < REPORT_PAGE_SIZE:
+ return sorted(apps)
+ offset += REPORT_PAGE_SIZE
+
+
+def _to_int(value: str) -> Optional[int]:
+ try:
+ return int(value)
+ except ValueError:
+ return None
+
+
+def _to_decimal(value: str) -> Optional[Decimal]:
+ try:
+ return Decimal(value) if value.strip() else None
+ except InvalidOperation:
+ return None
+
+
+def _normalize_csv_column(header: str) -> str:
+ # The CSV headers vary in case and spacing between report versions ("User Id", " Ad Placement").
+ return "_".join(header.strip().lower().split())
+
+
+def _user_ad_revenue_report(
+ session: requests.Session,
+ api_key: str,
+ day: date,
+ app: AppLovinApp,
+ logger: FilteringBoundLogger,
+) -> Iterator[list[dict[str, Any]]]:
+ package_name, platform = app.package_name, app.platform
+ params = {
+ "api_key": api_key,
+ "date": day.isoformat(),
+ "platform": platform,
+ "application": package_name,
+ "aggregated": "true",
+ }
+ response = _get(session, f"{APPLOVIN_API_BASE_URL}{USER_AD_REVENUE_PATH}?{urlencode(params)}", api_key)
+ if response.status_code == 404:
+ logger.debug(f"AppLovin: no user-level ad revenue report for {package_name} on {day.isoformat()}")
+ return
+ _raise_for_status(response, USER_AD_REVENUE_PATH, api_key)
+ body = _json_body(response, USER_AD_REVENUE_PATH, api_key)
+
+ status = _body_code(body, key="status")
+ if status == 404:
+ return
+ if status is not None and status != 200:
+ raise AppLovinAPIError(f"{_prefix_for_code(status)}: body status={status}, endpoint={USER_AD_REVENUE_PATH}")
+
+ report_url = body.get("ad_revenue_report_url") if isinstance(body, dict) else None
+ if not isinstance(report_url, str) or not report_url:
+ return
+
+ # The pre-signed link is a bearer credential for the report, so keep it out of request logs and errors.
+ try:
+ with redact_request_urls():
+ download = session.get(report_url, timeout=REQUEST_TIMEOUT_SECONDS, stream=True)
+ except requests.RequestException as e:
+ raise AppLovinAPIError(
+ f"{TRANSIENT_ERROR_PREFIX}: report download failed ({type(e).__name__}), endpoint={USER_AD_REVENUE_PATH}"
+ ) from None
+
+ with download:
+ if not download.ok:
+ # The download link is pre-signed and expires after an hour, so a failed download is
+ # retried with a fresh link instead of disabling the source.
+ raise AppLovinAPIError(
+ f"{TRANSIENT_ERROR_PREFIX}: report download status={download.status_code}, endpoint={USER_AD_REVENUE_PATH}"
+ )
+
+ download.raw.decode_content = True
+ # Read physical lines off the socket so quoted multi-line fields keep the terminators csv needs.
+ reader = csv.reader(codecs.getreader("utf-8-sig")(download.raw))
+ header = next(reader, None)
+ if not header:
+ return
+ columns = [_normalize_csv_column(column) for column in header]
+
+ batch: list[dict[str, Any]] = []
+ for record in reader:
+ if not any(record):
+ continue
+ row: dict[str, Any] = dict.fromkeys(USER_AD_REVENUE_PRIMARY_KEYS, "")
+ row.update(zip(columns, record))
+ row.update(day=day.isoformat(), package_name=package_name, platform=platform)
+ if "impressions" in row:
+ row["impressions"] = _to_int(row["impressions"])
+ if "revenue" in row:
+ row["revenue"] = _to_decimal(row["revenue"])
+ batch.append(row)
+ if len(batch) >= USER_AD_REVENUE_BATCH_SIZE:
+ yield batch
+ batch = []
+ if batch:
+ yield batch
+
+
+def get_user_ad_revenue_rows(
+ api_key: str,
+ logger: FilteringBoundLogger,
+ resumable_source_manager: "ResumableSourceManager[AppLovinResumeConfig]",
+ should_use_incremental_field: bool = False,
+ db_incremental_field_last_value: Any = None,
+) -> Iterator[list[dict[str, Any]]]:
+ session = make_tracked_session(redact_values=(api_key,))
+ # The report response body carries the pre-signed download link, so it must never be sampled.
+ report_session = make_tracked_session(redact_values=(api_key,), capture=False)
+
+ today = _utc_today()
+ earliest = today - timedelta(days=MAX_REQUEST_WINDOW_DAYS - 1)
+ # A day's report is published eight hours after the UTC day ends, so today never has one.
+ last_day = today - timedelta(days=1)
+
+ start = earliest
+ if should_use_incremental_field:
+ watermark = _to_date(db_incremental_field_last_value)
+ if watermark is not None:
+ start = max(earliest, watermark - timedelta(days=USER_AD_REVENUE_LOOKBACK_DAYS))
+
+ resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
+ if resume_config is not None and resume_config.next_window_start:
+ resumed = _to_date(resume_config.next_window_start)
+ if resumed is not None and start <= resumed <= last_day:
+ start = resumed
+ logger.debug(f"AppLovin: resuming {USER_AD_REVENUE_ENDPOINT} from {start.isoformat()}")
+
+ day = start
+ while day <= last_day:
+ # Every earlier day is fully yielded, so a resume from this day re-reads only this day.
+ resumable_source_manager.save_state(AppLovinResumeConfig(next_window_start=day.isoformat()))
+ resumable_source_manager.safe_point()
+
+ for app in _user_ad_revenue_apps(session, api_key, day, logger):
+ yielded = False
+ for rows in _user_ad_revenue_report(report_session, api_key, day, app, logger):
+ yielded = True
+ yield rows
+ if not yielded:
+ resumable_source_manager.safe_point()
+
+ day += timedelta(days=1)
+
+ resumable_source_manager.clear_state()
+
+
+def _management_session(api_key: str) -> requests.Session:
+ return make_tracked_session(headers={"Authorization": api_key}, redact_values=(api_key,))
+
+
+def _fetch_management_page(
+ session: requests.Session,
+ config: AppLovinManagementEndpointConfig,
+ api_key: str,
+ account_id: str,
+ params: dict[str, Any],
+ timeout: int = REQUEST_TIMEOUT_SECONDS,
+) -> list[dict[str, Any]]:
+ url = f"{APPLOVIN_MANAGEMENT_API_BASE_URL}{config.path}?{urlencode({'account_id': account_id, **params})}"
+ response = _get(session, url, api_key, timeout=timeout)
+ _raise_for_status(response, config.path, api_key, auth_prefix=MANAGEMENT_AUTH_ERROR_PREFIX)
+ body = _json_body(response, config.path, api_key)
+
+ # Every list endpoint returns a bare JSON array and an empty one past the last page.
+ if not isinstance(body, list):
+ raise AppLovinAPIError(f"{BAD_REQUEST_ERROR_PREFIX}: endpoint={config.path} did not return a JSON array")
+
+ return [row for row in body if isinstance(row, dict)]
+
+
+def get_management_list_rows(
+ api_key: str,
+ account_id: str,
+ endpoint: str,
+ logger: FilteringBoundLogger,
+ resumable_source_manager: "ResumableSourceManager[AppLovinResumeConfig]",
+) -> Iterator[list[dict[str, Any]]]:
+ config = MANAGEMENT_ENDPOINTS[endpoint]
+ session = _management_session(api_key)
+
+ page = 1
+ resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
+ if resume_config is not None and resume_config.next_page > 1:
+ page = resume_config.next_page
+ logger.debug(f"AppLovin: resuming {endpoint} from page {page}")
+
+ while True:
+ rows = _fetch_management_page(
+ session, config, api_key, account_id, {"page": page, "size": MANAGEMENT_PAGE_SIZE}
+ )
+ if len(rows) < MANAGEMENT_PAGE_SIZE:
+ if rows:
+ yield rows
+ break
+
+ page += 1
+ resumable_source_manager.save_state(AppLovinResumeConfig(next_page=page))
+ yield rows
+
+ resumable_source_manager.clear_state()
+
+
+def get_operation_log_rows(
+ api_key: str,
+ account_id: str,
+ logger: FilteringBoundLogger,
+ resumable_source_manager: "ResumableSourceManager[AppLovinResumeConfig]",
+ should_use_incremental_field: bool = False,
+ db_incremental_field_last_value: Any = None,
+) -> Iterator[list[dict[str, Any]]]:
+ config = MANAGEMENT_ENDPOINTS["campaign_operation_logs"]
+ session = _management_session(api_key)
+
+ today = _utc_today()
+ earliest = today - timedelta(days=OPERATION_LOG_WINDOW_DAYS - 1)
+
+ start = earliest
+ if should_use_incremental_field:
+ watermark = _to_date(db_incremental_field_last_value)
+ if watermark is not None:
+ # Operations never change once logged, and the merge dedupes on `operation_id`.
+ start = max(earliest, watermark)
+
+ resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
+ if resume_config is not None and resume_config.next_window_start:
+ resumed = _to_date(resume_config.next_window_start)
+ if resumed is not None and start <= resumed <= today:
+ start = resumed
+ logger.debug(f"AppLovin: resuming campaign_operation_logs from {start.isoformat()}")
+
+ day = start
+ while day <= today:
+ rows: list[dict[str, Any]] = []
+ page = 1
+ while True:
+ page_rows = _fetch_management_page(
+ session,
+ config,
+ api_key,
+ account_id,
+ {
+ "start_date": day.isoformat(),
+ "end_date": day.isoformat(),
+ "page": page,
+ "size": MANAGEMENT_PAGE_SIZE,
+ },
+ )
+ rows.extend(page_rows)
+ if len(page_rows) < MANAGEMENT_PAGE_SIZE:
+ break
+ page += 1
+
+ day += timedelta(days=1)
+ resumable_source_manager.save_state(AppLovinResumeConfig(next_window_start=day.isoformat()))
+ if rows:
+ # The API returns a day's operations newest-first. Days are walked oldest-first, so
+ # sorting each day makes `operation_time` only move forward (`sort_mode="asc"`).
+ rows.sort(key=lambda row: str(row.get("operation_time") or ""))
+ yield rows
+ else:
+ resumable_source_manager.safe_point()
+
+ resumable_source_manager.clear_state()
+
+
+def validate_management_credentials(api_key: str, account_id: str) -> bool:
+ """Confirm the Campaign Management API key can read campaigns on the given account."""
+ session = _management_session(api_key)
+ try:
+ _fetch_management_page(
+ session,
+ MANAGEMENT_ENDPOINTS["campaigns"],
+ api_key,
+ account_id,
+ {"page": 1, "size": 1},
+ timeout=VALIDATION_TIMEOUT_SECONDS,
+ )
+ except Exception: # noqa: BLE001 — a credential probe must never raise out of source create
+ return False
+ return True
+
+
def validate_credentials(api_key: str) -> bool:
"""Confirm the Report Key is accepted by at least one reporting endpoint.
@@ -269,8 +605,60 @@ def applovin_source(
resumable_source_manager: "ResumableSourceManager[AppLovinResumeConfig]",
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Optional[Any] = None,
+ campaign_api_key: Optional[str] = None,
+ account_id: Optional[str] = None,
) -> SourceResponse:
- config = APPLOVIN_ENDPOINTS[endpoint]
+ primary_keys = list(PRIMARY_KEYS[endpoint])
+
+ if endpoint in MANAGEMENT_ENDPOINTS:
+ if not campaign_api_key or not account_id:
+ raise AppLovinAPIError(f"{MANAGEMENT_CREDENTIALS_MISSING_PREFIX}: endpoint={endpoint}")
+ management_config = MANAGEMENT_ENDPOINTS[endpoint]
+ management_api_key, management_account_id = campaign_api_key, account_id
+
+ def management_items() -> Iterator[list[dict[str, Any]]]:
+ if endpoint == "campaign_operation_logs":
+ return get_operation_log_rows(
+ api_key=management_api_key,
+ account_id=management_account_id,
+ 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,
+ )
+ return get_management_list_rows(
+ api_key=management_api_key,
+ account_id=management_account_id,
+ endpoint=endpoint,
+ logger=logger,
+ resumable_source_manager=resumable_source_manager,
+ )
+
+ return SourceResponse(
+ name=endpoint,
+ items=management_items,
+ primary_keys=primary_keys,
+ partition_mode="datetime" if management_config.partition_key else None,
+ partition_keys=[management_config.partition_key] if management_config.partition_key else None,
+ sort_mode="asc",
+ )
+
+ if endpoint == USER_AD_REVENUE_ENDPOINT:
+ return SourceResponse(
+ name=endpoint,
+ items=lambda: get_user_ad_revenue_rows(
+ api_key=api_key,
+ 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,
+ ),
+ primary_keys=primary_keys,
+ partition_mode="datetime",
+ partition_keys=["day"],
+ # Days are walked oldest-first, and every row of a day carries that day.
+ sort_mode="asc",
+ )
return SourceResponse(
name=endpoint,
@@ -282,7 +670,7 @@ def applovin_source(
should_use_incremental_field=should_use_incremental_field,
db_incremental_field_last_value=db_incremental_field_last_value,
),
- primary_keys=list(config.primary_keys),
+ primary_keys=primary_keys,
partition_count=1,
partition_size=1,
# Windows are walked oldest-first and each request pins `sort_day=ASC`, so `day` only
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/canonical_descriptions.py
index 5446162a10e0..6d0664d75bc1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/canonical_descriptions.py
@@ -1,7 +1,7 @@
-"""Canonical, documentation-sourced descriptions for AppLovin report endpoints and columns.
+"""Canonical, documentation-sourced descriptions for AppLovin endpoints and columns.
Sourced from the AppLovin support centre API references linked per entry below. Keyed by the
-endpoint names in `settings.py` `APPLOVIN_ENDPOINTS`, which match the `ExternalDataSchema.name`
+endpoint names in `settings.py` `ENDPOINTS`, which match the `ExternalDataSchema.name`
of a synced AppLovin table. Columns absent here fall back to LLM enrichment.
"""
@@ -13,6 +13,10 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.can
_MAX_REPORT_DOCS_URL = "https://support.applovin.com/en/max/reporting-apis/revenue-reporting-api"
_REPORT_DOCS_URL = "https://support.applovin.com/en/growth/promoting-your-apps/api/reporting-api"
_COHORT_DOCS_URL = "https://support.applovin.com/en/max/reporting-apis/cohort-api"
+_USER_AD_REVENUE_DOCS_URL = "https://support.applovin.com/en/max/reporting-apis/user-level-ad-revenue-api"
+_CAMPAIGN_MANAGEMENT_DOCS_URL = (
+ "https://support.applovin.com/en/growth/promoting-your-apps/api/axon-campaign-management-api"
+)
def _horizon_columns(prefix: str, template: str) -> dict[str, str]:
@@ -150,4 +154,77 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
**_horizon_columns("session_length", "Daily usage divided by session count {days} days after the install."),
},
},
+ "user_ad_revenue": {
+ "description": "MAX ad revenue per user, ad unit and placement for each app and day, from the aggregated User-Level Ad Revenue API report.",
+ "docs_url": _USER_AD_REVENUE_DOCS_URL,
+ "columns": {
+ "day": _APP_DIMENSIONS["day"],
+ "package_name": _APP_DIMENSIONS["package_name"],
+ "platform": _APP_DIMENSIONS["platform"],
+ "ad_unit_id": "ID of the MAX ad unit that generated the revenue.",
+ "placement": "Ad placement name where the user generated impressions and revenue.",
+ "idfa": "Advertising identifier: IDFA on iOS devices, GAID on Android devices.",
+ "idfv": "Identifier for vendor.",
+ "user_id": "Optional ID of the user, set through the AppLovin SDK.",
+ "custom_data": "Custom data passed in from the integration.",
+ "impressions": "Number of ads displayed to the user.",
+ "revenue": "Revenue generated by the user, in USD, with six-decimal precision.",
+ },
+ },
+ "campaigns": {
+ "description": "AppLovin campaigns on the account, with status, budget, goal, tracking and country targeting. Archived campaigns are not listed.",
+ "docs_url": _CAMPAIGN_MANAGEMENT_DOCS_URL,
+ "columns": {
+ "id": "ID of the campaign.",
+ "hashed_id": "External ID of the campaign. Same value as campaign_id_external in the Reporting API.",
+ "name": "Name of the campaign.",
+ "status": "Current state of the campaign: LIVE or PAUSED.",
+ "type": "Type of the campaign.",
+ "platform": "Platform of the campaign.",
+ "package_name": "Package name associated with the campaign.",
+ "itunes_id": "iTunes ID associated with the campaign.",
+ "bidding_strategy": "Bidding strategy of the campaign.",
+ "budget": "Daily budget settings, either for all countries or per country.",
+ "goal": "Optimization goal type and goal value, either for all countries or per country.",
+ "tracking": "Attribution provider and the click and impression tracking URLs.",
+ "targeting": "Country targets of the campaign, with optional US regions or metro areas.",
+ "created_at": "Creation time of the campaign, in ISO 8601 format (UTC).",
+ "start_date": "Start date of the campaign, in ISO 8601 format (UTC).",
+ "end_date": "End date of the campaign, in ISO 8601 format (UTC).",
+ "is_continuous_delivery": "True when the campaign has no end date.",
+ "is_composite_banner_enabled": "Whether the auto-generated banner based on the app icon and name is enabled.",
+ },
+ },
+ "creative_sets": {
+ "description": "Creative sets on the account: the assets, languages and countries each set serves with.",
+ "docs_url": _CAMPAIGN_MANAGEMENT_DOCS_URL,
+ "columns": {
+ "id": "ID of the creative set.",
+ "hashed_id": "External ID of the creative set. Same value as creative_set_id in the Reporting API.",
+ "name": "Name of the creative set.",
+ "status": "Status of the creative set: LIVE or PAUSED.",
+ "type": "Type of the campaign.",
+ "campaign_id": "ID of the campaign this creative set belongs to.",
+ "campaign_ids": "IDs of the campaigns the creative set is included in.",
+ "assets": "Assets in the creative set, with their ID, name, status, type and URL.",
+ "languages": "Languages the creative set serves in.",
+ "countries": "ISO 3166-1 alpha-2 codes of the countries the creative set serves in.",
+ "product_page": "iOS custom product page or Android store listing of the creative set.",
+ "version": "Creative set version: V1 allows one asset per type, V2 allows several.",
+ },
+ },
+ "campaign_operation_logs": {
+ "description": "History of operations on campaigns: creation, updates, status changes and archiving.",
+ "docs_url": _CAMPAIGN_MANAGEMENT_DOCS_URL,
+ "columns": {
+ "operation_id": "Unique ID of the operation.",
+ "operation_time": "Time the operation occurred, in ISO 8601 format (UTC).",
+ "operation_type": "Type of operation: CREATE_CAMPAIGN, UPDATE_CAMPAIGN, UPDATE_CAMPAIGN_STATUS or ARCHIVE_CAMPAIGN.",
+ "operator_name": "Person or system that performed the operation.",
+ "campaign_id": "ID of the campaign the operation was performed on.",
+ "campaign_name": "Name of the campaign the operation was performed on.",
+ "campaign_type": "Type of the campaign.",
+ "detail": "Operation payload. Holds the campaign before and after an update, or the status before and after a status change.",
+ },
+ },
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/settings.py
index adc763f9ff0a..fd4b77a5e81c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/settings.py
@@ -1,8 +1,11 @@
from dataclasses import dataclass, field
+from typing import Optional
from products.warehouse_sources.backend.types import IncrementalField, IncrementalFieldType
APPLOVIN_API_BASE_URL = "https://r.applovin.com"
+# The Campaign Management API authenticates with its own key, separate from the Report Key.
+APPLOVIN_MANAGEMENT_API_BASE_URL = "https://api.ads.axon.ai/manage/v1"
# Every reporting endpoint rejects dates outside a rolling 45-day window, so 45 days is the
# deepest history the API can serve — older data is not retrievable through the API at all.
@@ -209,9 +212,73 @@ APPLOVIN_ENDPOINTS: dict[str, AppLovinEndpointConfig] = {
),
}
-ENDPOINTS = tuple(APPLOVIN_ENDPOINTS.keys())
+USER_AD_REVENUE_ENDPOINT = "user_ad_revenue"
+USER_AD_REVENUE_PATH = "/max/userAdRevenueReport"
+# The report is per (app, day), so the apps with MAX revenue on a day are listed first.
+USER_AD_REVENUE_APPS = AppLovinEndpointConfig(
+ name="user_ad_revenue_apps",
+ path="/maxReport",
+ dimensions=["day", "package_name", "platform"],
+ metrics=["impressions"],
+ primary_keys=["day", "package_name", "platform"],
+)
+# A day's report is final once it is published, eight hours after the UTC day ends. Re-reading
+# the last synced day picks up a day whose report was not yet published on the previous run.
+USER_AD_REVENUE_LOOKBACK_DAYS = 1
+USER_AD_REVENUE_BATCH_SIZE = 5000
+# The aggregated report has one row per user and ad placement, and every string column of the CSV
+# is part of that grain. A column the CSV leaves out is filled with "" so the key is never null.
+USER_AD_REVENUE_PRIMARY_KEYS = [
+ "day",
+ "package_name",
+ "platform",
+ "ad_unit_id",
+ "placement",
+ "idfa",
+ "idfv",
+ "user_id",
+ "custom_data",
+]
-# Every endpoint is a date-partitioned report filtered server-side by `start`/`end` on `day`.
+
+@dataclass(frozen=True)
+class AppLovinManagementEndpointConfig:
+ name: str
+ # Path under `APPLOVIN_MANAGEMENT_API_BASE_URL`.
+ path: str
+ primary_keys: list[str]
+ partition_key: Optional[str] = None
+
+
+# `size` maximum on every Campaign Management list endpoint.
+MANAGEMENT_PAGE_SIZE = 100
+# `/operation_logs/campaign` rejects dates older than the latest 30 UTC days, today included.
+OPERATION_LOG_WINDOW_DAYS = 30
+
+MANAGEMENT_ENDPOINTS: dict[str, AppLovinManagementEndpointConfig] = {
+ "campaigns": AppLovinManagementEndpointConfig(
+ name="campaigns", path="/campaign/list", primary_keys=["id"], partition_key="created_at"
+ ),
+ "creative_sets": AppLovinManagementEndpointConfig(
+ name="creative_sets", path="/creative_set/list", primary_keys=["id"]
+ ),
+ "campaign_operation_logs": AppLovinManagementEndpointConfig(
+ name="campaign_operation_logs",
+ path="/operation_logs/campaign",
+ primary_keys=["operation_id"],
+ partition_key="operation_time",
+ ),
+}
+
+ENDPOINTS = (*APPLOVIN_ENDPOINTS.keys(), USER_AD_REVENUE_ENDPOINT, *MANAGEMENT_ENDPOINTS.keys())
+
+PRIMARY_KEYS: dict[str, list[str]] = {
+ **{name: config.primary_keys for name, config in APPLOVIN_ENDPOINTS.items()},
+ USER_AD_REVENUE_ENDPOINT: USER_AD_REVENUE_PRIMARY_KEYS,
+ **{name: config.primary_keys for name, config in MANAGEMENT_ENDPOINTS.items()},
+}
+
+# The report endpoints are filtered server-side by `start`/`end` (or `date`) on `day`.
DAY_INCREMENTAL_FIELD: IncrementalField = {
"label": "day",
"type": IncrementalFieldType.DateTime,
@@ -219,4 +286,20 @@ DAY_INCREMENTAL_FIELD: IncrementalField = {
"field_type": IncrementalFieldType.DateTime,
}
-INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {name: [DAY_INCREMENTAL_FIELD] for name in ENDPOINTS}
+# Operation logs are filtered server-side by `start_date`/`end_date`. Campaigns and creative sets
+# have no server-side filter, so they sync as a full refresh.
+OPERATION_TIME_INCREMENTAL_FIELD: IncrementalField = {
+ "label": "operation_time",
+ "type": IncrementalFieldType.DateTime,
+ "field": "operation_time",
+ "field_type": IncrementalFieldType.DateTime,
+}
+
+INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
+ **{name: [DAY_INCREMENTAL_FIELD] for name in (*APPLOVIN_ENDPOINTS.keys(), USER_AD_REVENUE_ENDPOINT)},
+ "campaign_operation_logs": [OPERATION_TIME_INCREMENTAL_FIELD],
+}
+
+# Report rows restate as metrics settle, and operation log syncs re-read the watermark day, so
+# append would duplicate rows in both.
+MERGE_ONLY_ENDPOINTS = (*APPLOVIN_ENDPOINTS.keys(), USER_AD_REVENUE_ENDPOINT, "campaign_operation_logs")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/source.py
index c1abe91c4902..6f48807ab981 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/source.py
@@ -11,14 +11,19 @@ from products.warehouse_sources.backend.facade.source_config import (
from products.warehouse_sources.backend.temporal.data_imports.sources.applovin.applovin import (
AUTH_ERROR_PREFIX,
BAD_REQUEST_ERROR_PREFIX,
+ MANAGEMENT_AUTH_ERROR_PREFIX,
+ MANAGEMENT_CREDENTIALS_MISSING_PREFIX,
AppLovinResumeConfig,
applovin_source,
validate_credentials as validate_applovin_credentials,
+ validate_management_credentials,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.applovin.settings import (
- APPLOVIN_ENDPOINTS,
ENDPOINTS,
INCREMENTAL_FIELDS,
+ MANAGEMENT_ENDPOINTS,
+ MERGE_ONLY_ENDPOINTS,
+ PRIMARY_KEYS,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import FieldType, ResumableSource
from products.warehouse_sources.backend.temporal.data_imports.sources.common.canonical_descriptions import (
@@ -109,6 +114,8 @@ class AppLovinSource(ResumableSource[AppLovinSourceConfig, AppLovinResumeConfig]
return {
AUTH_ERROR_PREFIX: "AppLovin rejected your Report Key. Generate a new one under your account menu > Keys in the AppLovin dashboard, then reconnect.",
BAD_REQUEST_ERROR_PREFIX: "AppLovin rejected the report request. This usually means your account can't access the requested report.",
+ MANAGEMENT_AUTH_ERROR_PREFIX: "AppLovin rejected your Campaign Management API key or account ID. Check both under your account menu in the AppLovin dashboard, then update this source.",
+ MANAGEMENT_CREDENTIALS_MISSING_PREFIX: "Campaign tables need a Campaign Management API key and account ID. Add both to this source, then sync again.",
}
@property
@@ -121,7 +128,9 @@ class AppLovinSource(ResumableSource[AppLovinSourceConfig, AppLovinResumeConfig]
Find your **Report Key** in the AppLovin dashboard: click your account in the top right, then **Keys**.
-AppLovin only serves the last 45 days of report data, so anything older than that can't be backfilled.""",
+To sync campaigns, creative sets, and campaign operation logs, also add your **Campaign Management API key** (under **Keys**) and your **account ID** (under **Go to settings**, the number after your account name).
+
+AppLovin only serves the last 45 days of report data and the last 30 days of campaign operation logs, so anything older than that can't be backfilled.""",
iconPath="/static/services/applovin.png",
docsUrl="https://posthog.com/docs/cdp/sources/applovin",
releaseStatus=ReleaseStatus.ALPHA,
@@ -137,6 +146,22 @@ AppLovin only serves the last 45 days of report data, so anything older than tha
placeholder="",
secret=True,
),
+ SourceFieldInputConfig(
+ name="campaign_api_key",
+ label="Campaign Management API key",
+ type=SourceFieldInputConfigType.PASSWORD,
+ required=False,
+ placeholder="",
+ secret=True,
+ ),
+ SourceFieldInputConfig(
+ name="account_id",
+ label="Account ID",
+ type=SourceFieldInputConfigType.TEXT,
+ required=False,
+ placeholder="123456",
+ secret=False,
+ ),
],
),
)
@@ -150,12 +175,21 @@ AppLovin only serves the last 45 days of report data, so anything older than tha
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
- # Rows are aggregates that restate for a few days, so append would duplicate them.
- schemas = build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names, merge_only=ENDPOINTS)
+ schemas = build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names, merge_only=MERGE_ONLY_ENDPOINTS)
for schema in schemas:
- schema.detected_primary_keys = list(APPLOVIN_ENDPOINTS[schema.name].primary_keys)
+ schema.detected_primary_keys = list(PRIMARY_KEYS[schema.name])
return schemas
+ def get_endpoint_permissions(
+ self, config: AppLovinSourceConfig, team_id: int, endpoints: list[str], api_version: str | None = None
+ ) -> dict[str, str | None]:
+ missing = "Add a Campaign Management API key and account ID to this source to sync this table."
+ has_management_credentials = bool(config.campaign_api_key and config.account_id)
+ return {
+ endpoint: missing if endpoint in MANAGEMENT_ENDPOINTS and not has_management_credentials else None
+ for endpoint in endpoints
+ }
+
def validate_credentials(
self,
config: AppLovinSourceConfig,
@@ -163,10 +197,22 @@ AppLovin only serves the last 45 days of report data, so anything older than tha
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_applovin_credentials(config.api_key):
+ if not validate_applovin_credentials(config.api_key):
+ return False, "Invalid AppLovin Report Key"
+
+ if schema_name is not None and schema_name not in MANAGEMENT_ENDPOINTS:
return True, None
- return False, "Invalid AppLovin Report Key"
+ if bool(config.campaign_api_key) != bool(config.account_id):
+ return False, "Enter both the Campaign Management API key and the account ID, or leave both empty."
+
+ if config.campaign_api_key and config.account_id:
+ if not validate_management_credentials(config.campaign_api_key, config.account_id):
+ return False, "Invalid AppLovin Campaign Management API key or account ID"
+ elif schema_name in MANAGEMENT_ENDPOINTS:
+ return False, "Add a Campaign Management API key and account ID to sync this table."
+
+ return True, None
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[AppLovinResumeConfig]:
return ResumableSourceManager[AppLovinResumeConfig](inputs, AppLovinResumeConfig)
@@ -186,4 +232,6 @@ AppLovin only serves the last 45 days of report data, so anything older than tha
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
else None,
+ campaign_api_key=config.campaign_api_key,
+ account_id=config.account_id,
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin.py b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin.py
index ad5e66f2d6ba..a090a6d891d3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin.py
@@ -1,4 +1,5 @@
from datetime import UTC, date, datetime, timedelta
+from decimal import Decimal
from typing import Any, Optional
from urllib.parse import quote, quote_plus
@@ -9,6 +10,8 @@ import requests
from products.warehouse_sources.backend.temporal.data_imports.sources.applovin.applovin import (
AUTH_ERROR_PREFIX,
BAD_REQUEST_ERROR_PREFIX,
+ MANAGEMENT_AUTH_ERROR_PREFIX,
+ MANAGEMENT_CREDENTIALS_MISSING_PREFIX,
TRANSIENT_ERROR_PREFIX,
AppLovinAPIError,
AppLovinResumeConfig,
@@ -18,8 +21,9 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.applovin.a
)
from products.warehouse_sources.backend.temporal.data_imports.sources.applovin.settings import (
APPLOVIN_ENDPOINTS,
- ENDPOINTS,
+ MANAGEMENT_PAGE_SIZE,
MAX_REQUEST_WINDOW_DAYS,
+ OPERATION_LOG_WINDOW_DAYS,
REPORT_LOOKBACK_DAYS,
REPORT_PAGE_SIZE,
REPORT_WINDOW_DAYS,
@@ -41,6 +45,12 @@ def _driver(api_key: str = "key") -> SourceDriver:
return SourceDriver(AppLovinSource(), AppLovinSourceConfig(api_key=api_key))
+def _management_driver() -> SourceDriver:
+ return SourceDriver(
+ AppLovinSource(), AppLovinSourceConfig(api_key="key", campaign_api_key="campaign-key", account_id="42")
+ )
+
+
def _page(rows: Any = None) -> ScriptedResponse:
return ScriptedResponse(json={"code": 200, "results": rows})
@@ -244,6 +254,27 @@ class TestErrorClassification:
# The original exception carries the unredacted URL, so it must not be chained.
assert result.raised.__cause__ is None
+ @pytest.mark.parametrize(
+ "config, response, expected_prefix",
+ [
+ (AppLovinSourceConfig(api_key="key"), None, MANAGEMENT_CREDENTIALS_MISSING_PREFIX),
+ (
+ AppLovinSourceConfig(api_key="key", campaign_api_key="campaign-key", account_id="42"),
+ ScriptedResponse(status=401),
+ MANAGEMENT_AUTH_ERROR_PREFIX,
+ ),
+ ],
+ )
+ def test_campaign_management_credential_errors(
+ self, config: AppLovinSourceConfig, response: Optional[ScriptedResponse], expected_prefix: str
+ ) -> None:
+ result = SourceDriver(AppLovinSource(), config).run("campaigns", [response] if response else [])
+
+ assert isinstance(result.raised, AppLovinAPIError)
+ # The Report Key message would send the user to fix the wrong key.
+ assert str(result.raised).startswith(expected_prefix)
+ assert len(result.requests) == (1 if response else 0)
+
@pytest.mark.parametrize("api_key", ["super-secret", "ab/c+d=="])
def test_error_body_echoing_the_report_key_is_redacted(self, api_key: str) -> None:
result = _driver(api_key).run(
@@ -286,7 +317,7 @@ class TestSourceResponse:
class TestEndpointCatalogInvariants:
- @pytest.mark.parametrize("endpoint", list(ENDPOINTS))
+ @pytest.mark.parametrize("endpoint", list(APPLOVIN_ENDPOINTS))
def test_primary_keys_are_requested_dimensions(self, endpoint: str) -> None:
config = APPLOVIN_ENDPOINTS[endpoint]
# A key column absent from `columns` would never appear in a row, so every merge
@@ -308,3 +339,162 @@ class TestEndpointCatalogInvariants:
# A mid-window crash is only self-healing when the next run re-reads the full window.
assert REPORT_LOOKBACK_DAYS >= REPORT_WINDOW_DAYS
assert REPORT_WINDOW_DAYS < MAX_REQUEST_WINDOW_DAYS
+
+
+class TestUserAdRevenue:
+ REPORT_CSV = (
+ "Ad Unit ID,Placement,IDFA,IDFV,User Id,Impressions, Revenue\r\n"
+ "unit1,home,idfa-1,idfv-1,user-1,27,5.000025\r\n"
+ "unit1,level_end,idfa-2,idfv-2,,11,0.006100\r\n"
+ )
+
+ def _respond(self, app_day: date) -> Any:
+ def respond(request: RecordedRequest) -> ScriptedResponse:
+ if request.path == "/maxReport":
+ if request.param("start") != app_day.isoformat():
+ return _page([])
+ return _page(
+ [
+ {"day": app_day.isoformat(), "package_name": "com.app", "platform": "IOS"},
+ {"day": app_day.isoformat(), "package_name": "com.gone", "platform": "android"},
+ ]
+ )
+ if request.path == "/max/userAdRevenueReport":
+ if request.param("application") == "com.gone":
+ return ScriptedResponse(status=404)
+ return ScriptedResponse(
+ json={
+ "status": 200,
+ "ad_revenue_report_url": "https://reports.example.com/user_report.csv?X-Amz-Signature=sig",
+ }
+ )
+ if request.path == "/user_report.csv":
+ return ScriptedResponse(body=self.REPORT_CSV.encode(), headers={"Content-Type": "text/csv"})
+ raise AssertionError(f"unexpected request {request.url}")
+
+ return respond
+
+ def test_reads_each_app_report_for_each_day(self) -> None:
+ app_day = _today() - timedelta(days=3)
+ result = _driver().run("user_ad_revenue", self._respond(app_day))
+
+ assert result.raised is None
+ common = {"day": app_day.isoformat(), "package_name": "com.app", "platform": "ios", "custom_data": ""}
+ assert result.rows == [
+ {
+ **common,
+ "ad_unit_id": "unit1",
+ "placement": "home",
+ "idfa": "idfa-1",
+ "idfv": "idfv-1",
+ "user_id": "user-1",
+ "impressions": 27,
+ "revenue": Decimal("5.000025"),
+ },
+ {
+ **common,
+ "ad_unit_id": "unit1",
+ "placement": "level_end",
+ "idfa": "idfa-2",
+ "idfv": "idfv-2",
+ "user_id": "",
+ "impressions": 11,
+ "revenue": Decimal("0.006100"),
+ },
+ ]
+ report_requests = [request for request in result.requests if request.path == "/max/userAdRevenueReport"]
+ assert [(r.param("application"), r.param("platform"), r.param("date")) for r in report_requests] == [
+ ("com.app", "ios", app_day.isoformat()),
+ ("com.gone", "android", app_day.isoformat()),
+ ]
+ assert {r.param("aggregated") for r in report_requests} == {"true"}
+ assert result.final_state is None
+
+ @pytest.mark.parametrize(
+ "watermark_days_ago, expected_first_day_days_ago",
+ [
+ (None, MAX_REQUEST_WINDOW_DAYS - 1),
+ (2, 3),
+ (MAX_REQUEST_WINDOW_DAYS + 10, MAX_REQUEST_WINDOW_DAYS - 1),
+ ],
+ )
+ def test_walks_days_from_the_window_start_to_yesterday(
+ self, watermark_days_ago: Optional[int], expected_first_day_days_ago: int
+ ) -> None:
+ incremental: dict[str, Any] = {}
+ if watermark_days_ago is not None:
+ incremental = {
+ "should_use_incremental_field": True,
+ "db_incremental_field_last_value": (_today() - timedelta(days=watermark_days_ago)).isoformat(),
+ }
+ result = _driver().run("user_ad_revenue", always(_page([])), **incremental)
+
+ assert result.raised is None
+ days = result.params("start")
+ assert days[0] == (_today() - timedelta(days=expected_first_day_days_ago)).isoformat()
+ # A day's report is published after the day ends, so today is never requested.
+ assert days[-1] == (_today() - timedelta(days=1)).isoformat()
+ assert len(days) == expected_first_day_days_ago
+
+
+class TestCampaignManagement:
+ @pytest.mark.parametrize(
+ "endpoint, path", [("campaigns", "/campaign/list"), ("creative_sets", "/creative_set/list")]
+ )
+ def test_list_walks_pages_until_a_short_page(self, endpoint: str, path: str) -> None:
+ full_page = [{"id": str(index)} for index in range(MANAGEMENT_PAGE_SIZE)]
+ result = _management_driver().run(
+ endpoint, [ScriptedResponse(json=full_page), ScriptedResponse(json=[{"id": "x"}])]
+ )
+
+ assert result.raised is None
+ assert result.paths == [f"/manage/v1{path}"] * 2
+ assert result.params("page") == ["1", "2"]
+ assert set(result.params("account_id")) == {"42"}
+ assert {request.headers["authorization"] for request in result.requests} == {"campaign-key"}
+ assert [len(batch) for batch in result.items] == [MANAGEMENT_PAGE_SIZE, 1]
+ assert result.final_state is None
+
+ def test_list_resumes_from_the_saved_page(self) -> None:
+ result = _management_driver().run(
+ "campaigns", [ScriptedResponse(json=[])], resume_state=AppLovinResumeConfig(next_page=3)
+ )
+
+ assert result.raised is None
+ assert result.params("page") == ["3"]
+
+ @pytest.mark.parametrize(
+ "watermark_days_ago, expected_days",
+ [(None, OPERATION_LOG_WINDOW_DAYS), (1, 2), (OPERATION_LOG_WINDOW_DAYS + 5, OPERATION_LOG_WINDOW_DAYS)],
+ )
+ def test_operation_logs_walk_days_oldest_first_and_sort_each_day(
+ self, watermark_days_ago: Optional[int], expected_days: int
+ ) -> None:
+ today = _today()
+
+ def respond(request: RecordedRequest) -> ScriptedResponse:
+ if request.param("start_date") != today.isoformat():
+ return ScriptedResponse(json=[])
+ return ScriptedResponse(
+ json=[
+ {"operation_id": "2", "operation_time": f"{today.isoformat()}T05:32:38"},
+ {"operation_id": "1", "operation_time": f"{today.isoformat()}T05:32:20"},
+ ]
+ )
+
+ incremental: dict[str, Any] = {}
+ if watermark_days_ago is not None:
+ incremental = {
+ "should_use_incremental_field": True,
+ "db_incremental_field_last_value": f"{(today - timedelta(days=watermark_days_ago)).isoformat()}T10:00:00",
+ }
+ result = _management_driver().run("campaign_operation_logs", respond, **incremental)
+
+ assert result.raised is None
+ days = result.params("start_date")
+ assert days == result.params("end_date")
+ assert days[0] == (today - timedelta(days=expected_days - 1)).isoformat()
+ assert days[-1] == today.isoformat()
+ assert len(days) == expected_days
+ # The API returns newest-first; `sort_mode="asc"` needs oldest-first.
+ assert [row["operation_id"] for row in result.rows] == ["1", "2"]
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin_source.py
index 240c7102ef6f..b5db25a07b4c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/applovin/tests/test_applovin_source.py
@@ -53,6 +53,76 @@ class TestAppLovinSource:
assert error_message == expected_message
assert [request.param("api_key") for request in network.requests_log] == ["report-key"] * len(responses)
+ @pytest.mark.parametrize(
+ "campaign_api_key, account_id, schema_name, management_response, expected",
+ [
+ (
+ "campaign-key",
+ None,
+ None,
+ None,
+ (False, "Enter both the Campaign Management API key and the account ID, or leave both empty."),
+ ),
+ (
+ None,
+ None,
+ "campaigns",
+ None,
+ (False, "Add a Campaign Management API key and account ID to sync this table."),
+ ),
+ (None, None, "max_ad_revenue", None, (True, None)),
+ # A Campaign Management outage must not block settings for a report table.
+ ("campaign-key", "42", "max_ad_revenue", None, (True, None)),
+ (
+ "campaign-key",
+ "42",
+ None,
+ ScriptedResponse(status=401),
+ (False, "Invalid AppLovin Campaign Management API key or account ID"),
+ ),
+ ("campaign-key", "42", "campaigns", ScriptedResponse(json=[]), (True, None)),
+ ],
+ )
+ def test_validate_campaign_management_credentials(
+ self,
+ campaign_api_key: Optional[str],
+ account_id: Optional[str],
+ schema_name: Optional[str],
+ management_response: Optional[ScriptedResponse],
+ expected: tuple[bool, Optional[str]],
+ ) -> None:
+ config = AppLovinSourceConfig(api_key="report-key", campaign_api_key=campaign_api_key, account_id=account_id)
+ responses = [ScriptedResponse(json={"code": 200, "results": []})]
+ if management_response is not None:
+ responses.append(management_response)
+
+ with scripted_network(responses) as network:
+ assert self.source.validate_credentials(config, self.team_id, schema_name) == expected
+
+ assert len(network.requests_log) == len(responses)
+
+ @pytest.mark.parametrize(
+ "campaign_api_key, account_id, expected_campaigns_reason",
+ [
+ (None, None, "Add a Campaign Management API key and account ID to this source to sync this table."),
+ (
+ "campaign-key",
+ None,
+ "Add a Campaign Management API key and account ID to this source to sync this table.",
+ ),
+ ("campaign-key", "42", None),
+ ],
+ )
+ def test_endpoint_permissions_flag_campaign_tables_without_credentials(
+ self, campaign_api_key: Optional[str], account_id: Optional[str], expected_campaigns_reason: Optional[str]
+ ) -> None:
+ config = AppLovinSourceConfig(api_key="report-key", campaign_api_key=campaign_api_key, account_id=account_id)
+
+ assert self.source.get_endpoint_permissions(config, self.team_id, ["campaigns", "max_ad_revenue"]) == {
+ "campaigns": expected_campaigns_reason,
+ "max_ad_revenue": None,
+ }
+
class TestAppLovinCanonicalDescriptions:
def setup_method(self) -> None:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/applovin.py b/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/applovin.py
index 2cf74108b513..b8ea4e44f323 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/applovin.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/generated_configs/applovin.py
@@ -7,3 +7,5 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common imp
@config.config
class AppLovinSourceConfig(config.Config):
api_key: str
+ campaign_api_key: str | None = None
+ account_id: str | None = None