| 1 |
import { useEffect, useRef, useState } from '@wordpress/element'; |
| 2 |
import { INSIGHTS_HOST } from '@constants'; |
| 3 |
import { useGlobalStore } from '@launch/state/Global'; |
| 4 |
import { usePagesStore } from '@launch/state/Pages'; |
| 5 |
import { usePagesSelectionStore } from '@launch/state/pages-selections'; |
| 6 |
import { useUserSelectionStore } from '@launch/state/user-selections'; |
| 7 |
|
| 8 |
// Dev note: This entire section is opt-in only when partnerID is set as a constant |
| 9 |
export const useTelemetry = () => { |
| 10 |
const { goals, getGoalsPlugins, siteStructure, variation, siteProfile } = |
| 11 |
useUserSelectionStore(); |
| 12 |
const { pages: selectedPages, style: selectedStyle } = |
| 13 |
usePagesSelectionStore(); |
| 14 |
const selectedPlugins = getGoalsPlugins(); |
| 15 |
const { generating } = useGlobalStore(); |
| 16 |
const { pages, currentPageIndex } = usePagesStore(); |
| 17 |
const [stepProgress, setStepProgress] = useState([]); |
| 18 |
const [viewedStyles, setViewedStyles] = useState(new Set()); |
| 19 |
const running = useRef(false); |
| 20 |
|
| 21 |
useEffect(() => { |
| 22 |
const p = [...pages].map((p) => p[0]); |
| 23 |
// Add pages as they move around |
| 24 |
setStepProgress((progress) => |
| 25 |
// Return early if launched, or on the same page |
| 26 |
[p[currentPageIndex], 'launched'].includes(progress?.at(-1)) |
| 27 |
? progress |
| 28 |
: [...progress, p[currentPageIndex]], |
| 29 |
); |
| 30 |
}, [currentPageIndex, pages]); |
| 31 |
|
| 32 |
useEffect(() => { |
| 33 |
if (!generating) return; |
| 34 |
// They pressed Launch |
| 35 |
setStepProgress((progress) => [...progress, 'launched']); |
| 36 |
}, [generating]); |
| 37 |
|
| 38 |
useEffect(() => { |
| 39 |
if (!Object.keys(selectedStyle ?? {})?.length) return; |
| 40 |
// Add selectedStyle to the set |
| 41 |
setViewedStyles((styles) => { |
| 42 |
const newStyles = new Set(styles); |
| 43 |
newStyles.add(selectedStyle); |
| 44 |
return newStyles; |
| 45 |
}); |
| 46 |
}, [selectedStyle]); |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
let id = 0; |
| 50 |
let innerId = 0; |
| 51 |
const timeout = currentPageIndex ? 1000 : 0; |
| 52 |
id = window.setTimeout(() => { |
| 53 |
if (running.current) return; |
| 54 |
running.current = true; |
| 55 |
const controller = new AbortController(); |
| 56 |
innerId = window.setTimeout(() => { |
| 57 |
running.current = false; |
| 58 |
controller.abort(); |
| 59 |
}, 900); |
| 60 |
fetch(`${INSIGHTS_HOST}/api/v1/launch`, { |
| 61 |
method: 'POST', |
| 62 |
headers: { |
| 63 |
'Content-type': 'application/json', |
| 64 |
Accept: 'application/json', |
| 65 |
'X-Extendify': 'true', |
| 66 |
}, |
| 67 |
signal: controller.signal, |
| 68 |
body: JSON.stringify({ |
| 69 |
siteType: siteProfile?.aiSiteType, |
| 70 |
siteCategory: siteProfile?.aiSiteCategory, |
| 71 |
siteCreatedAt: window.extSharedData?.siteCreatedAt, |
| 72 |
style: variation?.title, |
| 73 |
siteStructure: siteStructure, |
| 74 |
pages: selectedPages?.map((p) => p.slug), |
| 75 |
goals: goals?.map((g) => g.slug), |
| 76 |
lastCompletedStep: stepProgress?.at(-1), |
| 77 |
progress: stepProgress, |
| 78 |
stylesViewed: [...viewedStyles] |
| 79 |
.filter((s) => s?.variation) |
| 80 |
.map((s) => s.variation.title), |
| 81 |
insightsId: window.extSharedData?.siteId, |
| 82 |
activeTests: |
| 83 |
window.extOnbData?.activeTests?.length > 0 |
| 84 |
? JSON.stringify(window.extOnbData?.activeTests) |
| 85 |
: undefined, |
| 86 |
hostPartner: window.extSharedData?.partnerId, |
| 87 |
language: window.extSharedData?.wpLanguage, |
| 88 |
siteURL: window.extSharedData?.homeUrl, |
| 89 |
}), |
| 90 |
}) |
| 91 |
.catch(() => undefined) |
| 92 |
.finally(() => { |
| 93 |
running.current = false; |
| 94 |
}); |
| 95 |
}, timeout); |
| 96 |
return () => { |
| 97 |
running.current = false; |
| 98 |
[id, innerId].forEach((i) => window.clearTimeout(i)); |
| 99 |
}; |
| 100 |
}, [ |
| 101 |
selectedPages, |
| 102 |
selectedPlugins, |
| 103 |
selectedStyle, |
| 104 |
pages, |
| 105 |
stepProgress, |
| 106 |
viewedStyles, |
| 107 |
currentPageIndex, |
| 108 |
goals, |
| 109 |
siteProfile?.aiSiteType, |
| 110 |
siteProfile?.aiSiteCategory, |
| 111 |
siteStructure, |
| 112 |
variation, |
| 113 |
]); |
| 114 |
}; |
| 115 |
|