Skip to content

Commit

Permalink
Feat: add properties for UTM codes (#2056)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalver1 committed May 1, 2024
2 parents 6820fca + 7a58def commit b2c5642
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
12 changes: 12 additions & 0 deletions benefits/core/analytics.py
Expand Up @@ -92,6 +92,18 @@ class ViewedPageEvent(Event):

def __init__(self, request):
super().__init__(request, "viewed page")
# Add UTM codes
utm_campaign = request.GET.get("utm_campaign")
utm_source = request.GET.get("utm_source")
utm_medium = request.GET.get("utm_medium")
utm_content = request.GET.get("utm_content")
utm_id = request.GET.get("utm_id")
self.update_event_properties(
utm_campaign=utm_campaign, utm_source=utm_source, utm_medium=utm_medium, utm_content=utm_content, utm_id=utm_id
)
self.update_user_properties(
utm_campaign=utm_campaign, utm_source=utm_source, utm_medium=utm_medium, utm_content=utm_content, utm_id=utm_id
)


class ChangedLanguageEvent(Event):
Expand Down
54 changes: 53 additions & 1 deletion tests/pytest/core/test_analytics.py
@@ -1,7 +1,41 @@
import pytest

import benefits.core.analytics
from benefits.core.analytics import Event
from benefits.core.analytics import Event, ViewedPageEvent
from django.contrib.sessions.middleware import SessionMiddleware
from django.middleware.locale import LocaleMiddleware


@pytest.fixture
def app_request_utm(rf):
"""
Fixture creates and initializes a new Django request object similar to a real application request with UTM codes.
"""
# create a request for the path with UTM codes, initialize
app_request = rf.get(
"/some/arbitrary/path?utm_campaign=campaign&utm_source=source&utm_medium=medium&utm_content=content&utm_id=id"
)

# https://stackoverflow.com/a/55530933/358804
middleware = [SessionMiddleware(lambda x: x), LocaleMiddleware(lambda x: x)]
for m in middleware:
m.process_request(app_request)

app_request.session.save()
benefits.core.analytics.session.reset(app_request)

return app_request


@pytest.fixture
def utm_code_data():
return {
"utm_campaign": "campaign",
"utm_source": "source",
"utm_medium": "medium",
"utm_content": "content",
"utm_id": "id",
}


@pytest.mark.django_db
Expand Down Expand Up @@ -80,3 +114,21 @@ def test_Event_update_user_properties(app_request):

assert key in event.user_properties
assert event.user_properties[key] == value


@pytest.mark.django_db
def test_ViewedPageEvent_with_UTM(app_request_utm, utm_code_data):
event = ViewedPageEvent(app_request_utm)

for key, value in utm_code_data.items():
assert event.event_properties[key] == value
assert event.user_properties[key] == value


@pytest.mark.django_db
def test_ViewedPageEvent_without_UTM(app_request, utm_code_data):
event = ViewedPageEvent(app_request)

for key in utm_code_data:
assert event.event_properties[key] is None
assert event.user_properties[key] is None

0 comments on commit b2c5642

Please sign in to comment.