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

Set cookie path to URL of homepage #67

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions wagtail_ab_testing/static/wagtail_ab_testing/js/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@
}

// Add this goal page/event into the goals data structure
goals[window.wagtailAbTesting.goalPageId] = goals[window.wagtailAbTesting.goalPageId] || {}
goals[window.wagtailAbTesting.goalPageId] = goals[window.wagtailAbTesting.goalPageId] || {};
goals[window.wagtailAbTesting.goalPageId][window.wagtailAbTesting.goalEvent] = goals[window.wagtailAbTesting.goalPageId][window.wagtailAbTesting.goalEvent] || [];

// Check if this user is already a participant in this test
// We could check the cookie instead, but it's possible that the user has cleared their cookies but not local storage
if (goals[window.wagtailAbTesting.goalPageId][window.wagtailAbTesting.goalEvent].indexOf(window.wagtailAbTesting.testId) === -1) {
var cookieName = 'wagtail-ab-testing_' + window.wagtailAbTesting.testId + '_version';
var cookiePath = window.wagtailAbTesting.cookiePath;
if (!document.cookie.includes(cookieName)) {
fetch(
window.wagtailAbTesting.urls.registerParticipant, {
Expand All @@ -64,7 +65,7 @@
// Put the version into a cookie so that Wagtail continues to serve this version
var expires = new Date();
expires.setFullYear(expires.getFullYear() + 1);
document.cookie = cookieName + '=' + window.wagtailAbTesting.version + '; expires=' + expires.toUTCString();
document.cookie = cookieName + '=' + window.wagtailAbTesting.version + '; path=' + cookiePath + '; expires=' + expires.toUTCString();

// Store the test ID against the goal event in the goals data structure
// We will use this for knowing when to call the goal reached API later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
version: '{{ version|escapejs }}',
goalEvent: '{{ test.goal_event|escapejs }}',
goalPageId: {% if test.goal_page %}{{ test.goal_page.id }}{% else %}null{% endif %},
cookiePath: '{{ cookie_path|escapejs }}',
{% endif %}

urls: {
Expand Down
3 changes: 3 additions & 0 deletions wagtail_ab_testing/templatetags/wagtail_ab_testing_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import template
from wagtail.models.sites import Site

from wagtail_ab_testing.models import AbTest
from wagtail_ab_testing.utils import request_is_trackable
Expand All @@ -9,11 +10,13 @@
@register.inclusion_tag('wagtail_ab_testing/script.html', takes_context=True)
def wagtail_ab_testing_script(context):
request = context['request']
site = Site.find_for_request(request)

Check warning on line 13 in wagtail_ab_testing/templatetags/wagtail_ab_testing_tags.py

View check run for this annotation

Codecov / codecov/patch

wagtail_ab_testing/templatetags/wagtail_ab_testing_tags.py#L13

Added line #L13 was not covered by tests
serving_variant = getattr(request, 'wagtail_ab_testing_serving_variant', False)

return {
'track': request_is_trackable(request),
'page': context.get('page', None),
'test': getattr(request, 'wagtail_ab_testing_test', None),
'version': AbTest.VERSION_VARIANT if serving_variant else AbTest.VERSION_CONTROL,
'cookie_path': site.root_page.url,
}