@plusguard/reels 1.9.2 → 1.10.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 CHANGED
@@ -23,9 +23,9 @@ From npm, through jsDelivr — the npm package is the code alone (about
23
23
  jsDelivr's GitHub route.
24
24
 
25
25
  ```html
26
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.9.2/index.css" />
26
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/index.css" />
27
27
  <script type="module">
28
- import { initReels } from "https://cdn.jsdelivr.net/npm/@plusguard/reels@1.9.2/dist/index.js";
28
+ import { initReels } from "https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/dist/index.js";
29
29
  initReels();
30
30
  </script>
31
31
  ```
@@ -106,6 +106,21 @@ below).
106
106
  `data-pg-reels-open`: the full view, on that group and clip. First the
107
107
  event `pg-reels:open` on the section, with `detail.group` and
108
108
  `detail.clip`; `preventDefault()` on it and the site opens its own.
109
+ - **Watched.** A clip that has played **3 seconds** — or half of it, if it
110
+ is shorter than 6 — on a card or in the full view: the event
111
+ `pg-reels:watched` on the section, once a clip a page visit, with
112
+ `detail.group`, `detail.clip`, `detail.id` (the group's
113
+ `data-pg-reels-id`) and `detail.where` (`"card"` or `"full"`). The
114
+ package counts nothing: the site sends it on — to its own server, which
115
+ counts and leaves out the same visitor twice a day — and writes the
116
+ number back in `data-pg-reels-views`.
117
+
118
+ ```js
119
+ section.addEventListener("pg-reels:watched", (e) => {
120
+ const { id, clip, where } = e.detail;
121
+ navigator.sendBeacon("/api/reels/watched", JSON.stringify({ id, clip, where }));
122
+ });
123
+ ```
109
124
  - **Reduced motion:** nothing plays by itself; a bar pressed shows its clip.
110
125
  - **Seen.** A group is seen once **every one of its clips** has been shown
111
126
  in the full view — one clip watched, and it is not; the rest watched
@@ -139,7 +154,7 @@ classic script in `<head>` after the stylesheets, marks each group as the
139
154
  browser reads it, before anything is drawn:
140
155
 
141
156
  ```html
142
- <script src="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.9.2/early.js"></script>
157
+ <script src="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/early.js"></script>
143
158
  ```
144
159
 
145
160
  No dependencies, nothing to call; `initReels()` takes over after. It reads
@@ -249,7 +264,7 @@ and videos: the box has the shape, what is inside is 100% of it.
249
264
  | `data-pg-reels-strip` | the row | Scrolls sideways |
250
265
  | `data-pg-reels-group` | each group | A card and what is under it |
251
266
  | `data-pg-reels-id` | a group | Its name, for what was seen to be remembered in this browser |
252
- | `data-pg-reels-views` | in the card | A clip's view count, the site's — one a clip, in order, as the products: only the playing clip's shows, and it is copied into the full view, under the close. Only one in the card: the group's, always shown |
267
+ | `data-pg-reels-views` | in the card | A clip's view count, the site's (counted from `pg-reels:watched`) — one a clip, in order, as the products: only the playing clip's shows, and it is copied into the full view, under the close. Only one in the card: the group's, always shown |
253
268
  | `data-pg-reels-seen` | a group, the full view's head | Written: `"true"` once seen in the full view, `"false"` before |
254
269
  | `data-pg-reels-card` | the card | The shape; its clips fill it |
255
270
  | `data-pg-reels-clip` | each clip | A video with its poster, or a picture |
@@ -313,7 +328,7 @@ So in React they go in the clip, as an element kept hidden:
313
328
  Give its pictures `loading="lazy"`: hidden, they load when the full view
314
329
  shows them. See [`examples/react/Example.tsx`](examples/react/Example.tsx)
315
330
  — the section from data (variants and categories too), and
316
- `pg-reels:open` counted.
331
+ `pg-reels:open` and `pg-reels:watched` sent on to the site's server.
317
332
 
318
333
  ### The React example, running
319
334
 
package/dist/index.js CHANGED
@@ -47,6 +47,13 @@
47
47
  * the event pg-reels:open on the section (detail: group, clip); a site
48
48
  * that calls preventDefault() on it opens its own.
49
49
  *
50
+ * Watched: a clip that has played 3 seconds — or half of it, if it is
51
+ * shorter than 6 — on a card or in the full view, sends pg-reels:watched
52
+ * on the section (detail: group, clip, id — its group's data-pg-reels-id —
53
+ * and where: "card" or "full"), once a clip a page visit. The package only
54
+ * says so; counting, and the number shown in data-pg-reels-views, are the
55
+ * site's.
56
+ *
50
57
  * The full view — a <dialog>, built here on <body>, its contents copies:
51
58
  * the clip playing, the group's data-pg-reels-head, the clip's product.
52
59
  * Within a group: the bars on top, a press on the video's right or left,
@@ -340,6 +347,21 @@ export function forgetReelsSeen() {
340
347
  watched = new WeakMap();
341
348
  paintSeen();
342
349
  }
350
+ /* ---------- Watched, said to the site ----------
351
+ A clip that has played long enough: 3 seconds, or half of it if it is
352
+ shorter — once a clip a page visit. The site counts. */
353
+ const WATCHED_MS = 3000;
354
+ const told = new WeakMap();
355
+ function watchedIf(g, clip, playedMs, totalMs, where) {
356
+ if (!(totalMs > 0) || playedMs < Math.min(WATCHED_MS, totalMs / 2))
357
+ return;
358
+ const said = told.get(g.el) ?? new Set();
359
+ if (said.has(clip))
360
+ return;
361
+ said.add(clip);
362
+ told.set(g.el, said);
363
+ g.root.dispatchEvent(new CustomEvent("pg-reels:watched", { bubbles: true, detail: { group: g.el, clip, id: g.el.getAttribute(ID), where } }));
364
+ }
343
365
  // The clips of each group shown in the full view so far, this visit.
344
366
  let watched = new WeakMap();
345
367
  /* ---------- Everything on the page ---------- */
@@ -452,10 +474,16 @@ export function initReels() {
452
474
  const clip = g.clips[g.at];
453
475
  const v = videoOf(clip);
454
476
  let progress;
455
- if (v)
477
+ if (v) {
456
478
  progress = v.duration ? v.currentTime / v.duration : 0;
457
- else
458
- progress = (g.shown + now - g.since) / Math.max(1, pictureTime(g, clip));
479
+ watchedIf(g, g.at, v.currentTime * 1000, v.duration * 1000, "card");
480
+ }
481
+ else {
482
+ const time = pictureTime(g, clip);
483
+ const played = g.shown + now - g.since;
484
+ progress = played / Math.max(1, time);
485
+ watchedIf(g, g.at, played, time, "card");
486
+ }
459
487
  g.bars[g.at]?.style.setProperty(PROGRESS, String(Math.min(1, progress)));
460
488
  if (!v && progress >= 1)
461
489
  due.push(g);
@@ -844,7 +872,10 @@ export function initReels() {
844
872
  const g = current();
845
873
  const clip = g.clips[view.ci];
846
874
  const v = viewVideo();
847
- const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : (view.shown + now - view.since) / Math.max(1, pictureTime(g, clip));
875
+ const time = v ? v.duration * 1000 : pictureTime(g, clip);
876
+ const played = v ? v.currentTime * 1000 : view.shown + now - view.since;
877
+ const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : played / Math.max(1, time);
878
+ watchedIf(g, view.ci, played, time, "full");
848
879
  viewBars.children[view.ci]?.setAttribute("style", `${PROGRESS}: ${Math.min(1, progress)}`);
849
880
  if (!v && progress >= 1) {
850
881
  nextClip(true);
package/early.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * A classic script, in <head>, after the page's stylesheets and before its
10
10
  * markup — not deferred, not a module:
11
11
  *
12
- * <script src="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.9.2/early.js"></script>
12
+ * <script src="https://cdn.jsdelivr.net/npm/@plusguard/reels@1.10.0/early.js"></script>
13
13
  *
14
14
  * or its text written inline there. No dependencies; nothing to call. It
15
15
  * reads --pg-reels-seen and --pg-reels-seen-for from the stylesheets, as
package/index.ts CHANGED
@@ -47,6 +47,13 @@
47
47
  * the event pg-reels:open on the section (detail: group, clip); a site
48
48
  * that calls preventDefault() on it opens its own.
49
49
  *
50
+ * Watched: a clip that has played 3 seconds — or half of it, if it is
51
+ * shorter than 6 — on a card or in the full view, sends pg-reels:watched
52
+ * on the section (detail: group, clip, id — its group's data-pg-reels-id —
53
+ * and where: "card" or "full"), once a clip a page visit. The package only
54
+ * says so; counting, and the number shown in data-pg-reels-views, are the
55
+ * site's.
56
+ *
50
57
  * The full view — a <dialog>, built here on <body>, its contents copies:
51
58
  * the clip playing, the group's data-pg-reels-head, the clip's product.
52
59
  * Within a group: the bars on top, a press on the video's right or left,
@@ -366,6 +373,22 @@ export function forgetReelsSeen(): void {
366
373
  paintSeen();
367
374
  }
368
375
 
376
+ /* ---------- Watched, said to the site ----------
377
+ A clip that has played long enough: 3 seconds, or half of it if it is
378
+ shorter — once a clip a page visit. The site counts. */
379
+
380
+ const WATCHED_MS = 3000;
381
+ const told = new WeakMap<Element, Set<number>>();
382
+
383
+ function watchedIf(g: Group, clip: number, playedMs: number, totalMs: number, where: "card" | "full"): void {
384
+ if (!(totalMs > 0) || playedMs < Math.min(WATCHED_MS, totalMs / 2)) return;
385
+ const said = told.get(g.el) ?? new Set<number>();
386
+ if (said.has(clip)) return;
387
+ said.add(clip);
388
+ told.set(g.el, said);
389
+ g.root.dispatchEvent(new CustomEvent("pg-reels:watched", { bubbles: true, detail: { group: g.el, clip, id: g.el.getAttribute(ID), where } }));
390
+ }
391
+
369
392
  // The clips of each group shown in the full view so far, this visit.
370
393
  let watched = new WeakMap<Element, Set<number>>();
371
394
 
@@ -480,8 +503,15 @@ export function initReels(): () => void {
480
503
  const clip = g.clips[g.at];
481
504
  const v = videoOf(clip);
482
505
  let progress: number;
483
- if (v) progress = v.duration ? v.currentTime / v.duration : 0;
484
- else progress = (g.shown + now - g.since) / Math.max(1, pictureTime(g, clip));
506
+ if (v) {
507
+ progress = v.duration ? v.currentTime / v.duration : 0;
508
+ watchedIf(g, g.at, v.currentTime * 1000, v.duration * 1000, "card");
509
+ } else {
510
+ const time = pictureTime(g, clip);
511
+ const played = g.shown + now - g.since;
512
+ progress = played / Math.max(1, time);
513
+ watchedIf(g, g.at, played, time, "card");
514
+ }
485
515
  g.bars[g.at]?.style.setProperty(PROGRESS, String(Math.min(1, progress)));
486
516
  if (!v && progress >= 1) due.push(g);
487
517
  });
@@ -869,7 +899,10 @@ export function initReels(): () => void {
869
899
  const g = current();
870
900
  const clip = g.clips[view.ci];
871
901
  const v = viewVideo();
872
- const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : (view.shown + now - view.since) / Math.max(1, pictureTime(g, clip));
902
+ const time = v ? v.duration * 1000 : pictureTime(g, clip);
903
+ const played = v ? v.currentTime * 1000 : view.shown + now - view.since;
904
+ const progress = v ? (v.duration ? v.currentTime / v.duration : 0) : played / Math.max(1, time);
905
+ watchedIf(g, view.ci, played, time, "full");
873
906
  viewBars.children[view.ci]?.setAttribute("style", `${PROGRESS}: ${Math.min(1, progress)}`);
874
907
  if (!v && progress >= 1) {
875
908
  nextClip(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusguard/reels",
3
- "version": "1.9.2",
3
+ "version": "1.10.0",
4
4
  "description": "Shoppable reels for a store: a strip of video cards — each a group of clips, each clip with its own product — that opens into a full view. Up and down between groups, left and right within one, the product beside the video on a computer and under it on a phone. No dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",