Skip to content

Commit

Permalink
feat(web-analytics): Use $entry in session prop names (#22019)
Browse files Browse the repository at this point in the history
Use $entry in session prop names
  • Loading branch information
robbie-c authored May 1, 2024
1 parent d6f60c8 commit 32de2ec
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 132 deletions.
6 changes: 3 additions & 3 deletions frontend/src/lib/taxonomy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ describe('taxonomy', () => {
const sessionPropertyNames = Object.keys(CORE_FILTER_DEFINITIONS_BY_GROUP.session_properties)
it('should have a $session_referring_domain property', () => {
const property: CoreFilterDefinition =
CORE_FILTER_DEFINITIONS_BY_GROUP.session_properties['$session_referring_domain']
expect(property.label).toEqual('Session Referring Domain')
CORE_FILTER_DEFINITIONS_BY_GROUP.session_properties['$entry_referring_domain']
expect(property.label).toEqual('Entry Referring Domain')
})
it(`should have every property in SESSION_PROPERTIES_ADAPTED_FROM_PERSON`, () => {
for (const property of Array.from(SESSION_INITIAL_PROPERTIES_ADAPTED_FROM_EVENTS.keys())) {
expect(sessionPropertyNames).toContain('$session_' + property.replace('$', ''))
expect(sessionPropertyNames).toContain('$entry_' + property.replace('$', ''))
}
})
})
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/lib/taxonomy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ export const CORE_FILTER_DEFINITIONS_BY_GROUP = {
description: <span>The timestamp of the last event from this session</span>,
examples: [new Date().toISOString()],
},
$entry_url: {
entry_current_url: {
label: 'Entry URL',
description: <span>The first URL visited in this session</span>,
examples: ['https://example.com/interesting-article?parameter=true'],
Expand All @@ -1026,7 +1026,7 @@ export const CORE_FILTER_DEFINITIONS_BY_GROUP = {
description: <span>The first pathname visited in this session</span>,
examples: ['https://example.com/interesting-article?parameter=true'],
},
$exit_url: {
$exit_current_url: {
label: 'Exit URL',
description: <span>The last URL visited in this session</span>,
examples: ['https://example.com/interesting-article?parameter=true'],
Expand Down Expand Up @@ -1094,9 +1094,9 @@ for (const [key, value] of Object.entries(CORE_FILTER_DEFINITIONS_BY_GROUP.event
CORE_FILTER_DEFINITIONS_BY_GROUP.person_properties[key] = value
}
if (SESSION_INITIAL_PROPERTIES_ADAPTED_FROM_EVENTS.has(key)) {
CORE_FILTER_DEFINITIONS_BY_GROUP.session_properties[`$session_${key.replace(/^\$/, '')}`] = {
CORE_FILTER_DEFINITIONS_BY_GROUP.session_properties[`$entry_${key.replace(/^\$/, '')}`] = {
...value,
label: `Session ${value.label}`,
label: `Entry ${value.label}`,
description:
'description' in value
? `${value.description} Data from the first event in this session.`
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/scenes/web-analytics/WebAnalyticsTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ export const webStatsBreakdownToPropertyName = (
case WebStatsBreakdown.InitialChannelType:
return { key: '$channel_type', type: PropertyFilterType.Session }
case WebStatsBreakdown.InitialReferringDomain:
return { key: '$session_referring_domain', type: PropertyFilterType.Session }
return { key: '$entry_referring_domain', type: PropertyFilterType.Session }
case WebStatsBreakdown.InitialUTMSource:
return { key: '$session_utm_source', type: PropertyFilterType.Session }
return { key: '$entry_utm_source', type: PropertyFilterType.Session }
case WebStatsBreakdown.InitialUTMCampaign:
return { key: '$session_utm_campaign', type: PropertyFilterType.Session }
return { key: '$entry_utm_campaign', type: PropertyFilterType.Session }
case WebStatsBreakdown.InitialUTMMedium:
return { key: '$session_utm_medium', type: PropertyFilterType.Session }
return { key: '$entry_utm_medium', type: PropertyFilterType.Session }
case WebStatsBreakdown.InitialUTMContent:
return { key: '$session_utm_content', type: PropertyFilterType.Session }
return { key: '$entry_utm_content', type: PropertyFilterType.Session }
case WebStatsBreakdown.InitialUTMTerm:
return { key: '$session_utm_term', type: PropertyFilterType.Session }
return { key: '$entry_utm_term', type: PropertyFilterType.Session }
case WebStatsBreakdown.Browser:
return { key: '$browser', type: PropertyFilterType.Event }
case WebStatsBreakdown.OS:
Expand Down
8 changes: 4 additions & 4 deletions posthog/api/test/__snapshots__/test_annotation.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@
FROM "posthog_annotation"
LEFT OUTER JOIN "posthog_dashboarditem" ON ("posthog_annotation"."dashboard_item_id" = "posthog_dashboarditem"."id")
LEFT OUTER JOIN "posthog_user" ON ("posthog_annotation"."created_by_id" = "posthog_user"."id")
WHERE ((("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 2)
AND NOT "posthog_annotation"."deleted")
WHERE (NOT "posthog_annotation"."deleted"
AND (("posthog_annotation"."organization_id" = '00000000-0000-0000-0000-000000000000'::uuid
AND "posthog_annotation"."scope" = 'organization')
OR "posthog_annotation"."team_id" = 2))
ORDER BY "posthog_annotation"."date_marker" DESC
LIMIT 1000
'''
Expand Down
38 changes: 19 additions & 19 deletions posthog/api/test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ def test_expected_session_properties(self):
"$autocapture_count",
"$channel_type",
"$end_timestamp",
"$entry_url",
"entry_current_url",
"$entry_pathname",
"$exit_url",
"$exit_current_url",
"$exit_pathname",
"$session_gad_source",
"$session_gclid",
"$session_referring_domain",
"$session_utm_campaign",
"$session_utm_content",
"$session_utm_medium",
"$session_utm_source",
"$session_utm_term",
"$entry_gad_source",
"$entry_gclid",
"$entry_referring_domain",
"$entry_utm_campaign",
"$entry_utm_content",
"$entry_utm_medium",
"$entry_utm_source",
"$entry_utm_term",
"$pageview_count",
"$session_duration",
"$start_timestamp",
Expand All @@ -57,11 +57,11 @@ def test_search_session_properties(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
actual_properties = {entry["name"] for entry in response.json()["results"]}
expected_properties = {
"$session_utm_campaign",
"$session_utm_content",
"$session_utm_medium",
"$session_utm_source",
"$session_utm_term",
"$entry_utm_campaign",
"$entry_utm_content",
"$entry_utm_medium",
"$entry_utm_source",
"$entry_utm_term",
}
assert actual_properties == expected_properties

Expand Down Expand Up @@ -107,7 +107,7 @@ def test_search_channel_type_values(self):
assert actual_values == expected_values

def test_list_session_property_values(self):
response = self.client.get(f"/api/projects/{self.team.pk}/sessions/values/?key=$session_utm_source")
response = self.client.get(f"/api/projects/{self.team.pk}/sessions/values/?key=$entry_utm_source")
self.assertEqual(response.status_code, status.HTTP_200_OK)
actual_values = {entry["name"] for entry in response.json()}
expected_values = {
Expand All @@ -117,7 +117,7 @@ def test_list_session_property_values(self):
assert actual_values == expected_values

def test_search_session_property_values(self):
response = self.client.get(f"/api/projects/{self.team.pk}/sessions/values/?key=$session_utm_source&value=tub")
response = self.client.get(f"/api/projects/{self.team.pk}/sessions/values/?key=$entry_utm_source&value=tub")
self.assertEqual(response.status_code, status.HTTP_200_OK)
actual_values = {entry["name"] for entry in response.json()}
expected_values = {
Expand All @@ -127,14 +127,14 @@ def test_search_session_property_values(self):

def test_search_session_property_no_matching_values(self):
response = self.client.get(
f"/api/projects/{self.team.pk}/sessions/values/?key=$session_utm_source&value=doesnotexist"
f"/api/projects/{self.team.pk}/sessions/values/?key=$entry_utm_source&value=doesnotexist"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
assert len(response.json()) == 0

def test_search_missing_session_property_values(self):
response = self.client.get(
f"/api/projects/{self.team.pk}/sessions/values/?key=$session_utm_source&value=doesnotexist"
f"/api/projects/{self.team.pk}/sessions/values/?key=$entry_utm_source&value=doesnotexist"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
assert len(response.json()) == 0
96 changes: 47 additions & 49 deletions posthog/hogql/database/schema/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@
"$start_timestamp": DateTimeDatabaseField(name="$start_timestamp"),
"$end_timestamp": DateTimeDatabaseField(name="$end_timestamp"),
"$urls": StringArrayDatabaseField(name="$urls"),
"$entry_url": StringDatabaseField(name="$entry_url"),
"entry_current_url": StringDatabaseField(name="entry_current_url"),
"$entry_pathname": StringDatabaseField(name="$entry_pathname"),
"$exit_url": StringDatabaseField(name="$exit_url"),
"$exit_current_url": StringDatabaseField(name="$exit_current_url"),
"$exit_pathname": StringDatabaseField(name="$exit_pathname"),
"$session_utm_source": StringDatabaseField(name="$session_utm_source"),
"$session_utm_campaign": StringDatabaseField(name="$session_utm_campaign"),
"$session_utm_medium": StringDatabaseField(name="$session_utm_medium"),
"$session_utm_term": StringDatabaseField(name="$session_utm_term"),
"$session_utm_content": StringDatabaseField(name="$session_utm_content"),
"$session_referring_domain": StringDatabaseField(name="$session_referring_domain"),
"$session_gclid": StringDatabaseField(name="$session_gclid"),
"$session_gad_source": StringDatabaseField(name="$session_gad_source"),
"$entry_utm_source": StringDatabaseField(name="$entry_utm_source"),
"$entry_utm_campaign": StringDatabaseField(name="$entry_utm_campaign"),
"$entry_utm_medium": StringDatabaseField(name="$entry_utm_medium"),
"$entry_utm_term": StringDatabaseField(name="$entry_utm_term"),
"$entry_utm_content": StringDatabaseField(name="$entry_utm_content"),
"$entry_referring_domain": StringDatabaseField(name="$entry_referring_domain"),
"$entry_gclid": StringDatabaseField(name="$entry_gclid"),
"$entry_gad_source": StringDatabaseField(name="$entry_gad_source"),
"$event_count_map": DatabaseField(name="$event_count_map"),
"$pageview_count": IntegerDatabaseField(name="$pageview_count"),
"$autocapture_count": IntegerDatabaseField(name="$autocapture_count"),
Expand Down Expand Up @@ -134,31 +134,29 @@ def select_from_sessions_table(
)
],
),
"$entry_url": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "entry_url"])]),
"entry_current_url": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "entry_url"])]),
"$entry_pathname": ast.Call(
name="path", args=[ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "entry_url"])])]
),
"$exit_url": ast.Call(name="argMaxMerge", args=[ast.Field(chain=[table_name, "exit_url"])]),
"$exit_current_url": ast.Call(name="argMaxMerge", args=[ast.Field(chain=[table_name, "exit_url"])]),
"$exit_pathname": ast.Call(
name="path",
args=[
ast.Call(name="argMaxMerge", args=[ast.Field(chain=[table_name, "exit_url"])]),
],
),
"$session_utm_source": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_source"])]),
"$session_utm_campaign": ast.Call(
"$entry_utm_source": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_source"])]),
"$entry_utm_campaign": ast.Call(
name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_campaign"])]
),
"$session_utm_medium": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_medium"])]),
"$session_utm_term": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_term"])]),
"$session_utm_content": ast.Call(
name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_content"])]
),
"$session_referring_domain": ast.Call(
"$entry_utm_medium": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_medium"])]),
"$entry_utm_term": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_term"])]),
"$entry_utm_content": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_utm_content"])]),
"$entry_referring_domain": ast.Call(
name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_referring_domain"])]
),
"$session_gclid": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_gclid"])]),
"$session_gad_source": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_gad_source"])]),
"$entry_gclid": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_gclid"])]),
"$entry_gad_source": ast.Call(name="argMinMerge", args=[ast.Field(chain=[table_name, "initial_gad_source"])]),
"$event_count_map": ast.Call(
name="sumMap",
args=[ast.Field(chain=[table_name, "event_count_map"])],
Expand Down Expand Up @@ -203,12 +201,12 @@ def select_from_sessions_table(
],
)
aggregate_fields["$channel_type"] = create_channel_type_expr(
campaign=aggregate_fields["$session_utm_campaign"],
medium=aggregate_fields["$session_utm_medium"],
source=aggregate_fields["$session_utm_source"],
referring_domain=aggregate_fields["$session_referring_domain"],
gclid=aggregate_fields["$session_gclid"],
gad_source=aggregate_fields["$session_gad_source"],
campaign=aggregate_fields["$entry_utm_campaign"],
medium=aggregate_fields["$entry_utm_medium"],
source=aggregate_fields["$entry_utm_source"],
referring_domain=aggregate_fields["$entry_referring_domain"],
gclid=aggregate_fields["$entry_gclid"],
gad_source=aggregate_fields["$entry_gad_source"],
)

select_fields: list[ast.Expr] = []
Expand Down Expand Up @@ -309,27 +307,27 @@ def get_property_type(field_name: str, field_definition: FieldOrTable):


SESSION_PROPERTY_TO_RAW_SESSIONS_EXPR_MAP = {
"$session_referring_domain": "finalizeAggregation(initial_referring_domain)",
"$session_utm_source": "finalizeAggregation(initial_utm_source)",
"$session_utm_campaign": "finalizeAggregation(initial_utm_campaign)",
"$session_utm_medium": "finalizeAggregation(initial_utm_medium)",
"$session_utm_term": "finalizeAggregation(initial_utm_term)",
"$session_utm_content": "finalizeAggregation(initial_utm_content)",
"$session_gclid": "finalizeAggregation(initial_gclid)",
"$session_gad_source": "finalizeAggregation(initial_gad_source)",
"$session_gclsrc": "finalizeAggregation(initial_gclsrc)",
"$session_dclid": "finalizeAggregation(initial_dclid)",
"$session_gbraid": "finalizeAggregation(initial_gbraid)",
"$session_wbraid": "finalizeAggregation(initial_wbraid)",
"$session_fbclid": "finalizeAggregation(initial_fbclid)",
"$session_msclkid": "finalizeAggregation(initial_msclkid)",
"$session_twclid": "finalizeAggregation(initial_twclid)",
"$session_li_fat_id": "finalizeAggregation(initial_li_fat_id)",
"$session_mc_cid": "finalizeAggregation(initial_mc_cid)",
"$session_igshid": "finalizeAggregation(initial_igshid)",
"$session_ttclid": "finalizeAggregation(initial_ttclid)",
"$entry_url": "finalizeAggregation(entry_url)",
"$exit_url": "finalizeAggregation(exit_url)",
"$entry_referring_domain": "finalizeAggregation(initial_referring_domain)",
"$entry_utm_source": "finalizeAggregation(initial_utm_source)",
"$entry_utm_campaign": "finalizeAggregation(initial_utm_campaign)",
"$entry_utm_medium": "finalizeAggregation(initial_utm_medium)",
"$entry_utm_term": "finalizeAggregation(initial_utm_term)",
"$entry_utm_content": "finalizeAggregation(initial_utm_content)",
"$entry_gclid": "finalizeAggregation(initial_gclid)",
"$entry_gad_source": "finalizeAggregation(initial_gad_source)",
"$entry_gclsrc": "finalizeAggregation(initial_gclsrc)",
"$entry_dclid": "finalizeAggregation(initial_dclid)",
"$entry_gbraid": "finalizeAggregation(initial_gbraid)",
"$entry_wbraid": "finalizeAggregation(initial_wbraid)",
"$entry_fbclid": "finalizeAggregation(initial_fbclid)",
"$entry_msclkid": "finalizeAggregation(initial_msclkid)",
"$entry_twclid": "finalizeAggregation(initial_twclid)",
"$entry_li_fat_id": "finalizeAggregation(initial_li_fat_id)",
"$entry_mc_cid": "finalizeAggregation(initial_mc_cid)",
"$entry_igshid": "finalizeAggregation(initial_igshid)",
"$entry_ttclid": "finalizeAggregation(initial_ttclid)",
"entry_current_url": "finalizeAggregation(entry_url)",
"$exit_current_url": "finalizeAggregation(exit_url)",
}


Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/database/schema/test/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_persons_and_sessions_on_events(self):

response = execute_hogql_query(
parse_select(
"select events.person_id, session.$session_utm_source from events where $session_id = {session_id} or $session_id = {session_id2} order by 2 asc",
"select events.person_id, session.$entry_utm_source from events where $session_id = {session_id} or $session_id = {session_id2} order by 2 asc",
placeholders={"session_id": ast.Constant(value=s1), "session_id2": ast.Constant(value=s2)},
),
self.team,
Expand Down
Loading

0 comments on commit 32de2ec

Please sign in to comment.