@recapi/client 0.15.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -3
- package/package.json +1 -1
- package/src/magento.js +15 -3
- package/src/shopify.js +26 -11
- package/src/woocommerce.js +17 -5
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ are, gzipped, and browsers cache them:
|
|
|
35
35
|
|
|
36
36
|
```html
|
|
37
37
|
<script type="module">
|
|
38
|
-
import { createClient } from 'https://api.example.com/sdk/0.15.
|
|
38
|
+
import { createClient } from 'https://api.example.com/sdk/0.15.1/index.js';
|
|
39
39
|
|
|
40
40
|
const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com', consent: false });
|
|
41
41
|
// From your cookie banner, once the visitor agrees: recapi.setConsent(true).
|
|
@@ -185,7 +185,11 @@ nothing to paste again. A file of its own rather than more exports of
|
|
|
185
185
|
only the short rows, without any product either row listed. A refused top-up
|
|
186
186
|
(a key revoked mid-page) keeps the cards already drawn. The second row
|
|
187
187
|
never shows the popular fallback (recapi slow, down or at a limit): popular
|
|
188
|
-
items are not what was bought together.
|
|
188
|
+
items are not what was bought together. From 0.15.1 its title is drawn only
|
|
189
|
+
when one of the cards drawn (after unavailable products are dropped and the
|
|
190
|
+
top-up added) has `why: bought_together`; otherwise the cards show without
|
|
191
|
+
it, the wrapper's class gains `recapi-more` and a `margin-top` of the grid's
|
|
192
|
+
gap, so they read as more of the first row.
|
|
189
193
|
Each card shows the picture (resized by Shopify's
|
|
190
194
|
CDN with `?width=400`), the title and `price_min` formatted with
|
|
191
195
|
`Intl.NumberFormat` in `Shopify.currency.active` and the page's `lang`
|
|
@@ -384,6 +388,9 @@ the first row's div once it has cards: the printed page doesn't change,
|
|
|
384
388
|
and a row with nothing to show leaves nothing. One row empty never hides
|
|
385
389
|
the other. *WooCommerce → recapi* turns it off ("Your product pages",
|
|
386
390
|
option `recapi_bought_together`); the cart and shortcode rows never have it.
|
|
391
|
+
From 0.15.1 the heading is added only when one of the cards drawn has
|
|
392
|
+
`why: bought_together`; otherwise the cards show without it, a `margin-top`
|
|
393
|
+
of the grid's gap under the first row.
|
|
387
394
|
|
|
388
395
|
The widget fills it like the Shopify snippet's, once it nears the screen,
|
|
389
396
|
asking for the 4 products it shows (recapi counts every product of a list
|
|
@@ -541,7 +548,10 @@ mirrors the visitor's id and clicked recommendations into a first-party
|
|
|
541
548
|
cookie so the server's purchases are credited like the SDK's own. On product
|
|
542
549
|
pages it fills recapi's row, drawing each card from the item's `metadata`
|
|
543
550
|
or, without it, from the store; the row is printed with the store's own
|
|
544
|
-
products, which stay while recapi has none.
|
|
551
|
+
products, which stay while recapi has none. Its second row, "Often bought
|
|
552
|
+
together", is printed hidden and empty; from 0.15.1 the script removes that
|
|
553
|
+
block's `.block-title` when none of the cards drawn has
|
|
554
|
+
`why: bought_together`, so its cards read as more of the first row.
|
|
545
555
|
|
|
546
556
|
#### The served storefront code
|
|
547
557
|
|
|
@@ -1105,6 +1115,13 @@ minifier, to keep zero dev dependencies.
|
|
|
1105
1115
|
|
|
1106
1116
|
## Changes
|
|
1107
1117
|
|
|
1118
|
+
- 2026-11-22: version 0.15.1. Shopify, WooCommerce and Magento: a product
|
|
1119
|
+
page's second row prints "Often bought together" only when one of the
|
|
1120
|
+
cards it shows was carted or bought with the product on screen (`why:
|
|
1121
|
+
bought_together`); otherwise the same cards show without the title, as
|
|
1122
|
+
more of the row above. When recapi answers the second call (the one that
|
|
1123
|
+
fills cards a shop can't sell) with popular products, only the first row
|
|
1124
|
+
takes them: Often bought together keeps the cards it has.
|
|
1108
1125
|
- 2026-11-22: version 0.15.0. `track()` takes a purchase's `value` and
|
|
1109
1126
|
`currency`, sent with the event for the sales figure of a lift test.
|
|
1110
1127
|
- 2026-11-21: version 0.14.0. Shopify: a row in a full-width section
|
package/package.json
CHANGED
package/src/magento.js
CHANGED
|
@@ -196,9 +196,10 @@ export async function magentoPage(page) {
|
|
|
196
196
|
const found = (response?.ok ? await response.json().catch(() => null) : null)?.cards;
|
|
197
197
|
for (const card of Array.isArray(found) ? found : []) if (cards.has(String(card?.id))) cards.set(String(card.id), card);
|
|
198
198
|
}
|
|
199
|
-
|
|
199
|
+
const reasons = new Map(items.map(({ itemId, why }) => [itemId, why]));
|
|
200
|
+
return [...cards].filter(([, card]) => card && web(card.url)).map(([id, card]) => ({ ...card, recommendationId, why: reasons.get(id) }));
|
|
200
201
|
};
|
|
201
|
-
const html = (cards) => cards.slice(0, ROW).map(({ recommendationId, ...card }) => {
|
|
202
|
+
const html = (cards) => cards.slice(0, ROW).map(({ recommendationId, why, ...card }) => {
|
|
202
203
|
const link = web(card.url);
|
|
203
204
|
// ?rec= only where a fragment can't carry the id: the address has its own
|
|
204
205
|
// (a theme's, not ours to overwrite), it's the page already open (a
|
|
@@ -223,7 +224,11 @@ export async function magentoPage(page) {
|
|
|
223
224
|
try {
|
|
224
225
|
const more = await ask(short.map((i) => ({ count: ROW - cards[i].length, placement: rows[i].placement })),
|
|
225
226
|
lists.flatMap(({ items }) => items.map(({ itemId }) => itemId)));
|
|
226
|
-
|
|
227
|
+
// A popular top-up (recapi slow or at a limit on this call) fills the
|
|
228
|
+
// first row's gap with what that row shows anyway when recapi is
|
|
229
|
+
// down, but under the bought-together title it would mislead: that
|
|
230
|
+
// row keeps the cards it has.
|
|
231
|
+
const extra = await Promise.all(more.map((list, k) => (rows[short[k]].together && list.fallback ? [] : cardsOf(list))));
|
|
227
232
|
short.forEach((i, k) => cards[i].push(...extra[k]));
|
|
228
233
|
} catch (error) {
|
|
229
234
|
// Rejected (a key revoked mid-page): the cards of the first call stay.
|
|
@@ -234,6 +239,13 @@ export async function magentoPage(page) {
|
|
|
234
239
|
const drawn = html(cards[i]);
|
|
235
240
|
if (drawn.length) {
|
|
236
241
|
row.slot.innerHTML = drawn.join('');
|
|
242
|
+
// The title only over a real pair among the cards drawn: a product
|
|
243
|
+
// nobody bought with another yet gets similar ones, true cards under
|
|
244
|
+
// a false title, so they show untitled, as more of the row above.
|
|
245
|
+
// Removed, not hidden: a theme's .block-title rule beats [hidden].
|
|
246
|
+
if (row.together && !cards[i].slice(0, ROW).some(({ why }) => why === 'bought_together')) {
|
|
247
|
+
blockOf(row.slot).querySelector?.('.block-title')?.remove();
|
|
248
|
+
}
|
|
237
249
|
blockOf(row.slot).hidden = false;
|
|
238
250
|
}
|
|
239
251
|
});
|
package/src/shopify.js
CHANGED
|
@@ -317,13 +317,15 @@ export async function shopifyTheme(options) {
|
|
|
317
317
|
const fromProduct = (p, recommendationId) => (p?.available
|
|
318
318
|
? `<a href="${escape(path(p.url, recommendationId))}"><img src="${escape(image(p.featured_image))}" alt="" loading="lazy"><span>${escape(p.title)}</span>${money && Number.isFinite(p.price_min) ? tag(money.format(p.price_min / 100)) : ''}</a>`
|
|
319
319
|
: null);
|
|
320
|
-
// A list's cards, in recapi's order
|
|
321
|
-
// own page data, one request each.
|
|
322
|
-
const cardsOf = async ({ recommendationId, items }) => (await Promise.all(items.map(async ({ itemId, metadata }) => {
|
|
323
|
-
|
|
324
|
-
if (
|
|
325
|
-
|
|
326
|
-
|
|
320
|
+
// A list's cards, in recapi's order, each with its reason code. The
|
|
321
|
+
// others come from the product's own page data, one request each.
|
|
322
|
+
const cardsOf = async ({ recommendationId, items }) => (await Promise.all(items.map(async ({ itemId, metadata, why }) => {
|
|
323
|
+
let html = fromMetadata(metadata, recommendationId);
|
|
324
|
+
if (!html) {
|
|
325
|
+
const response = await fetch(`${root}products/${encodeURIComponent(itemId)}.js`).catch(() => null);
|
|
326
|
+
html = fromProduct(response?.ok ? await response.json().catch(() => null) : null, recommendationId);
|
|
327
|
+
}
|
|
328
|
+
return html && { html, why };
|
|
327
329
|
}))).filter(Boolean);
|
|
328
330
|
const lists = await ask(rows.map((_, row) => [row, ROW]), []);
|
|
329
331
|
const cards = await Promise.all(lists.map(cardsOf));
|
|
@@ -336,17 +338,30 @@ export async function shopifyTheme(options) {
|
|
|
336
338
|
// Refused (a key revoked mid-page): the rows keep the cards they have.
|
|
337
339
|
const more = await ask(short.map((row) => [row, ROW - cards[row].length]), lists.flatMap(({ items }) => items.map(({ itemId }) => itemId)))
|
|
338
340
|
.catch((error) => { console.warn('recapi:', error); return []; });
|
|
339
|
-
|
|
341
|
+
// A popular top-up (recapi slow or at a limit on this call) fills the
|
|
342
|
+
// first row's gap with what that row shows anyway when recapi is down,
|
|
343
|
+
// but under the bought-together title it would mislead: that row keeps
|
|
344
|
+
// the cards it has.
|
|
345
|
+
await Promise.all(more.map(async (list, k) => {
|
|
346
|
+
if (short[k] === 1 && list.fallback) return;
|
|
347
|
+
cards[short[k]] = [...cards[short[k]], ...await cardsOf(list)];
|
|
348
|
+
}));
|
|
340
349
|
}
|
|
341
350
|
const [first, second = []] = cards.map((row) => row.slice(0, ROW));
|
|
342
|
-
if (first.length) slot.innerHTML = style + first.join('');
|
|
351
|
+
if (first.length) slot.innerHTML = style + first.map(({ html }) => html).join('');
|
|
343
352
|
else hide();
|
|
344
353
|
// Each row shows or hides on its own. A popular fallback is not what was
|
|
345
354
|
// bought together, so that row then stays away. The theme's own h2 rule
|
|
346
355
|
// styles its title.
|
|
347
356
|
if (second.length && !lists[1].fallback) {
|
|
348
|
-
|
|
349
|
-
|
|
357
|
+
// The title only over a real pair among the cards drawn: a product
|
|
358
|
+
// nobody bought with another yet gets similar ones, true cards under a
|
|
359
|
+
// false title, so they show untitled, the first row's gap apart, and
|
|
360
|
+
// read as more of it.
|
|
361
|
+
const paired = second.some(({ why }) => why === 'bought_together');
|
|
362
|
+
const title = together && paired ? `<h2 class="recapi-heading">${escape(together)}</h2>` : '';
|
|
363
|
+
const more = paired ? '' : '<style>:where(.recapi-bought-together.recapi-more){margin-top:1rem}</style>';
|
|
364
|
+
slot.insertAdjacentHTML('afterend', `<div class="recapi-bought-together${wide ? ' page-width' : ''}${paired ? '' : ' recapi-more'}">${title}<div id="recapi-bought-together">${styleOf('recapi-bought-together')}${more}${second.map(({ html }) => html).join('')}</div></div>`);
|
|
350
365
|
}
|
|
351
366
|
};
|
|
352
367
|
// recommendLists() rejects only on a bad request: a page still loses no more than its rows.
|
package/src/woocommerce.js
CHANGED
|
@@ -259,8 +259,10 @@ export async function woocommercePage({ apiKey, baseUrl, platform, page }) {
|
|
|
259
259
|
// no translation.
|
|
260
260
|
const price = ({ prices: p }) => money(Number(p?.price) / 10 ** p?.currency_minor_unit, p?.currency_code);
|
|
261
261
|
// A list's cards, in recapi's order: those whose metadata draws them, the
|
|
262
|
-
// others from WooCommerce's Store API (public, one request for all).
|
|
262
|
+
// others from WooCommerce's Store API (public, one request for all). Each
|
|
263
|
+
// keeps its item's reason code.
|
|
263
264
|
const cardsOf = async ({ recommendationId, items }) => {
|
|
265
|
+
const reasons = new Map(items.map(({ itemId, why }) => [itemId, why]));
|
|
264
266
|
const cards = new Map(items.map(({ itemId, metadata }) => [itemId, fromMetadata(metadata, recommendationId)]));
|
|
265
267
|
const ids = [...cards].filter(([id, html]) => !html && /^\d+$/.test(id)).map(([id]) => id);
|
|
266
268
|
let products = [];
|
|
@@ -277,7 +279,7 @@ export async function woocommercePage({ apiKey, baseUrl, platform, page }) {
|
|
|
277
279
|
cards.set(String(p.id), card(href, p.images?.[0]?.thumbnail || p.images?.[0]?.src || '', text(p.name), price(p)));
|
|
278
280
|
}
|
|
279
281
|
}
|
|
280
|
-
return [...cards.
|
|
282
|
+
return [...cards].filter(([, html]) => html).map(([id, html]) => ({ html, why: reasons.get(id) }));
|
|
281
283
|
};
|
|
282
284
|
const lists = await ask(rows.map((row) => [row, ROW]), []);
|
|
283
285
|
const shown = await Promise.all(lists.map(cardsOf));
|
|
@@ -290,7 +292,11 @@ export async function woocommercePage({ apiKey, baseUrl, platform, page }) {
|
|
|
290
292
|
// cards the first call already found.
|
|
291
293
|
try {
|
|
292
294
|
const more = await ask(short.map((i) => [rows[i], ROW - shown[i].length]), lists.flatMap((list) => list.items.map(({ itemId }) => itemId)));
|
|
293
|
-
|
|
295
|
+
// A popular top-up (recapi slow or at a limit on this call) fills the
|
|
296
|
+
// first row's gap with what that row shows anyway when recapi is
|
|
297
|
+
// down, but under the bought-together title it would mislead: that
|
|
298
|
+
// row keeps the cards it has.
|
|
299
|
+
const cards = await Promise.all(more.map((list, k) => (short[k] === 1 && list.fallback ? [] : cardsOf(list))));
|
|
294
300
|
short.forEach((row, k) => shown[row].push(...cards[k]));
|
|
295
301
|
} catch (error) {
|
|
296
302
|
console.warn('recapi:', error);
|
|
@@ -304,10 +310,16 @@ export async function woocommercePage({ apiKey, baseUrl, platform, page }) {
|
|
|
304
310
|
// at a limit) is popular products, not what shoppers bought together:
|
|
305
311
|
// under that title it would mislead, so that row waits for the next page.
|
|
306
312
|
if (together?.length && !lists[1].fallback) {
|
|
307
|
-
|
|
313
|
+
// The title only over a real pair among the cards drawn: a product
|
|
314
|
+
// nobody bought with another yet gets similar ones, true cards under a
|
|
315
|
+
// false title, so they show untitled, the first row's gap apart, and
|
|
316
|
+
// read as more of it.
|
|
317
|
+
const paired = together.some(({ why }) => why === 'bought_together');
|
|
318
|
+
const more = paired ? '' : `<style>:where(#${TOGETHER}){margin-top:1rem}</style>`;
|
|
319
|
+
slot.insertAdjacentHTML('afterend', `${paired ? heading(rows[1].heading) : ''}<div id="${TOGETHER}">${style.replaceAll('recapi-recommendations', TOGETHER)}${more}${together.map(({ html }) => html).join('')}</div>`);
|
|
308
320
|
}
|
|
309
321
|
if (first.length) {
|
|
310
|
-
slot.innerHTML = style + first.join('');
|
|
322
|
+
slot.innerHTML = style + first.map(({ html }) => html).join('');
|
|
311
323
|
if (page.heading) slot.insertAdjacentHTML('beforebegin', heading(page.heading));
|
|
312
324
|
} else {
|
|
313
325
|
hide();
|