(async () => { const froomleApi = "https://yoursubdomain.froomle.com/api/yourenvironment"; const context = { channel: "...", page_type: "...", context_item: "...", context_item_type: "...", device_id: "...", user_id: "..." }; async function sendPageVisit() { const response = await fetch(`${froomleApi}/events`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [Object.assign({ event_type: "page_visit" }, context)] }) }); if (!response.ok) { throw Error(response.statusText); } return await response.json(); } async function sendDetailPageview() { const response = await fetch(`${froomleApi}/events`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [Object.assign({ event_type: "detail_pageview", action_item: context.context_item, action_item_type: context.context_item_type }, context)] }) }); if (!response.ok) { throw Error(response.statusText); } return await response.json(); } async function sendImpressions(recommendations, list_name) { const action_items = recommendations.lists .find(l => l.list_name === list_name) .items .map(i => i.item_id); const response = await fetch(`${froomleApi}/events`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ events: action_items.map(id => Object.assign({ event_type: "impression", list_name: list_name, action_item: id, action_item_type: "article", request_id: recommendations.request_id, user_group: recommendations.user_group }, context)) }) }); if (!response.ok) { throw Error(response.statusText); } return await response.json(); } async function sendClickOnRecommendation(recommendations, list_name, action_item) { const response = await fetch(`${froomleApi}/events`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ events: [Object.assign({ event_type: "click_on_recommendation", list_name: list_name, action_item: action_item, action_item_type: "article", request_id: recommendations.request_id, user_group: recommendations.user_group }, context)] }) }); if (!response.ok) { throw Error(response.statusText); } return await response.json(); } async function fetchRecommendations(list_names, list_sizes) { const response = await fetch(`${froomleApi}/recommendations/requests`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(Object.assign({ lists: list_names.map((name, i) => ({ list_name: name, list_size: list_sizes[i] })) }, context)) }); if (!response.ok) { throw Error(response.statusText); } return await response.json(); } const list_name = "some_list"; const recommendations = await fetchRecommendations([list_name], [5]); const recommended_items = recommendations.lists .find(l => l.list_name === list_name) .items; // Assuming the HTML contains some