@vouchfor/embeds 0.0.0-experiment.f4df018 → 0.0.0-experiment.f6e3687

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. package/dist/es/browser-0cb0f446.js +433 -0
  2. package/dist/es/browser-0cb0f446.js.map +1 -0
  3. package/dist/es/embeds.js +12 -1499
  4. package/dist/es/embeds.js.map +1 -1
  5. package/dist/es/index-326f29ee.js +9536 -0
  6. package/dist/es/index-326f29ee.js.map +1 -0
  7. package/dist/es/src/components/DialogEmbed/DialogOverlay.d.ts +19 -0
  8. package/dist/es/src/components/DialogEmbed/DialogPortal.d.ts +35 -0
  9. package/dist/es/src/components/DialogEmbed/index.d.ts +36 -0
  10. package/dist/es/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  11. package/dist/es/{components/Embed → src/components/PlayerEmbed}/controllers/fetcher.d.ts +5 -4
  12. package/dist/es/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  13. package/dist/es/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  14. package/dist/es/{components/Embed → src/components/PlayerEmbed}/index.d.ts +28 -19
  15. package/dist/es/src/components/PlayerEmbed/tests/data.d.ts +3 -0
  16. package/dist/es/src/index.d.ts +2 -0
  17. package/dist/iife/dialog-embed/browser-fcb64823.js +433 -0
  18. package/dist/iife/dialog-embed/browser-fcb64823.js.map +1 -0
  19. package/dist/iife/dialog-embed/embed.iife.js +1723 -0
  20. package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
  21. package/dist/iife/dialog-embed/embed.js +5 -0
  22. package/dist/iife/dialog-embed/embed.js.map +1 -0
  23. package/dist/iife/dialog-embed/index-804846ae.js +33256 -0
  24. package/dist/iife/dialog-embed/index-804846ae.js.map +1 -0
  25. package/dist/iife/dialog-embed/src/components/DialogEmbed/DialogOverlay.d.ts +19 -0
  26. package/dist/iife/dialog-embed/src/components/DialogEmbed/DialogPortal.d.ts +35 -0
  27. package/dist/iife/dialog-embed/src/components/DialogEmbed/index.d.ts +36 -0
  28. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  29. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/fetcher.d.ts +14 -0
  30. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  31. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  32. package/dist/iife/dialog-embed/src/components/PlayerEmbed/index.d.ts +73 -0
  33. package/dist/iife/dialog-embed/src/components/PlayerEmbed/tests/data.d.ts +3 -0
  34. package/dist/iife/dialog-embed/src/index.d.ts +2 -0
  35. package/dist/iife/dialog-embed/src/utils/env.d.ts +12 -0
  36. package/dist/iife/dialog-embed/src/utils/events.d.ts +2 -0
  37. package/dist/iife/embeds.iife.js +637 -344
  38. package/dist/iife/embeds.iife.js.map +1 -1
  39. package/dist/iife/player-embed/browser-910b9dcb.js +433 -0
  40. package/dist/iife/player-embed/browser-910b9dcb.js.map +1 -0
  41. package/dist/iife/player-embed/embed.iife.js +1585 -0
  42. package/dist/iife/player-embed/embed.iife.js.map +1 -0
  43. package/dist/iife/player-embed/embed.js +5 -0
  44. package/dist/iife/player-embed/embed.js.map +1 -0
  45. package/dist/iife/player-embed/index-154c6a23.js +32811 -0
  46. package/dist/iife/player-embed/index-154c6a23.js.map +1 -0
  47. package/dist/iife/player-embed/src/components/DialogEmbed/DialogOverlay.d.ts +19 -0
  48. package/dist/iife/player-embed/src/components/DialogEmbed/DialogPortal.d.ts +35 -0
  49. package/dist/iife/player-embed/src/components/DialogEmbed/index.d.ts +36 -0
  50. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  51. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/fetcher.d.ts +14 -0
  52. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  53. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  54. package/dist/iife/player-embed/src/components/PlayerEmbed/index.d.ts +73 -0
  55. package/dist/iife/player-embed/src/components/PlayerEmbed/tests/data.d.ts +3 -0
  56. package/dist/iife/player-embed/src/index.d.ts +2 -0
  57. package/dist/iife/player-embed/src/utils/env.d.ts +12 -0
  58. package/dist/iife/player-embed/src/utils/events.d.ts +2 -0
  59. package/package.json +13 -5
  60. package/src/components/DialogEmbed/Dialog.stories.ts +91 -0
  61. package/src/components/DialogEmbed/DialogOverlay.ts +131 -0
  62. package/src/components/DialogEmbed/DialogPortal.ts +126 -0
  63. package/src/components/DialogEmbed/index.ts +97 -0
  64. package/src/components/{Embed/Embed.stories.ts → PlayerEmbed/PlayerEmbed.stories.ts} +27 -14
  65. package/src/components/{Embed → PlayerEmbed}/controllers/event-forwarder.ts +6 -5
  66. package/src/components/PlayerEmbed/controllers/fetcher.ts +152 -0
  67. package/src/components/PlayerEmbed/controllers/tracking/index.ts +224 -0
  68. package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
  69. package/src/components/{Embed → PlayerEmbed}/index.ts +78 -27
  70. package/src/components/PlayerEmbed/tests/PlayerEmbed.spec.ts +80 -0
  71. package/src/components/PlayerEmbed/tests/data.ts +183 -0
  72. package/src/index.ts +2 -1
  73. package/dist/es/components/Embed/controllers/tracking.d.ts +0 -28
  74. package/dist/es/index.d.ts +0 -1
  75. package/src/components/Embed/controllers/fetcher.ts +0 -113
  76. package/src/components/Embed/controllers/tracking.ts +0 -261
  77. /package/dist/es/{utils → src/utils}/env.d.ts +0 -0
  78. /package/dist/es/{utils → src/utils}/events.d.ts +0 -0
@@ -0,0 +1,224 @@
1
+ import type { PlayerEmbed } from '../..';
2
+ import type { VideoEventDetail } from '@vouchfor/media-player';
3
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
4
+
5
+ import { findVouchId, getReportingMetadata, getUids } from './utils';
6
+ import { getEnvUrls } from '~/utils/env';
7
+
8
+ const MINIMUM_SEND_THRESHOLD = 1;
9
+
10
+ type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
11
+
12
+ type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
13
+ type TrackingPayload = {
14
+ vouchId?: string;
15
+ answerId?: string;
16
+ streamStart?: number;
17
+ streamEnd?: number;
18
+ };
19
+
20
+ type BatchEvent = {
21
+ event: TrackingEvent;
22
+ payload: TrackingPayload & {
23
+ time: string;
24
+ };
25
+ };
26
+
27
+ type TimeMap = {
28
+ [key: string]: number;
29
+ };
30
+
31
+ type BooleanMap = {
32
+ [key: string]: boolean;
33
+ };
34
+
35
+ class TrackingController implements ReactiveController {
36
+ host: PlayerEmbedHost;
37
+
38
+ private _batchedEvents: BatchEvent[] = [];
39
+ private _hasPlayed = false;
40
+ private _hasLoaded: BooleanMap = {};
41
+ private _answersViewed: BooleanMap = {};
42
+ private _streamStartTime: TimeMap = {};
43
+ private _streamLatestTime: TimeMap = {};
44
+ private _currentlyPlayingVideo: VideoEventDetail | null = null;
45
+
46
+ constructor(host: PlayerEmbedHost) {
47
+ this.host = host;
48
+ host.addController(this);
49
+ }
50
+
51
+ private _createTrackingEvent = (event: TrackingEvent, payload?: TrackingPayload) => {
52
+ const vouchId = findVouchId(payload, this.host.vouch);
53
+
54
+ if (!vouchId || this.host.disableTracking) {
55
+ return;
56
+ }
57
+
58
+ this._batchedEvents.push({
59
+ event,
60
+ payload: {
61
+ ...payload,
62
+ vouchId,
63
+ time: new Date().toISOString()
64
+ }
65
+ });
66
+ };
67
+
68
+ private _sendTrackingEvent = () => {
69
+ if (this._batchedEvents.length <= 0) {
70
+ return;
71
+ }
72
+
73
+ const { publicApiUrl } = getEnvUrls(this.host.env);
74
+ const { client, tab, request, visitor } = getUids(this.host.env);
75
+
76
+ navigator.sendBeacon(
77
+ `${publicApiUrl}/api/batchevents`,
78
+ JSON.stringify({
79
+ payload: {
80
+ events: this._batchedEvents
81
+ },
82
+ context: {
83
+ 'x-uid-client': client,
84
+ 'x-uid-tab': tab,
85
+ 'x-uid-request': request,
86
+ 'x-uid-visitor': visitor,
87
+ 'x-reporting-metadata': getReportingMetadata(this.host.trackingSource)
88
+ }
89
+ })
90
+ );
91
+
92
+ this._batchedEvents = [];
93
+ };
94
+
95
+ private _streamEnded = () => {
96
+ if (this._currentlyPlayingVideo) {
97
+ const { id, key } = this._currentlyPlayingVideo;
98
+ // Don't send a tracking event when seeking backwards
99
+ if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
100
+ // Send a video streamed event any time the stream ends to capture the time between starting
101
+ // the video and the video stopping for any reason (pausing, deleting the embed node or closing the browser)
102
+ this._createTrackingEvent('VIDEO_STREAMED', {
103
+ answerId: id,
104
+ streamStart: this._streamStartTime[key],
105
+ streamEnd: this._streamLatestTime[key]
106
+ });
107
+ }
108
+
109
+ // Make sure these events are only sent once by deleting the start and latest times
110
+ delete this._streamStartTime[key];
111
+ delete this._streamLatestTime[key];
112
+ }
113
+ };
114
+
115
+ private _handleVouchLoaded = ({ detail: vouchId }: CustomEvent<string>) => {
116
+ if (!vouchId) {
117
+ return;
118
+ }
119
+
120
+ // Only send loaded event once per session
121
+ if (!this._hasLoaded[vouchId]) {
122
+ this._createTrackingEvent('VOUCH_LOADED', { vouchId });
123
+ this._hasLoaded[vouchId] = true;
124
+ }
125
+ };
126
+
127
+ private _handlePlay = () => {
128
+ // Only send the video played event once per session
129
+ if (!this._hasPlayed) {
130
+ this._createTrackingEvent('VIDEO_PLAYED', {
131
+ streamStart: this.host.currentTime
132
+ });
133
+ this._hasPlayed = true;
134
+ }
135
+ };
136
+
137
+ private _handleVideoPlay = ({ detail: { id, key } }: CustomEvent<VideoEventDetail>) => {
138
+ // Only increment play count once per session
139
+ if (!this._answersViewed[key]) {
140
+ this._createTrackingEvent('VOUCH_RESPONSE_VIEWED', {
141
+ answerId: id
142
+ });
143
+ this._answersViewed[key] = true;
144
+ }
145
+ };
146
+
147
+ private _handleVideoTimeUpdate = ({ detail: { id, key, node } }: CustomEvent<VideoEventDetail>) => {
148
+ if (
149
+ // We only want to count any time that the video is actually playing
150
+ !this.host.paused &&
151
+ // Only update the latest time if this event fires for the currently active video
152
+ id === this.host.scene?.video?.id
153
+ ) {
154
+ this._currentlyPlayingVideo = { id, key, node };
155
+ this._streamLatestTime[key] = node.currentTime;
156
+
157
+ if (!this._streamStartTime[key]) {
158
+ this._streamStartTime[key] = node.currentTime;
159
+ this._streamLatestTime[key] = node.currentTime;
160
+ }
161
+ }
162
+ };
163
+
164
+ private _handleVideoPause = ({ detail: { id, key } }: CustomEvent<VideoEventDetail>) => {
165
+ if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
166
+ this._createTrackingEvent('VIDEO_STREAMED', {
167
+ answerId: id,
168
+ streamStart: this._streamStartTime[key],
169
+ streamEnd: this._streamLatestTime[key]
170
+ });
171
+ }
172
+ delete this._streamStartTime[key];
173
+ delete this._streamLatestTime[key];
174
+ };
175
+
176
+ private _pageUnloading = () => {
177
+ this._streamEnded();
178
+ this._sendTrackingEvent();
179
+ };
180
+
181
+ private _handleVisibilityChange = () => {
182
+ if (document.visibilityState === 'hidden') {
183
+ this._pageUnloading();
184
+ }
185
+ };
186
+
187
+ private _handlePageHide = () => {
188
+ this._pageUnloading();
189
+ };
190
+
191
+ hostConnected() {
192
+ requestAnimationFrame(() => {
193
+ if ('onvisibilitychange' in document) {
194
+ document.addEventListener('visibilitychange', this._handleVisibilityChange);
195
+ } else {
196
+ window.addEventListener('pagehide', this._handlePageHide);
197
+ }
198
+ this.host.addEventListener('vouch:loaded', this._handleVouchLoaded);
199
+ this.host.mediaPlayer?.addEventListener('play', this._handlePlay);
200
+ this.host.mediaPlayer?.addEventListener('video:play', this._handleVideoPlay);
201
+ this.host.mediaPlayer?.addEventListener('video:pause', this._handleVideoPause);
202
+ this.host.mediaPlayer?.addEventListener('video:timeupdate', this._handleVideoTimeUpdate);
203
+ });
204
+ }
205
+
206
+ hostDisconnected() {
207
+ // Send events if DOM node is destroyed
208
+ this._pageUnloading();
209
+
210
+ if ('onvisibilitychange' in document) {
211
+ document.removeEventListener('visibilitychange', this._handleVisibilityChange);
212
+ } else {
213
+ window.removeEventListener('pagehide', this._handlePageHide);
214
+ }
215
+ this.host.removeEventListener('vouch:loaded', this._handleVouchLoaded);
216
+ this.host.mediaPlayer?.removeEventListener('play', this._handlePlay);
217
+ this.host.mediaPlayer?.removeEventListener('video:play', this._handleVideoPlay);
218
+ this.host.mediaPlayer?.removeEventListener('video:pause', this._handleVideoPause);
219
+ this.host.mediaPlayer?.removeEventListener('video:timeupdate', this._handleVideoTimeUpdate);
220
+ }
221
+ }
222
+
223
+ export { TrackingController };
224
+ export type { TrackingEvent, TrackingPayload };
@@ -0,0 +1,95 @@
1
+ import { TEMPLATE_VERSION } from '@vouchfor/canvas-video';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+
4
+ import type { TrackingPayload } from '.';
5
+ import type { Vouch } from '@vouchfor/media-player';
6
+ import type { Environment } from '~/utils/env';
7
+
8
+ import packageJson from '~/../package.json';
9
+ import { getEnvUrls } from '~/utils/env';
10
+
11
+ function createVisitor(env: Environment) {
12
+ const { publicApiUrl } = getEnvUrls(env);
13
+ const visitorId = uuidv4();
14
+ navigator.sendBeacon(`${publicApiUrl}/api/visitor`, JSON.stringify({ visitorId }));
15
+ return visitorId;
16
+ }
17
+
18
+ function getUids(env: Environment) {
19
+ if (typeof window === 'undefined') {
20
+ return {
21
+ client: null,
22
+ tab: null,
23
+ request: uuidv4()
24
+ };
25
+ }
26
+
27
+ // Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
28
+ let visitorId = window.localStorage?.getItem?.('vouch-uid-visitor');
29
+ // Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
30
+ let clientId = window.localStorage?.getItem?.('vouch-uid-client');
31
+ // Persisted in session storage, so we can search for everything the user has done in a specific tab
32
+ let tabId = window.sessionStorage?.getItem?.('vouch-uid-tab');
33
+ // Not persisted, allows us to search for any logs related to a single FE request
34
+ // E.g. BE should pass this request ID through all other services to be able to group logs
35
+ const requestId = uuidv4();
36
+
37
+ // Cache uids
38
+ if (!visitorId) {
39
+ visitorId = createVisitor(env);
40
+ window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
41
+ }
42
+
43
+ if (!clientId) {
44
+ clientId = uuidv4();
45
+ window.localStorage?.setItem?.('vouch-uid-client', clientId);
46
+ }
47
+
48
+ if (!tabId) {
49
+ tabId = uuidv4();
50
+ window.sessionStorage?.setItem?.('vouch-uid-tab', tabId);
51
+ }
52
+
53
+ return {
54
+ client: clientId,
55
+ tab: tabId,
56
+ request: requestId,
57
+ visitor: visitorId
58
+ };
59
+ }
60
+
61
+ function findVouchId(payload?: TrackingPayload, vouch?: Vouch) {
62
+ if (payload && 'vouchId' in payload) {
63
+ return payload.vouchId;
64
+ }
65
+ return vouch?.id ?? null;
66
+ }
67
+
68
+ function getReportingMetadata(source = 'embedded_player') {
69
+ const [country, region] = Intl.DateTimeFormat().resolvedOptions().timeZone?.split?.('/') ?? [];
70
+
71
+ const utmParams: any = {};
72
+ [...new URLSearchParams(location.search).entries()].forEach(([key, value]) => {
73
+ if (/utm/.test(key)) {
74
+ const param = key.toLowerCase().replace(/[-_][a-z0-9]/g, (group) => group.slice(-1).toUpperCase());
75
+ utmParams[param] = value;
76
+ }
77
+ });
78
+
79
+ return {
80
+ source,
81
+ time: new Date(),
82
+ region,
83
+ country,
84
+ screenHeight: window.screen.height,
85
+ screenWidth: window.screen.width,
86
+ referrer: document.referrer,
87
+ currentUrl: location.href,
88
+ embedType: 'media-player-embed',
89
+ embedVersion: packageJson.version,
90
+ templateVersion: TEMPLATE_VERSION,
91
+ ...utmParams
92
+ };
93
+ }
94
+
95
+ export { getUids, findVouchId, getReportingMetadata };
@@ -1,11 +1,10 @@
1
- import { html, LitElement } from 'lit';
1
+ import { css, html, LitElement } from 'lit';
2
2
  import { customElement, property, state } from 'lit/decorators.js';
3
3
  import { ifDefined } from 'lit/directives/if-defined.js';
4
4
  import { createRef, ref } from 'lit/directives/ref.js';
5
5
 
6
- import type { Scene, TemplateInstance } from '@vouchfor/canvas-video';
6
+ import type { Scene, Scenes, TemplateInstance } from '@vouchfor/canvas-video';
7
7
  import type { MediaPlayer, MediaPlayerProps } from '@vouchfor/media-player';
8
- import type { Ref } from 'lit/directives/ref.js';
9
8
  import type { Environment } from '~/utils/env';
10
9
 
11
10
  import { EventForwardController } from './controllers/event-forwarder';
@@ -14,30 +13,41 @@ import { TrackingController } from './controllers/tracking';
14
13
 
15
14
  import '@vouchfor/media-player';
16
15
 
17
- type EmbedProps = Pick<MediaPlayerProps, 'data' | 'aspectRatio' | 'preload' | 'autoplay' | 'controls'> & {
16
+ type PlayerEmbedProps = Pick<MediaPlayerProps, 'data' | 'aspectRatio' | 'preload' | 'autoplay' | 'controls'> & {
18
17
  env: Environment;
19
18
  apiKey: string;
19
+ disableTracking?: boolean;
20
20
  trackingSource?: string;
21
21
  vouchId?: string;
22
22
  templateId?: string;
23
+ // Index of the questions to include starting from 1
24
+ questions?: number[];
23
25
  };
24
26
 
25
- @customElement('vouch-embed')
26
- class Embed extends LitElement {
27
- private _mediaPlayerRef: Ref<MediaPlayer> = createRef();
28
-
29
- @property({ type: Object, attribute: 'data' }) data: EmbedProps['data'];
30
- @property({ type: String }) vouchId: EmbedProps['vouchId'];
31
- @property({ type: String }) templateId: EmbedProps['templateId'];
32
-
33
- @property({ type: String }) env: EmbedProps['env'] = 'prod';
34
- @property({ type: String }) apiKey: EmbedProps['apiKey'] = '';
35
- @property({ type: String }) trackingSource: EmbedProps['trackingSource'] = 'embed';
36
-
37
- @property({ type: Array }) controls: EmbedProps['controls'];
38
- @property({ type: String }) preload: EmbedProps['preload'] = 'auto';
39
- @property({ type: Boolean }) autoplay: EmbedProps['autoplay'] = false;
40
- @property({ type: Number }) aspectRatio: EmbedProps['aspectRatio'] = 0;
27
+ @customElement('vouch-embed-player')
28
+ class PlayerEmbed extends LitElement {
29
+ static styles = [
30
+ css`
31
+ :host {
32
+ display: flex;
33
+ }
34
+ `
35
+ ];
36
+
37
+ @property({ type: Object }) data: PlayerEmbedProps['data'];
38
+ @property({ type: String }) vouchId: PlayerEmbedProps['vouchId'];
39
+ @property({ type: String }) templateId: PlayerEmbedProps['templateId'];
40
+ @property({ type: Array }) questions: PlayerEmbedProps['questions'];
41
+
42
+ @property({ type: String }) env: PlayerEmbedProps['env'] = 'prod';
43
+ @property({ type: String }) apiKey: PlayerEmbedProps['apiKey'] = '';
44
+ @property({ type: Boolean }) disableTracking: PlayerEmbedProps['disableTracking'] = false;
45
+ @property({ type: String }) trackingSource: PlayerEmbedProps['trackingSource'] = 'embedded_player';
46
+
47
+ @property({ type: Array }) controls: PlayerEmbedProps['controls'];
48
+ @property({ type: String }) preload: PlayerEmbedProps['preload'] = 'auto';
49
+ @property({ type: Boolean }) autoplay: PlayerEmbedProps['autoplay'] = false;
50
+ @property({ type: Number }) aspectRatio: PlayerEmbedProps['aspectRatio'] = 0;
41
51
 
42
52
  private eventController = new EventForwardController(this, [
43
53
  'durationchange',
@@ -57,6 +67,7 @@ class Embed extends LitElement {
57
67
  'waiting',
58
68
 
59
69
  'video:loadeddata',
70
+ 'video:seeking',
60
71
  'video:seeked',
61
72
  'video:play',
62
73
  'video:playing',
@@ -71,17 +82,23 @@ class Embed extends LitElement {
71
82
  // @ts-ignore
72
83
  private _trackingController = new TrackingController(this);
73
84
 
74
- @state() vouch: EmbedProps['data'];
85
+ @state() vouch: PlayerEmbedProps['data'];
75
86
  @state() template: TemplateInstance | undefined;
76
87
 
77
88
  get fetching() {
78
89
  return this._fetcherController.fetching;
79
90
  }
80
91
 
92
+ private _mediaPlayerRef = createRef<MediaPlayer>();
93
+
81
94
  get waiting() {
82
95
  return this._mediaPlayerRef.value?.waiting;
83
96
  }
84
97
 
98
+ get initialised() {
99
+ return this._mediaPlayerRef.value?.initialised;
100
+ }
101
+
85
102
  get seeking() {
86
103
  return this._mediaPlayerRef.value?.seeking;
87
104
  }
@@ -146,6 +163,10 @@ class Embed extends LitElement {
146
163
  return this._mediaPlayerRef.value?.scenes ?? [];
147
164
  }
148
165
 
166
+ get sceneConfig(): Scenes | null {
167
+ return this._mediaPlayerRef.value?.sceneConfig ?? null;
168
+ }
169
+
149
170
  get videoState() {
150
171
  return this._mediaPlayerRef.value?.videoState;
151
172
  }
@@ -162,13 +183,43 @@ class Embed extends LitElement {
162
183
  this._mediaPlayerRef.value?.pause();
163
184
  }
164
185
 
186
+ reset(time = 0, play = false) {
187
+ this._mediaPlayerRef.value?.reset(time, play);
188
+ }
189
+
165
190
  setScene(index: number) {
166
191
  this._mediaPlayerRef.value?.setScene(index);
167
192
  }
168
193
 
194
+ private _renderStyles() {
195
+ if (!this.aspectRatio) {
196
+ return html`
197
+ <style>
198
+ :host {
199
+ width: 100%;
200
+ height: 100%;
201
+ }
202
+ </style>
203
+ `;
204
+ }
205
+
206
+ if (typeof this.aspectRatio === 'number') {
207
+ return html`
208
+ <style>
209
+ :host {
210
+ aspect-ratio: ${this.aspectRatio};
211
+ }
212
+ </style>
213
+ `;
214
+ }
215
+
216
+ return null;
217
+ }
218
+
169
219
  render() {
170
220
  return html`
171
- <vmp-new-media-player
221
+ ${this._renderStyles()}
222
+ <vmp-media-player
172
223
  ${ref(this._mediaPlayerRef)}
173
224
  ${this.eventController.register()}
174
225
  ?autoplay=${this.autoplay}
@@ -178,22 +229,22 @@ class Embed extends LitElement {
178
229
  aspectRatio=${ifDefined(this.aspectRatio)}
179
230
  preload=${ifDefined(this.preload)}
180
231
  .controls=${this.controls}
181
- ></vmp-new-media-player>
232
+ ></vmp-media-player>
182
233
  `;
183
234
  }
184
235
  }
185
236
 
186
237
  declare global {
187
238
  interface HTMLElementTagNameMap {
188
- 'vouch-embed': Embed;
239
+ 'vouch-embed-player': PlayerEmbed;
189
240
  }
190
241
 
191
242
  namespace JSX {
192
243
  interface IntrinsicElements {
193
- 'vouch-embed': Embed;
244
+ 'vouch-embed-player': PlayerEmbed;
194
245
  }
195
246
  }
196
247
  }
197
248
 
198
- export { Embed };
199
- export type { EmbedProps };
249
+ export { PlayerEmbed };
250
+ export type { PlayerEmbedProps };
@@ -0,0 +1,80 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ import { expect, fixture, waitUntil } from '@open-wc/testing';
3
+ import { html } from 'lit';
4
+ import sinon from 'sinon';
5
+
6
+ import type { PlayerEmbed } from '../index.js';
7
+ import type { VideoMap } from '@vouchfor/media-player';
8
+
9
+ import { data } from './data.js';
10
+
11
+ // Can't use typescript aliases with esbuild file transforms apparently
12
+ // No idea what a good way to actually build before testing is, the examples give nothing
13
+ // https://modern-web.dev/guides/test-runner/typescript/
14
+ import '../../../test/lib/embeds.js';
15
+
16
+ function getVideo(videos: VideoMap) {
17
+ return Object.values(videos)[0];
18
+ }
19
+
20
+ function playerLoaded(player: PlayerEmbed) {
21
+ return waitUntil(
22
+ () => {
23
+ return player.mediaPlayer?.initialised;
24
+ },
25
+ 'Player has not loaded video',
26
+ { timeout: 20000 }
27
+ );
28
+ }
29
+
30
+ describe('Embeds', () => {
31
+ it('passes', async () => {
32
+ const player = await fixture<PlayerEmbed>(
33
+ html`<vouch-embed-player env="dev" .data=${data} aspectratio=${1}></vouch-embed-player>`
34
+ );
35
+ // @ts-ignore - accessing private property
36
+ const sendTrackingSpy = sinon.spy(player._trackingController, '_sendTrackingEvent');
37
+ // @ts-ignore - accessing private property
38
+ const createTrackingSpy = sinon.spy(player._trackingController, '_createTrackingEvent');
39
+
40
+ await playerLoaded(player);
41
+ // Have to mute the player because we can't programmatically play videos with sound
42
+ player.muted = true;
43
+ player.play();
44
+ expect(player.paused).eq(false);
45
+ await waitUntil(
46
+ () => {
47
+ // Video plays for 3 seconds
48
+ return (getVideo(player.mediaPlayer!.videos)?.node?.currentTime ?? 0) > 3;
49
+ },
50
+ 'Video did not play for 3 seconds',
51
+ { timeout: 20000 }
52
+ );
53
+ expect(getVideo(player.mediaPlayer!.videos)?.node?.paused).eq(false);
54
+ player.pause();
55
+ expect(getVideo(player.mediaPlayer!.videos)?.node?.paused).eq(true);
56
+ expect(sendTrackingSpy.callCount).to.be.eq(0);
57
+ // Destroy node because events are sent when node is removed from the document
58
+ player.remove();
59
+ expect(sendTrackingSpy.callCount).to.be.eq(1);
60
+ expect(createTrackingSpy.args[0]).to.eql([
61
+ 'VIDEO_PLAYED',
62
+ {
63
+ streamStart: 0
64
+ }
65
+ ]);
66
+ expect(createTrackingSpy.args[1]).to.eql([
67
+ 'VOUCH_RESPONSE_VIEWED',
68
+ {
69
+ answerId: '5c66bb3a-ed68-41a0-a601-a49865104418'
70
+ }
71
+ ]);
72
+ expect(createTrackingSpy.args[2][0]).to.eq('VIDEO_STREAMED');
73
+ // Remove streamStart and streamEnd as these are non-deterministic
74
+ expect({ ...createTrackingSpy.args[2][1], streamStart: undefined, streamEnd: undefined }).to.eql({
75
+ answerId: '5c66bb3a-ed68-41a0-a601-a49865104418',
76
+ streamStart: undefined,
77
+ streamEnd: undefined
78
+ });
79
+ });
80
+ });