Skip to content

Commit

Permalink
Handle optional values in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonatan-lopes committed Feb 19, 2024
1 parent 5c676ce commit 528b3d9
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions network-api/networkapi/templates/wagtail_ab_testing/script.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,40 @@

<script nonce="{{ request.csp_nonce }}">
// Read values from the DOM
const pageId = JSON.parse(document.getElementById('abtesting-page-id').textContent);
const testId = JSON.parse(document.getElementById('abtesting-test-id').textContent);
const version = JSON.parse(document.getElementById('abtesting-version').textContent);
const goalEvent = JSON.parse(document.getElementById('abtesting-goal-event').textContent);
const goalPageId = JSON.parse(document.getElementById('abtesting-goal-page-id').textContent);
const registerParticipantUrl = JSON.parse(document.getElementById('abtesting-register-participant-url').textContent);
const goalReachedUrl = JSON.parse(document.getElementById('abtesting-goal-reached-url').textContent);

window.wagtailAbTesting = {
pageId: pageId,
testId: testId,
version: version,
goalEvent: goalEvent,
goalPageId: goalPageId,

urls :{
registerParticipant: registerParticipantUrl,
goalReached: goalReachedUrl
}
};

if (document.getElementById('abtesting-page-id')) {
const pageId = JSON.parse(document.getElementById('abtesting-page-id').textContent);
window.wagtailAbTesting.pageId = pageId;
}

let testId = null;
let version = null;

if (document.getElementById('abtesting-test-id')) {
testId = JSON.parse(document.getElementById('abtesting-test-id').textContent);
}

if (document.getElementById('abtesting-version')) {
version = JSON.parse(document.getElementById('abtesting-version').textContent);
}

if (testId && version) {
const goalEvent = JSON.parse(document.getElementById('abtesting-goal-event').textContent);
const goalPageId = JSON.parse(document.getElementById('abtesting-goal-page-id').textContent);
window.wagtailAbTesting.testId = testId;
window.wagtailAbTesting.version = version;
window.wagtailAbTesting.goalEvent = goalEvent;
window.wagtailAbTesting.goalPageId = goalPageId;
}
</script>

<script nonce="{{ request.csp_nonce }}" src="{% static 'wagtail_ab_testing/js/tracker.js' %}" defer async></script>
Expand Down

0 comments on commit 528b3d9

Please sign in to comment.