Skip to content

Commit 632b0aa

Browse files
authored
[MM] Add new configuration for enabling on scroll (#98)
1 parent 66f468f commit 632b0aa

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

docs/error.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Example:
1818
});
1919
```
2020

21+
## Error 3
22+
Description: Sizes must be of type `Array` unless breakpoints have been specified
23+
24+
2125
## Error 2
2226
Description: The Plugin or Network has not been included in your bundle.
2327
Please manually include the script tag associated with this plugin or network.
@@ -38,10 +42,6 @@ Example:
3842
</script>
3943
```
4044

41-
## Error 3
42-
Description: Sizes must be of type `Array` unless breakpoints have been specified
43-
44-
4545
## Error 4
4646
Description: An ad must be passed into the GenericPlugin class. If your Plugin inherits from GenericPlugin
4747
and overrides the constructor make sure you are calling "super" and that you are passing in an

docs/lazy-load-plugin.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The Auto Render Plugin adds two options to ad instantiation
6767
|---|---|---|
6868
|autoRender|false|When set to true, AdJS will automatically render the ad when it enters the viewport (plus the offset provided if any)|
6969
|renderOffset|0|A measurement in pixels or percentage that AdJS will use to determine how far away from the viewport to load the ad|
70+
|enableByScroll|false|If enabled, offsets which evaluate to in view on load will not allow execution until user scrolls|
7071

7172
## Examples
7273

@@ -83,6 +84,7 @@ const page = new AdJS.Page(Network, {
8384
defaults: {
8485
autoLoad: true,
8586
renderOffset: 0,
87+
enableByScroll: false,
8688
}
8789
});
8890
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "adjs",
3-
"version": "2.0.0-beta.23",
3+
"version": "2.0.0-beta.24",
44
"description": "Ad Library to simplify and optimize integration with ad networks such as DFP",
55
"main": "./core.js",
66
"types": "./types.d.ts",

src/plugins/AutoRender.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ class AutoRender extends GenericPlugin {
99
return;
1010
}
1111

12-
const { container, configuration, el } = this.ad;
12+
const {
13+
container,
14+
configuration: { renderOffset, offset, enableByScroll },
15+
el: { id } } = this.ad;
1316

14-
const renderOffset = configuration.renderOffset || configuration.offset || 0;
17+
const finalOffset = renderOffset || offset || 0;
1518

16-
ScrollMonitor.subscribe(el.id, container, renderOffset, this.onEnterViewport);
19+
ScrollMonitor.subscribe(id, container, finalOffset, this.onEnterViewport, undefined, undefined, enableByScroll);
1720
dispatchEvent(this.ad.id, LOG_LEVELS.INFO, 'AutoRender Plugin', `Ad's scroll monitor has been created.`);
1821
}
1922

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export interface IAdConfiguration {
164164
autoRefresh?: boolean;
165165
autoRender?: boolean;
166166
sticky?: boolean;
167+
enableByScroll?: boolean;
167168

168169
breakpoints?: IAdBreakpoints;
169170
offset?: number;
@@ -184,6 +185,8 @@ export interface IScrollMonitorRegisteredAd {
184185
offset: number;
185186
inView: boolean;
186187
fullyInView: boolean;
188+
enableByScroll?: boolean;
189+
hasViewBeenScrolled: boolean;
187190
onEnterViewport: any[];
188191
onFullyEnterViewport: any[];
189192
onExitViewport: any[];

src/utils/scrollMonitor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ScrollMonitor {
2525
onEnterViewport?: () => any,
2626
onFullyEnterViewport?: () => any,
2727
onExitViewport?: () => any,
28+
enableByScroll?: boolean,
2829
) => {
2930
ScrollMonitor.monitorScroll();
3031

@@ -51,6 +52,8 @@ class ScrollMonitor {
5152
offset,
5253
inView: false,
5354
fullyInView: false,
55+
enableByScroll,
56+
hasViewBeenScrolled: false,
5457
onEnterViewport: onEnterViewport ? [onEnterViewport] : [],
5558
onFullyEnterViewport: onFullyEnterViewport ? [onFullyEnterViewport] : [],
5659
onExitViewport: onExitViewport ? [onExitViewport] : [],
@@ -80,12 +83,17 @@ class ScrollMonitor {
8083
const windowHeight = window.innerHeight;
8184

8285
Object.entries(ScrollMonitor.registeredAds).forEach(([key, ad]) => {
86+
ad.hasViewBeenScrolled = true;
8387
ScrollMonitor.evaulateCurrentViewability(ad, windowHeight);
8488
});
8589

8690
}, ScrollMonitor.throttleDuration)
8791

8892
private static evaulateCurrentViewability = (ad: IScrollMonitorRegisteredAd, windowHeight: number) => {
93+
if (ad.enableByScroll && !ad.hasViewBeenScrolled) {
94+
return;
95+
}
96+
8997
const bounding = ad.element.getBoundingClientRect();
9098

9199
const inView = (bounding.top - ad.offset) <= windowHeight && (bounding.top + bounding.height) >= 0;

0 commit comments

Comments
 (0)