Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add utm_source to the matrix #18424

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions posthog/demo/matrix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"][utm_key] = utm_value
if properties:
if referrer := properties.get("$referrer"):
referring_domain = urlparse(referrer).netloc if referrer != "$direct" else referrer
Expand Down
30 changes: 21 additions & 9 deletions posthog/demo/products/hedgebox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -342,14 +342,15 @@ 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:
self.go_to_files()
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:
Expand All @@ -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 (
Expand All @@ -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)
Expand Down Expand Up @@ -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)
)
Loading