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(sessions): add surfacing_score to session_replay_events (#57997)

Arno Hillen037e5312fa74
diff --git a/posthog/clickhouse/migrations/0265_add_surfacing_score_to_session_replay_events.py b/posthog/clickhouse/migrations/0265_add_surfacing_score_to_session_replay_events.py
new file mode 100644
index 000000000000..2cc4dd7437f5
--- /dev/null
+++ b/posthog/clickhouse/migrations/0265_add_surfacing_score_to_session_replay_events.py
@@ -0,0 +1,70 @@
+from posthog import settings
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.session_recordings.sql.session_replay_event_migrations_sql import (
+ ADD_SURFACING_SCORE_DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL,
+ ADD_SURFACING_SCORE_SESSION_REPLAY_EVENTS_TABLE_SQL,
+ ADD_SURFACING_SCORE_WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL,
+ DROP_KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL,
+ DROP_SESSION_REPLAY_EVENTS_TABLE_MV_SQL,
+)
+from posthog.session_recordings.sql.session_replay_event_sql import (
+ DROP_KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL,
+ DROP_SESSION_REPLAY_EVENTS_WS_MV_SQL,
+ KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL,
+ KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL,
+ SESSION_REPLAY_EVENTS_TABLE_MV_SQL,
+ SESSION_REPLAY_EVENTS_WS_MV_SQL,
+)
+
+# Add surfacing_score to session_replay_events. Cloud uses WarpStream and is dropping MSK;
+# non-cloud only has MSK.
+
+_IS_CLOUD = settings.CLOUD_DEPLOYMENT in ("US", "EU", "DEV")
+
+operations = [
+ # Drop MV + Kafka tables (DROP IF EXISTS, so WS drops no-op in non-cloud).
+ run_sql_with_exceptions(
+ DROP_SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ ),
+ run_sql_with_exceptions(
+ DROP_KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ ),
+ run_sql_with_exceptions(DROP_SESSION_REPLAY_EVENTS_WS_MV_SQL, node_roles=[NodeRole.INGESTION_SMALL]),
+ run_sql_with_exceptions(DROP_KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL, node_roles=[NodeRole.INGESTION_SMALL]),
+ # Add the column: sharded, distributed, writable.
+ run_sql_with_exceptions(
+ ADD_SURFACING_SCORE_SESSION_REPLAY_EVENTS_TABLE_SQL(),
+ node_roles=[NodeRole.DATA],
+ sharded=True,
+ is_alter_on_replicated_table=True,
+ ),
+ run_sql_with_exceptions(
+ ADD_SURFACING_SCORE_DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL(),
+ node_roles=[NodeRole.DATA],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ ),
+ run_sql_with_exceptions(
+ ADD_SURFACING_SCORE_WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL(),
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ node_roles=[NodeRole.INGESTION_SMALL],
+ ),
+ # Recreate Kafka + MV: WarpStream in cloud, MSK elsewhere.
+ *(
+ [
+ run_sql_with_exceptions(KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
+ run_sql_with_exceptions(SESSION_REPLAY_EVENTS_WS_MV_SQL(), node_roles=[NodeRole.INGESTION_SMALL]),
+ ]
+ if _IS_CLOUD
+ else [
+ run_sql_with_exceptions(
+ KAFKA_SESSION_REPLAY_EVENTS_TABLE_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ ),
+ run_sql_with_exceptions(
+ SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=False), node_roles=[NodeRole.INGESTION_SMALL]
+ ),
+ ]
+ ),
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 093470765f58..2d77ed240330 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0264_web_vitals_paths_preaggregated
+0265_add_surfacing_score_to_session_replay_events
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index a53547742f48..061cc64e6d5c 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -610,6 +610,7 @@
ai_tags_fixed Array(String),
ai_tags_freeform Array(String),
ai_highlighted UInt8,
+ surfacing_score Nullable(Float32),
) ENGINE = Kafka(msk_cluster, kafka_topic_list = 'clickhouse_session_replay_events_test', kafka_group_name = 'group1', kafka_format = 'JSONEachRow')
'''
@@ -2468,6 +2469,7 @@
ai_tags_fixed Array(String),
ai_tags_freeform Array(String),
ai_highlighted UInt8,
+ surfacing_score Nullable(Float32),
) ENGINE = Kafka(msk_cluster, kafka_topic_list = 'clickhouse_session_replay_events_test', kafka_group_name = 'group1', kafka_format = 'JSONEachRow')
'''
@@ -4687,6 +4689,8 @@
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-- AI-generated flag indicating the session is highlighted / worth watching
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
+ -- Score in [0, 1] from the surfacing scoring sweep used to surface sessions for downstream summarization
+ surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
) ENGINE = Distributed('posthog', 'posthog_test', 'sharded_session_replay_events', sipHash64(distinct_id))
'''
@@ -4717,6 +4721,7 @@
,`ai_tags_fixed` SimpleAggregateFunction(groupUniqArrayArray, Array(String))
,`ai_tags_freeform` SimpleAggregateFunction(groupUniqArrayArray, Array(String))
,`ai_highlighted` SimpleAggregateFunction(max, UInt8)
+ ,`surfacing_score` SimpleAggregateFunction(max, Nullable(Float32))
)
AS SELECT
session_id,
@@ -4757,6 +4762,7 @@
,groupUniqArrayArray(ai_tags_fixed) as ai_tags_fixed
,groupUniqArrayArray(ai_tags_freeform) as ai_tags_freeform
,max(ai_highlighted) as ai_highlighted
+ ,max(surfacing_score) as surfacing_score
FROM posthog_test.kafka_session_replay_events
group by session_id, team_id
@@ -6008,6 +6014,8 @@
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-- AI-generated flag indicating the session is highlighted / worth watching
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
+ -- Score in [0, 1] from the surfacing scoring sweep used to surface sessions for downstream summarization
+ surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.session_replay_events', '{replica}')
PARTITION BY toYYYYMM(min_first_timestamp)
@@ -7366,6 +7374,8 @@
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-- AI-generated flag indicating the session is highlighted / worth watching
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
+ -- Score in [0, 1] from the surfacing scoring sweep used to surface sessions for downstream summarization
+ surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
) ENGINE = Distributed('posthog', 'posthog_test', 'sharded_session_replay_events', sipHash64(distinct_id))
'''
@@ -9484,6 +9494,8 @@
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-- AI-generated flag indicating the session is highlighted / worth watching
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
+ -- Score in [0, 1] from the surfacing scoring sweep used to surface sessions for downstream summarization
+ surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
) ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/tables/77f1df52-4b43-11e9-910f-b8ca3a9b9f3e_{shard}/posthog.session_replay_events', '{replica}')
PARTITION BY toYYYYMM(min_first_timestamp)
@@ -9991,6 +10003,8 @@
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-- AI-generated flag indicating the session is highlighted / worth watching
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
+ -- Score in [0, 1] from the surfacing scoring sweep used to surface sessions for downstream summarization
+ surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
) ENGINE = Distributed('posthog', 'posthog_test', 'sharded_session_replay_events', sipHash64(distinct_id))
'''
diff --git a/posthog/session_recordings/sql/session_replay_event_migrations_sql.py b/posthog/session_recordings/sql/session_replay_event_migrations_sql.py
index 2f4af4588cf9..1297210e4b11 100644
--- a/posthog/session_recordings/sql/session_replay_event_migrations_sql.py
+++ b/posthog/session_recordings/sql/session_replay_event_migrations_sql.py
@@ -376,3 +376,30 @@ ADD_AI_COLUMNS_WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL = lambda: ALTER_SESSION_
ADD_AI_COLUMNS_DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL = lambda: ALTER_SESSION_REPLAY_ADD_AI_COLUMNS.format(
table_name="session_replay_events",
)
+
+# =========================
+# MIGRATION: Add surfacing_score column for the surfacing scoring sweep
+# Nullable so NULL = "not scored yet" is distinct from "scored as 0". SimpleAggregateFunction(max)
+# is NULL-safe on merge, so the scorer's value wins over the events MV's NULL row.
+# =========================
+
+ALTER_SESSION_REPLAY_ADD_SURFACING_SCORE_COLUMN = """
+ ALTER TABLE {table_name}
+ ADD COLUMN IF NOT EXISTS surfacing_score SimpleAggregateFunction(max, Nullable(Float32))
+"""
+
+ADD_SURFACING_SCORE_SESSION_REPLAY_EVENTS_TABLE_SQL = lambda: ALTER_SESSION_REPLAY_ADD_SURFACING_SCORE_COLUMN.format(
+ table_name=SESSION_REPLAY_EVENTS_DATA_TABLE(),
+)
+
+ADD_SURFACING_SCORE_WRITABLE_SESSION_REPLAY_EVENTS_TABLE_SQL = (
+ lambda: ALTER_SESSION_REPLAY_ADD_SURFACING_SCORE_COLUMN.format(
+ table_name="writable_session_replay_events",
+ )
+)
+
+ADD_SURFACING_SCORE_DISTRIBUTED_SESSION_REPLAY_EVENTS_TABLE_SQL = (
+ lambda: ALTER_SESSION_REPLAY_ADD_SURFACING_SCORE_COLUMN.format(
+ table_name="session_replay_events",
+ )
+)
diff --git a/posthog/session_recordings/sql/session_replay_event_sql.py b/posthog/session_recordings/sql/session_replay_event_sql.py
index 9410043daffa..333e8b4fb7d4 100644
--- a/posthog/session_recordings/sql/session_replay_event_sql.py
+++ b/posthog/session_recordings/sql/session_replay_event_sql.py
@@ -47,6 +47,7 @@ CREATE TABLE IF NOT EXISTS {table_name} {on_cluster_clause}
ai_tags_fixed Array(String),
ai_tags_freeform Array(String),
ai_highlighted UInt8,
+ surfacing_score Nullable(Float32),
) ENGINE = {engine}
"""
@@ -105,6 +106,8 @@ CREATE TABLE IF NOT EXISTS {table_name} {on_cluster_clause}
ai_tags_freeform SimpleAggregateFunction(groupUniqArrayArray, Array(String)),
-- AI-generated flag indicating the session is highlighted / worth watching
ai_highlighted SimpleAggregateFunction(max, UInt8) DEFAULT 0,
+ -- Score in [0, 1] from the surfacing scoring sweep used to surface sessions for downstream summarization
+ surfacing_score SimpleAggregateFunction(max, Nullable(Float32)),
) ENGINE = {engine}
"""
@@ -178,6 +181,7 @@ def SESSION_REPLAY_EVENTS_TABLE_MV_SQL(on_cluster=True, exclude_columns=None):
{",`ai_tags_fixed` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_fixed" not in exclude_columns else ""}
{",`ai_tags_freeform` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_freeform" not in exclude_columns else ""}
{",`ai_highlighted` SimpleAggregateFunction(max, UInt8)" if "ai_highlighted" not in exclude_columns else ""}
+{",`surfacing_score` SimpleAggregateFunction(max, Nullable(Float32))" if "surfacing_score" not in exclude_columns else ""}
)"""
return f"""
@@ -222,6 +226,7 @@ max(_timestamp) as _timestamp
{",groupUniqArrayArray(ai_tags_fixed) as ai_tags_fixed" if "ai_tags_fixed" not in exclude_columns else ""}
{",groupUniqArrayArray(ai_tags_freeform) as ai_tags_freeform" if "ai_tags_freeform" not in exclude_columns else ""}
{",max(ai_highlighted) as ai_highlighted" if "ai_highlighted" not in exclude_columns else ""}
+{",max(surfacing_score) as surfacing_score" if "surfacing_score" not in exclude_columns else ""}
FROM {database}.kafka_session_replay_events
group by session_id, team_id
"""
@@ -323,6 +328,7 @@ def SESSION_REPLAY_EVENTS_WS_MV_SQL(on_cluster=False, exclude_columns=None):
{",`ai_tags_fixed` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_fixed" not in exclude_columns else ""}
{",`ai_tags_freeform` SimpleAggregateFunction(groupUniqArrayArray, Array(String))" if "ai_tags_freeform" not in exclude_columns else ""}
{",`ai_highlighted` SimpleAggregateFunction(max, UInt8)" if "ai_highlighted" not in exclude_columns else ""}
+{",`surfacing_score` SimpleAggregateFunction(max, Nullable(Float32))" if "surfacing_score" not in exclude_columns else ""}
)"""
return f"""
@@ -357,6 +363,7 @@ max(_timestamp) as _timestamp
{",groupUniqArrayArray(ai_tags_fixed) as ai_tags_fixed" if "ai_tags_fixed" not in exclude_columns else ""}
{",groupUniqArrayArray(ai_tags_freeform) as ai_tags_freeform" if "ai_tags_freeform" not in exclude_columns else ""}
{",max(ai_highlighted) as ai_highlighted" if "ai_highlighted" not in exclude_columns else ""}
+{",max(surfacing_score) as surfacing_score" if "surfacing_score" not in exclude_columns else ""}
FROM {database}.{KAFKA_SESSION_REPLAY_EVENTS_WS_TABLE}
group by session_id, team_id
"""