: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(hogql): register marketing session precompute table (#104540)
Javier Bahamondes5d9b0b7c03fc
diff --git a/docs/published/handbook/engineering/databases/hogql-python.md b/docs/published/handbook/engineering/databases/hogql-python.mdindex 0e048a855458..2f49d2c21187 100644--- a/docs/published/handbook/engineering/databases/hogql-python.md+++ b/docs/published/handbook/engineering/databases/hogql-python.md@@ -149,3 +149,6 @@ If you access `poe.properties.$browser`, we will actually access the field `persIn practice, you should avoid both and access `person.properties.$browser`, which will choose the right approach for you.Add new tables and fields as needed! Just make sure each table has a `team_id` column.++Internal marketing queries can read cached session dimensions from `posthog.web_sessions_dimensional_preaggregated`.+Rows include a precompute job ID and the person ID at computation time; readers must resolve current identities separately.diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex cda04ac98b37..57bd8171db81 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -149,6 +149,7 @@ from posthog.hogql.database.schema.web_analytics_preaggregated import ()from posthog.hogql.database.schema.web_goals_preaggregated import WebGoalsPreaggregatedTablefrom posthog.hogql.database.schema.web_overview_preaggregated import WebOverviewPreaggregatedTable+from posthog.hogql.database.schema.web_sessions_preaggregated import WebSessionsPreaggregatedTablefrom posthog.hogql.database.schema.web_stats_frustration_preaggregated import WebStatsFrustrationPreaggregatedTablefrom posthog.hogql.database.schema.web_stats_paths_preaggregated import WebStatsPathsPreaggregatedTablefrom posthog.hogql.database.schema.web_stats_preaggregated import WebStatsPreaggregatedTable@@ -578,6 +579,10 @@ def _construct_database_root_node(*, include_posthog_tables: bool) -> TableNode:"web_overview_preaggregated": TableNode(name="web_overview_preaggregated", table=WebOverviewPreaggregatedTable()),+ "web_sessions_dimensional_preaggregated": TableNode(+ name="web_sessions_dimensional_preaggregated",+ table=WebSessionsPreaggregatedTable(),+ ),"marketing_touchpoints_preaggregated": TableNode(name="marketing_touchpoints_preaggregated",table=MarketingTouchpointsPreaggregatedTable(),diff --git a/posthog/hogql/database/schema/web_sessions_preaggregated.py b/posthog/hogql/database/schema/web_sessions_preaggregated.pynew file mode 100644index 000000000000..cec879e9f967--- /dev/null+++ b/posthog/hogql/database/schema/web_sessions_preaggregated.py@@ -0,0 +1,79 @@+from typing import TYPE_CHECKING++from pydantic import Field++from posthog.hogql.constants import HogQLQuerySettings+from posthog.hogql.database.models import (+ DateTimeDatabaseField,+ FieldOrTable,+ IntegerDatabaseField,+ StringDatabaseField,+ Table,+)++from posthog.clickhouse.preaggregation.web_sessions_sql import DISTRIBUTED_WEB_SESSIONS_TABLE++if TYPE_CHECKING:+ from posthog.hogql.context import HogQLContext++_DIMENSIONS = {+ "channel_type": "Channel the session was attributed to, classified at write time.",+ "utm_source": "Entry utm_source.",+ "utm_medium": "Entry utm_medium.",+ "utm_campaign": "Entry utm_campaign.",+ "utm_term": "Entry utm_term.",+ "utm_content": "Entry utm_content.",+ "referring_domain": "Entry referring domain.",+ "entry_pathname": "Path of the session's first pageview.",+}+++def _build_fields() -> dict[str, FieldOrTable]:+ fields: dict[str, FieldOrTable] = {+ "team_id": IntegerDatabaseField(name="team_id"),+ "job_id": StringDatabaseField(+ name="job_id", description="Identifier of the preaggregation job that produced this row."+ ),+ "period_bucket": DateTimeDatabaseField(+ name="period_bucket", description="Hourly UTC bucket on the session's start timestamp."+ ),+ "session_id_v7": IntegerDatabaseField(name="session_id_v7", description="The session this row describes."),+ "person_id": StringDatabaseField(+ name="person_id", description="Person ID when this row was computed; it can change after a merge."+ ),+ "start_timestamp": DateTimeDatabaseField(name="start_timestamp", description="When the session started (UTC)."),+ "min_event_timestamp": DateTimeDatabaseField(+ name="min_event_timestamp", description="Earliest event timestamp in the session (UTC)."+ ),+ "max_event_timestamp": DateTimeDatabaseField(+ name="max_event_timestamp", description="Latest event timestamp in the session (UTC)."+ ),+ }+ for name, description in _DIMENSIONS.items():+ fields[name] = StringDatabaseField(name=name, description=description)+ fields["pageview_count"] = IntegerDatabaseField(name="pageview_count", description="Pageviews in the session.")+ fields["computed_at"] = DateTimeDatabaseField(+ name="computed_at", description="When this row was computed; also the ReplacingMergeTree version."+ )+ fields["expires_at"] = DateTimeDatabaseField(+ name="expires_at", description="When this row expires and is dropped via TTL."+ )+ return fields+++class WebSessionsPreaggregatedTable(Table):+ description: str = (+ "Internal preaggregated sessions (one row per session and stored person), carrying the "+ "session's entry attribution with the channel already classified. Feeds attribution in marketing analytics."+ )+ top_level_settings: HogQLQuerySettings | None = Field(+ default_factory=lambda: HogQLQuerySettings(load_balancing="in_order")+ )++ fields: dict[str, FieldOrTable] = _build_fields()++ def to_printed_clickhouse(self, context: "HogQLContext") -> str:+ return DISTRIBUTED_WEB_SESSIONS_TABLE()++ def to_printed_hogql(self) -> str:+ return "web_sessions_dimensional_preaggregated"