Skip to content

Commit

Permalink
Another attempt at channel stats table
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Dec 17, 2023
1 parent 7ba143b commit dfd9aec
Showing 1 changed file with 76 additions and 55 deletions.
131 changes: 76 additions & 55 deletions posthog/hogql_queries/web_analytics/stats_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,70 +166,90 @@ def to_channel_query(self):
top_sources_query = parse_select(
"""
SELECT
multiIf(
match(person.properties.$initial_utm_campaign, 'cross-network'),
'Cross Network',
(
match(person.properties.$initial_utm_medium, '^(.*cp.*|ppc|retargeting|paid.*)$') OR
person.properties.$initial_gclid IS NOT NULL OR
person.properties.$initial_gad_source IS NOT NULL
),
coalesce(
hogql_lookupPaidSourceType(person.properties.$initial_utm_source),
hogql_lookupPaidDomainType(person.properties.$initial_referring_domain),
if(
match(person.properties.$initial_utm_campaign, '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Paid Shopping',
NULL
counts.breakdown_value as "context.columns.breakdown_value",
counts.total_pageviews as "context.columns.views",
counts.unique_visitors as "context.columns.visitors"
FROM
(SELECT
multiIf(
match(initial_utm_campaign, 'cross-network'),
'Cross Network',
(
match(initial_utm_medium, '^(.*cp.*|ppc|retargeting|paid.*)$') OR
initial_gclid IS NOT NULL OR
initial_gad_source IS NOT NULL
),
hogql_lookupPaidMediumType(person.properties.$initial_utm_medium),
multiIf (
person.properties.$initial_gad_source = '1',
'Paid Search',
coalesce(
hogql_lookupPaidSourceType(initial_utm_source),
hogql_lookupPaidDomainType(initial_referring_domain),
if(
match(initial_utm_campaign, '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Paid Shopping',
NULL
),
hogql_lookupPaidMediumType(initial_utm_medium),
multiIf (
initial_gad_source = '1',
'Paid Search',
match(person.properties.$initial_utm_campaign, '^(.*video.*)$'),
'Paid Video',
match(initial_utm_campaign, '^(.*video.*)$'),
'Paid Video',
'Paid Other'
)
),
(
person.properties.$initial_referring_domain = '$direct'
AND (person.properties.$initial_utm_medium IS NULL OR person.properties.$initial_utm_medium = '')
AND (person.properties.$initial_utm_source IS NULL OR person.properties.$initial_utm_source IN ('', '(direct)', 'direct'))
),
'Direct',
coalesce(
hogql_lookupOrganicSourceType(person.properties.$initial_utm_source),
hogql_lookupOrganicDomainType(person.properties.$initial_referring_domain),
if(
match(person.properties.$initial_utm_campaign, '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Organic Shopping',
NULL
'Paid Other'
)
),
hogql_lookupOrganicMediumType(person.properties.$initial_utm_medium),
multiIf(
match(person.properties.$initial_utm_campaign, '^(.*video.*)$'),
'Organic Video',
match(person.properties.$initial_utm_medium, 'push$'),
'Push',
(
initial_referring_domain = '$direct'
AND (initial_utm_medium IS NULL OR initial_utm_medium = '')
AND (initial_utm_source IS NULL OR initial_utm_source IN ('', '(direct)', 'direct'))
),
'Direct',
coalesce(
hogql_lookupOrganicSourceType(initial_utm_source),
hogql_lookupOrganicDomainType(initial_referring_domain),
if(
match(initial_utm_campaign, '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Organic Shopping',
NULL
),
hogql_lookupOrganicMediumType(initial_utm_medium),
multiIf(
match(initial_utm_campaign, '^(.*video.*)$'),
'Organic Video',
NULL
match(initial_utm_medium, 'push$'),
'Push',
NULL
)
)
) AS breakdown_value,
count() as total_pageviews,
uniq(pid) as unique_visitors
FROM
(SELECT
person.properties.$initial_utm_campaign AS initial_utm_campaign,
person.properties.$initial_utm_medium AS initial_utm_medium,
person.properties.$initial_utm_source AS initial_utm_source,
person.properties.$initial_referring_domain AS initial_referring_domain,
person.properties.$initial_gclid AS initial_gclid,
person.properties.$initial_gad_source AS initial_gad_source,
person_id AS pid
FROM events
WHERE
(event = '$pageview')
AND ({counts_where})
)
) AS "context.columns.breakdown_value",
count() as "context.columns.views",
uniq(events.person_id) as "context.columns.visitors"
FROM
events
GROUP BY breakdown_value
) AS counts
WHERE
(event = '$pageview')
AND ({counts_where})
GROUP BY "context.columns.breakdown_value"
{where_breakdown}
ORDER BY
"context.columns.views" DESC,
"context.columns.breakdown_value" DESC
Expand All @@ -239,6 +259,7 @@ def to_channel_query(self):
backend="cpp",
placeholders={
"counts_where": self.events_where(),
"where_breakdown": self.where_breakdown(),
},
)

Expand Down

0 comments on commit dfd9aec

Please sign in to comment.