Created
January 23, 2023 16:40
-
-
Save gabrielecanepa/82f576b528b19652b410265bd6e91d5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import algoliasearch from 'algoliasearch' | |
import searchInsights from 'search-insights' | |
import { chunks } from './utils.js' | |
const APP_ID = 'YOUR_APP_ID' | |
const API_KEY = 'YOUR_SEARCH_API_KEY' | |
const INDEX_NAME = 'YOUR_INDEX_NAME' | |
const FACET_NAME = 'categories' // change this value according to your configuration | |
const client = algoliasearch(APP_ID, API_KEY) | |
const index = client.initIndex(INDEX_NAME) | |
searchInsights('init', { | |
appId: APP_ID, | |
apiKey: API_KEY, | |
}) | |
const userTokens = [...Array(100).keys()].map(i => `user-${i + 1}`) // user-1 to user-100 | |
const run = async () => { | |
try { | |
const { facets } = await index.search('', { facets: '*' }) | |
const queries = Object.keys(facets[FACET_NAME]).map(category => ({ | |
indexName: INDEX_NAME, | |
query: '', | |
params: { | |
clickAnalytics: true, | |
hitsPerPage: 1000, | |
filters: `${FACET_NAME}:'${category}'`, | |
} | |
})) | |
const categories = await chunks(queries, 50).reduce(async (memo, chunk) => { | |
const { results } = await client.multipleQueries(chunk) | |
return [...await memo, ...results.map(({ hits, queryID }) => ({ objectIDs: hits.map(h => h.objectID), queryID }))] | |
}, []) | |
const events = userTokens.reduce((events, userToken) => { | |
const userCategories = categories.slice(0, Math.floor(Math.random() * categories.length) + 1) | |
for (const { objectIDs, queryID } of userCategories) { | |
const userObjectIDs = objectIDs.slice(0, Math.floor(Math.random() * objectIDs.length) + 1) | |
for (const objectID of userObjectIDs) { | |
const eventType = Math.random() > 0.5 ? 'click' : 'conversion' | |
const eventName = eventType === 'click' ? 'PLP: Product Clicked' : 'PLP: Product Added to Cart' | |
const event = { eventType, eventName, index: INDEX_NAME, userToken, queryID, objectIDs: [objectID] } | |
if (eventType === 'click') event.positions = [objectIDs.indexOf(objectID) + 1] | |
events.push(event) | |
} | |
} | |
return events | |
}, []) | |
console.log(`Sending ${events.length} events...`) | |
for (const chunk of chunks(events, 100)) { | |
await searchInsights('sendEvents', chunk) | |
} | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const chunks = (arr, n) => arr.reduce((result, el, i) => { | |
const chunk = Math.floor(i / n) | |
result[chunk] = [].concat((result[chunk] || []), el) | |
return result | |
}, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment