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 optimizely attributes and results tables (#123997)

Tom Owers936acdd69d7d
diff --git a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
index a1ef91eb5708..e5efa6d3aa73 100644
--- a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
+++ b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
@@ -823,7 +823,6 @@
1 products/warehouse_sources/backend/temporal/data_imports/sources/opinion_stage/settings.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/opsgenie/opsgenie.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/opsgenie/settings.py
-1 products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/settings.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/opuswatch/opuswatch.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/orb/orb.py
1 products/warehouse_sources/backend/temporal/data_imports/sources/orb/settings.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 ff0cdee83484..5c15a1f861ef 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
@@ -8917,10 +8917,10 @@ Today (6): `audiences`, `campaigns`, `events`, `experiments`, `pages`, `projects
Diffed against: <https://api.optimizely.com/v2/swagger.json>
-- [ ] `GET /experiments/{experiment_id}/results` — the experiment results payload (variation conversions, lift, significance) — the product's headline metric, entirely absent today (high)
-- [ ] `GET /experiments/{experiment_id}/timeseries` — day-by-day results series, required to plot experiment progression rather than a single snapshot (high)
-- [ ] `GET /campaigns/{campaign_id}/results` — same headline metric for the campaigns table already synced (high)
-- [ ] `GET /attributes` — lookup resolving the visitor attribute IDs referenced by synced audiences and experiment targeting (high)
+- [x] `GET /experiments/{experiment_id}/results` — the experiment results payload (variation conversions, lift, significance) — the product's headline metric, entirely absent today (high)
+- [x] `GET /experiments/{experiment_id}/timeseries` — day-by-day results series, required to plot experiment progression rather than a single snapshot (high)
+- [x] `GET /campaigns/{campaign_id}/results` — same headline metric for the campaigns table already synced (high)
+- [x] `GET /attributes` — lookup resolving the visitor attribute IDs referenced by synced audiences and experiment targeting (high)
- [ ] `GET /environments` — lookup resolving the environment IDs carried on experiments and features (medium)
- [ ] `GET /features` — feature (flag) definitions are a first-class experimentation object here, not vendor plumbing — needed to join rollout results (medium)
- [ ] `GET /changes` — change history across experiments and pages — audit/transition table for who changed what and when (medium)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/canonical_descriptions.py
index 3b4284813992..3b89e8d5e754 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/canonical_descriptions.py
@@ -19,6 +19,15 @@ _COMMON_COLUMNS = {
}
+# Fields shared by the experiment and campaign results payloads.
+_RESULTS_COLUMNS = {
+ "start_time": "Start of the time interval (inclusive) the results cover.",
+ "end_time": "End of the time interval (exclusive) the results cover.",
+ "confidence_threshold": "Significance level at which variations are declared winning or losing.",
+ "is_stale": "Whether the results came from an out-of-date cache.",
+}
+
+
def _columns(**overrides: str) -> dict[str, str]:
return {**_COMMON_COLUMNS, **overrides}
@@ -105,4 +114,48 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
latest="Latest time the campaign served.",
),
},
+ "attributes": {
+ "description": "A custom visitor attribute that audiences and experiment targeting conditions can reference.",
+ "docs_url": "https://library.optimizely.com/docs/api/app/v2/index.html#tag/Attributes",
+ "columns": {
+ "id": "Unique numeric identifier for the attribute.",
+ "name": "The attribute's name. For Full Stack projects, this is set to the attribute's key.",
+ "last_modified": "Time at which the attribute was last modified.",
+ "project_id": "ID of the project the attribute belongs to.",
+ "key": "Unique string identifier for the attribute within the project.",
+ "description": "Description of the attribute.",
+ "condition_type": "Whether the attribute is a custom attribute or a read-only custom dimension from an Optimizely Classic experiment.",
+ "archived": "Whether the attribute has been archived.",
+ },
+ },
+ "experiment_results": {
+ "description": "Current results snapshot for an experiment: reach and per-metric, per-variation conversions, lift, and statistical significance.",
+ "docs_url": "https://library.optimizely.com/docs/api/app/v2/index.html#tag/Experiments",
+ "columns": {
+ **_RESULTS_COLUMNS,
+ "experiment_id": "ID of the experiment the results are for.",
+ "metrics": "Results per metric, keyed by variation. Index 0 is the primary metric.",
+ "reach": "Number of visitors exposed to each variation and in total.",
+ "stats_config": "Stats Engine configuration used to compute the results.",
+ },
+ },
+ "experiment_timeseries": {
+ "description": "Results for an experiment as a time series, so a metric's progression over the experiment's run can be plotted.",
+ "docs_url": "https://library.optimizely.com/docs/api/app/v2/index.html#tag/Experiments",
+ "columns": {
+ **_RESULTS_COLUMNS,
+ "experiment_id": "ID of the experiment the results are for.",
+ "metrics": "Per-metric results, each holding a list of time series data points per variation.",
+ "stats_config": "Stats Engine configuration used to compute the results.",
+ },
+ },
+ "campaign_results": {
+ "description": "Current results snapshot for a personalization campaign: per-metric conversions and lift for the campaign against its holdback.",
+ "docs_url": "https://library.optimizely.com/docs/api/app/v2/index.html#tag/Campaigns",
+ "columns": {
+ **_RESULTS_COLUMNS,
+ "campaign_id": "ID of the campaign the results are for.",
+ "metrics": "Results per metric. Index 0 is the primary metric.",
+ },
+ },
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/optimizely.py b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/optimizely.py
index f1e763d6b233..621e33e3706f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/optimizely.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/optimizely.py
@@ -6,9 +6,14 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.res
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.paginators import (
HeaderLinkPaginator,
+ SinglePagePaginator,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.resource import Resource
-from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.typing import ClientConfig
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.typing import (
+ ClientConfig,
+ EndpointResource,
+ ResponseAction,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.source_helpers import validate_via_probe
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
from products.warehouse_sources.backend.temporal.data_imports.sources.optimizely.settings import (
@@ -21,6 +26,12 @@ OPTIMIZELY_BASE_URL = f"https://{OPTIMIZELY_API_HOST}/v2"
# Optimizely list pages cap at 100 items.
PAGE_SIZE = 100
+_SKIP_INACCESSIBLE: list[ResponseAction] = [
+ {"status_code": 400, "action": "ignore"},
+ {"status_code": 403, "action": "ignore"},
+ {"status_code": 404, "action": "ignore"},
+]
+
def _client_config(api_token: str) -> ClientConfig:
return {
@@ -58,45 +69,76 @@ def _simple_source(
return rest_api_resource(rest_config, team_id, job_id, None)
+def _project_scoped_resource(name: str, path: str) -> EndpointResource:
+ return {
+ "name": name,
+ "endpoint": {
+ # project_id rides in a query param; embed the resolve placeholder in the path so
+ # the binding lands in the query string (resolve only substitutes into paths).
+ "path": f"{path}?project_id={{project_id}}",
+ "params": {
+ "project_id": {"type": "resolve", "resource": "projects", "field": "id"},
+ "per_page": PAGE_SIZE,
+ },
+ # A project without access to this feature (e.g. campaigns on a non-Web project)
+ # returns 400/403/404 — treat that as a valid empty page and move to the next
+ # project rather than failing the whole stream, matching the old per-project skip.
+ "response_actions": _SKIP_INACCESSIBLE,
+ },
+ }
+
+
+def _per_parent_resource(endpoint: str, path: str, parent: str, parent_id_param: str) -> EndpointResource:
+ return {
+ "name": endpoint,
+ "endpoint": {
+ "path": path,
+ "params": {
+ parent_id_param: {"type": "resolve", "resource": parent, "field": "id"},
+ },
+ # Results come back as one object per parent, not a list.
+ "data_selector": "$",
+ "paginator": SinglePagePaginator(),
+ # 202 means the results are still being computed. These tables are full refresh, so
+ # skipping the parent would drop its previous snapshot: retry with the client's backoff,
+ # and fail the sync (keeping the old table) if the results never finish. 204 means the
+ # parent has no results yet (e.g. a draft experiment). 4xx covers parents whose results
+ # the token can't read.
+ "response_actions": [
+ {"status_code": 202, "action": "retry", "message": "Optimizely results are still being computed"},
+ {"status_code": 204, "action": "ignore"},
+ *_SKIP_INACCESSIBLE,
+ ],
+ },
+ }
+
+
def _fan_out_source(
api_token: str, endpoint: str, config: OptimizelyEndpointConfig, team_id: int, job_id: str
) -> Resource:
+ resources: list[str | EndpointResource] = [
+ {
+ "name": "projects",
+ "endpoint": {
+ "path": "/projects",
+ "params": {"per_page": PAGE_SIZE},
+ },
+ },
+ ]
+ if config.parent is None or config.parent_id_param is None:
+ resources.append(_project_scoped_resource(endpoint, config.path))
+ else:
+ resources.append(_project_scoped_resource(config.parent, OPTIMIZELY_ENDPOINTS[config.parent].path))
+ resources.append(_per_parent_resource(endpoint, config.path, config.parent, config.parent_id_param))
+
rest_config: RESTAPIConfig = {
"client": _client_config(api_token),
"resource_defaults": {},
- "resources": [
- {
- "name": "projects",
- "endpoint": {
- "path": "/projects",
- "params": {"per_page": PAGE_SIZE},
- },
- },
- {
- "name": endpoint,
- "endpoint": {
- # project_id rides in a query param; embed the resolve placeholder in the path so
- # the binding lands in the query string (resolve only substitutes into paths).
- "path": f"{config.path}?project_id={{project_id}}",
- "params": {
- "project_id": {"type": "resolve", "resource": "projects", "field": "id"},
- "per_page": PAGE_SIZE,
- },
- # A project without access to this feature (e.g. campaigns on a non-Web project)
- # returns 400/403/404 — treat that as a valid empty page and move to the next
- # project rather than failing the whole stream, matching the old per-project skip.
- "response_actions": [
- {"status_code": 400, "action": "ignore"},
- {"status_code": 403, "action": "ignore"},
- {"status_code": 404, "action": "ignore"},
- ],
- },
- },
- ],
+ "resources": resources,
}
- resources = rest_api_resources(rest_config, team_id, job_id, None)
- # Only the child rows are emitted; the projects list is iterated internally to drive the fan-out.
- return next(resource for resource in resources if resource.name == endpoint)
+ built = rest_api_resources(rest_config, team_id, job_id, None)
+ # Only the requested endpoint's rows are emitted; its ancestors are iterated internally to drive the fan-out.
+ return next(resource for resource in built if resource.name == endpoint)
def optimizely_source(api_token: str, endpoint: str, team_id: int, job_id: str) -> SourceResponse:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/settings.py
index 7bae7b97dc01..b985e849c882 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/settings.py
@@ -1,13 +1,17 @@
-from dataclasses import dataclass
+from posthog.dataclasses import frozen
-@dataclass
+@frozen
class OptimizelyEndpointConfig:
path: str
# Most v2 list endpoints are project-scoped and need a project_id param,
# so the transport fans out over the projects list.
project_scoped: bool = True
primary_key: str = "id"
+ # Per-entity endpoints (results, timeseries) take one parent row's id in the path, so the
+ # transport walks projects -> parent list -> this endpoint, one request per parent row.
+ parent: str | None = None
+ parent_id_param: str | None = None
# Optimizely's v2 list endpoints have no updated-since filters, and all the
@@ -34,6 +38,29 @@ OPTIMIZELY_ENDPOINTS: dict[str, OptimizelyEndpointConfig] = {
"campaigns": OptimizelyEndpointConfig(
path="/campaigns",
),
+ "attributes": OptimizelyEndpointConfig(
+ path="/attributes",
+ ),
+ # Results are a point-in-time computation over the experiment's (or campaign's) whole run, with
+ # no change cursor, so they are full refresh too: each sync replaces the snapshot per parent.
+ "experiment_results": OptimizelyEndpointConfig(
+ path="/experiments/{experiment_id}/results",
+ primary_key="experiment_id",
+ parent="experiments",
+ parent_id_param="experiment_id",
+ ),
+ "experiment_timeseries": OptimizelyEndpointConfig(
+ path="/experiments/{experiment_id}/timeseries",
+ primary_key="experiment_id",
+ parent="experiments",
+ parent_id_param="experiment_id",
+ ),
+ "campaign_results": OptimizelyEndpointConfig(
+ path="/campaigns/{campaign_id}/results",
+ primary_key="campaign_id",
+ parent="campaigns",
+ parent_id_param="campaign_id",
+ ),
}
ENDPOINTS = tuple(OPTIMIZELY_ENDPOINTS.keys())
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/source.py
index 585bdae71810..9757a1d3f8ae 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/source.py
@@ -112,7 +112,7 @@ class OptimizelySource(SimpleSource[OptimizelySourceConfig]):
label="Optimizely",
caption="""Enter your Optimizely personal access token to pull your experimentation data into the PostHog Data warehouse.
-An admin can generate a personal access token in Optimizely under Account Settings > API Access. Tokens are non-expiring. Project-scoped data (experiments, audiences, events, pages, campaigns) is synced across all projects the token can access.""",
+An admin can generate a personal access token in Optimizely under Account Settings > API Access. Tokens are non-expiring. Project-scoped data (experiments and their results, audiences, attributes, events, pages, campaigns and their results) is synced across all projects the token can access.""",
iconPath="/static/services/optimizely.com.png",
docsUrl="https://posthog.com/docs/cdp/sources/optimizely",
releaseStatus=ReleaseStatus.GA,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/tests/test_optimizely.py b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/tests/test_optimizely.py
index 6a18d4339d75..c4801daa2a19 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/tests/test_optimizely.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/optimizely/tests/test_optimizely.py
@@ -102,6 +102,52 @@ class TestProjectScopedFanOut:
assert result.requests[1].param("project_id") == "11"
+class TestPerParentResults:
+ @parameterized.expand(
+ [
+ ("experiment_results", "/v2/experiments", "experiment_id"),
+ ("experiment_timeseries", "/v2/experiments", "experiment_id"),
+ ("campaign_results", "/v2/campaigns", "campaign_id"),
+ ]
+ )
+ def test_walks_projects_then_parents_retrying_pending_results(
+ self, endpoint: str, parent_path: str, id_field: str
+ ) -> None:
+ suffix = endpoint.split("_")[1]
+ result = _driver().run(
+ endpoint,
+ route(
+ {
+ "/v2/projects": [_response([{"id": 11}, {"id": 22}])],
+ parent_path: [
+ _response([{"id": 1}, {"id": 2}]),
+ _response([{"id": 3}]),
+ ],
+ f"/1/{suffix}": [
+ ScriptedResponse(status=202),
+ ScriptedResponse(json={id_field: 1, "metrics": []}),
+ ],
+ f"/2/{suffix}": [ScriptedResponse(status=204)],
+ f"/3/{suffix}": [ScriptedResponse(json={id_field: 3, "metrics": [{"name": "Clicks"}]})],
+ }
+ ),
+ )
+
+ # A 202 (still computing) is retried rather than dropped; a 204 (no results yet) is skipped.
+ assert result.raised is None
+ assert result.paths == [
+ "/v2/projects",
+ parent_path,
+ f"{parent_path}/1/{suffix}",
+ f"{parent_path}/1/{suffix}",
+ f"{parent_path}/2/{suffix}",
+ parent_path,
+ f"{parent_path}/3/{suffix}",
+ ]
+ assert result.params("project_id") == [None, "11", None, None, None, "22", None]
+ assert result.rows == [{id_field: 1, "metrics": []}, {id_field: 3, "metrics": [{"name": "Clicks"}]}]
+
+
class TestOptimizelySourceResponse:
@parameterized.expand([(endpoint,) for endpoint in ENDPOINTS])
def test_response_metadata_per_endpoint(self, endpoint: str) -> None: