@recapi/client 0.6.7 → 0.7.0
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 +38 -13
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/client.js +4 -2
- package/src/history.js +14 -6
package/README.md
CHANGED
|
@@ -35,9 +35,11 @@ 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.
|
|
38
|
+
import { createClient } from 'https://api.example.com/sdk/0.7.0/index.js';
|
|
39
39
|
|
|
40
|
-
const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com' });
|
|
40
|
+
const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com', consent: false });
|
|
41
|
+
// From your cookie banner, once the visitor agrees: recapi.setConsent(true).
|
|
42
|
+
// A site that needs no banner passes consent: true instead (see Consent).
|
|
41
43
|
recapi.track('item-42', 'view'); // and the calls of the quick start below
|
|
42
44
|
</script>
|
|
43
45
|
```
|
|
@@ -78,7 +80,8 @@ import { createClient } from '@recapi/client'; // or the /sdk/ URL above
|
|
|
78
80
|
const recapi = createClient({
|
|
79
81
|
apiKey: 'rk_pub_…', // public key only, never a secret key
|
|
80
82
|
baseUrl: 'https://api.example.com',
|
|
81
|
-
placement: 'homepage'
|
|
83
|
+
placement: 'homepage',
|
|
84
|
+
consent: false // until your cookie banner calls recapi.setConsent(true)
|
|
82
85
|
});
|
|
83
86
|
|
|
84
87
|
// The user opened an item: a product, an article, a video… (level 3).
|
|
@@ -411,8 +414,10 @@ A client is one visitor, like a browser tab: its history, user id and
|
|
|
411
414
|
attribution belong to whoever it was used for, and one isolate serves many
|
|
412
415
|
requests at once. Share a client only for lists that are no one's, such as
|
|
413
416
|
popular items, and make one per request for a visitor's events and
|
|
414
|
-
recommendations.
|
|
415
|
-
anonymous id
|
|
417
|
+
recommendations. Without consent, the default, a client draws no
|
|
418
|
+
anonymous id, so a client per request doesn't make a new visitor each time;
|
|
419
|
+
it sends the user id you give `identify()`. The recipe says `consent: false`
|
|
420
|
+
anyway, so the visitor's client reads as what it is.
|
|
416
421
|
|
|
417
422
|
```js
|
|
418
423
|
// Cloudflare Workers
|
|
@@ -527,7 +532,7 @@ for (const item of items) {
|
|
|
527
532
|
| `maxBatch` | `100` | events per request (max 1000); a full queue flushes at once |
|
|
528
533
|
| `timeoutMs` | `5000` | per-request timeout; `recommend()` waits 1.5 s at most |
|
|
529
534
|
| `historyConfig` | spec defaults | `{ halfLifeSeconds, topK, recentSize }`; must match the server |
|
|
530
|
-
| `consent` | `
|
|
535
|
+
| `consent` | `false` | storage consent: `false` stores nothing until `setConsent(true)`, see [Consent](#consent-and-eprivacy) |
|
|
531
536
|
| `respectDoNotTrack` | `true` | when DNT or Global Privacy Control is on: store nothing, send no events, see [Do Not Track](#do-not-track) |
|
|
532
537
|
| `onError` | `console.warn` for lost events and lists it answered with a fallback | gets errors from background work (flushes, storage, rejected events) and those `recommend()` and `popular()` fell back from |
|
|
533
538
|
| `now`, `fetch`, `sleep` | `Date.now`, global `fetch`, `setTimeout` | for tests |
|
|
@@ -785,16 +790,18 @@ so a batch already in flight when the user navigates away still arrives.
|
|
|
785
790
|
|
|
786
791
|
Under the EU ePrivacy Directive (art. 5(3)), storing data on the user's
|
|
787
792
|
device, or reading it back, generally needs prior consent unless it is
|
|
788
|
-
strictly necessary. A recommendation history usually is not.
|
|
789
|
-
|
|
793
|
+
strictly necessary. A recommendation history usually is not. So the SDK
|
|
794
|
+
stores nothing until it is told the visitor consented: `consent` defaults to
|
|
795
|
+
`false`. Wire it to your consent banner:
|
|
790
796
|
|
|
791
797
|
```js
|
|
792
798
|
const recapi = createClient({ apiKey, baseUrl, consent: cmp.hasConsent('personalisation') });
|
|
793
799
|
cmp.onChange((granted) => recapi.setConsent(granted));
|
|
794
800
|
```
|
|
795
801
|
|
|
796
|
-
|
|
797
|
-
|
|
802
|
+
A site that needs no banner (outside the EU, or with its own legal basis)
|
|
803
|
+
passes `consent: true`. Without consent, the SDK never reads or writes storage
|
|
804
|
+
and creates no anonymous id. The history and the attribution map live in memory for the
|
|
798
805
|
current page only. A single-page app keeps attribution. On a multi-page
|
|
799
806
|
site, a click that loads the item's page is not attributed unless you
|
|
800
807
|
pass the id yourself, for example in the link. A fragment (`#${recommendationId}`,
|
|
@@ -852,7 +859,9 @@ const storage = {
|
|
|
852
859
|
setItem: (k, v) => { cache.set(k, v); AsyncStorage.setItem(k, v); },
|
|
853
860
|
removeItem: (k) => { cache.delete(k); AsyncStorage.removeItem(k); }
|
|
854
861
|
};
|
|
855
|
-
|
|
862
|
+
// An app asks for consent as a site does: pass the user's answer, and call
|
|
863
|
+
// client.setConsent() when it changes (see Consent).
|
|
864
|
+
const client = createClient({ apiKey, baseUrl, project: 'myproject', storage, consent: true });
|
|
856
865
|
```
|
|
857
866
|
|
|
858
867
|
React Native has no page-hide event, so send the queue when the app leaves
|
|
@@ -868,14 +877,30 @@ key on a format change makes old data invisible.
|
|
|
868
877
|
## Size budget
|
|
869
878
|
|
|
870
879
|
Budget: **6 KB gzip** after minification. The sources ship unminified with
|
|
871
|
-
full JSDoc:
|
|
880
|
+
full JSDoc: 61 KB raw in five files, 21 KB as the API serves them (gzip -9),
|
|
872
881
|
and the browser caches them. With comments stripped and no
|
|
873
|
-
minification they are
|
|
882
|
+
minification they are 9 KB gzip. A bundler's minifier should bring
|
|
874
883
|
that under the budget, but this has not been measured: the repo has no
|
|
875
884
|
minifier, to keep zero dev dependencies.
|
|
876
885
|
|
|
877
886
|
## Changes
|
|
878
887
|
|
|
888
|
+
- 2026-11-11: version 0.7.0. `consent` defaults to `false`: a client stores
|
|
889
|
+
nothing on the device and sends no `anon_id` until `consent: true` or
|
|
890
|
+
`setConsent(true)`, so a tag pasted as is keeps the promise that our script
|
|
891
|
+
stores nothing until the visitor consents. Before, it defaulted to `true`.
|
|
892
|
+
A site that wires no banner keeps the history for the current page only,
|
|
893
|
+
and a click that opens a new page goes uncredited, until it adds the line
|
|
894
|
+
([Consent](#consent-and-eprivacy)). Older versions' paths redirect to this
|
|
895
|
+
one, so tags pasted earlier get it too. The Shopify snippet, the WooCommerce
|
|
896
|
+
plugin and the Magento module pass their banner's answer and are unchanged.
|
|
897
|
+
The Shopify pixel, which runs only with Analytics consent, now passes
|
|
898
|
+
`consent: true`: a pixel pasted before this version credits purchases
|
|
899
|
+
without the visitor's id until it is pasted again from the quick start.
|
|
900
|
+
- 2026-11-07: version 0.6.8. A saved time ahead of the clock reads as now and a
|
|
901
|
+
damaged weight is sent as the API's limit (1e6). A device whose clock ran months
|
|
902
|
+
ahead and was then corrected used to get a 400 on every list until the entry had
|
|
903
|
+
decayed back under that limit.
|
|
879
904
|
- 2026-11-07: version 0.6.7. One damaged shown entry in the browser's storage
|
|
880
905
|
costs that item's click credit; it used to lose the visitor's id and history.
|
|
881
906
|
An entry whose time isn't a whole number is now damaged too.
|
package/index.d.ts
CHANGED
|
@@ -76,7 +76,7 @@ export interface ClientOptions {
|
|
|
76
76
|
/** Per-request timeout, default 5000; `recommend()` waits at most 1500. */
|
|
77
77
|
timeoutMs?: number;
|
|
78
78
|
historyConfig?: Partial<HistoryConfig>;
|
|
79
|
-
/** Initial storage consent, default `true`. */
|
|
79
|
+
/** Initial storage consent, default `false`: nothing is stored until `true` or `setConsent(true)`. */
|
|
80
80
|
consent?: boolean;
|
|
81
81
|
/**
|
|
82
82
|
* Default `true`: when Do Not Track / Global Privacy Control is on, store
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -82,7 +82,7 @@ const PUBLIC_KEY_PREFIX = 'rk_pub_';
|
|
|
82
82
|
* @property {number} [maxBatch]
|
|
83
83
|
* @property {number} [timeoutMs] Per-request timeout.
|
|
84
84
|
* @property {Partial<HistoryConfig>} [historyConfig]
|
|
85
|
-
* @property {boolean} [consent] Initial storage consent (default `
|
|
85
|
+
* @property {boolean} [consent] Initial storage consent (default `false`).
|
|
86
86
|
* @property {boolean} [respectDoNotTrack] When the browser sends Do Not
|
|
87
87
|
* Track or Global Privacy Control (default `true`): store nothing, send no
|
|
88
88
|
* events and no visitor ids; recommendations are still asked for.
|
|
@@ -179,7 +179,9 @@ export function createClient(options) {
|
|
|
179
179
|
// the visitor's standing one. `respectDoNotTrack: false` is for a site with
|
|
180
180
|
// its own legal basis.
|
|
181
181
|
const quiet = (options.respectDoNotTrack ?? true) && doNotTrackEnabled();
|
|
182
|
-
|
|
182
|
+
// Without an answer from the site's banner the client stores nothing, so a
|
|
183
|
+
// snippet pasted as is keeps the homepage's promise (ePrivacy art. 5(3)).
|
|
184
|
+
let consent = !quiet && (options.consent ?? false);
|
|
183
185
|
|
|
184
186
|
/** @type {HistoryState} */
|
|
185
187
|
let history = emptyHistory();
|
package/src/history.js
CHANGED
|
@@ -64,6 +64,9 @@ export const DEFAULT_CONFIG = Object.freeze({
|
|
|
64
64
|
*/
|
|
65
65
|
export const REJECTED_SIZE = 200;
|
|
66
66
|
|
|
67
|
+
/** The API answers 400 above this, which fails the list: a damaged score is sent as this. */
|
|
68
|
+
const MAX_REQUEST_WEIGHT = 1e6;
|
|
69
|
+
|
|
67
70
|
/**
|
|
68
71
|
* Fills missing fields with spec defaults so callers can override a single
|
|
69
72
|
* parameter (e.g. a shorter half-life for news sites).
|
|
@@ -105,7 +108,8 @@ function decay(config, dtMs) {
|
|
|
105
108
|
}
|
|
106
109
|
|
|
107
110
|
/**
|
|
108
|
-
* Weight of an entry decayed to time `T`.
|
|
111
|
+
* Weight of an entry decayed to time `T`. A time ahead of `T` reads as `T`: a
|
|
112
|
+
* clock that ran ahead must not grow the weight past what the API takes.
|
|
109
113
|
*
|
|
110
114
|
* @param {TopEntry} entry
|
|
111
115
|
* @param {number} T Unix ms.
|
|
@@ -113,7 +117,7 @@ function decay(config, dtMs) {
|
|
|
113
117
|
* @returns {number}
|
|
114
118
|
*/
|
|
115
119
|
export function weightAt(entry, T, config) {
|
|
116
|
-
return entry.s * decay(config, T - entry.t);
|
|
120
|
+
return entry.s * decay(config, Math.max(0, T - entry.t));
|
|
117
121
|
}
|
|
118
122
|
|
|
119
123
|
/**
|
|
@@ -154,7 +158,11 @@ function compareIds(a, b) {
|
|
|
154
158
|
*/
|
|
155
159
|
function prune(top, T, config) {
|
|
156
160
|
if (top.size <= config.topK) return top;
|
|
157
|
-
|
|
161
|
+
// Read at the newest entry when an event arrives out of order: at the old
|
|
162
|
+
// event's time rank() would flatten the newer entries to their score.
|
|
163
|
+
let at = T;
|
|
164
|
+
for (const entry of top.values()) at = Math.max(at, entry.t);
|
|
165
|
+
const kept = rank(top, at, config).slice(0, config.topK);
|
|
158
166
|
return new Map(kept.map(({ item }) => [item, top.get(item)]));
|
|
159
167
|
}
|
|
160
168
|
|
|
@@ -250,8 +258,8 @@ export function applyEvent(state, { item, level, t }, config) {
|
|
|
250
258
|
}
|
|
251
259
|
|
|
252
260
|
/**
|
|
253
|
-
* Wire format of a recommendation request: weights decayed to `T
|
|
254
|
-
* rounded to 4 decimals, integer age in seconds (clamped at 0 so a skewed
|
|
261
|
+
* Wire format of a recommendation request: weights decayed to `T`, at most
|
|
262
|
+
* what the API accepts, and rounded to 4 decimals, integer age in seconds (clamped at 0 so a skewed
|
|
255
263
|
* clock never yields negative ages). `rejected` (oldest mark first) is sent
|
|
256
264
|
* in the request's `exclude`.
|
|
257
265
|
*
|
|
@@ -265,7 +273,7 @@ export function toWire(state, T, config) {
|
|
|
265
273
|
.slice(0, config.topK)
|
|
266
274
|
.map(({ item, weight, t }) => /** @type {WireEntry} */ ([
|
|
267
275
|
item,
|
|
268
|
-
Math.round(weight * 1e4) / 1e4,
|
|
276
|
+
Math.round(Math.min(weight, MAX_REQUEST_WEIGHT) * 1e4) / 1e4,
|
|
269
277
|
Math.max(0, Math.floor((T - t) / 1000))
|
|
270
278
|
]));
|
|
271
279
|
const recent = state.recent.slice(-config.recentSize).map((entry) => entry.item);
|