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

Feat: add properties for UTM codes #2056

Merged
merged 2 commits into from May 1, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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