@dailymotion/ad-sdk-web-in-feed 0.0.0-stage → 1.1.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
@@ -1,3 +1,324 @@
1
- # Temporary Holding Version
1
+ # Ad SDK Web In-Feed
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A lightweight web wrapper for Dailymotion's Ad SDK – load, play and track video ads displayed in their own vertical
4
+ video, anywhere in a feed.
5
+
6
+ [![npm](https://img.shields.io/npm/v/@dailymotion/ad-sdk-web-in-feed.svg)](https://www.npmjs.com/package/@dailymotion/ad-sdk-web-in-feed)
7
+
8
+ ## ⚙️ Required HTML & CSS Setup for the Ad Container
9
+
10
+ To use Ad SDK Web In-Feed, you must define an HTML element, styled correctly, for each ad. The SDK mounts the ad video
11
+ inside it.
12
+
13
+ ### CSS styles to add
14
+
15
+ ```
16
+ .inFeedAdContainer {
17
+ position: relative;
18
+ width: 100%;
19
+ aspect-ratio: 9 / 16;
20
+ }
21
+ ```
22
+
23
+ `.inFeedAdContainer` is the element where the SDK mounts the ad container and the ad video.
24
+
25
+ The container is positioned relative to allow absolute positioning of the ad overlays.
26
+
27
+ The aspect ratio gives the vertical format of the ad. A horizontal ad video is letterboxed, never cropped.
28
+
29
+ ## Quick start
30
+
31
+ ```typescript
32
+ import { AdSdkWebInFeedAd } from '@dailymotion/ad-sdk-web-in-feed'
33
+
34
+ const ad = new AdSdkWebInFeedAd(container)
35
+ ad.on(ad.Events.VOLUME_CHANGE, ({ muted }) => syncTheSoundOfTheFeed(muted))
36
+
37
+ const infos = await ad.loadInFeedAd({ vastUrl, environment }) // or { vastXml, environment }
38
+
39
+ ad.play() // when the ad is in front of the viewer
40
+ ad.pause() // when it leaves the screen, the tab goes to the background, the page changes...
41
+ ad.trackEvent('like') // when the viewer likes the ad, 'unlike' when they take it back
42
+ ad.destroy() // when it is removed
43
+ ```
44
+
45
+ ## 📘 Public API Documentation
46
+
47
+ ### `new AdSdkWebInFeedAd(container: HTMLElement)`
48
+
49
+ Creates the ad video (`div.ad-container > video.ad-video`) inside the container.<br />
50
+ Create one instance per ad.
51
+
52
+ **Parameters:**
53
+
54
+ - `container`: `HTMLElement`
55
+ The DOM element where the ad container will be mounted.
56
+
57
+ **Throws:**
58
+ If the container is missing.
59
+
60
+ ---
61
+
62
+ ### `async loadInFeedAd(params: InFeedAdParams): Promise<InFeedAdInfos | null>`
63
+
64
+ Loads the Ad SDK script (once per page), gets the VAST and selects the creative. It does not start the playback: call
65
+ `play()` when the ad is visible.
66
+
67
+ The media file is attached to the ad video (with `preload="auto"`) as soon as the ad is loaded, so that the browser can
68
+ start downloading it before the ad plays: call `loadInFeedAd` when the ad is about to be shown, not long before.
69
+
70
+ **Parameters:**
71
+
72
+ - `params`: `InFeedAdParams`
73
+ The source of the ad and the context of the app.
74
+ 👉 **See [InFeedAdParams](#InFeedAdParams)**
75
+
76
+ **Returns:**
77
+ The `InFeedAdInfos` of the ad, what the host needs to draw it (its video, its kind, the advertiser, the DSA infos).
78
+ 👉 **See [InFeedAdInfos](#InFeedAdInfos)**
79
+
80
+ **Throws:**
81
+ If the ad has been destroyed, if the Ad SDK script fails to load, or if no ad can be loaded. `AD_ERROR` is emitted
82
+ too.
83
+
84
+ ---
85
+
86
+ ### `play(): Promise<void>`
87
+
88
+ Starts the ad (first call), or resumes it. Call it once `loadInFeedAd` has resolved.
89
+
90
+ The ad loops: when it ends, it starts again. Each loop is a new view of the ad: `AD_END`, then `AD_LOAD` and
91
+ `AD_START` are emitted again, and the VAST tracking (impression, start, quartiles, complete) is sent again.
92
+
93
+ When the ad is not muted and the browser refuses to play it with sound without a user action (e.g. Safari), the ad
94
+ plays muted instead. `VOLUME_CHANGE` is not emitted for it: it only tells the host about the changes of the sound
95
+ made by the viewer or by `setMuted`, so the host keeps the sound of its feed.
96
+
97
+ ---
98
+
99
+ ### `pause(): void`
100
+
101
+ Pauses the ad.
102
+
103
+ ---
104
+
105
+ ### `setMuted(muted: boolean): void`
106
+
107
+ Mutes or unmutes the ad. `VOLUME_CHANGE` is emitted.
108
+
109
+ **Parameters:**
110
+
111
+ - `muted`: `boolean`
112
+ `true` to mute the ad, `false` to unmute it.
113
+
114
+ ---
115
+
116
+ ### `trackEvent(event: InFeedAdTrackingEvent): void`
117
+
118
+ Calls the URLs the VAST gives for this event (in `events` of the [`dm-infeed-infos` extension](#The-VAST)), with the
119
+ same macros as the VAST tracking (`[ADPLAYHEAD]`, `[TIMESTAMP]`...). Does nothing before the ad is loaded.
120
+
121
+ **Parameters:**
122
+
123
+ - `event`: `'like' | 'unlike'`
124
+ `like` when the viewer likes the ad, `unlike` when they take it back.
125
+
126
+ ---
127
+
128
+ ### `on(event: InFeedAdEvent, callback: EventCallback): void`
129
+
130
+ Subscribes to an event of the ad. Listeners can be added before `loadInFeedAd`.
131
+
132
+ **Parameters:**
133
+
134
+ - `event`: `InFeedAdEvent`
135
+ The event name to listen for.
136
+ 👉 **See [`InFeedAdEvents`](#InFeedAdEvents) for available event names.**
137
+
138
+ - `callback`: `EventCallback`
139
+ The callback function to execute when the event occurs.
140
+
141
+ **Callback signature depends on the event:**
142
+ - For `AD_ERROR`: `(code?: number, message?: string) => void`
143
+ The callback receives optional VAST error code and message parameters.
144
+ - For `VOLUME_CHANGE`: `(payload: { muted?: boolean, volume?: number }) => void`
145
+ Fired whenever the muted state or the volume of the ad changes, including the changes made by `setMuted`, but
146
+ not when the ad plays muted because the browser refused the sound (see `play()`).
147
+ - For all other events: `() => void`
148
+ The callback receives no parameters.
149
+
150
+ **Example:**
151
+
152
+ ```typescript
153
+ // Standard event callback (no parameters)
154
+ ad.on(ad.Events.AD_START, () => {
155
+ console.log('Ad started')
156
+ })
157
+
158
+ // Error event callback (with code and message)
159
+ ad.on(ad.Events.AD_ERROR, (code, message) => {
160
+ console.error('Ad error:', code, message)
161
+ })
162
+ ```
163
+
164
+ ---
165
+
166
+ ### `off(event: InFeedAdEvent, callback: EventCallback): void`
167
+
168
+ Unsubscribes from an event of the ad.
169
+
170
+ **Parameters:**
171
+
172
+ - `event`: `InFeedAdEvent`
173
+ The event name to stop listening for.
174
+ 👉 **See [`InFeedAdEvents`](#InFeedAdEvents) for available event names.**
175
+
176
+ - `callback`: `EventCallback`
177
+ The callback function to remove. Must be the same function reference that was passed to `on()`.
178
+
179
+ ---
180
+
181
+ ### `destroy(): void`
182
+
183
+ Stops the playback, removes every listener and the elements created by the SDK. It also stops the download of an ad
184
+ that never played. Safe to call twice.
185
+
186
+ ---
187
+
188
+ ### `static catchUserAction(): void`
189
+
190
+ Unlocks the sound of the ads on the browsers that require a user action first.
191
+
192
+ Call it synchronously, inside your click/tap handler (e.g. on unmute):
193
+
194
+ ```ts
195
+ unmuteButton.addEventListener('click', () => {
196
+ AdSdkWebInFeedAd.catchUserAction()
197
+ // ...
198
+ })
199
+ ```
200
+
201
+ Each call unlocks the ads that exist at that moment. Call it on every user action to unlock the ads created since. An ad
202
+ that is
203
+ not unlocked plays muted if the browser refuses the sound.
204
+
205
+ ---
206
+
207
+ ### `Events`
208
+
209
+ `InFeedAdEvents`, the names of the events of the ad.
210
+ 👉 **See [`InFeedAdEvents`](#InFeedAdEvents)**
211
+
212
+ ---
213
+
214
+ <br />
215
+
216
+ # Data structures
217
+
218
+ ## InFeedAdParams
219
+
220
+ The ad comes from the URL of a VAST **or** from the VAST itself: give exactly one of `vastUrl` and `vastXml`.
221
+
222
+ | Parameter | Type | Required | Default | Description |
223
+ |-------------|--------------------|----------|---------|----------------------------------------------------------------|
224
+ | vastUrl | string | * | — | URL of the VAST. The SDK fetches it. |
225
+ | vastXml | string | * | — | The VAST as an XML string. |
226
+ | environment | EnvironmentContext | yes | — | Context about the app environment, locale, and device details. |
227
+ | isMuted | boolean | no | `true` | Starts the ad muted, required for autoplay in a feed. |
228
+
229
+ ## EnvironmentContext
230
+
231
+ | Parameter | Type | Required | Description |
232
+ |--------------------------------------------|------------------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
233
+ | appName | string | yes | Name of the app or website embedding the ad. |
234
+ | locale | string | yes | Locale or language setting for the user session. |
235
+ | topDomain | string | yes | The top-level domain of the page embedding the ad. |
236
+ | embedder | string | yes | Identifier or name of the embedding entity or partner. |
237
+ | clientType | string | yes | Type of client (e.g., web, mobile app, CTV). |
238
+ | deviceType | string | no | Device type reported by the host (e.g., `desktop`, `mobile`, `tablet`, `tv`). |
239
+ | deviceId | string | no | **Native app context.** Unique identifier for the user’s device. |
240
+ | uid | string | no | Dailymotion unique user ID. |
241
+ | readerId | string | no | Reader id (connected user id). |
242
+ | dailymotionAdvertisingId | string | no | Dailymotion advertising ID. |
243
+ | eid | Record<string, string> | no | Extended User Identifier key/value pairs. |
244
+ | isNativeApp | boolean | no | Whether the client is a native (in-app) client. |
245
+ | appTrackingTransparencyAuthorizationStatus | number (0–3) | no | **Native app context.** App Tracking Transparency (iOS) authorization status. **0** = not determined; **1** = restricted; **2** = denied; **3** = authorized. |
246
+ | isTrackingAllowed | boolean | no | **Native app context.** Whether tracking is allowed for this session/device. |
247
+ | isLimitAdTracking | boolean | no | **Native app context.** Whether limit ad tracking is enabled for the user/device. |
248
+ | trafficSegment | number | yes | Numeric segment ID used for traffic or user segmentation. |
249
+ | v1st | string | yes | A unique ID automatically assigned to your device by Dailymotion. |
250
+ | is3rdPartyCookiesAvailable | boolean | yes | Indicates if third-party cookies are accessible in this context. |
251
+ | osFamily | string | no | Operating system family parsed from UA, e.g., `Linux`. |
252
+ | osName | string | no | Operating system name parsed from UA, e.g., `Ubuntu`. |
253
+ | uaFamily | string | no | User agent family, e.g., `Chromium`. |
254
+ | uaName | string | no | User agent name, e.g., `Chrome`. |
255
+ | uaVersion | string | no | User agent version, e.g., `120.0.0`. |
256
+ | asNumber | string | no | Autonomous system number of the visitor's network, e.g., `AS15169`. |
257
+ | continent | string | no | Continent code of the visitor, e.g., `EU`. |
258
+ | country | string | no | Country code of the visitor, e.g., `FR`. |
259
+ | region | string | no | Region of the visitor, e.g., `Ile-de-France`. |
260
+
261
+ ## InFeedAdInfos
262
+
263
+ Read from the [`dm-infeed-infos` extension](#The-VAST) of the VAST. Every field is `null` when the VAST does not give
264
+ it.
265
+
266
+ | Field | Type | Description |
267
+ |-------------|---------------------|----------------------------------------------------------------------------------------------------|
268
+ | xid | string \| null | The video of the ad. The host gets everything else it shows (title, hashtags...) from it. |
269
+ | adType | string \| null | `boosted` (a video of a creator, promoted) or `standalone` (a creative that only exists as an ad). |
270
+ | inventoryId | string \| null | The inventory the ad was served on. |
271
+ | ownerName | string \| null | The advertiser, to show instead of the name of the channel. |
272
+ | ownerImage | string \| null | The image of the advertiser, to show instead of the profile image of the channel. |
273
+ | dsa | InFeedAdDsa \| null | What the Digital Services Act requires to show about the ad. |
274
+
275
+ ## InFeedAdDsa
276
+
277
+ | Field | Type | Description |
278
+ |--------|------------------|----------------------------------------------|
279
+ | behalf | string \| null | On behalf of whom the ad is displayed. |
280
+ | paid | string \| null | Who paid for the ad. |
281
+ | params | number[] \| null | The DSA parameters of the ad, e.g. `[1, 2]`. |
282
+
283
+ <br />
284
+
285
+ # Internal data models
286
+
287
+ ## InFeedAdEvents
288
+
289
+ | Parameter | Description | Callback Signature |
290
+ |---------------|---------------------------------------------------------------|-----------------------------------------------------------|
291
+ | AD_LOAD | Sent when the ad has loaded | `() => void` |
292
+ | AD_START | Sent when the ad has started | `() => void` |
293
+ | AD_PLAY | Sent when the ad is resumed | `() => void` |
294
+ | AD_PAUSE | Sent when the ad is paused | `() => void` |
295
+ | AD_END | Sent when the ad has ended | `() => void` |
296
+ | AD_ERROR | Sent when an error preventing to load or play the ad occurs | `(code?: number, message?: string) => void` |
297
+ | VOLUME_CHANGE | Sent whenever the muted state or the volume of the ad changes | `(payload: { muted?: boolean, volume?: number }) => void` |
298
+
299
+ **Note:** The `AD_ERROR` event callback receives optional `code` (number) and `message` (string) parameters containing
300
+ VAST error information. The error code corresponds to IAB VAST error codes (e.g., 100, 300, 400, etc.). The
301
+ `VOLUME_CHANGE` callback receives a `payload` with `muted` and/or `volume`, so the host can keep the sound of the feed
302
+ in sync. All other events use a callback with no parameters.
303
+
304
+ <br />
305
+
306
+ # The VAST
307
+
308
+ A VAST 4.x with a stream video (MP4) and, to give the video and the kind of the ad, the Dailymotion extension. Its JSON
309
+ must be valid, otherwise the extension is ignored:
310
+
311
+ ```xml
312
+
313
+ <Extension type="dm-infeed-infos"><![CDATA[{
314
+ "version": 1,
315
+ "xid": "x9abc12",
316
+ "invId": "INVENTORY_ID",
317
+ "events": {
318
+ "like": ["https://TRACKING_URL/like?pos=[ADPLAYHEAD]&ts=[TIMESTAMP]"],
319
+ "unlike": ["https://TRACKING_URL/unlike?pos=[ADPLAYHEAD]&ts=[TIMESTAMP]"]
320
+ },
321
+ "adInfo": { "type": "standalone", "ownerName": "Advertiser", "ownerImage": "https://..." },
322
+ "dsa": { "behalf": "Advertiser", "paid": "Advertiser Inc.", "params": [1, 2] }
323
+ }]]></Extension>
324
+ ```
@@ -0,0 +1,148 @@
1
+ interface VolumeChangePayload {
2
+ muted?: boolean;
3
+ volume?: number;
4
+ }
5
+
6
+ type StandardCallback = () => void;
7
+ type ErrorCallback = (code?: number, message?: string) => void;
8
+ type VolumeChangeCallback = (payload: VolumeChangePayload) => void;
9
+
10
+ declare enum AdEvents {
11
+ AD_BREAK_START = "AD_BREAK_START",
12
+ AD_BREAK_END = "AD_BREAK_END",
13
+ AD_LOAD = "AD_LOAD",
14
+ AD_START = "AD_START",
15
+ AD_PLAY = "AD_PLAY",
16
+ AD_PAUSE = "AD_PAUSE",
17
+ AD_END = "AD_END",
18
+ AD_ERROR = "AD_ERROR",
19
+ CONTENT_PAUSE_REQUESTED = "CONTENT_PAUSE_REQUESTED",
20
+ CONTENT_RESUME_REQUESTED = "CONTENT_RESUME_REQUESTED",
21
+ AD_READY_TO_FETCH = "AD_READY_TO_FETCH",
22
+ AD_DEBUG_LOG = "AD_DEBUG_LOG",
23
+ ADS_SEQUENCE_LOADED = "ADS_SEQUENCE_LOADED",
24
+ VOLUME_CHANGE = "VOLUME_CHANGE"
25
+ }
26
+
27
+ type AppTrackingTransparencyAuthorizationStatus = 0 | 1 | 2 | 3;
28
+ /**
29
+ * Player display mode
30
+ * - regular: main player (PIP off or PIP on with main player displayed)
31
+ * - pip_expanded: PIP running, expanded
32
+ * - pip_collapsed: PIP running, small viewport
33
+ * - pip_corner: PIP running, not small viewport
34
+ */
35
+ type PlayerDisplayMode = 'regular' | 'pip_expanded' | 'pip_collapsed' | 'pip_corner';
36
+ interface EnvironmentContext {
37
+ appName: string;
38
+ locale: string;
39
+ topDomain: string;
40
+ embedder: string;
41
+ clientType: 'ctv' | string;
42
+ deviceType?: string;
43
+ deviceId?: string;
44
+ uid?: string;
45
+ readerId?: string;
46
+ dailymotionAdvertisingId?: string;
47
+ eid?: Record<string, string>;
48
+ playerDisplayMode?: PlayerDisplayMode;
49
+ trafficSegment: number;
50
+ v1st: string;
51
+ is3rdPartyCookiesAvailable: boolean;
52
+ isNativeApp?: boolean;
53
+ appTrackingTransparencyAuthorizationStatus?: AppTrackingTransparencyAuthorizationStatus;
54
+ isTrackingAllowed?: boolean;
55
+ isLimitAdTracking?: boolean;
56
+ osFamily?: string | null;
57
+ osName?: string | null;
58
+ uaFamily?: string | null;
59
+ uaName?: string | null;
60
+ uaVersion?: string | null;
61
+ asNumber?: string;
62
+ continent?: string;
63
+ country?: string;
64
+ region?: string;
65
+ }
66
+
67
+ interface InFeedAdInfos {
68
+ xid: string | null;
69
+ adType: string | null;
70
+ inventoryId: string | null;
71
+ ownerName: string | null;
72
+ ownerImage: string | null;
73
+ dsa: InFeedAdDsa | null;
74
+ }
75
+ interface InFeedAdDsa {
76
+ behalf: string | null;
77
+ paid: string | null;
78
+ params: number[] | null;
79
+ }
80
+ type InFeedAdSource = {
81
+ vastUrl: string;
82
+ vastXml?: never;
83
+ } | {
84
+ vastXml: string;
85
+ vastUrl?: never;
86
+ };
87
+ type InFeedAdParams = InFeedAdSource & {
88
+ environment: EnvironmentContext;
89
+ isMuted?: boolean;
90
+ };
91
+ declare const InFeedAdEvents: {
92
+ readonly AD_LOAD: AdEvents.AD_LOAD;
93
+ readonly AD_START: AdEvents.AD_START;
94
+ readonly AD_PLAY: AdEvents.AD_PLAY;
95
+ readonly AD_PAUSE: AdEvents.AD_PAUSE;
96
+ readonly AD_END: AdEvents.AD_END;
97
+ readonly AD_ERROR: AdEvents.AD_ERROR;
98
+ readonly VOLUME_CHANGE: AdEvents.VOLUME_CHANGE;
99
+ };
100
+ type InFeedAdEvent = (typeof InFeedAdEvents)[keyof typeof InFeedAdEvents];
101
+ type InFeedAdTrackingEvent = 'like' | 'unlike';
102
+
103
+ declare class AdSdkWebInFeedAd {
104
+ /**
105
+ * Lets the ads of the page play with sound on the browsers that require a user action for it (e.g. Safari).
106
+ * Call it synchronously in the handler of a user action (e.g. the click on unmute), it only unlocks the ads that
107
+ * exist at that moment. An ad that cannot play with sound plays muted.
108
+ */
109
+ static catchUserAction(): void;
110
+ private adContainer;
111
+ private adVideoElement;
112
+ private instance;
113
+ private isDestroyed;
114
+ constructor(container: HTMLElement);
115
+ /**
116
+ * Gets the VAST, from its URL (`vastUrl`) or from its XML (`vastXml`), and selects the creative.
117
+ * Does not start the playback: call `play()` when the ad is visible.
118
+ * Gives what the host needs to draw the ad (`InFeedAdInfos`): its video, its kind, the advertiser and the DSA infos.
119
+ * Rejects when the ad cannot be loaded.
120
+ */
121
+ loadInFeedAd(params: InFeedAdParams): Promise<InFeedAdInfos | null>;
122
+ /** Starts the ad or resumes it. */
123
+ play(): Promise<void>;
124
+ pause(): void;
125
+ setMuted(muted: boolean): void;
126
+ /** Calls the URLs the VAST gives for this event, with the same macros as the VAST tracking. */
127
+ trackEvent(event: InFeedAdTrackingEvent): void;
128
+ /** Fired on every volume or mute change of the ad video, including the ones made by `setMuted`. */
129
+ on(event: AdEvents.VOLUME_CHANGE, callback: VolumeChangeCallback): void;
130
+ on(event: AdEvents.AD_ERROR, callback: ErrorCallback): void;
131
+ on(event: Exclude<InFeedAdEvent, AdEvents.VOLUME_CHANGE | AdEvents.AD_ERROR>, callback: StandardCallback): void;
132
+ off(event: AdEvents.VOLUME_CHANGE, callback: VolumeChangeCallback): void;
133
+ off(event: AdEvents.AD_ERROR, callback: ErrorCallback): void;
134
+ off(event: Exclude<InFeedAdEvent, AdEvents.VOLUME_CHANGE | AdEvents.AD_ERROR>, callback: StandardCallback): void;
135
+ /** Stops the playback, removes every listener and the elements created by the SDK. */
136
+ destroy(): void;
137
+ /** The events an in-feed ad emits. */
138
+ Events: typeof InFeedAdEvents;
139
+ private pendingListeners;
140
+ /**
141
+ * Browsers unlock the sound of a video touched within the user action, for as long as it exists.
142
+ * A video that has already played holds a started ad, loading it again would restart that ad.
143
+ */
144
+ private unlockVideo;
145
+ }
146
+
147
+ export { AdSdkWebInFeedAd, InFeedAdEvents };
148
+ export type { AppTrackingTransparencyAuthorizationStatus, EnvironmentContext, InFeedAdDsa, InFeedAdEvent, InFeedAdInfos, InFeedAdParams, InFeedAdSource, InFeedAdTrackingEvent, PlayerDisplayMode, VolumeChangePayload };
package/dist/index.js ADDED
@@ -0,0 +1,102 @@
1
+ var D = Object.defineProperty;
2
+ var h = (i, e, t) => e in i ? D(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
+ var d = (i, e, t) => h(i, typeof e != "symbol" ? e + "" : e, t);
4
+ var n = /* @__PURE__ */ ((i) => (i.AD_BREAK_START = "AD_BREAK_START", i.AD_BREAK_END = "AD_BREAK_END", i.AD_LOAD = "AD_LOAD", i.AD_START = "AD_START", i.AD_PLAY = "AD_PLAY", i.AD_PAUSE = "AD_PAUSE", i.AD_END = "AD_END", i.AD_ERROR = "AD_ERROR", i.CONTENT_PAUSE_REQUESTED = "CONTENT_PAUSE_REQUESTED", i.CONTENT_RESUME_REQUESTED = "CONTENT_RESUME_REQUESTED", i.AD_READY_TO_FETCH = "AD_READY_TO_FETCH", i.AD_DEBUG_LOG = "AD_DEBUG_LOG", i.ADS_SEQUENCE_LOADED = "ADS_SEQUENCE_LOADED", i.VOLUME_CHANGE = "VOLUME_CHANGE", i))(n || {});
5
+ const c = {
6
+ AD_LOAD: n.AD_LOAD,
7
+ AD_START: n.AD_START,
8
+ AD_PLAY: n.AD_PLAY,
9
+ AD_PAUSE: n.AD_PAUSE,
10
+ AD_END: n.AD_END,
11
+ AD_ERROR: n.AD_ERROR,
12
+ VOLUME_CHANGE: n.VOLUME_CHANGE
13
+ }, E = "https://asw.dmcdn.net/2.0.0/core.js";
14
+ let o = null;
15
+ const _ = () => window.AdSdkInFeedAd ? Promise.resolve() : o || (o = new Promise((i, e) => {
16
+ const t = document.createElement("script");
17
+ t.src = E, t.onload = () => i(), t.onerror = () => {
18
+ t.remove(), o = null, e(new Error(`Failed to load script: ${E}`));
19
+ }, document.head.appendChild(t);
20
+ }), o), a = /* @__PURE__ */ new Set();
21
+ class m {
22
+ constructor(e) {
23
+ d(this, "adContainer", null);
24
+ d(this, "adVideoElement", null);
25
+ d(this, "instance", null);
26
+ d(this, "isDestroyed", !1);
27
+ /** The events an in-feed ad emits. */
28
+ d(this, "Events", c);
29
+ d(this, "pendingListeners", []);
30
+ if (!e) throw new Error("No container provided");
31
+ this.adContainer = document.createElement("div"), this.adContainer.classList.add("ad-container"), this.adVideoElement = document.createElement("video"), this.adVideoElement.classList.add("ad-video"), this.adVideoElement.setAttribute("playsinline", ""), this.adVideoElement.setAttribute("webkit-playsinline", ""), this.adVideoElement.muted = !0, this.adVideoElement.style.objectFit = "contain", this.adVideoElement.style.backgroundColor = "#0D0D0D", this.adContainer.appendChild(this.adVideoElement), e.appendChild(this.adContainer), a.add(this);
32
+ }
33
+ /**
34
+ * Lets the ads of the page play with sound on the browsers that require a user action for it (e.g. Safari).
35
+ * Call it synchronously in the handler of a user action (e.g. the click on unmute), it only unlocks the ads that
36
+ * exist at that moment. An ad that cannot play with sound plays muted.
37
+ */
38
+ static catchUserAction() {
39
+ a.forEach((e) => e.unlockVideo());
40
+ }
41
+ /**
42
+ * Gets the VAST, from its URL (`vastUrl`) or from its XML (`vastXml`), and selects the creative.
43
+ * Does not start the playback: call `play()` when the ad is visible.
44
+ * Gives what the host needs to draw the ad (`InFeedAdInfos`): its video, its kind, the advertiser and the DSA infos.
45
+ * Rejects when the ad cannot be loaded.
46
+ */
47
+ async loadInFeedAd(e) {
48
+ if (this.isDestroyed) throw new Error("In-feed ad has been destroyed");
49
+ if (await _(), this.isDestroyed) throw new Error("In-feed ad has been destroyed");
50
+ this.instance || (this.instance = AdSdkInFeedAd.createInFeedAd({
51
+ adVideoTag: this.adVideoElement,
52
+ adContainer: this.adContainer
53
+ }));
54
+ const t = this.instance;
55
+ return this.pendingListeners.forEach(([s, r]) => t.on(s, r)), this.pendingListeners = [], t.loadInFeedAd(e);
56
+ }
57
+ /** Starts the ad or resumes it. */
58
+ play() {
59
+ var e;
60
+ return ((e = this.instance) == null ? void 0 : e.play()) ?? Promise.resolve();
61
+ }
62
+ pause() {
63
+ var e;
64
+ (e = this.instance) == null || e.pause();
65
+ }
66
+ setMuted(e) {
67
+ var t;
68
+ this.adVideoElement && (this.adVideoElement.muted = e), (t = this.instance) == null || t.setMuted(e);
69
+ }
70
+ /** Calls the URLs the VAST gives for this event, with the same macros as the VAST tracking. */
71
+ trackEvent(e) {
72
+ var t;
73
+ (t = this.instance) == null || t.trackEvent(e);
74
+ }
75
+ on(e, t) {
76
+ const s = t;
77
+ this.instance ? this.instance.on(e, s) : this.pendingListeners.push([e, s]);
78
+ }
79
+ off(e, t) {
80
+ var r;
81
+ const s = t;
82
+ this.pendingListeners = this.pendingListeners.filter(
83
+ ([l, A]) => l !== e || A !== s
84
+ ), (r = this.instance) == null || r.off(e, s);
85
+ }
86
+ /** Stops the playback, removes every listener and the elements created by the SDK. */
87
+ destroy() {
88
+ var e, t;
89
+ this.isDestroyed || (this.isDestroyed = !0, a.delete(this), (e = this.instance) == null || e.destroy(), this.instance = null, this.pendingListeners = [], (t = this.adContainer) == null || t.remove(), this.adContainer = null, this.adVideoElement = null);
90
+ }
91
+ /**
92
+ * Browsers unlock the sound of a video touched within the user action, for as long as it exists.
93
+ * A video that has already played holds a started ad, loading it again would restart that ad.
94
+ */
95
+ unlockVideo() {
96
+ !this.adVideoElement || this.adVideoElement.played.length > 0 || this.adVideoElement.load();
97
+ }
98
+ }
99
+ export {
100
+ m as AdSdkWebInFeedAd,
101
+ c as InFeedAdEvents
102
+ };
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@dailymotion/ad-sdk-web-in-feed",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "author": "Dailymotion <ad-sdk-web@dailymotion.com> (https://developer.dailymotion.com/)",
4
+ "version": "1.1.0",
5
+ "description": "JavaScript SDK to display video ads in a feed, by Dailymotion",
6
+ "keywords": [
7
+ "vast",
8
+ "ad",
9
+ "advertising",
10
+ "feed",
11
+ "video"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/dailymotion/ad-sdk-sample"
16
+ },
17
+ "type": "module",
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "files": [
21
+ "dist/index.js",
22
+ "dist/index.d.ts"
23
+ ],
24
+ "publishConfig": {
25
+ "registry": "https://registry.npmjs.org"
26
+ },
27
+ "scripts": {
28
+ "dev": "vite",
29
+ "build": "vite build --config vite.sdk.config.js && rollup -c vite.dts.config.js",
30
+ "build:examples": "tsc && vite build",
31
+ "deploy": "sh .deployment/deploy.sh",
32
+ "preview": "vite preview",
33
+ "style": "eslint .",
34
+ "type-check": "tsc --noEmit",
35
+ "test": "vitest run --passWithNoTests"
36
+ },
37
+ "devDependencies": {
38
+ "@eslint/css": "^0.7.0",
39
+ "@eslint/js": "^9.26.0",
40
+ "@stylistic/eslint-plugin": "^4.4.1",
41
+ "@types/node": "^24.0.14",
42
+ "eslint": "^9.26.0",
43
+ "globals": "^16.0.0",
44
+ "rollup-plugin-dts": "^6.2.1",
45
+ "typescript": "~5.7.2",
46
+ "typescript-eslint": "^8.32.0",
47
+ "vite": "^6.2.0",
48
+ "vitest": "^3.2.4"
49
+ }
50
+ }