From 10874d1c2be7657d288b74663706549f85c70acf Mon Sep 17 00:00:00 2001 From: wpalani Date: Wed, 29 Nov 2023 11:21:20 -0700 Subject: [PATCH] add `abTestPluginHome` flag checker --- static/js/deactivation-survey.js | 42 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/static/js/deactivation-survey.js b/static/js/deactivation-survey.js index ab65621..851fb4f 100644 --- a/static/js/deactivation-survey.js +++ b/static/js/deactivation-survey.js @@ -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: { @@ -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'); @@ -162,7 +184,7 @@ // Skip listener if (e.target.hasAttribute('nfd-deactivation-survey-skip')) { e.preventDefault(); - deactivatePlugin(); + submitSurvey(true); } }); })