Skip to content

Commit

Permalink
add search.query event api for search results (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Apr 25, 2022
1 parent 00e34d9 commit 6895d28
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/platforms/vtex/clients/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => {
const response = await fetch(info, init)

if (response.ok) {
return response.json()
return response.status !== 204 ? response.json() : undefined
}

console.error(info, init, response)
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/platforms/vtex/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { VtexCommerce } from './commerce'
import { IntelligentSearch } from './search'
import { SP } from './sp'
import type { Context, Options } from '..'

export type Clients = ReturnType<typeof getClients>

export const getClients = (options: Options, ctx: Context) => {
const search = IntelligentSearch(options, ctx)
const commerce = VtexCommerce(options, ctx)
const sp = SP(options, ctx)

return {
search,
commerce,
sp,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ProductSearchResult {
translated: boolean
locale: string
query: string
operator: string
operator: 'and' | 'or'
fuzzy: string
correction: Correction
}
Expand Down
67 changes: 67 additions & 0 deletions packages/api/src/platforms/vtex/clients/sp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Client for SP, Intelligent search's analytics event API
* More info at: https://www.notion.so/vtexhandbook/Event-API-Documentation-48eee26730cf4d7f80f8fd7262231f84
*/
import { fetchAPI } from '../fetch'
import type { Options, Context } from '../../index'

const THIRTY_MINUTES_S = 30 * 60
const ONE_YEAR_S = 365 * 24 * 3600

const randomUUID = () => (Math.random() * 1e6).toFixed(0)

const timelapsed = (past: number) => (Date.now() - past) / 1e3

const createId = (expiresSecond: number) => {
let payload = randomUUID()
let createdAt = Date.now()

return () => {
if (timelapsed(createdAt) > expiresSecond) {
payload = randomUUID()
createdAt = Date.now()
}

return payload
}
}

const user = {
anonymous: createId(ONE_YEAR_S),
session: createId(THIRTY_MINUTES_S),
}

export type SearchEvent = {
type: 'search.query'
text: string // 'zapatilha'
misspelled: boolean
match: number
operator: 'and' | 'or'
session?: string
anonymous?: string
}

export const SP = ({ account }: Options, _: Context) => {
const base = `https://sp.vtex.com/event-api/v1/${account}/event`

const sendEvent = (options: SearchEvent) => {
return fetchAPI(base, {
method: 'POST',
body: JSON.stringify({
...options,
agent: '@faststore/api',
anonymous: user.anonymous(),
session: user.session(),
// session: 'zZlNhqz1vFJP6iPG5Oqtt',
// anonymous: 'Om1TNluGvgmSgU5OOTvkkd',
}),
headers: {
'content-type': 'application/json',
},
})
}

return {
sendEvent,
}
}
14 changes: 13 additions & 1 deletion packages/api/src/platforms/vtex/resolvers/searchResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ const REMOVED_FACETS_FROM_COLLECTION_PAGE = ['departamento']
export const StoreSearchResult: Record<string, Resolver<Root>> = {
products: async (searchArgs, _, ctx) => {
const {
clients: { search },
clients: { search, sp },
} = ctx

const products = await search.products(searchArgs)

// Raise event on search's analytics API when performing
// a full text search.
if (searchArgs.query) {
sp.sendEvent({
type: 'search.query',
text: searchArgs.query,
misspelled: products.correction.misspelled,
match: products.total,
operator: products.operator,
}).catch(console.error)
}

const skus = products.products
.map((product) => {
const [maybeSku] = product.skus
Expand Down

1 comment on commit 6895d28

@vercel
Copy link

@vercel vercel bot commented on 6895d28 Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.