Skip to content

Commit

Permalink
Lint tracker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Jul 20, 2021
1 parent 8097fa8 commit 9f8ca35
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions wagtail_ab_testing/static/wagtail_ab_testing/js/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

function getCookie(cookieName) {
var cookies = document.cookie.split(';');
for(var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(cookieName + '=') == 0) {
return cookie.substring(cookieName.length + 1, cookie.length);
}
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(cookieName + '=') == 0) {
return cookie.substring(cookieName.length + 1, cookie.length);
}
}
return '';
}
}

// Does the current page have an A/B test running?
if (window.wagtailAbTesting) {
Expand All @@ -29,7 +29,7 @@
// This data structure looks like:
// {
// <id of goal page> : {
// <goal event>: [<ids of tests with this goal page/event>]
// <goal event>: [<ids of tests with this goal page/eveqnt>]
// }
// }
var goals = window.localStorage.getItem('abtesting-goals');
Expand All @@ -49,8 +49,7 @@
var cookieName = 'wagtail-ab-testing_' + window.wagtailAbTesting.testId + '_version';
if (!document.cookie.includes(cookieName)) {
fetch(
window.wagtailAbTesting.urls.registerParticipant,
{
window.wagtailAbTesting.urls.registerParticipant, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand All @@ -60,7 +59,7 @@
version: window.wagtailAbTesting.version
})
}
).then(function (response) {
).then(function(response) {
if (response.status === 200) {
// Put the version into a cookie so that Wagtail continues to serve this version
var expires = new Date();
Expand All @@ -77,7 +76,7 @@
}
}

window.wagtailAbTesting.triggerEvent = function (event) {
window.wagtailAbTesting.triggerEvent = function(event) {
// Check if any goals were reached
var goalsJson = window.localStorage.getItem('abtesting-goals');
if (!goalsJson) {
Expand All @@ -86,7 +85,7 @@

var goals = JSON.parse(goalsJson);

var checkGoalReached = function (pageId) {
var checkGoalReached = function(pageId) {
var goalsForPage = goals[pageId];
if (!goalsForPage) {
return;
Expand All @@ -96,14 +95,13 @@
return;
}

goalsForEvent.forEach(function (testId) {
goalsForEvent.forEach(function(testId) {
var cookieName = 'wagtail-ab-testing_' + testId + '_version';
var version = getCookie(cookieName);

if (version) {
fetch(
window.wagtailAbTesting.urls.goalReached,
{
window.wagtailAbTesting.urls.goalReached, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down

0 comments on commit 9f8ca35

Please sign in to comment.