@websline/cms-view-utils 1.10.0 → 1.10.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ads/triggers.js +18 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websline/cms-view-utils",
3
- "version": "1.10.0",
3
+ "version": "1.10.2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src",
@@ -39,30 +39,30 @@ const afterDelay = (seconds, fire, timers) => {
39
39
  return () => timers.clearTimeout(id);
40
40
  };
41
41
 
42
- /**
43
- * How far down the visitor is, in percent of what the page can scroll. A page
44
- * that fits the window cannot be scrolled and so never reaches any share.
45
- */
46
- const scrolledPercent = (target, doc) => {
47
- const scrollable =
48
- (doc?.documentElement?.scrollHeight ?? 0) - (target.innerHeight ?? 0);
49
-
50
- return scrollable > 0 ? (target.scrollY / scrollable) * 100 : 0;
51
- };
42
+ /** How far the page can scroll at all; zero for a page that fits the window. */
43
+ const scrollableHeight = (target, doc) =>
44
+ Math.max((doc?.documentElement?.scrollHeight ?? 0) - (target.innerHeight ?? 0), 0);
52
45
 
53
46
  /**
54
- * Fixed rather than set per ad: far enough to mean interest, early enough that
55
- * most visitors get there. Not offered in the CMS, since no editor tunes it.
47
+ * Fixed rather than set per ad, and in pixels rather than a share of the page: a
48
+ * share of a long page lies far down, so the ad would come late or not at all.
56
49
  */
57
- const SCROLL_SHARE_PERCENT = 30;
50
+ const SCROLL_DISTANCE_PX = 300;
58
51
 
59
52
  /**
60
- * Keeps listening until the share is reached: `once` would drop the listener on
61
- * an overscroll bounce back at the top and lose the ad for that visit.
53
+ * Keeps listening until the distance is reached: `once` would drop the listener
54
+ * on an overscroll bounce back at the top and lose the ad for that visit. A page
55
+ * shorter than the distance counts once scrolled to its end.
62
56
  */
63
57
  const onScrolledPast = (fire, target, doc) => {
64
- const reached = () =>
65
- target.scrollY > 0 && scrolledPercent(target, doc) >= SCROLL_SHARE_PERCENT;
58
+ const reached = () => {
59
+ const scrollable = scrollableHeight(target, doc);
60
+
61
+ // A pixel of slack: at the very end, zoomed pages report a fractional scrollY.
62
+ return (
63
+ scrollable > 0 && target.scrollY >= Math.min(SCROLL_DISTANCE_PX, scrollable) - 1
64
+ );
65
+ };
66
66
 
67
67
  // Already that far when the ad arrived — no further event may come.
68
68
  if (reached()) {
@@ -156,4 +156,4 @@ const watchTrigger = (trigger, onFire, env = {}) => {
156
156
  }
157
157
  };
158
158
 
159
- export { countPageView, PAGE_VIEWS_KEY, SCROLL_SHARE_PERCENT, watchTrigger };
159
+ export { countPageView, PAGE_VIEWS_KEY, SCROLL_DISTANCE_PX, watchTrigger };