Skip to content

Commit

Permalink
add abTestPluginHome flag checker
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Nov 29, 2023
1 parent dd8bf18 commit 10874d1
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions static/js/deactivation-survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,35 @@
});
}

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
}

if (getABTestPluginHome()) {
eventData.abTestPluginHome = true;
}

await fetch(runtimeData.eventsEndpoint, {
method: 'POST',
headers: {
Expand All @@ -126,18 +146,20 @@
},
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 === true
);
}

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

0 comments on commit 10874d1

Please sign in to comment.