From a3797ce3cfdffde16409551392fdb94b6d649066 Mon Sep 17 00:00:00 2001 From: Robbie Date: Mon, 24 Jun 2024 15:37:17 +0100 Subject: [PATCH] fix(web-analytics): Make channel type comparison case-insensitive (#23182) * Make channel type comparison case-insensitive * Add comment --- posthog/hogql/database/schema/channel_type.py | 12 ++++++++--- .../database/schema/test/test_channel_type.py | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/posthog/hogql/database/schema/channel_type.py b/posthog/hogql/database/schema/channel_type.py index c45c71458d7d1..13c65c2045565 100644 --- a/posthog/hogql/database/schema/channel_type.py +++ b/posthog/hogql/database/schema/channel_type.py @@ -68,6 +68,12 @@ def wrap_with_null_if_empty(expr: ast.Expr) -> ast.Expr: args=[ast.Call(name="nullIf", args=[expr, ast.Constant(value="")]), ast.Constant(value="null")], ) + def wrap_with_lower(expr: ast.Expr) -> ast.Expr: + return ast.Call( + name="lower", + args=[expr], + ) + # This logic is referenced in our docs https://posthog.com/docs/data/channel-type, be sure to update both if you # update either. return parse_expr( @@ -131,9 +137,9 @@ def wrap_with_null_if_empty(expr: ast.Expr) -> ast.Expr: )""", start=None, placeholders={ - "campaign": wrap_with_null_if_empty(campaign), - "medium": wrap_with_null_if_empty(medium), - "source": wrap_with_null_if_empty(source), + "campaign": wrap_with_lower(wrap_with_null_if_empty(campaign)), + "medium": wrap_with_lower(wrap_with_null_if_empty(medium)), + "source": wrap_with_lower(wrap_with_null_if_empty(source)), "referring_domain": referring_domain, "gclid": wrap_with_null_if_empty(gclid), "gad_source": wrap_with_null_if_empty(gad_source), diff --git a/posthog/hogql/database/schema/test/test_channel_type.py b/posthog/hogql/database/schema/test/test_channel_type.py index 6f70dbd9620f4..712ed61404371 100644 --- a/posthog/hogql/database/schema/test/test_channel_type.py +++ b/posthog/hogql/database/schema/test/test_channel_type.py @@ -549,3 +549,23 @@ def test_duckduckgo_paid_click(self): # "https://l.facebook.com/", # ), # ) + + def test_zendesk_ticket_14945(self): + # see https://posthoghelp.zendesk.com/agent/tickets/14945 + + # In this ticket, a customer's paid social traffic was incorrect tagged as organic social, because we + # didn't recognise the word "Paid" with an uppercase 'P' as a paid source. Really, this should have + # been case-insensitive, and that is what the fix was. + self.assertEqual( + "Paid Social", + self._get_session_channel_type( + { + "utm_source": "Facebook", + "utm_medium": "Paid", + "utm_campaign": "Foo", + "referring_domain": "l.facebook.com", + "gclid": "", + "gad_source": "", + } + ), + )