-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move in-page script to
tracker.js
(#73)
* Remove inline script * Lint * Add tests
- Loading branch information
1 parent
29a807f
commit e42afe9
Showing
4 changed files
with
227 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 1 addition & 25 deletions
26
wagtail_ab_testing/templates/wagtail_ab_testing/script.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,6 @@ | ||
{% load static %} | ||
|
||
{% if track %} | ||
<script> | ||
window.wagtailAbTesting = { | ||
{# Is this a Wagtail Page? #} | ||
{# This value is used to check if a goal has been reached #} | ||
{% if page %} | ||
pageId: {{ page.id }}, | ||
{% endif %} | ||
|
||
{# Is there a test running on the current page? #} | ||
{% if test and version %} | ||
testId: {{ test.id }}, | ||
version: '{{ version|escapejs }}', | ||
goalEvent: '{{ test.goal_event|escapejs }}', | ||
goalPageId: {% if test.goal_page %}{{ test.goal_page.id }}{% else %}null{% endif %}, | ||
{% endif %} | ||
|
||
urls: { | ||
{% url 'wagtail_ab_testing:register_participant' as register_participant_url %} | ||
registerParticipant: '{{ register_participant_url|escapejs }}', | ||
{% url 'wagtail_ab_testing:goal_reached' as goal_reached_url %} | ||
goalReached: '{{ goal_reached_url|escapejs }}', | ||
} | ||
}; | ||
</script> | ||
|
||
{{ tracking_parameters|json_script:"abtesting-tracking-params" }} | ||
<script src="{% static 'wagtail_ab_testing/js/tracker.js' %}" defer async></script> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
from unittest.mock import patch | ||
|
||
from django.urls import reverse | ||
from django.test import RequestFactory, TestCase | ||
|
||
from wagtail.models import Page | ||
|
||
from wagtail_ab_testing.models import AbTest | ||
from wagtail_ab_testing.templatetags.wagtail_ab_testing_tags import ( | ||
wagtail_ab_testing_script, | ||
) | ||
|
||
|
||
@patch( | ||
"wagtail_ab_testing.templatetags.wagtail_ab_testing_tags.request_is_trackable", | ||
return_value=True, | ||
) | ||
class TestWagtailAbTestingScriptTemplateTag(TestCase): | ||
def setUp(self): | ||
# Create test page with a draft revision | ||
self.page = Page.objects.get(id=2).add_child( | ||
instance=Page(title="Test", slug="test") | ||
) | ||
self.page.title = "Changed title" | ||
self.page.save_revision() | ||
|
||
# Create an A/B test | ||
self.ab_test = AbTest.objects.create( | ||
page=self.page, | ||
name="Test", | ||
variant_revision=self.page.get_latest_revision(), | ||
status=AbTest.STATUS_RUNNING, | ||
goal_page_id=2, | ||
goal_event="visit-page", | ||
sample_size=100, | ||
) | ||
|
||
# Create a request factory: | ||
self.factory = RequestFactory() | ||
|
||
def test_ab_testing_script_tag_test_with_trackable_urls( | ||
self, mock_request_is_callable | ||
): | ||
"""Test the tag for a page that is part of an A/B test serving a control version.""" | ||
# A/B test page url: | ||
url = (reverse("wagtail_ab_testing:goal_reached", args=[]),) | ||
# Create a request | ||
request = self.factory.post( | ||
url, | ||
{ | ||
"test_id": self.ab_test.id, | ||
"version": "control", | ||
}, | ||
) | ||
|
||
request.wagtail_ab_testing_test = self.ab_test | ||
request.wagtail_ab_testing_serving_variant = False | ||
|
||
context = { | ||
"request": request, | ||
"page": self.page, | ||
} | ||
|
||
result = wagtail_ab_testing_script(context) | ||
|
||
expected_tracking_parameters = { | ||
"urls": { | ||
"registerParticipant": reverse( | ||
"wagtail_ab_testing:register_participant" | ||
), | ||
"goalReached": reverse("wagtail_ab_testing:goal_reached"), | ||
}, | ||
"pageId": self.page.id, | ||
"testId": self.ab_test.id, | ||
"version": AbTest.VERSION_CONTROL, | ||
"goalEvent": self.ab_test.goal_event, | ||
"goalPageId": self.ab_test.goal_page.id, | ||
} | ||
|
||
self.assertEqual(result["track"], True) | ||
self.assertDictEqual( | ||
result["tracking_parameters"], expected_tracking_parameters | ||
) | ||
|
||
def test_ab_testing_script_tag_test_serving_variant(self, mock_request_is_callable): | ||
"""Test the tag for a page serving a variant in an A/B test.""" | ||
# A/B test page url: | ||
url = (reverse("wagtail_ab_testing:goal_reached", args=[]),) | ||
# Create a request | ||
request = self.factory.post( | ||
url, | ||
{ | ||
"test_id": self.ab_test.id, | ||
"version": "variant", | ||
}, | ||
) | ||
|
||
request.wagtail_ab_testing_test = self.ab_test | ||
request.wagtail_ab_testing_serving_variant = True | ||
|
||
context = { | ||
"request": request, | ||
"page": self.page, | ||
} | ||
|
||
result = wagtail_ab_testing_script(context) | ||
|
||
expected_tracking_parameters = { | ||
"urls": { | ||
"registerParticipant": reverse( | ||
"wagtail_ab_testing:register_participant" | ||
), | ||
"goalReached": reverse("wagtail_ab_testing:goal_reached"), | ||
}, | ||
"pageId": self.page.id, | ||
"testId": self.ab_test.id, | ||
"version": AbTest.VERSION_VARIANT, | ||
"goalEvent": self.ab_test.goal_event, | ||
"goalPageId": self.ab_test.goal_page.id, | ||
} | ||
|
||
self.assertEqual(result["track"], True) | ||
self.assertDictEqual( | ||
result["tracking_parameters"], expected_tracking_parameters | ||
) | ||
|
||
def test_ab_testing_script_tag_without_test_in_request( | ||
self, mock_request_is_callable | ||
): | ||
"""Test the tag for a page that is not part of an A/B test.""" | ||
# Create a request for a page that is not part of an A/B test | ||
request = self.factory.get("/") | ||
|
||
context = { | ||
"request": request, | ||
"page": self.page, | ||
} | ||
|
||
result = wagtail_ab_testing_script(context) | ||
|
||
expected_tracking_parameters = { | ||
"urls": { | ||
"registerParticipant": reverse( | ||
"wagtail_ab_testing:register_participant" | ||
), | ||
"goalReached": reverse("wagtail_ab_testing:goal_reached"), | ||
}, | ||
"pageId": self.page.id, | ||
} | ||
|
||
# There is still a test running and this user is being tracked: | ||
self.assertEqual(result["track"], True) | ||
self.assertDictEqual(result["tracking_parameters"], expected_tracking_parameters) | ||
|
||
def test_ab_testing_script_tag_request_without_tracking( | ||
self, mock_request_is_callable | ||
): | ||
"""Test the tag for a request that should not be tracked.""" | ||
# A/B test page url: | ||
url = (reverse("wagtail_ab_testing:goal_reached", args=[]),) | ||
# Create a request | ||
request = self.factory.get( | ||
url, | ||
{ | ||
"test_id": self.ab_test.id, | ||
"version": "variant", | ||
}, | ||
) | ||
|
||
request.wagtail_ab_testing_test = self.ab_test | ||
request.wagtail_ab_testing_serving_variant = True | ||
|
||
context = { | ||
"request": request, | ||
"page": self.page, | ||
} | ||
|
||
# Mock a request from a bot or with a DNT | ||
mock_request_is_callable.return_value = False | ||
|
||
result = wagtail_ab_testing_script(context) | ||
|
||
self.assertEqual(result["track"], False) | ||
self.assertIsNone(result["tracking_parameters"]) |