Skip to content

Commit

Permalink
Merge pull request #3 from newfold-labs/add/ab-test-data
Browse files Browse the repository at this point in the history
Add `abTestPluginHome` capability to deactivation event
wpalani authored Nov 29, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents dd8bf18 + f19e8f0 commit 3cc97a3
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions static/js/deactivation-survey.js
Original file line number Diff line number Diff line change
@@ -109,15 +109,36 @@
});
}

const submitSurvey = async () => {
const submitSurvey = async (skipped = false) => {
isSubmitting();
const surveyInput = document.getElementById('nfd-deactivation-survey__input').value;

let surveyInput = 'No input';
if (!skipped) {
inputValue = document.getElementById('nfd-deactivation-survey__input').value;
if (inputValue.length > 0) {
surveyInput = inputValue;
}
}

// Send event
const send = await sendEvent(surveyInput);
deactivatePlugin();
}

const sendEvent = async (surveyInput) => {
let eventData = {
'label_key': 'survey_input',
'survey_input': surveyInput,
'category': 'user_action',
'brand': runtimeData.brand,
'page': window.location.href
}

// Attach abTestPluginHome flag value if exists
if (typeof getABTestPluginHome() === 'boolean') {
eventData.abTestPluginHome = getABTestPluginHome();
}

await fetch(runtimeData.eventsEndpoint, {
method: 'POST',
headers: {
@@ -126,18 +147,18 @@
},
body: JSON.stringify({
action: 'deactivation_survey_freeform',
data: {
'label_key': 'survey_input',
'survey_input': surveyInput.length > 0 ? surveyInput : 'No input',
'category': 'user_action',
'brand': runtimeData.brand,
'page': window.location.href
}
data: eventData
})
});
return true;
}

const getABTestPluginHome = () => {
const { NewfoldRuntime } = window;

return NewfoldRuntime?.capabilities?.abTestPluginHome;
}

// Attach events listeners
window.addEventListener('DOMContentLoaded', () => {
const wpAdmin = document.querySelector('body.wp-admin');
@@ -162,7 +183,7 @@
// Skip listener
if (e.target.hasAttribute('nfd-deactivation-survey-skip')) {
e.preventDefault();
deactivatePlugin();
submitSurvey(true);
}
});
})

0 comments on commit 3cc97a3

Please sign in to comment.