| 1 |
import { INSIGHTS_HOST } from '@constants'; |
| 2 |
import { |
| 3 |
selectActivePlugins, |
| 4 |
selectAllRecommendations, |
| 5 |
selectInstalledPlugins, |
| 6 |
} from '@recommendations/selectors/plugin-search'; |
| 7 |
import { usePluginSearchStore } from '@recommendations/state/plugin-search'; |
| 8 |
import { objectFromKeys } from '@recommendations/utils/object-from-keys'; |
| 9 |
|
| 10 |
const { extSharedData, extRecommendationsData } = window; |
| 11 |
|
| 12 |
const showPartnerBranding = !!( |
| 13 |
extRecommendationsData?.showPartnerBranding && extSharedData?.partnerLogo |
| 14 |
); |
| 15 |
|
| 16 |
const stores = { |
| 17 |
'plugin-search': usePluginSearchStore, |
| 18 |
}; |
| 19 |
|
| 20 |
const validSlots = Object.keys(stores); |
| 21 |
|
| 22 |
export const recordActivity = ({ slot, event, product }) => { |
| 23 |
if (!event || !validSlots.includes(slot)) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
const state = stores[slot].getState(); |
| 28 |
|
| 29 |
const installedPlugins = selectInstalledPlugins(state); |
| 30 |
const activePlugins = selectActivePlugins(state); |
| 31 |
const recommendations = selectAllRecommendations(state).map( |
| 32 |
(recommendation) => |
| 33 |
objectFromKeys(recommendation, [ |
| 34 |
'slug', |
| 35 |
'title', |
| 36 |
'description', |
| 37 |
'ctaContent', |
| 38 |
'ctaType', |
| 39 |
'triggerContent', |
| 40 |
'triggerType', |
| 41 |
]), |
| 42 |
); |
| 43 |
const recommendation = recommendations.find( |
| 44 |
(recommendation) => recommendation.slug === product, |
| 45 |
)?.slug; |
| 46 |
|
| 47 |
const payload = { |
| 48 |
event: event, |
| 49 |
timestamp: new Date().toISOString(), |
| 50 |
slot, |
| 51 |
recommendation, |
| 52 |
recommendations, |
| 53 |
recommendationsLimit: state.recommendationsLimit, |
| 54 |
query: decodeURIComponent(state.query), |
| 55 |
searchResults: state.searchPlugins, |
| 56 |
searchResultsLimit: state.searchPluginsLimit, |
| 57 |
installedPlugins, |
| 58 |
activePlugins, |
| 59 |
partnerBrandingEnabled: showPartnerBranding, |
| 60 |
partnerId: extSharedData.partnerId, |
| 61 |
siteId: extSharedData.siteId, |
| 62 |
wpVersion: extSharedData.wpVersion, |
| 63 |
wpLocale: extSharedData.wpLanguage, |
| 64 |
extendifyVersion: extSharedData.version, |
| 65 |
devbuild: extSharedData.devbuild, |
| 66 |
}; |
| 67 |
|
| 68 |
const controller = new AbortController(); |
| 69 |
|
| 70 |
window.setTimeout(() => { |
| 71 |
controller.abort(); |
| 72 |
}, 900); |
| 73 |
|
| 74 |
fetch(`${INSIGHTS_HOST}/api/v1/recommendations/activity`, { |
| 75 |
method: 'POST', |
| 76 |
headers: { |
| 77 |
Accept: 'application/json', |
| 78 |
'Content-Type': 'application/json', |
| 79 |
'X-Extendify-Site-ID': extSharedData.siteId, |
| 80 |
}, |
| 81 |
signal: controller.signal, |
| 82 |
body: JSON.stringify(payload), |
| 83 |
}).catch(() => null); |
| 84 |
}; |
| 85 |
|