| 1 |
<template> |
| 2 |
<div class="wpfnl-mm-setup-wizard"> |
| 3 |
<Sidebar |
| 4 |
:currentStep="currentStep" |
| 5 |
:selectedGoal="'improve-checkout'" |
| 6 |
:storeCheckoutPhase="storeCheckoutPhase" |
| 7 |
@show-exit-modal="handleShowExitModal" |
| 8 |
/> |
| 9 |
<Content |
| 10 |
ref="contentComponent" |
| 11 |
:currentStep="currentStep" |
| 12 |
:selectedBuilder="selectedBuilder" |
| 13 |
:agreeToShare="agreeToShare" |
| 14 |
:prefetchedTemplates="prefetchedTemplates" |
| 15 |
@next-step="handleNextStep" |
| 16 |
@prev-step="handlePrevStep" |
| 17 |
@store-checkout-phase="handleStoreCheckoutPhase" |
| 18 |
/> |
| 19 |
</div> |
| 20 |
</template> |
| 21 |
|
| 22 |
<script> |
| 23 |
import Content from './Content/Content.vue' |
| 24 |
import Sidebar from './Sidebar/Sidebar.vue' |
| 25 |
import apiFetch from '@wordpress/api-fetch' |
| 26 |
import { addQueryArgs } from '@wordpress/url' |
| 27 |
|
| 28 |
const nonce = window.setup_wizard_obj.nonce |
| 29 |
apiFetch.use(apiFetch.createNonceMiddleware(nonce)) |
| 30 |
|
| 31 |
export default { |
| 32 |
name: 'Wizard', |
| 33 |
components: { |
| 34 |
Content, |
| 35 |
Sidebar, |
| 36 |
}, |
| 37 |
data() { |
| 38 |
return { |
| 39 |
currentStep: 1, |
| 40 |
agreeToShare: false, |
| 41 |
selectedBuilder: '', |
| 42 |
stepEnteredAt: Date.now(), |
| 43 |
consentSaved: false, |
| 44 |
prefetchedTemplates: [], |
| 45 |
storeCheckoutPhase: 'select', |
| 46 |
} |
| 47 |
}, |
| 48 |
|
| 49 |
mounted() { |
| 50 |
this.trackStep('viewed', this.currentStep); |
| 51 |
this.prefetchTemplates('gutenberg'); |
| 52 |
}, |
| 53 |
|
| 54 |
methods: { |
| 55 |
prefetchTemplates(builder) { |
| 56 |
const resolved = builder === 'divi' ? 'divi-builder' |
| 57 |
: builder === 'others' ? 'gutenberg' |
| 58 |
: (builder || 'gutenberg'); |
| 59 |
|
| 60 |
const baseUrl = (window.setup_wizard_obj?.rest_api_url || '').replace(/\/$/, ''); |
| 61 |
const path = addQueryArgs(`${baseUrl}/wpfunnels/v1/templates/get_templates`, { |
| 62 |
builder: resolved, |
| 63 |
type: 'store_checkout', |
| 64 |
}); |
| 65 |
|
| 66 |
apiFetch({ path }) |
| 67 |
.then(response => { |
| 68 |
if (response.success && response.templates && response.templates.length > 0) { |
| 69 |
this.prefetchedTemplates = response.templates; |
| 70 |
} else if (resolved !== 'gutenberg') { |
| 71 |
this.prefetchTemplates('gutenberg'); |
| 72 |
} |
| 73 |
}) |
| 74 |
.catch(() => {}); |
| 75 |
}, |
| 76 |
|
| 77 |
getStepName(stepIndex) { |
| 78 |
return { 1: 'welcome', 2: 'required_installation', 3: 'store_checkout' }[stepIndex] || 'unknown'; |
| 79 |
}, |
| 80 |
|
| 81 |
trackStep(eventType, stepIndex) { |
| 82 |
const stepName = this.getStepName(stepIndex); |
| 83 |
const timeOnStep = Math.round((Date.now() - this.stepEnteredAt) / 1000); |
| 84 |
|
| 85 |
const wizardObj = window.setup_wizard_obj || {}; |
| 86 |
const restApiUrl = wizardObj.rest_api_url || ''; |
| 87 |
const url = restApiUrl |
| 88 |
? (restApiUrl.endsWith('/') ? restApiUrl : restApiUrl + '/') + 'wpfunnels/v1/setup-wizard/track-step' |
| 89 |
: null; |
| 90 |
|
| 91 |
if (!url) return; |
| 92 |
|
| 93 |
apiFetch({ |
| 94 |
url, |
| 95 |
method: 'POST', |
| 96 |
data: { |
| 97 |
event_type: eventType, |
| 98 |
step_name: stepName, |
| 99 |
step_index: stepIndex, |
| 100 |
goal: 'improve-checkout', |
| 101 |
time_on_step: eventType === 'viewed' ? 0 : timeOnStep, |
| 102 |
}, |
| 103 |
}).catch(() => {}); |
| 104 |
}, |
| 105 |
|
| 106 |
saveConsent(consented) { |
| 107 |
if (this.consentSaved) return; |
| 108 |
this.consentSaved = true; |
| 109 |
|
| 110 |
const wizardObj = window.setup_wizard_obj || {}; |
| 111 |
const restApiUrl = wizardObj.rest_api_url || ''; |
| 112 |
const url = restApiUrl |
| 113 |
? (restApiUrl.endsWith('/') ? restApiUrl : restApiUrl + '/') + 'wpfunnels/v1/setup-wizard/save-consent' |
| 114 |
: null; |
| 115 |
|
| 116 |
if (!url) return; |
| 117 |
|
| 118 |
apiFetch({ |
| 119 |
url, |
| 120 |
method: 'POST', |
| 121 |
data: { consented: !!consented }, |
| 122 |
}).catch(() => {}); |
| 123 |
}, |
| 124 |
|
| 125 |
handleNextStep(data) { |
| 126 |
if (data && data.agreeToShare !== undefined) { |
| 127 |
this.agreeToShare = data.agreeToShare; |
| 128 |
} |
| 129 |
if (data && data.builder) { |
| 130 |
this.selectedBuilder = data.builder; |
| 131 |
this.prefetchTemplates(data.builder); |
| 132 |
} |
| 133 |
|
| 134 |
if (this.currentStep === 1) { |
| 135 |
this.saveConsent(this.agreeToShare); |
| 136 |
} |
| 137 |
|
| 138 |
this.trackStep('completed', this.currentStep); |
| 139 |
if (this.currentStep < 3) { |
| 140 |
this.currentStep++; |
| 141 |
this.stepEnteredAt = Date.now(); |
| 142 |
this.trackStep('viewed', this.currentStep); |
| 143 |
} |
| 144 |
}, |
| 145 |
|
| 146 |
handlePrevStep() { |
| 147 |
if (this.currentStep > 1) { |
| 148 |
this.trackStep('completed', this.currentStep); |
| 149 |
this.currentStep--; |
| 150 |
this.stepEnteredAt = Date.now(); |
| 151 |
this.trackStep('viewed', this.currentStep); |
| 152 |
} |
| 153 |
}, |
| 154 |
|
| 155 |
handleShowExitModal() { |
| 156 |
if (this.$refs.contentComponent) { |
| 157 |
this.$refs.contentComponent.showExitModal(); |
| 158 |
} |
| 159 |
}, |
| 160 |
|
| 161 |
handleStoreCheckoutPhase(phase) { |
| 162 |
this.storeCheckoutPhase = phase; |
| 163 |
}, |
| 164 |
}, |
| 165 |
} |
| 166 |
</script> |
| 167 |
|