|
| 1 | +import { AnalyticsEvent, PageViewEvent } from '@faststore/sdk' |
| 2 | +import { |
| 3 | + CategoryView, |
| 4 | + DepartmentView, |
| 5 | + HomeView, |
| 6 | + IntelligentSearchQueryEvent, |
| 7 | + InternalSiteSearchView, |
| 8 | + OtherView, |
| 9 | + ProductView, |
| 10 | + SearchSelectItemEvent, |
| 11 | +} from '../../types' |
| 12 | + |
| 13 | +const EventNames = { |
| 14 | + OTHER_VIEW: 'otherView', |
| 15 | + HOME_VIEW: 'homeView', |
| 16 | + CATEGORY_VIEW: 'categoryView', |
| 17 | + DEPARTMENT_VIEW: 'departmentView', |
| 18 | + INTERNAL_SITE_SEARCH_VIEW: 'internalSiteSearchView', |
| 19 | + PRODUCT_VIEW: 'productView', |
| 20 | +} as const |
| 21 | + |
| 22 | +type EventNames = (typeof EventNames)[keyof typeof EventNames] |
| 23 | +type EventParams = |
| 24 | + | HomeView |
| 25 | + | CategoryView |
| 26 | + | DepartmentView |
| 27 | + | InternalSiteSearchView |
| 28 | + | ProductView |
| 29 | + | OtherView |
| 30 | + |
| 31 | +const sendEvent = (eventName: EventNames, eventParams: EventParams) => { |
| 32 | + window?.sendrc?.(eventName, eventParams) |
| 33 | +} |
| 34 | + |
| 35 | +const handleEvent = ( |
| 36 | + event: AnalyticsEvent | SearchSelectItemEvent | IntelligentSearchQueryEvent |
| 37 | +) => { |
| 38 | + let eventParams |
| 39 | + switch (event.name) { |
| 40 | + //TODO: add missing events - eg 'add_to_cart' and 'view_cart' |
| 41 | + case 'page_view': |
| 42 | + eventParams = (event as PageViewEvent).params |
| 43 | + switch (eventParams.type) { |
| 44 | + case 'plp': |
| 45 | + const collection = eventParams?.data?.collection |
| 46 | + const collectionCategoryList = |
| 47 | + collection?.breadcrumbList?.itemListElement |
| 48 | + if (collectionCategoryList.length > 1) { |
| 49 | + // console.log('✨ Category View Event:', eventParams) |
| 50 | + sendEvent(EventNames.CATEGORY_VIEW, { |
| 51 | + //TODO: add missing arg - categoryId |
| 52 | + departmentId: collection?.id, |
| 53 | + departmentName: collectionCategoryList[0]?.name, |
| 54 | + categoryName: |
| 55 | + collectionCategoryList[collectionCategoryList.length - 1]?.name, |
| 56 | + }) |
| 57 | + } else { |
| 58 | + // console.log('✨ Department View Event:', eventParams) |
| 59 | + sendEvent(EventNames.DEPARTMENT_VIEW, { |
| 60 | + departmentId: collection?.id, |
| 61 | + departmentName: collectionCategoryList[0]?.name, |
| 62 | + }) |
| 63 | + } |
| 64 | + break |
| 65 | + case 'pdp': |
| 66 | + // console.log('✨ Product View Event: ', eventParams) |
| 67 | + const product = eventParams?.data?.product |
| 68 | + const offers = product?.offers |
| 69 | + const productCategoryList = product?.breadcrumbList?.itemListElement |
| 70 | + sendEvent(EventNames.PRODUCT_VIEW, { |
| 71 | + //TODO: add missing args - skuStockOutFromProductDetail, productReferenceId, |
| 72 | + // productEans, skuStocks, productBrandId, productDepartmentId, |
| 73 | + // productCategoryId, productListPrice, sellerIds |
| 74 | + productId: product?.isVariantOf?.productGroupID, |
| 75 | + productName: product?.isVariantOf?.name, |
| 76 | + productBrandName: product?.brand?.name, |
| 77 | + productDepartmentName: productCategoryList |
| 78 | + ? productCategoryList[0]?.name |
| 79 | + : '', |
| 80 | + productCategoryName: |
| 81 | + productCategoryList.length > 1 |
| 82 | + ? productCategoryList[productCategoryList.length - 2]?.name |
| 83 | + : '', |
| 84 | + productPrice: offers?.price, |
| 85 | + sellerId: offers?.seller?.id, |
| 86 | + }) |
| 87 | + break |
| 88 | + default: |
| 89 | + if (eventParams?.page?.type === 'home') { |
| 90 | + // console.log('✨ Home View Event:', eventParams) |
| 91 | + sendEvent(EventNames.HOME_VIEW, {}) |
| 92 | + } else { |
| 93 | + // console.log('✨ Other View Event:', eventParams) |
| 94 | + sendEvent(EventNames.OTHER_VIEW, {}) |
| 95 | + } |
| 96 | + } |
| 97 | + break |
| 98 | + case 'intelligent_search_query': |
| 99 | + console.log('✨ Internal Site Search View Event:', event) |
| 100 | + eventParams = (event as IntelligentSearchQueryEvent).params |
| 101 | + sendEvent(EventNames.INTERNAL_SITE_SEARCH_VIEW, { |
| 102 | + siteSearchTerm: eventParams.term, |
| 103 | + siteSearchForm: eventParams.url, |
| 104 | + siteSearchResults: eventParams.totalCount, |
| 105 | + }) |
| 106 | + break |
| 107 | + default: |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export default handleEvent |
0 commit comments