Skip to content

Commit 97913a4

Browse files
committed
Edits to adding event types and cloudflare workers docs
1 parent 328b485 commit 97913a4

File tree

1 file changed

+28
-67
lines changed

1 file changed

+28
-67
lines changed

README.md

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,13 @@ Finally, add the tracking script to your base HTML template:
9090

9191
## Goal events
9292

93-
Each A/B test has a goal that is measured after a user visits the page that the A/B test is running on.
93+
## Implementing custom goal event types
9494

95-
The goal is defined by a destination page and and event type. For example, if the A/B test needs to measure how a change on the page affects the number of users who go on to submit a "Contact us" form, then the 'destination page' would be the "Contact us" page and the 'event type' would be "Submit form".
96-
97-
Out of the box, the only 'event type' that Wagtail A/B testing supports is visiting the destination page.
98-
If you need to measure something else (such as submitting a form, purchasing something, or just clicking a link), you can implement a custom 'event type'.
99-
100-
### Implementing a custom goal event type
101-
102-
Custom event types are implemented for specific types of destination page.
103-
104-
Firstly, you need to register the 'event type' using the `register_ab_testing_event_types` hook,
105-
this displays the goal 'event type' in the list of options when an A/B test is being created:
95+
Out of the box, Wagtail A/B testing provides a "Visit page" goal event type which you can use to track when users visit a goal page.
96+
It also supports custom goal types, which can be used for tracking other events such as making a purchase, submitting a form, or clicking a link.
10697

98+
To implement a custom goal event type, firstly register your type using the ``register_ab_testing_event_types`` hook, this would
99+
add your goal type to the list of options shown to users when they create A/B tests:
107100

108101
```python
109102
# myapp/wagtail_hooks.py
@@ -132,31 +125,20 @@ def register_submit_form_event_type():
132125

133126
```
134127

135-
Next you need to add logic in that logs a conversion when the user reaches that goal.
136-
To do this, you can copy/adapt the following code snippet:
128+
Next, you need to tell Wagtail A/B testing whenever a user triggers the goal. This can be done by calling ``wagtailAbTesting.triggerEvent()``
129+
in the browser:
137130

138-
```python
139-
# Check if the user is trackable
140-
if request_is_trackable(request):
141-
# Check if the page is the goal of any running tests
142-
tests = AbTest.objects.filter(goal_event='slug-of-the-event-type', goal_page=the_page, status=AbTest.STATUS_RUNNING)
143-
for test in tests:
144-
# Is the user a participant in this test?
145-
if f'wagtail-ab-testing_{test.id}_version' not in request.session:
146-
continue
147-
148-
# Has the user already completed the test?
149-
if f'wagtail-ab-testing_{test.id}_completed' in request.session:
150-
continue
151-
152-
# Log a conversion
153-
test.log_conversion(request.session[f'wagtail-ab-testing_{test.id}_version'])
154-
request.session[f'wagtail-ab-testing_{test.id}_completed'] = 'yes'
131+
```javascript
132+
if (window.wagtailAbTesting) {
133+
wagtailAbTesting.triggerEvent('slug-of-the-event-type');
134+
}
155135
```
156136

137+
The JavaScript library tracks A/B tests using ``localStorage``, so this will only call the server if the user is participating in an A/B test with the provided goal type and the current page is the goal page.
138+
157139
#### Example: Adding a "Submit form" event type
158140

159-
In this example, we will add a "Submit form" event type for a ``ContactUsFormPage`` page type.
141+
We will add a "Submit form" event type for a ``ContactUsFormPage`` page type in this example.
160142

161143
Firstly, we need to register the event type. To do this, implement a handler for the ``register_ab_testing_event_types`` hook in your app:
162144

@@ -187,43 +169,22 @@ def register_submit_form_event_type():
187169
}
188170
```
189171

190-
This allows users to select the "Submit form page" event type when their goal page is set to any instance of ``ContactUsFormPage``.
191-
192-
Next, we need to add some code to record conversions for this event type.
193-
To do this, we will customise the ``.render_landing_page()`` method that is inherited from the ``AbstractForm`` model.
194-
This method is a view that returns the "thank you" page to the user. It's ideal for this use because user's will can
195-
only get there by submitting the form, and we have the ``request`` object available which is required for some of the logic.:
196-
197-
```python
198-
# myapp/models.py
199-
200-
from wagtail.contrib.forms.models import AbstractFormPage
201-
202-
from wagtail_ab_testing.models import AbTest
203-
from wagtail_ab_testing.utils import request_is_trackable
204-
205-
206-
class ContactUsFormPage(AbstractForm):
172+
Next, we need to add some code to the frontend to trigger this event whenever a user submits the form:
207173

208-
def render_landing_page(self, request, *args, **kwargs):
209-
# Check if the user is trackable
210-
if request_is_trackable(request):
211-
# Check if submitting this form is the goal of any running tests
212-
tests = AbTest.objects.filter(goal_event='submit-contact-us-form', goal_page=self, status=AbTest.STATUS_RUNNING)
213-
for test in tests:
214-
# Is the user a participant in this test?
215-
if f'wagtail-ab-testing_{test.id}_version' not in request.session:
216-
continue
217-
218-
# Has the user already completed the test?
219-
if f'wagtail-ab-testing_{test.id}_completed' in request.session:
220-
continue
174+
```django+HTML
175+
# templates/forms/contact_us_form_page.html
221176
222-
# Log a conversion
223-
test.log_conversion(request.session[f'wagtail-ab-testing_{test.id}_version'])
224-
request.session[f'wagtail-ab-testing_{test.id}_completed'] = 'yes'
177+
<form id="form">
178+
...
179+
</form>
225180
226-
return super().render_landing_page(request, *args, **kwargs)
181+
<script>
182+
if (window.wagtailAbTesting) {
183+
document.getElementById('form').addEventListener('submit', function() {
184+
wagtailAbTesting.triggerEvent('submit-contact-us-form');
185+
});
186+
}
187+
</script>
227188
```
228189

229190
## Running A/B tests on a site that uses Cloudflare caching
@@ -305,6 +266,6 @@ addEventListener('fetch', event => {
305266
});
306267
```
307268

308-
Add a variable to the worker called ``WAGTAIL_AB_TESTING_WORKER_TOKEN`` giving it the same token value that you generated earlier.
269+
Add a variable to the worker called ``WAGTAIL_AB_TESTING_WORKER_TOKEN``, giving it the same token value that you generated earlier.
309270

310271
Finally, add a route into Cloudflare so that it routes all traffic through this worker.

0 commit comments

Comments
 (0)