@dailymotion/ad-sdk-web-in-feed 1.0.0 → 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,108 +1,309 @@
1
- # @dailymotion/ad-sdk-web-in-feed
1
+ # Ad SDK Web In-Feed
2
2
 
3
- Plays an ad in its own vertical video, so that a host can show it anywhere in a feed.
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.
4
5
 
5
- It is independent from `@dailymotion/ad-sdk-web` (ads inside a content video) but loads the same `core.js`. Every ad is
6
- independent: several of them can live on one page.
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
7
 
8
- ```ts
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
9
32
  import { AdSdkWebInFeedAd } from '@dailymotion/ad-sdk-web-in-feed'
10
33
 
11
- // The container is positioned (e.g. `position: relative`) and has the size of the ad (e.g. a 9:16 box)
12
34
  const ad = new AdSdkWebInFeedAd(container)
13
- ad.on(ad.Events.AD_ERROR, () => removeTheCard())
35
+ ad.on(ad.Events.VOLUME_CHANGE, ({ muted }) => syncTheSoundOfTheFeed(muted))
14
36
 
15
- const infos = await ad.loadInFeedAd({ vastUrl, environment }) // or { vastXml, ... }
37
+ const infos = await ad.loadInFeedAd({ vastUrl, environment }) // or { vastXml, environment }
16
38
 
17
- ad.play() // when the ad is in front of the viewer
18
- ad.pause() // when it leaves the screen, the tab goes to the background, the page changes...
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...
19
41
  ad.trackEvent('like') // when the viewer likes the ad, 'unlike' when they take it back
20
- ad.destroy() // when it is removed
42
+ ad.destroy() // when it is removed
21
43
  ```
22
44
 
23
- ## API
24
-
25
- | Method | Description |
26
- |------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
27
- | `new AdSdkWebInFeedAd(container)` | Creates the ad video (`div.ad-container > video.ad-video`) inside `container`. |
28
- | `loadInFeedAd(params)` | Gets the VAST and selects the creative. Does not play. Gives the `InFeedAdInfos` (below). Rejects and emits `AD_ERROR` when no ad can be loaded. |
29
- | `play()` | Starts the ad (first call), or resumes it. Call it once `loadInFeedAd` has resolved. |
30
- | `pause()` | Pauses the ad. |
31
- | `setMuted(muted)` | Mutes or unmutes the ad. |
32
- | `trackEvent(event)` | Calls the URLs the VAST gives for `like` or `unlike` (in `events` of the extension below), with the same macros as the VAST tracking (`[ADPLAYHEAD]`, `[TIMESTAMP]`...). Does nothing before the ad is loaded. |
33
- | `on(event, cb)` / `off(event, cb)` | See the events below. |
34
- | `destroy()` | Stops the playback and removes everything the SDK created. Safe to call twice. |
35
- | `Events` | `InFeedAdEvents`, the names of the events below. |
36
-
37
- ### `loadInFeedAd` params
38
-
39
- The ad comes from the URL of a VAST **or** from the VAST itself, exactly one of them:
40
-
41
- | Param | Description |
42
- |---------------|----------------------------------------------------------------------|
43
- | `vastUrl` | URL of the VAST, from the Dailymotion ad server. The SDK fetches it. |
44
- | `vastXml` | The VAST as an XML string, from the Dailymotion ad server. |
45
- | `environment` | Same shape as in `@dailymotion/ad-sdk-web` (`EnvironmentContext`). |
46
- | `isMuted` | Start muted (default `true`, required for autoplay in a feed). |
47
-
48
- It gives the `InFeedAdInfos`, read from the `dm-infeed-infos` extension of the VAST (below). Every field is `null` when
49
- the VAST does not give it:
50
-
51
- | Field | |
52
- |---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
53
- | `xid` | The video of the ad. The host gets everything else it shows (creator, title...) from it. |
54
- | `adType` | `boosted` (a video of a creator, promoted) or `standalone` (a creative that only exists as an ad). |
55
- | `inventoryId` | The inventory the ad was served on. |
56
- | `ownerName`, `ownerImage` | The advertiser, to show instead of the name and the profile image of the channel. |
57
- | `dsa` | `{ behalf, paid, params }`, what the Digital Services Act requires to show about the ad. `params` is a list of numbers (e.g. `[1, 2]`). |
58
-
59
- The ad is a stream video. The media file is attached to the video (with `preload="auto"`) as soon as the ad is loaded,
60
- so that the browser can start downloading it
61
- before the ad plays: call `loadInFeedAd` when the ad is about to be shown, not long before. `destroy()` stops the
62
- download of an ad that never played.
63
-
64
- ### Events
65
-
66
- | Event | Callback |
67
- |-----------------|--------------------------------------------------------------------------------|
68
- | `AD_LOAD` | `() => void`: the ad was loaded |
69
- | `AD_START` | `() => void`: the ad starts playing |
70
- | `AD_PLAY` | `() => void`: the ad resumes |
71
- | `AD_PAUSE` | `() => void`: the ad is paused |
72
- | `AD_END` | `() => void`: the ad reached its end |
73
- | `AD_ERROR` | `(code?: number, message?: string) => void`: the ad cannot be loaded or played |
74
- | `VOLUME_CHANGE` | `({ muted, volume }) => void`: the volume or the mute state of the ad changed |
75
-
76
- ## Tracking
77
-
78
- The ad uses the same VAST tracking as the other ads of ad-sdk-web. The ad server is called for:
79
-
80
- | Event | When |
81
- |-----------------------------------------------------|--------------------------------------------------------------|
82
- | `Impression`, `creativeView`, `start` | The ad starts playing. |
83
- | `firstQuartile`, `midpoint`, `thirdQuartile` | The ad reaches 25%, 50%, 75%. |
84
- | `complete` | The ad reaches its end. |
85
- | `mute`, `unmute` | The sound of the ad changes. |
86
- | `events.like`, `events.unlike` of `dm-infeed-infos` | The host calls `trackEvent('like')`, `trackEvent('unlike')`. |
87
- | `pause`, `resume` | The ad is paused, resumed. |
88
- | `Error` | The ad cannot be loaded or played. |
89
-
90
- The macros of VAST (`[TIMESTAMP]`, `[ADPLAYHEAD]`, `[CACHEBUSTING]`...) are replaced.
91
-
92
- ## Event bus
93
-
94
- The ad sends its events to the Dailymotion event bus like the other ads, but with its own identity:
95
-
96
- | Field | In-feed ad | Other ads |
97
- |-----------------------------------------------|-------------------------|---------------------------------------------------|
98
- | `info.player.type` | `Ad-SDK-Web-In-Feed-Ad` | `Ad-SDK-Web` |
99
- | `ad_type` (position of the ad) | `infeed` | `preroll`, `midroll`, `postroll` or `liveadbreak` |
100
- | `ad_client_inventory_id` | `<viewId>_INFEED_0_1` | `<viewId>_PRE_0_1`, `_MID_`... |
101
- | `ad_log_id` of the SDK errors (`adLog` event) | `adSdkInFeedAdError` | `adSdkError` |
102
-
103
- The position is `infeed` for the whole life of the ad. In-feed ads do not run any verification script (OM SDK) for now.
104
-
105
- ## The VAST
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
106
307
 
107
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
108
309
  must be valid, otherwise the extension is ignored:
@@ -121,11 +322,3 @@ must be valid, otherwise the extension is ignored:
121
322
  "dsa": { "behalf": "Advertiser", "paid": "Advertiser Inc.", "params": [1, 2] }
122
323
  }]]></Extension>
123
324
  ```
124
-
125
- ## Example
126
-
127
- `npm run dev:in-feed` from the repository root starts the core and an example
128
- feed (https://adsdkweb.local.dailymotion.com:9098):
129
- a vertical feed that loads an ad after the 2nd video, plays it when it is in front of the viewer and pauses it when it
130
- leaves. A side panel tries the API by hand (`play()`, `pause()`), loads an ad from a VAST URL
131
- or from a VAST XML, and lists the events. `?vastUrl=<url>` loads a VAST from a URL.
package/dist/index.d.ts CHANGED
@@ -101,6 +101,12 @@ type InFeedAdEvent = (typeof InFeedAdEvents)[keyof typeof InFeedAdEvents];
101
101
  type InFeedAdTrackingEvent = 'like' | 'unlike';
102
102
 
103
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;
104
110
  private adContainer;
105
111
  private adVideoElement;
106
112
  private instance;
@@ -131,6 +137,11 @@ declare class AdSdkWebInFeedAd {
131
137
  /** The events an in-feed ad emits. */
132
138
  Events: typeof InFeedAdEvents;
133
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;
134
145
  }
135
146
 
136
147
  export { AdSdkWebInFeedAd, InFeedAdEvents };
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  var D = Object.defineProperty;
2
- var l = (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) => l(i, typeof e != "symbol" ? e + "" : e, t);
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
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 _ = {
5
+ const c = {
6
6
  AD_LOAD: n.AD_LOAD,
7
7
  AD_START: n.AD_START,
8
8
  AD_PLAY: n.AD_PLAY,
@@ -10,25 +10,33 @@ const _ = {
10
10
  AD_END: n.AD_END,
11
11
  AD_ERROR: n.AD_ERROR,
12
12
  VOLUME_CHANGE: n.VOLUME_CHANGE
13
- }, o = "https://asw.dmcdn.net/2.0.0/core.js";
14
- let r = null;
15
- const h = () => window.AdSdkInFeedAd ? Promise.resolve() : r || (r = new Promise((i, e) => {
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
16
  const t = document.createElement("script");
17
- t.src = o, t.onload = () => i(), t.onerror = () => {
18
- t.remove(), r = null, e(new Error(`Failed to load script: ${o}`));
17
+ t.src = E, t.onload = () => i(), t.onerror = () => {
18
+ t.remove(), o = null, e(new Error(`Failed to load script: ${E}`));
19
19
  }, document.head.appendChild(t);
20
- }), r);
21
- class R {
20
+ }), o), a = /* @__PURE__ */ new Set();
21
+ class m {
22
22
  constructor(e) {
23
23
  d(this, "adContainer", null);
24
24
  d(this, "adVideoElement", null);
25
25
  d(this, "instance", null);
26
26
  d(this, "isDestroyed", !1);
27
27
  /** The events an in-feed ad emits. */
28
- d(this, "Events", _);
28
+ d(this, "Events", c);
29
29
  d(this, "pendingListeners", []);
30
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);
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());
32
40
  }
33
41
  /**
34
42
  * Gets the VAST, from its URL (`vastUrl`) or from its XML (`vastXml`), and selects the creative.
@@ -38,13 +46,13 @@ class R {
38
46
  */
39
47
  async loadInFeedAd(e) {
40
48
  if (this.isDestroyed) throw new Error("In-feed ad has been destroyed");
41
- if (await h(), 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");
42
50
  this.instance || (this.instance = AdSdkInFeedAd.createInFeedAd({
43
51
  adVideoTag: this.adVideoElement,
44
52
  adContainer: this.adContainer
45
53
  }));
46
54
  const t = this.instance;
47
- return this.pendingListeners.forEach(([s, a]) => t.on(s, a)), this.pendingListeners = [], t.loadInFeedAd(e);
55
+ return this.pendingListeners.forEach(([s, r]) => t.on(s, r)), this.pendingListeners = [], t.loadInFeedAd(e);
48
56
  }
49
57
  /** Starts the ad or resumes it. */
50
58
  play() {
@@ -69,19 +77,26 @@ class R {
69
77
  this.instance ? this.instance.on(e, s) : this.pendingListeners.push([e, s]);
70
78
  }
71
79
  off(e, t) {
72
- var a;
80
+ var r;
73
81
  const s = t;
74
82
  this.pendingListeners = this.pendingListeners.filter(
75
- ([E, A]) => E !== e || A !== s
76
- ), (a = this.instance) == null || a.off(e, s);
83
+ ([l, A]) => l !== e || A !== s
84
+ ), (r = this.instance) == null || r.off(e, s);
77
85
  }
78
86
  /** Stops the playback, removes every listener and the elements created by the SDK. */
79
87
  destroy() {
80
88
  var e, t;
81
- this.isDestroyed || (this.isDestroyed = !0, (e = this.instance) == null || e.destroy(), this.instance = null, this.pendingListeners = [], (t = this.adContainer) == null || t.remove(), this.adContainer = null, this.adVideoElement = null);
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();
82
97
  }
83
98
  }
84
99
  export {
85
- R as AdSdkWebInFeedAd,
86
- _ as InFeedAdEvents
100
+ m as AdSdkWebInFeedAd,
101
+ c as InFeedAdEvents
87
102
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dailymotion/ad-sdk-web-in-feed",
3
3
  "author": "Dailymotion <ad-sdk-web@dailymotion.com> (https://developer.dailymotion.com/)",
4
- "version": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "description": "JavaScript SDK to display video ads in a feed, by Dailymotion",
6
6
  "keywords": [
7
7
  "vast",
@@ -28,6 +28,7 @@
28
28
  "dev": "vite",
29
29
  "build": "vite build --config vite.sdk.config.js && rollup -c vite.dts.config.js",
30
30
  "build:examples": "tsc && vite build",
31
+ "deploy": "sh .deployment/deploy.sh",
31
32
  "preview": "vite preview",
32
33
  "style": "eslint .",
33
34
  "type-check": "tsc --noEmit",