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 3 missing tables to the Tenjin source (#117427)

Tom Owers94099fbbe01f
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 74907a5a0b82..6f658352321b 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
@@ -12153,10 +12153,10 @@ Today (4): `ad_revenue_report`, `app_report`, `campaign_report`, `sk_ad_network_
Diffed against: <https://api-docs.tenjin.com/assets/index-BBavDbPH.js>
-- [ ] `Cohort retention and revenue reports (GET /v2/reports/spend?metrics=users_1d,sessions_1d,retention_1d,revenues_1d,roas_1d)` — measure retention and return on advertising spend, which the fixed spend metric set cannot produce (high)
-- [ ] `Spend by campaign and country (GET /v2/reports/spend?group_by=campaign,country)` — compare acquisition costs and conversion rates across countries within each campaign (medium)
+- [x] `Cohort retention and revenue reports (GET /v2/reports/spend?metrics=users_1d,sessions_1d,retention_1d,revenues_1d,roas_1d)` — measure retention and return on advertising spend, which the fixed spend metric set cannot produce (high)
+- [x] `Spend by campaign and country (GET /v2/reports/spend?group_by=campaign,country)` — compare acquisition costs and conversion rates across countries within each campaign (medium)
- [ ] `Spend by creative (GET /v2/reports/spend?group_by=creative)` — compare creative performance and identify inefficient advertising spend (medium)
-- [ ] `Ad revenue by placement (GET /v2/reports/ad_revenue?group_by=ad_placement)` — compare monetization revenue and efficiency across advertising placements (medium)
+- [x] `Ad revenue by placement (GET /v2/reports/ad_revenue?group_by=ad_placement)` — compare monetization revenue and efficiency across advertising placements (medium)
- [ ] `SKAN Apple reports (GET /v2/reports/skan_apple)` — analyze Apple-sourced SKAdNetwork postbacks alongside network-sourced reports (medium)
- [ ] `Campaigns (GET /v2/campaigns)` — add campaign metadata and account relationships to daily campaign performance reports (medium)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/canonical_descriptions.py
index cdc5b73ab9b8..c47c3566dee9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/canonical_descriptions.py
@@ -29,6 +29,45 @@ _CHANNEL_COLUMNS = {
"ad_network_name": "The name of the channel.",
}
+_CAMPAIGN_COLUMNS = {
+ **_APP_COLUMNS,
+ "name": "The name of the campaign in Tenjin.",
+ "campaign_id": "The ID of the campaign in Tenjin.",
+ "app_name": "The name of the app in Tenjin.",
+ "app_icon_url": "The icon of the app in the App Store.",
+ **_CHANNEL_COLUMNS,
+ "icon_url": "The icon of the channel.",
+ "ad_network_icon_url": "The icon of the channel.",
+}
+
+_AD_REVENUE_COLUMNS = {
+ **_APP_COLUMNS,
+ "app_name": "The name of the app in Tenjin.",
+ "app_icon_url": "The icon of the app in the App Store.",
+ **_CHANNEL_COLUMNS,
+ "ad_network_icon_url": "The icon of the channel.",
+ "ad_network_short_id": "Friendly URL slug used to identify the channel.",
+ "ad_network_custom": "Whether the channel is custom or natively supported by Tenjin.",
+ "ad_revenue": "Ad revenue attributed to the row, in the account currency.",
+ "impressions": "Number of ad impressions shown in the app.",
+ "clicks": "Number of clicks on ads shown in the app.",
+ "ecpm": "Effective cost per thousand impressions.",
+ "ecpc": "Effective cost per click.",
+}
+
+_COHORT_METRIC_COLUMNS = {
+ column: description
+ for days in (1, 7, 30)
+ for column, description in {
+ f"users_{days}d": f"Number of {days}-day retained users from the install cohort.",
+ f"sessions_{days}d": f"Number of {days}-day sessions from the install cohort.",
+ f"retention_{days}d": f"Average {days}-day retention rate percentage of the install cohort.",
+ f"revenues_{days}d": f"Total {days}-day in-app purchase lifetime value of the install cohort.",
+ f"total_rev_{days}d": f"Total {days}-day lifetime value from ad network revenue, in-app purchases, and subscriptions.",
+ f"roas_{days}d": f"Average {days}-day return on ad spend percentage (ad network revenue, in-app purchases, and subscriptions).",
+ }.items()
+}
+
CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"app_report": {
"description": "Daily user-acquisition performance per app from Tenjin's spend report.",
@@ -43,35 +82,12 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"campaign_report": {
"description": "Daily user-acquisition performance per campaign from Tenjin's spend report, including the campaign's app and channel.",
"docs_url": _DOCS_URL,
- "columns": {
- **_APP_COLUMNS,
- "name": "The name of the campaign in Tenjin.",
- "campaign_id": "The ID of the campaign in Tenjin.",
- "app_name": "The name of the app in Tenjin.",
- "app_icon_url": "The icon of the app in the App Store.",
- **_CHANNEL_COLUMNS,
- "icon_url": "The icon of the channel.",
- "ad_network_icon_url": "The icon of the channel.",
- **_SPEND_METRIC_COLUMNS,
- },
+ "columns": {**_CAMPAIGN_COLUMNS, **_SPEND_METRIC_COLUMNS},
},
"ad_revenue_report": {
"description": "Daily ad monetization revenue per app and channel from Tenjin's ad revenue report.",
"docs_url": _DOCS_URL,
- "columns": {
- **_APP_COLUMNS,
- "app_name": "The name of the app in Tenjin.",
- "app_icon_url": "The icon of the app in the App Store.",
- **_CHANNEL_COLUMNS,
- "ad_network_icon_url": "The icon of the channel.",
- "ad_network_short_id": "Friendly URL slug used to identify the channel.",
- "ad_network_custom": "Whether the channel is custom or natively supported by Tenjin.",
- "ad_revenue": "Ad revenue attributed to the row, in the account currency.",
- "impressions": "Number of ad impressions shown in the app.",
- "clicks": "Number of clicks on ads shown in the app.",
- "ecpm": "Effective cost per thousand impressions.",
- "ecpc": "Effective cost per click.",
- },
+ "columns": _AD_REVENUE_COLUMNS,
},
"sk_ad_network_report": {
"description": "Daily SKAdNetwork postback aggregates per app, channel, SKAN campaign, source app, and conversion value (iOS only).",
@@ -94,4 +110,32 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"conversion_value_avg": "Average of conversion values.",
},
},
+ "campaign_cohort_report": {
+ "description": "Daily install cohorts per campaign from Tenjin's spend report: retained users, sessions, retention rate, lifetime value, and ROAS at day 1, 7, and 30.",
+ "docs_url": _DOCS_URL,
+ "columns": {
+ **_CAMPAIGN_COLUMNS,
+ "spend": "Ad spend attributed to the row, in the account currency.",
+ "installs": "Number of attributed installs.",
+ **_COHORT_METRIC_COLUMNS,
+ },
+ },
+ "campaign_country_report": {
+ "description": "Daily user-acquisition performance per campaign and country from Tenjin's spend report.",
+ "docs_url": _DOCS_URL,
+ "columns": {
+ **_CAMPAIGN_COLUMNS,
+ "country": "The ISO 3166 two-letter country code.",
+ **_SPEND_METRIC_COLUMNS,
+ },
+ },
+ "ad_placement_report": {
+ "description": "Daily ad monetization revenue per ad placement from Tenjin's ad revenue report.",
+ "docs_url": _DOCS_URL,
+ "columns": {
+ **_AD_REVENUE_COLUMNS,
+ "ad_placement_id": "The ID of the ad placement in Tenjin.",
+ "ad_placement_name": "The name of the ad placement.",
+ },
+ },
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/settings.py
index f4bedb877c23..faea7402cf86 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/settings.py
@@ -19,6 +19,23 @@ _DATE_INCREMENTAL_FIELDS: list[IncrementalField] = [
_SPEND_METRICS = "spend,impressions,clicks,installs,cpi,ctr,cvr,tcpi,tracked_installs"
_AD_REVENUE_METRICS = "ad_revenue,clicks,impressions,ecpm,ecpc"
_SK_AD_NETWORK_METRICS = "conversion_value_avg,conversion_value_count,conversion_value_total"
+# Spend and installs give the cohort size and cost; the N-day metrics follow that install cohort.
+_COHORT_DAYS = (1, 7, 30)
+_COHORT_METRICS = ",".join(
+ [
+ "spend",
+ "installs",
+ *(
+ f"{metric}_{days}d"
+ for days in _COHORT_DAYS
+ for metric in ("users", "sessions", "retention", "revenues", "total_rev", "roas")
+ ),
+ ]
+)
+
+# Tenjin keeps restating recent days (late SKAN postbacks, ad network cost corrections), so an
+# incremental run re-reads a trailing window; the merge dedupes on the dimension key.
+DEFAULT_LOOKBACK_DAYS = 7
@frozen
@@ -33,6 +50,7 @@ class TenjinReportConfig:
# natural key. Every key column must be a field the chosen group_by returns.
primary_keys: list[str]
description: str
+ lookback_days: int = DEFAULT_LOOKBACK_DAYS
# One entry per report shape we expose. Each Reporting Metrics endpoint is a single report whose
@@ -85,6 +103,34 @@ TENJIN_REPORTS: dict[str, TenjinReportConfig] = {
],
description="Daily SKAdNetwork postback aggregates per app, channel, SKAN campaign, source app, and conversion value (iOS only).",
),
+ "campaign_cohort_report": TenjinReportConfig(
+ name="campaign_cohort_report",
+ path="/reports/spend",
+ group_by="campaign",
+ metrics=_COHORT_METRICS,
+ primary_keys=["date", "campaign_id"],
+ description="Daily install cohorts per campaign: retained users, sessions, retention rate, revenue, and ROAS at day 1, 7, and 30.",
+ # A cohort's day-30 metrics keep changing for 30 days after its install date.
+ lookback_days=max(_COHORT_DAYS) + DEFAULT_LOOKBACK_DAYS,
+ ),
+ "campaign_country_report": TenjinReportConfig(
+ name="campaign_country_report",
+ path="/reports/spend",
+ group_by="campaign,country",
+ metrics=_SPEND_METRICS,
+ primary_keys=["date", "campaign_id", "country"],
+ description="Daily user-acquisition performance per campaign and country, including the campaign's app and channel.",
+ ),
+ "ad_placement_report": TenjinReportConfig(
+ name="ad_placement_report",
+ path="/reports/ad_revenue",
+ group_by="ad_placement",
+ metrics=_AD_REVENUE_METRICS,
+ # Tenjin documents ad_placement_id as a UUID but not whether it is unique across apps and
+ # channels, so the app and channel it belongs to are part of the key too.
+ primary_keys=["date", "app_id", "ad_network_id", "ad_placement_id"],
+ description="Daily ad monetization revenue per ad placement, including the placement's app and channel.",
+ ),
}
ENDPOINTS = tuple(TENJIN_REPORTS.keys())
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tenjin.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tenjin.py
index 4d8e02f846e1..bc1f874706bf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tenjin.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tenjin.py
@@ -14,14 +14,14 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.res
)
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
-from products.warehouse_sources.backend.temporal.data_imports.sources.tenjin.settings import TENJIN_REPORTS
+from products.warehouse_sources.backend.temporal.data_imports.sources.tenjin.settings import (
+ DEFAULT_LOOKBACK_DAYS,
+ TENJIN_REPORTS,
+)
TENJIN_BASE_URL = "https://api.tenjin.com/v2"
# First sync backfill depth. Attribution platforms rarely keep report data useful beyond this.
MAX_HISTORY_DAYS = 730
-# Tenjin keeps restating recent days (late SKAN postbacks, ad network cost corrections), so an
-# incremental run re-reads a trailing window; the merge dedupes on the dimension key.
-LOOKBACK_DAYS = 7
# The documented maximum, to keep the request count low on long backfills.
PER_PAGE = 1000
VALIDATE_TIMEOUT_SECONDS = 30
@@ -70,12 +70,13 @@ def resolve_start_date(
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
today: date,
+ lookback_days: int = DEFAULT_LOOKBACK_DAYS,
) -> date:
earliest = today - timedelta(days=MAX_HISTORY_DAYS)
if should_use_incremental_field:
watermark = _to_date(db_incremental_field_last_value)
if watermark is not None:
- return min(max(watermark - timedelta(days=LOOKBACK_DAYS), earliest), today)
+ return min(max(watermark - timedelta(days=lookback_days), earliest), today)
return earliest
@@ -93,7 +94,9 @@ def tenjin_source(
raise ValueError(f"Unknown Tenjin report: {endpoint}")
today = _today()
- start = resolve_start_date(should_use_incremental_field, db_incremental_field_last_value, today)
+ start = resolve_start_date(
+ should_use_incremental_field, db_incremental_field_last_value, today, lookback_days=report.lookback_days
+ )
config: RESTAPIConfig = {
"client": {
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tests/test_tenjin.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tests/test_tenjin.py
index 0c55582d259c..89c966e9d58a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tests/test_tenjin.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tenjin/tests/test_tenjin.py
@@ -67,14 +67,26 @@ class TestResolveStartDate:
class TestRequestShaping:
- def test_incremental_requests_only_the_watermark_range(self) -> None:
+ @parameterized.expand(
+ [
+ ("spend_report", "app_report", "2026-06-28", "2026-06-21"),
+ ("ad_revenue_report", "ad_placement_report", "2026-06-28", "2026-06-21"),
+ # Day-30 cohort metrics keep changing for 30 days after the install date.
+ ("cohort_report", "campaign_cohort_report", "2026-06-28", "2026-05-22"),
+ ("cohort_window_clamped_to_history", "campaign_cohort_report", "2024-07-01", "2024-06-30"),
+ ]
+ )
+ def test_incremental_requests_only_the_watermark_range(
+ self, _name: str, endpoint: str, watermark: str, expected_start: str
+ ) -> None:
result = _drive(
- [_report_page([{"date": "2026-06-28", "app_id": "a1"}])],
+ [_report_page([{"date": watermark}])],
+ endpoint=endpoint,
should_use_incremental_field=True,
- db_incremental_field_last_value="2026-06-28",
+ db_incremental_field_last_value=watermark,
)
- assert result.requests[0].param("start_date") == "2026-06-21"
+ assert result.requests[0].param("start_date") == expected_start
assert result.requests[0].param("end_date") == "2026-06-30"
@parameterized.expand([(name,) for name in TENJIN_REPORTS])