@ethicly/react-native 0.3.0 → 0.4.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/CHANGELOG.md +59 -0
- package/README.md +112 -13
- package/lib/commonjs/core/cache.js +2 -0
- package/lib/commonjs/core/client.js +168 -53
- package/lib/commonjs/core/config.js +4 -1
- package/lib/commonjs/core/houseTracker.js +92 -0
- package/lib/commonjs/react/EthiclyInterstitial.js +23 -4
- package/lib/commonjs/react/useEthiclyInterstitial.js +22 -6
- package/lib/commonjs/version.js +1 -1
- package/lib/module/core/cache.js +2 -0
- package/lib/module/core/client.js +168 -53
- package/lib/module/core/config.js +4 -1
- package/lib/module/core/houseTracker.js +88 -0
- package/lib/module/react/EthiclyInterstitial.js +23 -4
- package/lib/module/react/useEthiclyInterstitial.js +22 -6
- package/lib/module/version.js +1 -1
- package/lib/typescript/core/cache.d.ts +3 -1
- package/lib/typescript/core/client.d.ts +24 -4
- package/lib/typescript/core/houseTracker.d.ts +53 -0
- package/lib/typescript/core/index.d.ts +1 -1
- package/lib/typescript/core/types.d.ts +44 -2
- package/lib/typescript/react/EthiclyInterstitial.d.ts +7 -0
- package/lib/typescript/react/useEthiclyInterstitial.d.ts +16 -2
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/core/cache.ts +4 -2
- package/src/core/client.ts +186 -55
- package/src/core/config.ts +4 -1
- package/src/core/houseTracker.ts +108 -0
- package/src/core/index.ts +1 -1
- package/src/core/types.ts +44 -2
- package/src/react/EthiclyInterstitial.tsx +26 -4
- package/src/react/useEthiclyInterstitial.ts +38 -8
- package/src/version.ts +1 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// The tracker for a house ad: the publisher's own creative, shown when no paid
|
|
2
|
+
// ad fills. Nothing is billed, so there is no /impression, /viewable, video
|
|
3
|
+
// event or click token: only the unbilled /fallback-impression and
|
|
4
|
+
// /fallback-click counters, fire-and-forget, and none of them in test mode.
|
|
5
|
+
import { BEACON_TIMEOUT_MS } from './beacons';
|
|
6
|
+
import { isWebUrl } from './click';
|
|
7
|
+
import { httpRequest } from './http';
|
|
8
|
+
export class HouseTracker {
|
|
9
|
+
constructor(d) {
|
|
10
|
+
this.d = d;
|
|
11
|
+
this.shownOnce = false;
|
|
12
|
+
this.clickedOnce = false;
|
|
13
|
+
this.pending = new Set();
|
|
14
|
+
this.houseAdPress = d.onPress;
|
|
15
|
+
}
|
|
16
|
+
/** Resolves when every counter in flight has settled. For tests. */
|
|
17
|
+
async settle() {
|
|
18
|
+
while (this.pending.size)
|
|
19
|
+
await Promise.all(Array.from(this.pending)).catch(() => undefined);
|
|
20
|
+
}
|
|
21
|
+
isExpired() {
|
|
22
|
+
return this.d.ctx.now() >= this.d.ad.expiresAt;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One POST to a house-ad counter, never retried, never awaited by the
|
|
26
|
+
* caller. The answer (200, 400 or 429) changes nothing. Never rejects.
|
|
27
|
+
*/
|
|
28
|
+
send(route) {
|
|
29
|
+
const { ctx } = this.d;
|
|
30
|
+
if (this.d.test)
|
|
31
|
+
return;
|
|
32
|
+
const p = httpRequest(ctx.fetch, `${ctx.serveBase()}/${route}`, {
|
|
33
|
+
method: 'POST', headers: ctx.headers(), body: { fallback_id: this.d.fallbackId }, timeoutMs: BEACON_TIMEOUT_MS,
|
|
34
|
+
}).then((res) => {
|
|
35
|
+
if (res.status !== 200)
|
|
36
|
+
ctx.log.debug(`/${route} answered ${res.status || 'nothing'}`);
|
|
37
|
+
}).catch((e) => ctx.log.debug(`/${route} failed`, e));
|
|
38
|
+
this.pending.add(p);
|
|
39
|
+
p.finally(() => this.pending.delete(p)).catch(() => undefined);
|
|
40
|
+
}
|
|
41
|
+
shown() {
|
|
42
|
+
if (this.shownOnce)
|
|
43
|
+
return Promise.resolve();
|
|
44
|
+
this.shownOnce = true;
|
|
45
|
+
if (this.isExpired()) {
|
|
46
|
+
this.d.ctx.log.error(`house ad ${this.d.fallbackId} expired before it was shown; load a new one`);
|
|
47
|
+
return Promise.resolve();
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
this.d.onShown();
|
|
51
|
+
}
|
|
52
|
+
catch { /* never throw */ }
|
|
53
|
+
this.send('fallback-impression');
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
}
|
|
56
|
+
progress(_positionMs, _opts) { }
|
|
57
|
+
complete() { }
|
|
58
|
+
skipped(_positionMs) { }
|
|
59
|
+
error() { }
|
|
60
|
+
/**
|
|
61
|
+
* Counts the tap once (fire-and-forget) and opens the destination right
|
|
62
|
+
* away. `{ open: false }` only counts it: the app shows its own screen.
|
|
63
|
+
*/
|
|
64
|
+
async clicked(opts) {
|
|
65
|
+
const url = this.d.ad.destinationUrl;
|
|
66
|
+
if (!this.clickedOnce) {
|
|
67
|
+
this.clickedOnce = true;
|
|
68
|
+
this.send('fallback-click');
|
|
69
|
+
}
|
|
70
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.open) === false)
|
|
71
|
+
return url;
|
|
72
|
+
if (!isWebUrl(url)) {
|
|
73
|
+
this.d.ctx.log.error(`house ad ${this.d.fallbackId}: destination is not an http(s) URL, not opened`);
|
|
74
|
+
return url;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
if (this.d.openUrl)
|
|
78
|
+
await this.d.openUrl(url);
|
|
79
|
+
else
|
|
80
|
+
this.d.ctx.log.warn('no openUrl adapter: the host must open the returned URL');
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
this.d.ctx.log.error('openUrl failed', e);
|
|
84
|
+
}
|
|
85
|
+
return url;
|
|
86
|
+
}
|
|
87
|
+
async closed() { }
|
|
88
|
+
}
|
|
@@ -2,7 +2,9 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
2
2
|
// Optional ready-made fullscreen screen. Needs expo-video (Expo SDK 52+).
|
|
3
3
|
// Layout follows a typical in-app video player: 9:16 video, a skip control that counts
|
|
4
4
|
// down then reads "Skip Ad", a white call-to-action button, an optional
|
|
5
|
-
// "Remove ads" slot and a small "Sponsored by Ethicly.ch" line under it.
|
|
5
|
+
// "Remove ads" slot and a small "Sponsored by Ethicly.ch" line under it. The
|
|
6
|
+
// line follows ad.showSponsorLabel: hidden for house ads, and for paid ads when
|
|
7
|
+
// the publisher switched it off in the dashboard.
|
|
6
8
|
// Video starts with sound unless the effective audio (the `audio` prop, else
|
|
7
9
|
// ad.audio: createEthicly's `audio` option, else /config) is 'none' or 'optional'.
|
|
8
10
|
// The controls keep clear of the notch, status bar and home indicator: see
|
|
@@ -96,7 +98,7 @@ function useAppActive() {
|
|
|
96
98
|
}, []);
|
|
97
99
|
return active;
|
|
98
100
|
}
|
|
99
|
-
function InterstitialBody({ ad, tracker, onClose, onRemoveAds, removeAdsLabel, renderBelowButton, skipStyle, skipTextStyle, insets, backRef, audio: audioProp, }) {
|
|
101
|
+
function InterstitialBody({ ad, tracker, onClose, onRemoveAds, removeAdsLabel, onHouseAdPress, renderBelowButton, skipStyle, skipTextStyle, insets, backRef, audio: audioProp, }) {
|
|
100
102
|
const audio = effectiveAudio(audioProp, ad);
|
|
101
103
|
const safe = useSafeInsets(insets);
|
|
102
104
|
const isVideo = ad.media.kind === 'video' && !!ad.media.videoUri;
|
|
@@ -225,13 +227,30 @@ function InterstitialBody({ ad, tracker, onClose, onRemoveAds, removeAdsLabel, r
|
|
|
225
227
|
onClose();
|
|
226
228
|
}, [ended, failed, isVideo, onClose, tracker]);
|
|
227
229
|
const onCta = useCallback(() => {
|
|
230
|
+
// A house ad the app routes to its own screen: count the tap, close the
|
|
231
|
+
// ad the normal way, then hand over. Never for a paid ad.
|
|
232
|
+
const housePress = ad.house
|
|
233
|
+
? onHouseAdPress || tracker.houseAdPress
|
|
234
|
+
: undefined;
|
|
235
|
+
if (housePress) {
|
|
236
|
+
// A second tap before the screen unmounts must not run the handler twice.
|
|
237
|
+
if (closedRef.current)
|
|
238
|
+
return;
|
|
239
|
+
void tracker.clicked({ open: false });
|
|
240
|
+
close();
|
|
241
|
+
try {
|
|
242
|
+
housePress(ad);
|
|
243
|
+
}
|
|
244
|
+
catch { /* the app's handler must not break the screen */ }
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
228
247
|
if (isVideo)
|
|
229
248
|
player.pause();
|
|
230
249
|
// Once the user has visited the advertiser, they can always close the ad
|
|
231
250
|
// when they come back.
|
|
232
251
|
setClicked(true);
|
|
233
252
|
void tracker.clicked();
|
|
234
|
-
}, [isVideo, player, tracker]);
|
|
253
|
+
}, [ad, close, isVideo, onHouseAdPress, player, tracker]);
|
|
235
254
|
const toggleSound = useCallback(() => {
|
|
236
255
|
setMuted((m) => {
|
|
237
256
|
player.muted = !m;
|
|
@@ -243,7 +262,7 @@ function InterstitialBody({ ad, tracker, onClose, onRemoveAds, removeAdsLabel, r
|
|
|
243
262
|
backRef.current = canSkip ? close : null;
|
|
244
263
|
return (_jsxs(View, { style: styles.root, children: [isVideo ? (_jsxs(_Fragment, { children: [ad.media.posterUri ? _jsx(Image, { source: { uri: ad.media.posterUri }, style: StyleSheet.absoluteFill, resizeMode: "cover" }) : null, _jsx(VideoView, { player: player, style: StyleSheet.absoluteFill, contentFit: "cover", nativeControls: false, allowsPictureInPicture: false })] })) : (_jsx(Image, { source: { uri: ad.media.uri }, style: StyleSheet.absoluteFill, resizeMode: "cover", onLoad: onImageLoad, onError: onImageError })), _jsxs(View, { style: [styles.topRow, { top: Math.max(safe.top + TOP_GAP, MIN_TOP), left: safe.left + SIDE_GAP_TOP, right: safe.right + SIDE_GAP_TOP }], pointerEvents: "box-none", children: [_jsxs(View, { style: styles.leftGroup, children: [_jsx(Text, { style: styles.adLabel, accessibilityRole: "text", children: "Ad" }), ad.test ? _jsx(Text, { style: [styles.adLabel, styles.testLabel], accessibilityRole: "text", children: "Test ad" }) : null, isVideo && audio !== 'none' ? (_jsx(Pressable, { onPress: toggleSound, style: styles.soundButton, accessibilityRole: "button", accessibilityLabel: muted ? 'Turn sound on' : 'Mute', hitSlop: 8, children: _jsx(Text, { style: styles.soundText, children: muted ? 'Sound off' : 'Sound on' }) })) : null] }), _jsx(Pressable, { onPress: canSkip ? close : undefined, disabled: !canSkip, style: [styles.skip, skipStyle], accessibilityRole: "button", accessibilityLabel: canSkip ? 'Skip ad' : `Skip in ${remainingS} seconds`, hitSlop: 8, children: canSkip ? (_jsxs(View, { style: styles.skipRow, children: [_jsx(Text, { style: [styles.skipText, skipTextStyle], children: "Skip Ad" }), _jsx(SkipIcon, {})] })) : (_jsx(Text, { style: [styles.skipText, styles.skipTextWaiting, skipTextStyle], children: `Skip in ${remainingS}` })) })] }), _jsxs(View, { style: [styles.bottom, {
|
|
245
264
|
bottom: Math.max(safe.bottom + BOTTOM_GAP, MIN_BOTTOM), left: safe.left + SIDE_GAP_BOTTOM, right: safe.right + SIDE_GAP_BOTTOM,
|
|
246
|
-
}], pointerEvents: "box-none", children: [_jsx(Pressable, { onPress: onCta, style: ({ pressed }) => [styles.cta, pressed && styles.ctaPressed], accessibilityRole: "link", children: _jsx(Text, { style: styles.ctaText, numberOfLines: 1, children: ad.ctaLabel || 'Learn more' }) }), renderBelowButton ? renderBelowButton() : onRemoveAds ? (_jsx(Pressable, { onPress: onRemoveAds, accessibilityRole: "button", hitSlop: 8, children: _jsx(Text, { style: styles.removeAds, children: removeAdsLabel || 'Remove ads' }) })) : null, _jsx(Text, { style: styles.sponsor, children: "Sponsored by Ethicly.ch" })] })] }));
|
|
265
|
+
}], pointerEvents: "box-none", children: [_jsx(Pressable, { onPress: onCta, style: ({ pressed }) => [styles.cta, pressed && styles.ctaPressed], accessibilityRole: "link", children: _jsx(Text, { style: styles.ctaText, numberOfLines: 1, children: ad.ctaLabel || 'Learn more' }) }), renderBelowButton ? renderBelowButton() : onRemoveAds ? (_jsx(Pressable, { onPress: onRemoveAds, accessibilityRole: "button", hitSlop: 8, children: _jsx(Text, { style: styles.removeAds, children: removeAdsLabel || 'Remove ads' }) })) : null, ad.showSponsorLabel ? _jsx(Text, { style: styles.sponsor, children: "Sponsored by Ethicly.ch" }) : null] })] }));
|
|
247
266
|
}
|
|
248
267
|
/** Skip-to-end mark (a play triangle against a bar), drawn so it never depends on a font glyph. */
|
|
249
268
|
function SkipIcon() {
|
|
@@ -4,6 +4,9 @@ import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
4
4
|
export function useEthiclyInterstitial(ethicly, options = {}) {
|
|
5
5
|
var _a, _b;
|
|
6
6
|
const preload = options.preload !== false;
|
|
7
|
+
const houseAds = options.houseAds;
|
|
8
|
+
const housePressRef = useRef(options.onHouseAdPress);
|
|
9
|
+
housePressRef.current = options.onHouseAdPress;
|
|
7
10
|
const [current, setCurrent] = useState(null);
|
|
8
11
|
const [reason, setReason] = useState(null);
|
|
9
12
|
const [loading, setLoading] = useState(false);
|
|
@@ -13,20 +16,33 @@ export function useEthiclyInterstitial(ethicly, options = {}) {
|
|
|
13
16
|
useEffect(() => {
|
|
14
17
|
mounted.current = true;
|
|
15
18
|
if (preload)
|
|
16
|
-
void ethicly.preload();
|
|
19
|
+
void ethicly.preload({ houseAds });
|
|
17
20
|
return () => {
|
|
18
21
|
var _a;
|
|
19
22
|
mounted.current = false;
|
|
20
23
|
void ((_a = currentRef.current) === null || _a === void 0 ? void 0 : _a.tracker.closed());
|
|
21
24
|
};
|
|
22
|
-
}, [ethicly, preload]);
|
|
25
|
+
}, [ethicly, preload, houseAds]);
|
|
23
26
|
const show = useCallback(async () => {
|
|
24
27
|
setLoading(true);
|
|
25
28
|
try {
|
|
26
|
-
const r = await ethicly.loadInterstitial();
|
|
29
|
+
const r = await ethicly.loadInterstitial({ houseAds });
|
|
27
30
|
if (!mounted.current)
|
|
28
31
|
return false;
|
|
29
32
|
if (r.ad) {
|
|
33
|
+
if (r.ad.house) {
|
|
34
|
+
// Read at tap time: the hook's latest handler wins, else the client's
|
|
35
|
+
// (set when the ad was built), else the link opens. A handler added or
|
|
36
|
+
// removed after the ad loaded is respected.
|
|
37
|
+
const t = r.tracker;
|
|
38
|
+
const clientPress = t.houseAdPress;
|
|
39
|
+
Object.defineProperty(t, 'houseAdPress', {
|
|
40
|
+
configurable: true,
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: () => { var _a; return (_a = housePressRef.current) !== null && _a !== void 0 ? _a : clientPress; },
|
|
43
|
+
set: () => { },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
30
46
|
setCurrent({ ad: r.ad, tracker: r.tracker });
|
|
31
47
|
setReason(null);
|
|
32
48
|
return true;
|
|
@@ -38,14 +54,14 @@ export function useEthiclyInterstitial(ethicly, options = {}) {
|
|
|
38
54
|
if (mounted.current)
|
|
39
55
|
setLoading(false);
|
|
40
56
|
}
|
|
41
|
-
}, [ethicly]);
|
|
57
|
+
}, [ethicly, houseAds]);
|
|
42
58
|
const dismiss = useCallback(() => {
|
|
43
59
|
var _a;
|
|
44
60
|
void ((_a = currentRef.current) === null || _a === void 0 ? void 0 : _a.tracker.closed());
|
|
45
61
|
setCurrent(null);
|
|
46
62
|
if (preload)
|
|
47
|
-
void ethicly.preload();
|
|
48
|
-
}, [ethicly, preload]);
|
|
63
|
+
void ethicly.preload({ houseAds });
|
|
64
|
+
}, [ethicly, preload, houseAds]);
|
|
49
65
|
const canShow = useCallback(() => ethicly.canShow(), [ethicly]);
|
|
50
66
|
const nextAdStatus = useCallback(() => ethicly.nextAdStatus(), [ethicly]);
|
|
51
67
|
return {
|
package/lib/module/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** SDK version, sent as sdk_version and in X-Ethicly-SDK. Keep in step with package.json. */
|
|
2
|
-
export const SDK_VERSION = '0.
|
|
2
|
+
export const SDK_VERSION = '0.4.0';
|
|
@@ -59,8 +59,10 @@ export declare class CreativeCache {
|
|
|
59
59
|
* `allowed()` is checked before every download so setAdsEnabled(false)
|
|
60
60
|
* stops a download that has not started yet; cancel() also stops the one in
|
|
61
61
|
* flight when the adapter supports it.
|
|
62
|
+
* `adId` keys the entry and names the files: house ads pass `house-<id>`
|
|
63
|
+
* because their ids can collide with paid ad ids.
|
|
62
64
|
*/
|
|
63
|
-
ensure(adId: number, urls: string[], limits: CacheLimits, allowed: () => boolean): Promise<CachedMedia>;
|
|
65
|
+
ensure(adId: string | number, urls: string[], limits: CacheLimits, allowed: () => boolean): Promise<CachedMedia>;
|
|
64
66
|
private ensureNow;
|
|
65
67
|
private size;
|
|
66
68
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AppStateLike } from './adapters';
|
|
2
|
-
import type { AudioPolicy, EthiclyConfig, FetchLike, FileCacheAdapter, FrequencyOptions, FrequencyUpdate, InterstitialResult, KeyValueStorage, Logger, NextAdStatus, NoFillReason, Platform } from './types';
|
|
2
|
+
import type { AudioPolicy, EthiclyAd, EthiclyConfig, FetchLike, FileCacheAdapter, FrequencyOptions, FrequencyUpdate, InterstitialResult, KeyValueStorage, LoadOptions, Logger, NextAdStatus, NoFillReason, Platform } from './types';
|
|
3
3
|
export interface EthiclyOptions {
|
|
4
4
|
/** Publisher API key (UUID). */
|
|
5
5
|
apiKey: string;
|
|
@@ -41,6 +41,20 @@ export interface EthiclyOptions {
|
|
|
41
41
|
* behaviour. Change it later with setFrequency().
|
|
42
42
|
*/
|
|
43
43
|
frequency?: FrequencyOptions;
|
|
44
|
+
/**
|
|
45
|
+
* Show the publisher's house ad (uploaded in the dashboard) when no paid ad
|
|
46
|
+
* fills. Default true: the upload is the opt-in, so a publisher without one
|
|
47
|
+
* sees no change. false never asks for one, so the app can show its own.
|
|
48
|
+
* Change it later with setHouseAds(), or per call with { houseAds }.
|
|
49
|
+
*/
|
|
50
|
+
houseAds?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Where EthiclyInterstitial sends a tap on a house ad's button instead of
|
|
53
|
+
* opening its link, for example the app's premium screen. The tap is still
|
|
54
|
+
* counted and the ad closes first. useEthiclyInterstitial's and the
|
|
55
|
+
* screen's own onHouseAdPress win over this. Never used for paid ads.
|
|
56
|
+
*/
|
|
57
|
+
onHouseAdPress?: (ad: EthiclyAd) => void;
|
|
44
58
|
storage?: KeyValueStorage;
|
|
45
59
|
fileCache?: FileCacheAdapter;
|
|
46
60
|
openUrl?: (url: string) => unknown;
|
|
@@ -57,9 +71,15 @@ export interface Ethicly {
|
|
|
57
71
|
/** Resolves when the stored config is loaded and the first /config fetch settled. Never rejects. */
|
|
58
72
|
readonly ready: Promise<void>;
|
|
59
73
|
/** Serve and download an interstitial ahead of time. Does not consume it. */
|
|
60
|
-
preload(): Promise<InterstitialResult>;
|
|
61
|
-
/**
|
|
62
|
-
|
|
74
|
+
preload(opts?: LoadOptions): Promise<InterstitialResult>;
|
|
75
|
+
/**
|
|
76
|
+
* The ad to show now: the preloaded one if still valid, else a fresh serve.
|
|
77
|
+
* Consumes it. A preloaded house ad never stands in for a paid ad: a fresh
|
|
78
|
+
* serve is tried first, within loadTimeoutMs. Check `ad.house`.
|
|
79
|
+
*/
|
|
80
|
+
loadInterstitial(opts?: LoadOptions): Promise<InterstitialResult>;
|
|
81
|
+
/** Turn house ads on or off for this client (options.houseAds). Off drops a preloaded house ad. */
|
|
82
|
+
setHouseAds(enabled: boolean): void;
|
|
63
83
|
/**
|
|
64
84
|
* Frequency caps, the ads switch and the kill switch, checked client-side.
|
|
65
85
|
* false until the stored frequency state has loaded (see `ready`).
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type BeaconContext } from './beacons';
|
|
2
|
+
import type { EthiclyAd, Tracker } from './types';
|
|
3
|
+
export interface HouseTrackerDeps {
|
|
4
|
+
ctx: BeaconContext;
|
|
5
|
+
ad: EthiclyAd;
|
|
6
|
+
/** The house ad id the server sent (fallback_id on the wire). */
|
|
7
|
+
fallbackId: number;
|
|
8
|
+
/** Test mode: the counters are never sent. */
|
|
9
|
+
test: boolean;
|
|
10
|
+
openUrl?: (url: string) => unknown;
|
|
11
|
+
/** Called once when the ad is shown: a house ad counts toward the frequency caps. */
|
|
12
|
+
onShown(): void;
|
|
13
|
+
/** createEthicly's onHouseAdPress, if any. */
|
|
14
|
+
onPress?: (ad: EthiclyAd) => void;
|
|
15
|
+
}
|
|
16
|
+
export declare class HouseTracker implements Tracker {
|
|
17
|
+
private d;
|
|
18
|
+
/**
|
|
19
|
+
* Where the ready-made screen sends a tap instead of opening the link:
|
|
20
|
+
* createEthicly's onHouseAdPress, replaced by useEthiclyInterstitial's.
|
|
21
|
+
* EthiclyInterstitial's own prop wins over both.
|
|
22
|
+
*/
|
|
23
|
+
/** @internal Set by the SDK; apps pass onHouseAdPress instead. */
|
|
24
|
+
houseAdPress: ((ad: EthiclyAd) => void) | undefined;
|
|
25
|
+
private shownOnce;
|
|
26
|
+
private clickedOnce;
|
|
27
|
+
private pending;
|
|
28
|
+
constructor(d: HouseTrackerDeps);
|
|
29
|
+
/** Resolves when every counter in flight has settled. For tests. */
|
|
30
|
+
settle(): Promise<void>;
|
|
31
|
+
isExpired(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* One POST to a house-ad counter, never retried, never awaited by the
|
|
34
|
+
* caller. The answer (200, 400 or 429) changes nothing. Never rejects.
|
|
35
|
+
*/
|
|
36
|
+
private send;
|
|
37
|
+
shown(): Promise<void>;
|
|
38
|
+
progress(_positionMs: number, _opts: {
|
|
39
|
+
visible: boolean;
|
|
40
|
+
durationMs?: number;
|
|
41
|
+
}): void;
|
|
42
|
+
complete(): void;
|
|
43
|
+
skipped(_positionMs: number): void;
|
|
44
|
+
error(): void;
|
|
45
|
+
/**
|
|
46
|
+
* Counts the tap once (fire-and-forget) and opens the destination right
|
|
47
|
+
* away. `{ open: false }` only counts it: the app shows its own screen.
|
|
48
|
+
*/
|
|
49
|
+
clicked(opts?: {
|
|
50
|
+
open?: boolean;
|
|
51
|
+
}): Promise<string>;
|
|
52
|
+
closed(): Promise<void>;
|
|
53
|
+
}
|
|
@@ -5,4 +5,4 @@ export type { AppStateLike, ExpoFileSystemLike, ExpoFileSystemModernLike } from
|
|
|
5
5
|
export { appendClickToken, CLICK_TIMEOUT_MS } from './click';
|
|
6
6
|
export { compareVersions, BUNDLED_BASE_URL } from './config';
|
|
7
7
|
export { SDK_VERSION } from '../version';
|
|
8
|
-
export type { AudioPolicy, EthiclyAd, EthiclyAdMedia, EthiclyConfig, FetchLike, FileCacheAdapter, FrequencyOptions, FrequencyUpdate, InterstitialResult, KeyValueStorage, Logger, NextAdReason, NextAdStatus, NoFillReason, Platform, Tracker, } from './types';
|
|
8
|
+
export type { AudioPolicy, EthiclyAd, EthiclyAdMedia, EthiclyConfig, FetchLike, FileCacheAdapter, FrequencyOptions, FrequencyUpdate, InterstitialResult, KeyValueStorage, LoadOptions, Logger, NextAdReason, NextAdStatus, NoFillReason, Platform, Tracker, } from './types';
|
|
@@ -13,6 +13,16 @@ export interface EthiclyConfig {
|
|
|
13
13
|
max_per_session: number;
|
|
14
14
|
min_gap_s: number;
|
|
15
15
|
audio: AudioPolicy;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the paid-ad screen shows "Sponsored by Ethicly.ch", set in the
|
|
18
|
+
* publisher's dashboard. Absent from the server: true.
|
|
19
|
+
*/
|
|
20
|
+
sponsor_label: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* The publisher's dashboard master switch for house ads. false: the SDK
|
|
23
|
+
* never asks for one. Absent from the server: true.
|
|
24
|
+
*/
|
|
25
|
+
house_ads: boolean;
|
|
16
26
|
};
|
|
17
27
|
prefetch: {
|
|
18
28
|
max_cached_creatives: number;
|
|
@@ -115,6 +125,18 @@ export interface EthiclyAd {
|
|
|
115
125
|
servedAt: number;
|
|
116
126
|
/** Epoch ms after which the ad must not be shown (served + token_ttl_s). */
|
|
117
127
|
expiresAt: number;
|
|
128
|
+
/**
|
|
129
|
+
* True for the publisher's own house ad, uploaded in the dashboard and shown
|
|
130
|
+
* when no paid ad fills. Unbilled, but it counts toward the frequency caps.
|
|
131
|
+
*/
|
|
132
|
+
house: boolean;
|
|
133
|
+
/** For a house ad: why no paid ad filled (the server's reason). Unset for a paid ad. */
|
|
134
|
+
noFillReason?: NoFillReason;
|
|
135
|
+
/**
|
|
136
|
+
* Show "Sponsored by Ethicly.ch" with this ad. false for house ads, else the
|
|
137
|
+
* dashboard setting (/config interstitial.sponsor_label, default true).
|
|
138
|
+
*/
|
|
139
|
+
showSponsorLabel: boolean;
|
|
118
140
|
}
|
|
119
141
|
export interface Tracker {
|
|
120
142
|
/** Call the moment the ad is rendered. Sends /impression immediately. */
|
|
@@ -130,13 +152,28 @@ export interface Tracker {
|
|
|
130
152
|
complete(): void;
|
|
131
153
|
skipped(positionMs: number): void;
|
|
132
154
|
error(): void;
|
|
133
|
-
/**
|
|
134
|
-
|
|
155
|
+
/**
|
|
156
|
+
* POST /click (500 ms budget), then open the destination. Resolves with the URL opened.
|
|
157
|
+
* House ads only: `{ open: false }` counts the tap without opening anything,
|
|
158
|
+
* for an app that sends it to its own screen. A paid ad always opens the
|
|
159
|
+
* advertiser's page.
|
|
160
|
+
*/
|
|
161
|
+
clicked(opts?: {
|
|
162
|
+
open?: boolean;
|
|
163
|
+
}): Promise<string>;
|
|
135
164
|
/** Call when the ad screen goes away. Sends queued video events. */
|
|
136
165
|
closed(): Promise<void>;
|
|
137
166
|
/** True once the ad is older than token_ttl_s: it must not be shown. */
|
|
138
167
|
isExpired(): boolean;
|
|
139
168
|
}
|
|
169
|
+
/** Per-call options for preload() and loadInterstitial(). */
|
|
170
|
+
export interface LoadOptions {
|
|
171
|
+
/**
|
|
172
|
+
* false treats a house ad as no fill for this call, so the app can show its
|
|
173
|
+
* own fallback. Default: the client's setting (options.houseAds, default true).
|
|
174
|
+
*/
|
|
175
|
+
houseAds?: boolean;
|
|
176
|
+
}
|
|
140
177
|
export type InterstitialResult = {
|
|
141
178
|
ad: EthiclyAd;
|
|
142
179
|
tracker: Tracker;
|
|
@@ -163,9 +200,14 @@ export interface WireAd {
|
|
|
163
200
|
mime?: string | null;
|
|
164
201
|
} | null;
|
|
165
202
|
}
|
|
203
|
+
/** Raw house ad, sent next to `ad: null` on an interstitial no-fill when the serve asked with house_ads=1. */
|
|
204
|
+
export interface WireHouseAd extends WireAd {
|
|
205
|
+
test?: boolean;
|
|
206
|
+
}
|
|
166
207
|
export interface WireServe {
|
|
167
208
|
ad: WireAd | null;
|
|
168
209
|
reason?: string;
|
|
210
|
+
house_ad?: WireHouseAd | null;
|
|
169
211
|
publisher_site_id?: number;
|
|
170
212
|
country_code?: string;
|
|
171
213
|
impression_token?: string;
|
|
@@ -16,6 +16,13 @@ export interface EthiclyInterstitialProps {
|
|
|
16
16
|
/** Shows a "Remove ads" link under the button that calls this. */
|
|
17
17
|
onRemoveAds?(): void;
|
|
18
18
|
removeAdsLabel?: string;
|
|
19
|
+
/**
|
|
20
|
+
* House ads only: a tap on the button counts the tap, closes the ad (as
|
|
21
|
+
* onClose does) and calls this instead of opening the house ad's link, for
|
|
22
|
+
* example to open the app's premium screen. Wins over the hook's and
|
|
23
|
+
* createEthicly's onHouseAdPress. Paid ads always open the advertiser's page.
|
|
24
|
+
*/
|
|
25
|
+
onHouseAdPress?(ad: EthiclyAd): void;
|
|
19
26
|
/** Replaces the "Remove ads" link with anything the app wants. */
|
|
20
27
|
renderBelowButton?(): React.ReactNode;
|
|
21
28
|
/** Default true: wraps the screen in a fullscreen Modal. */
|
|
@@ -3,12 +3,26 @@ import type { EthiclyAd, NextAdStatus, NoFillReason, Tracker } from '../core/typ
|
|
|
3
3
|
export interface UseEthiclyInterstitialOptions {
|
|
4
4
|
/** Preload on mount and after each dismiss. Default true. */
|
|
5
5
|
preload?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* false treats a house ad as no fill, so `reason` is set and the app shows
|
|
8
|
+
* its own fallback. Default: the client's setting (createEthicly houseAds).
|
|
9
|
+
*/
|
|
10
|
+
houseAds?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* House ads only: where EthiclyInterstitial sends a tap on the button
|
|
13
|
+
* instead of opening the link (the ad closes first). Wins over
|
|
14
|
+
* createEthicly's onHouseAdPress; the screen's own prop wins over this.
|
|
15
|
+
*/
|
|
16
|
+
onHouseAdPress?: (ad: EthiclyAd) => void;
|
|
6
17
|
}
|
|
7
18
|
export interface EthiclyInterstitialState {
|
|
8
|
-
/** The ad to render, set by a successful show(). */
|
|
19
|
+
/** The ad to render, set by a successful show(). `ad.house` is true for the publisher's house ad. */
|
|
9
20
|
ad: EthiclyAd | null;
|
|
10
21
|
tracker: Tracker | null;
|
|
11
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Why the last show() found no ad: show your own fallback if you have one,
|
|
24
|
+
* except for 'ads_disabled' (a paid user). Null when a house ad was shown.
|
|
25
|
+
*/
|
|
12
26
|
reason: NoFillReason | null;
|
|
13
27
|
loading: boolean;
|
|
14
28
|
/** Load the ad to show now. Resolves true when `ad` is set, false on no fill. */
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** SDK version, sent as sdk_version and in X-Ethicly-SDK. Keep in step with package.json. */
|
|
2
|
-
export declare const SDK_VERSION = "0.
|
|
2
|
+
export declare const SDK_VERSION = "0.4.0";
|
package/package.json
CHANGED
package/src/core/cache.ts
CHANGED
|
@@ -168,14 +168,16 @@ export class CreativeCache {
|
|
|
168
168
|
* `allowed()` is checked before every download so setAdsEnabled(false)
|
|
169
169
|
* stops a download that has not started yet; cancel() also stops the one in
|
|
170
170
|
* flight when the adapter supports it.
|
|
171
|
+
* `adId` keys the entry and names the files: house ads pass `house-<id>`
|
|
172
|
+
* because their ids can collide with paid ad ids.
|
|
171
173
|
*/
|
|
172
|
-
ensure(adId: number, urls: string[], limits: CacheLimits, allowed: () => boolean): Promise<CachedMedia> {
|
|
174
|
+
ensure(adId: string | number, urls: string[], limits: CacheLimits, allowed: () => boolean): Promise<CachedMedia> {
|
|
173
175
|
const run = this.chain.then(() => this.ensureNow(adId, urls, limits, allowed));
|
|
174
176
|
this.chain = run.catch(() => undefined);
|
|
175
177
|
return run;
|
|
176
178
|
}
|
|
177
179
|
|
|
178
|
-
private async ensureNow(adId: number, urls: string[], limits: CacheLimits, allowed: () => boolean): Promise<CachedMedia> {
|
|
180
|
+
private async ensureNow(adId: string | number, urls: string[], limits: CacheLimits, allowed: () => boolean): Promise<CachedMedia> {
|
|
179
181
|
await this.load();
|
|
180
182
|
if (limits.maxCreatives <= 0 || limits.maxBytes <= 0) return { uris: {} };
|
|
181
183
|
const key = `${adId}:${hash(urls.join('|'))}`;
|