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(warehouse-sources): add device model and API level breakdowns to Google Play vitals (#98074)

jake sciottob3db66fe587a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/canonical_descriptions.py
index bd64b69fdc62..470f14f03e6b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/canonical_descriptions.py
@@ -1,5 +1,9 @@
from products.warehouse_sources.backend.temporal.data_imports.sources.common.canonical_descriptions import (
CanonicalDescriptions,
+ CanonicalEndpoint,
+)
+from products.warehouse_sources.backend.temporal.data_imports.sources.google_play_console.settings import (
+ BREAKDOWN_TABLES,
)
# Columns every vitals metric-set table carries: the app and window the row aggregates, plus the
@@ -13,7 +17,7 @@ _TIMELINE_COLUMNS: dict[str, str] = {
"distinctUsers": "Count of distinct users the metrics are computed over, after Play's privacy thresholding.",
}
-CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
+_BASE_DESCRIPTIONS: CanonicalDescriptions = {
"crash_rate": {
"description": (
"Daily Android vitals crash rate: the fraction of distinct users who experienced at least "
@@ -197,3 +201,37 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
},
},
}
+
+
+_BREAKDOWN_COLUMNS: dict[str, dict[str, str]] = {
+ "deviceModel": {
+ "deviceModel": "Device model the row is sliced by, as Play's brand and device code.",
+ "deviceModelLabel": "Marketing name of the device model.",
+ },
+ "apiLevel": {
+ "apiLevel": "Android API level of the devices the row is sliced by.",
+ "apiLevelLabel": "Human-readable label for the Android API level.",
+ },
+}
+
+_BREAKDOWN_PHRASES: dict[str, str] = {"deviceModel": "device model", "apiLevel": "Android API level"}
+
+
+def _breakdown_entry(base: CanonicalEndpoint, dimension: str) -> CanonicalEndpoint:
+ return {
+ "description": (
+ f"{base['description'].rstrip('.')}, then by {_BREAKDOWN_PHRASES[dimension]}. Off by default, "
+ f"because the extra dimension multiplies rows per day and rows synced is billed."
+ ),
+ "docs_url": base["docs_url"],
+ "columns": {**base["columns"], **_BREAKDOWN_COLUMNS[dimension]},
+ }
+
+
+CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
+ **_BASE_DESCRIPTIONS,
+ **{
+ name: _breakdown_entry(_BASE_DESCRIPTIONS[base], dimension)
+ for name, (base, dimension) in BREAKDOWN_TABLES.items()
+ },
+}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/settings.py
index 0b6f0d2d6a33..8fdae70c95c5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/settings.py
@@ -40,6 +40,7 @@ class MetricSetEndpoint:
# daily data, but the error backend serves only about the last two months, and it rejects
# a query that starts earlier with a bare `400 INVALID_ARGUMENT`.
history_days: int = METRIC_SET_HISTORY_DAYS
+ should_sync_default: bool = True
@dataclass(frozen=True)
@@ -63,12 +64,21 @@ class ListEndpoint:
datetime_fields: tuple[str, ...] = ()
-# Every vitals metric set is sliced by `versionCode` only. Play drops rows whose user counts fall
-# under its privacy threshold, and each extra dimension multiplies the slices, so a wider default
-# breakdown would silently lose data on smaller apps.
_VERSION_CODE = ("versionCode",)
-METRIC_SETS: dict[str, MetricSetEndpoint] = {
+BREAKDOWN_DIMENSIONS: tuple[tuple[str, str], ...] = (
+ ("device_model", "deviceModel"),
+ ("api_level", "apiLevel"),
+)
+
+_DIMENSION_PHRASES: dict[str, str] = {
+ "versionCode": "app version",
+ "startType": "start type",
+ "deviceModel": "device model",
+ "apiLevel": "Android API level",
+}
+
+_BASE_METRIC_SETS: dict[str, MetricSetEndpoint] = {
"crash_rate": MetricSetEndpoint(
name="crash_rate",
resource="crashRateMetricSet",
@@ -177,6 +187,47 @@ METRIC_SETS: dict[str, MetricSetEndpoint] = {
),
}
+_VITALS_RATE_METRIC_SETS: tuple[str, ...] = tuple(name for name in _BASE_METRIC_SETS if name != "error_counts")
+
+BREAKDOWN_TABLES: dict[str, tuple[str, str]] = {
+ f"{base}_by_{suffix}": (base, dimension)
+ for base in _VITALS_RATE_METRIC_SETS
+ for suffix, dimension in BREAKDOWN_DIMENSIONS
+}
+
+
+def _dimension_phrase(dimensions: tuple[str, ...]) -> str:
+ phrases = [_DIMENSION_PHRASES[dimension] for dimension in dimensions]
+ if len(phrases) == 1:
+ return phrases[0]
+ return f"{', '.join(phrases[:-1])} and {phrases[-1]}"
+
+
+def _breakdown_endpoint(name: str, base: MetricSetEndpoint, dimension: str) -> MetricSetEndpoint:
+ dimensions = (*base.dimensions, dimension)
+ subject = base.description.split(", broken out by")[0].rstrip(".")
+ return MetricSetEndpoint(
+ name=name,
+ resource=base.resource,
+ metrics=base.metrics,
+ dimensions=dimensions,
+ description=(
+ f"{subject}, broken out by {_dimension_phrase(dimensions)}. Off by default, because the "
+ f"extra dimension multiplies rows per day and rows synced is billed."
+ ),
+ history_days=base.history_days,
+ should_sync_default=False,
+ )
+
+
+METRIC_SETS: dict[str, MetricSetEndpoint] = {
+ **_BASE_METRIC_SETS,
+ **{
+ name: _breakdown_endpoint(name, _BASE_METRIC_SETS[base], dimension)
+ for name, (base, dimension) in BREAKDOWN_TABLES.items()
+ },
+}
+
LIST_ENDPOINTS: dict[str, ListEndpoint] = {
"apps": ListEndpoint(
name="apps",
@@ -236,6 +287,8 @@ DESCRIPTIONS: dict[str, str] = {
**{name: endpoint.description for name, endpoint in LIST_ENDPOINTS.items()},
}
+SHOULD_SYNC_DEFAULT: dict[str, bool] = {name: endpoint.should_sync_default for name, endpoint in METRIC_SETS.items()}
+
LOOKBACK_SECONDS: dict[str, int] = {
**dict.fromkeys(METRIC_SETS, METRIC_SET_LOOKBACK_SECONDS),
"error_reports": ERROR_REPORT_LOOKBACK_SECONDS,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/source.py
index 768e214d0d91..caf9867a78cf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/source.py
@@ -39,6 +39,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.google_pla
LOOKBACK_SECONDS,
MERGE_ONLY,
PRIMARY_KEYS,
+ SHOULD_SYNC_DEFAULT,
)
from products.warehouse_sources.backend.types import ExternalDataSourceType
@@ -155,6 +156,7 @@ Leave the package names blank to sync every app the service account can see.""",
names=names,
merge_only=MERGE_ONLY,
descriptions=DESCRIPTIONS,
+ should_sync_default=SHOULD_SYNC_DEFAULT,
)
for schema in schemas:
schema.default_incremental_lookback_seconds = LOOKBACK_SECONDS.get(schema.name)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console.py
index ba2638aa268d..a1cab0c98f5d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console.py
@@ -410,6 +410,50 @@ def test_metric_set_windows_the_timeline_and_stops_at_the_freshness_date() -> No
assert manager.saved == [GooglePlayConsoleResumeConfig(app="com.example.app", date="2024-03-10")]
+def test_a_wider_metric_set_asks_play_for_the_extra_dimension() -> None:
+ endpoint = METRIC_SETS["crash_rate_by_device_model"]
+ bodies: list[dict[str, Any]] = []
+
+ def request(method: str, path: str, params: Any = None, body: Any = None) -> dict[str, Any]:
+ if path.endswith(":query"):
+ bodies.append(body)
+ return {"rows": [_metric_row(dt.date(2024, 3, 5), 12, "0.01")]}
+ return _freshness(dt.date(2024, 3, 10))
+
+ with mock.patch.object(GooglePlayConsoleClient, "request", side_effect=request):
+ client = _client(mock.MagicMock())
+ list(
+ _iter_metric_set_rows(
+ client=client,
+ endpoint=endpoint,
+ package_names=["com.example.app"],
+ history_start=dt.date(2024, 3, 1),
+ manager=_manager(),
+ resume=None,
+ )
+ )
+
+ assert bodies[0]["dimensions"] == ["versionCode", "deviceModel"]
+ assert bodies[0]["metrics"] == list(METRIC_SETS["crash_rate"].metrics)
+
+
+def test_a_wider_metric_set_row_carries_its_extra_dimension_column() -> None:
+ row = _metric_row_to_dict(
+ {
+ "startTime": {"year": 2024, "month": 3, "day": 2},
+ "endTime": {"year": 2024, "month": 3, "day": 3},
+ "dimensions": [{"dimension": "deviceModel", "stringValue": "Pixel 8", "valueLabel": "Google Pixel 8"}],
+ "metrics": [],
+ },
+ "com.example.app",
+ METRIC_SETS["crash_rate_by_device_model"],
+ )
+
+ assert row["deviceModel"] == "Pixel 8"
+ assert row["deviceModelLabel"] == "Google Pixel 8"
+ assert row["versionCode"] is None
+
+
def test_metric_set_skips_an_app_without_freshness() -> None:
manager = _manager()
calls: list[str] = []
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console_source.py
index c5f4da23149e..10eaafebe453 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_play_console/tests/test_google_play_console_source.py
@@ -11,6 +11,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.google_pla
CANONICAL_DESCRIPTIONS,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.google_play_console.settings import (
+ BREAKDOWN_TABLES,
ENDPOINTS,
LIST_ENDPOINTS,
METRIC_SETS,
@@ -68,6 +69,54 @@ def test_metric_sets_sync_incrementally_on_date_but_never_append(name: str) -> N
assert schema.default_incremental_lookback_seconds == 7 * 24 * 60 * 60
+BASE_VITALS_TABLES = (
+ "crash_rate",
+ "anr_rate",
+ "excessive_wakeup_rate",
+ "stuck_background_wakelock_rate",
+ "slow_start_rate",
+ "slow_rendering_rate",
+ "lmk_rate",
+)
+
+
+def test_the_default_vitals_tables_keep_their_grain() -> None:
+ assert METRIC_SETS["crash_rate"].dimensions == ("versionCode",)
+ assert METRIC_SETS["slow_start_rate"].dimensions == ("startType", "versionCode")
+ assert METRIC_SETS["error_counts"].dimensions == ("reportType", "versionCode")
+ assert PRIMARY_KEYS["crash_rate"] == ["app", "date", "versionCode"]
+ assert PRIMARY_KEYS["slow_start_rate"] == ["app", "date", "startType", "versionCode"]
+ assert PRIMARY_KEYS["error_counts"] == ["app", "date", "reportType", "versionCode"]
+
+
+def test_every_vitals_rate_metric_set_gets_a_device_model_and_an_api_level_table() -> None:
+ assert set(BREAKDOWN_TABLES) == {
+ f"{base}_by_{suffix}" for base in BASE_VITALS_TABLES for suffix in ("device_model", "api_level")
+ }
+ assert "error_counts_by_device_model" not in METRIC_SETS
+
+
+def test_the_wider_tables_are_the_only_ones_off_by_default() -> None:
+ schemas = GooglePlayConsoleSource().get_schemas(_config(), team_id=1)
+
+ assert {schema.name for schema in schemas if not schema.should_sync_default} == set(BREAKDOWN_TABLES)
+
+
+ "name,base,dimension", [(name, base, dimension) for name, (base, dimension) in sorted(BREAKDOWN_TABLES.items())]
+)
+def test_a_wider_table_adds_one_dimension_to_its_base_table(name: str, base: str, dimension: str) -> None:
+ endpoint = METRIC_SETS[name]
+ base_endpoint = METRIC_SETS[base]
+
+ assert endpoint.dimensions == (*base_endpoint.dimensions, dimension)
+ assert endpoint.resource == base_endpoint.resource
+ assert endpoint.metrics == base_endpoint.metrics
+ assert endpoint.history_days == base_endpoint.history_days
+ assert PRIMARY_KEYS[name] == ["app", "date", *base_endpoint.dimensions, dimension]
+ assert "Off by default" in endpoint.description
+
+
def test_error_reports_sync_incrementally_on_event_time() -> None:
schema = next(s for s in GooglePlayConsoleSource().get_schemas(_config(), team_id=1) if s.name == "error_reports")