PluginProbe
Extendify / 0.10.0
Extendify v0.10.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Onboarding / hooks / useTelemetry.js

useTelemetry.js in Extendify 0.10.0, at src/Onboarding/hooks/useTelemetry.js

123 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useState } from '@wordpress/element'
2 import { createOrder } from '@onboarding/api/DataApi'
3 import { useGlobalStore } from '@onboarding/state/Global'
4 import { usePagesStore } from '@onboarding/state/Pages'
5 import { useUserSelectionStore } from '@onboarding/state/UserSelections'
6
7 export const useTelemetry = () => {
8 const {
9 goals: selectedGoals,
10 pages: selectedPages,
11 plugins: selectedPlugins,
12 siteType: selectedSiteType,
13 style: selectedStyle,
14 feedbackMissingSiteType,
15 feedbackMissingGoal,
16 siteTypeSearch,
17 } = useUserSelectionStore()
18 const { orderId, setOrderId, generating } = useGlobalStore()
19 const { pages, currentPageIndex } = usePagesStore()
20 const [url, setUrl] = useState()
21 const [stepProgress, setStepProgress] = useState([])
22 const [viewedStyles, setViewedStyles] = useState(new Set())
23
24 useEffect(() => {
25 const p = [...pages].map((p) => p[0])
26 // Add pages as they move around
27 setStepProgress((progress) =>
28 progress?.at(-1) === p[currentPageIndex]
29 ? progress
30 : [...progress, p[currentPageIndex]],
31 )
32 }, [currentPageIndex, pages])
33
34 useEffect(() => {
35 if (!generating) return
36 // They pressed Launch
37 setStepProgress((progress) => [...progress, 'launched'])
38 }, [generating])
39
40 useEffect(() => {
41 if (!Object.keys(selectedStyle ?? {})?.length) return
42 // Add selectedStyle to the set
43 setViewedStyles((styles) => {
44 const newStyles = new Set(styles)
45 newStyles.add(selectedStyle.recordId)
46 return newStyles
47 })
48 }, [selectedStyle])
49
50 useEffect(() => {
51 let mode = 'onboarding'
52 const search = window.location?.search
53 mode = search?.indexOf('DEVMODE') > -1 ? 'onboarding-dev' : mode
54 mode = search?.indexOf('LOCALMODE') > -1 ? 'onboarding-local' : mode
55 setUrl(window?.extOnbData?.config?.api[mode])
56 }, [])
57
58 useEffect(() => {
59 if (!url || orderId?.length) return
60 // Create a order that persists over local storage
61 createOrder().then((response) => {
62 setOrderId(response.data.id)
63 })
64 }, [url, setOrderId, orderId])
65
66 useEffect(() => {
67 if (!url || !orderId) return
68 let id = 0
69 id = window.setTimeout(() => {
70 fetch(`${url}/progress`, {
71 method: 'POST',
72 headers: { 'Content-type': 'application/json' },
73 body: JSON.stringify({
74 orderId,
75 selectedGoals: selectedGoals?.map((g) => g.id),
76 selectedPages: selectedPages?.map((p) => p.id),
77 selectedPlugins,
78 selectedSiteType: selectedSiteType?.recordId
79 ? [selectedSiteType.recordId]
80 : [],
81 selectedStyle: selectedStyle?.recordId
82 ? [selectedStyle.recordId]
83 : [],
84 stepProgress,
85 pages,
86 viewedStyles: [...viewedStyles].slice(1),
87 feedbackMissingSiteType,
88 feedbackMissingGoal,
89 siteTypeSearch,
90 perfStyles: getPerformance('style'),
91 perfPages: getPerformance('page'),
92 insightsId: window.extOnbData?.insightsId,
93 activeTests: JSON.stringify(window.extOnbData?.activeTests),
94 }),
95 })
96 }, 1000)
97 return () => window.clearTimeout(id)
98 }, [
99 url,
100 selectedGoals,
101 selectedPages,
102 selectedPlugins,
103 selectedSiteType,
104 selectedStyle,
105 pages,
106 orderId,
107 stepProgress,
108 viewedStyles,
109 feedbackMissingSiteType,
110 feedbackMissingGoal,
111 siteTypeSearch,
112 ])
113 }
114
115 const getPerformance = (type) => {
116 return performance
117 .getEntriesByType('measure')
118 .filter(
119 (m) => m?.detail?.extendify && m?.detail?.context?.type === type,
120 )
121 .map((m) => ({ [m.name]: m.duration }))
122 }
123