@websline/cms-view-utils 1.10.0 → 1.10.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/package.json +1 -1
- package/src/ads/triggers.js +18 -18
package/package.json
CHANGED
package/src/ads/triggers.js
CHANGED
|
@@ -39,30 +39,30 @@ const afterDelay = (seconds, fire, timers) => {
|
|
|
39
39
|
return () => timers.clearTimeout(id);
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
|
|
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
|
|
55
|
-
*
|
|
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
|
|
50
|
+
const SCROLL_DISTANCE_PX = 300;
|
|
58
51
|
|
|
59
52
|
/**
|
|
60
|
-
* Keeps listening until the
|
|
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
|
-
|
|
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,
|
|
159
|
+
export { countPageView, PAGE_VIEWS_KEY, SCROLL_DISTANCE_PX, watchTrigger };
|