From c2e306cd318fa772c35774780814700a412d2099 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Mon, 6 Nov 2023 16:07:24 +0000 Subject: [PATCH 1/2] Add utm_source to the matrix --- posthog/demo/matrix/models.py | 15 +++++++++--- posthog/demo/products/hedgebox/models.py | 30 +++++++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/posthog/demo/matrix/models.py b/posthog/demo/matrix/models.py index a2e7796518914..235fd2de23f8a 100644 --- a/posthog/demo/matrix/models.py +++ b/posthog/demo/matrix/models.py @@ -19,7 +19,7 @@ Set, TypeVar, ) -from urllib.parse import urlparse +from urllib.parse import urlparse, parse_qs from uuid import UUID if TYPE_CHECKING: @@ -55,6 +55,9 @@ class Target(Enum): PROPERTY_GEOIP_COUNTRY_CODE = "$geoip_country_code" +UTM_QUERY_PROPERTIES = {"utm_source", "utm_campaign", "utm_medium", "utm_term", "utm_content"} + + # Properties who get `$set_once` implicitly as `$initial_foo` - source of truth in plugin-server/src/utils/db/utils.ts PROPERTIES_WITH_IMPLICIT_INITIAL_VALUE_TRACKING = { "utm_source", @@ -215,15 +218,21 @@ def capture(self, event: str, properties: Optional[Properties] = None): "$session_id": self.active_session_id, "$device_id": self.device_id, } + if "$set" not in combined_properties: + combined_properties["$set"] = {} if self.super_properties: combined_properties.update(self.super_properties) if self.current_url is not None: parsed_current_url = urlparse(self.current_url) + parsed_current_url_query = parse_qs(parsed_current_url.query) combined_properties["$current_url"] = self.current_url combined_properties["$host"] = parsed_current_url.netloc combined_properties["$pathname"] = parsed_current_url.path - if "$set" not in combined_properties: - combined_properties["$set"] = {} + for utm_key in UTM_QUERY_PROPERTIES: + if utm_key in parsed_current_url_query: + utm_value = parsed_current_url_query[utm_key][0] + combined_properties[utm_key] = utm_value + combined_properties["$set"].update({utm_key: utm_value}) if properties: if referrer := properties.get("$referrer"): referring_domain = urlparse(referrer).netloc if referrer != "$direct" else referrer diff --git a/posthog/demo/products/hedgebox/models.py b/posthog/demo/products/hedgebox/models.py index 1c0a0e4ffd0da..add6fb8c73ce8 100644 --- a/posthog/demo/products/hedgebox/models.py +++ b/posthog/demo/products/hedgebox/models.py @@ -12,12 +12,12 @@ Tuple, cast, ) +from urllib.parse import urlencode, urlparse, urlunparse +from zoneinfo import ZoneInfo import pytz -from zoneinfo import ZoneInfo from posthog.demo.matrix.models import Effect, SimPerson, SimSessionIntent - from .taxonomy import * if TYPE_CHECKING: @@ -323,13 +323,13 @@ def simulate_session(self): if self.active_session_intent == HedgeboxSessionIntent.CONSIDER_PRODUCT: entered_url_directly = self.cluster.random.random() < 0.18 self.active_client.register({"$referrer": "$direct" if entered_url_directly else "https://www.google.com/"}) - self.go_to_home() + self.go_to_home(None if entered_url_directly else {"utm_source": "google"}) elif self.active_session_intent == HedgeboxSessionIntent.CHECK_MARIUS_TECH_TIPS_LINK: entered_url_directly = self.cluster.random.random() < 0.62 self.active_client.register( {"$referrer": "$direct" if entered_url_directly else "https://www.youtube.com/"} ) - self.go_to_marius_tech_tips() + self.go_to_marius_tech_tips(None if entered_url_directly else {"utm_source": "youtube"}) elif self.active_session_intent in ( HedgeboxSessionIntent.UPLOAD_FILE_S, HedgeboxSessionIntent.DELETE_FILE_S, @@ -342,6 +342,7 @@ def simulate_session(self): ): entered_url_directly = self.cluster.random.random() < 0.71 self.active_client.register({"$referrer": "$direct" if entered_url_directly else "https://www.google.com/"}) + if entered_url_directly: used_files_page_url = self.cluster.random.random() < 0.48 if used_files_page_url: @@ -349,7 +350,7 @@ def simulate_session(self): else: self.go_to_home() else: - self.go_to_home() + self.go_to_home(None if entered_url_directly else {"utm_source": "google"}) elif self.active_session_intent == HedgeboxSessionIntent.VIEW_SHARED_FILE: self.active_client.register({"$referrer": "$direct"}) if not self.file_to_view: @@ -367,8 +368,8 @@ def simulate_session(self): # Path directions - def go_to_home(self): - self.active_client.capture_pageview(URL_HOME) + def go_to_home(self, query_params=None): + self.active_client.capture_pageview(add_params_to_url(URL_HOME, query_params)) self.advance_timer(1.8 + self.cluster.random.betavariate(1.5, 3) * 300) # Viewing the page self.satisfaction += (self.cluster.random.betavariate(1.6, 1.2) - 0.5) * 0.1 # It's a somewhat nice page if self.active_session_intent in ( @@ -393,8 +394,8 @@ def go_to_home(self): elif self.need > 0.5 and self.satisfaction >= 0.8 and self.cluster.random.random() < 0.6: self.go_to_sign_up() - def go_to_marius_tech_tips(self): - self.active_client.capture_pageview(URL_MARIUS_TECH_TIPS) + def go_to_marius_tech_tips(self, query_params=None): + self.active_client.capture_pageview(add_params_to_url(URL_MARIUS_TECH_TIPS, query_params)) self.advance_timer(1.2 + self.cluster.random.betavariate(1.5, 2) * 150) # Viewing the page self.satisfaction += (self.cluster.random.betavariate(1.6, 1.2) - 0.5) * 0.4 # The user may be in target or not self.need += self.cluster.random.uniform(-0.05, 0.15) @@ -783,3 +784,14 @@ def invitable_neighbors(self) -> List["HedgeboxPerson"]: for neighbor in cast(List[HedgeboxPerson], self.cluster.list_neighbors(self)) if neighbor.is_invitable ] + + +def add_params_to_url(url, query_params): + if not query_params: + return url + parsed_url = urlparse(url) + encoded_query = urlencode(query_params) + new_query = f"{parsed_url.query}&{encoded_query}" if parsed_url.query else encoded_query + return urlunparse( + (parsed_url.scheme, parsed_url.netloc, parsed_url.path, parsed_url.params, new_query, parsed_url.fragment) + ) From 0360e5f52617bf1c90dbf76e448df1dbe594138e Mon Sep 17 00:00:00 2001 From: Robbie Date: Mon, 6 Nov 2023 23:07:11 +0000 Subject: [PATCH 2/2] Update posthog/demo/matrix/models.py Co-authored-by: Michael Matloka --- posthog/demo/matrix/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/demo/matrix/models.py b/posthog/demo/matrix/models.py index 235fd2de23f8a..e1698d7dd7b3b 100644 --- a/posthog/demo/matrix/models.py +++ b/posthog/demo/matrix/models.py @@ -232,7 +232,7 @@ def capture(self, event: str, properties: Optional[Properties] = None): if utm_key in parsed_current_url_query: utm_value = parsed_current_url_query[utm_key][0] combined_properties[utm_key] = utm_value - combined_properties["$set"].update({utm_key: utm_value}) + combined_properties["$set"][utm_key] = utm_value if properties: if referrer := properties.get("$referrer"): referring_domain = urlparse(referrer).netloc if referrer != "$direct" else referrer