Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: autocomplete dataLayer events #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions react/components/Autocomplete/components/TileList/TileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ interface TileListProps {
totalProducts: number
layout: ProductLayout
isLoading: boolean
onProductClick: (product: string, position: number, term: string) => void
onProductClick: (
position: number,
term: string,
productSummary: Product
) => void
onSeeAllClick: (term: string) => void
HorizontalProductSummary?: React.ComponentType<{
product: Product
Expand Down Expand Up @@ -77,14 +81,14 @@ const TileList: FC<TileListProps> = ({
product={productSummary}
placement={AUTOCOMPLETE_PLACEMENT}
actionOnClick={() => {
onProductClick(productSummary.productId, index, term)
onProductClick(index, term, productSummary)
}}
/>
) : (
<CustomListItem
product={productSummary}
onClick={() => {
onProductClick(productSummary.productId, index, term)
onProductClick(index, term, productSummary)
}}
/>
)
Expand All @@ -94,7 +98,7 @@ const TileList: FC<TileListProps> = ({
product={productSummary}
placement={AUTOCOMPLETE_PLACEMENT}
actionOnClick={() => {
onProductClick(productSummary.productId, index, term)
onProductClick(index, term, productSummary)
}}
/>
)}
Expand Down
22 changes: 7 additions & 15 deletions react/components/Autocomplete/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { withRuntime } from '../../utils/withRuntime'
import { decodeUrlString, encodeUrlString } from '../../utils/string-utils'
import {
EventType,
handleAutocompleteSearch,
handleItemClick,
handleProductClick,
handleSeeAllClick,
Expand Down Expand Up @@ -310,6 +309,7 @@ class AutoComplete extends React.Component<
const session = await getSession(this.props.runtime.rootPath)
const shippingOptions =
session?.map((item: Record<string, string>) => item.value) ?? []

const advertisementOptions: AdvertisementOptions = {
showSponsored: true,
sponsoredCount: 2,
Expand All @@ -330,18 +330,6 @@ class AutoComplete extends React.Component<
advertisementOptions
)

if (!queryFromHover) {
const { count, operator, misspelled } = result.data.productSuggestions

handleAutocompleteSearch(
this.props.push,
operator,
misspelled,
count,
term
)
}

this.setState({
isProductsLoading: false,
})
Expand Down Expand Up @@ -529,8 +517,12 @@ class AutoComplete extends React.Component<
totalProducts={totalProducts || 0}
layout={this.getProductLayout()}
isLoading={isProductsLoading}
onProductClick={(id, position, term) => {
handleProductClick(push, runtime.page)(id, position, term)
onProductClick={(position, term, productSummary) => {
handleProductClick(push, runtime.page)(
position,
term,
productSummary
)
this.closeModal()
}}
onSeeAllClick={term => {
Expand Down
55 changes: 19 additions & 36 deletions react/utils/pixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,36 @@ export enum EventType {
TopSearchClick = 'top_search_click',
HistoryClick = 'history_click',
Search = 'search',
SeeAllClick = 'see_all_click',
SeeAllClick = 'see_all_products_click',
}

export function handleProductClick(push: (data: any) => void, page: string) {
return (productId: string, position: number, term: string) =>
return (position: number, term: string, productSummary: Product) => {
const {
productName,
brand,
categories,
sku,
productId,
productReference,
} = productSummary

push({
page,
event: EVENT_NAME,
event: 'productClick',
eventType: EventType.ProductClick,
product: {
productName,
brand,
categories,
sku,
productId,
position,
productReference,
},
position,
term,
})
}
}

export function handleItemClick(
Expand Down Expand Up @@ -52,35 +67,3 @@ export function handleSeeAllClick(push: (data: any) => void, page: string) {
},
})
}

export function handleAutocompleteSearch(
push: (data: any) => void,
operator: string,
misspelled: boolean,
count: number,
term: string
) {
try {
push({
event: EVENT_NAME,
eventType: EventType.Search,
search: {
operator,
misspelled,
text: decodeURI(term),
match: count,
},
})
} catch (e) {
push({
event: EVENT_NAME,
eventType: EventType.Search,
search: {
operator,
misspelled,
text: term,
match: count,
},
})
}
}