@vouchfor/embeds 0.0.0-experiment.f112aaa → 0.0.0-experiment.f289234
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/es/embeds.js +1143 -7
- package/dist/es/embeds.js.map +1 -1
- package/dist/es/src/components/DialogEmbed/DialogOverlay.d.ts +20 -0
- package/dist/es/src/components/DialogEmbed/DialogPortal.d.ts +36 -0
- package/dist/es/src/components/DialogEmbed/index.d.ts +38 -0
- package/dist/es/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
- package/dist/es/src/components/{Embed → PlayerEmbed}/controllers/fetcher.d.ts +4 -4
- package/dist/es/src/components/{Embed/controllers/tracking.d.ts → PlayerEmbed/controllers/tracking/index.d.ts} +14 -11
- package/dist/es/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
- package/dist/es/src/components/{Embed → PlayerEmbed}/index.d.ts +27 -22
- package/dist/es/src/components/PlayerEmbed/tests/data.d.ts +3 -0
- package/dist/es/src/index.d.ts +2 -1
- package/dist/iife/dialog-embed/embed.iife.js +1755 -0
- package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
- package/dist/iife/embeds.iife.js +578 -381
- package/dist/iife/embeds.iife.js.map +1 -1
- package/dist/iife/player-embed/embed.iife.js +1617 -0
- package/dist/iife/player-embed/embed.iife.js.map +1 -0
- package/package.json +43 -31
- package/src/components/DialogEmbed/Dialog.stories.ts +91 -0
- package/src/components/DialogEmbed/DialogOverlay.ts +131 -0
- package/src/components/DialogEmbed/DialogPortal.ts +126 -0
- package/src/components/DialogEmbed/index.ts +97 -0
- package/src/components/{Embed/Embed.stories.ts → PlayerEmbed/PlayerEmbed.stories.ts} +30 -16
- package/src/components/{Embed → PlayerEmbed}/controllers/event-forwarder.ts +6 -5
- package/src/components/{Embed → PlayerEmbed}/controllers/fetcher.ts +11 -11
- package/src/components/{Embed/controllers/tracking.ts → PlayerEmbed/controllers/tracking/index.ts} +47 -117
- package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
- package/src/components/{Embed → PlayerEmbed}/index.ts +61 -23
- package/src/components/PlayerEmbed/tests/PlayerEmbed.spec.ts +80 -0
- package/src/components/PlayerEmbed/tests/data.ts +183 -0
- package/src/index.ts +2 -1
- package/dist/es/browser-f5bec026.js +0 -433
- package/dist/es/browser-f5bec026.js.map +0 -1
- package/dist/es/index-6ce8276f.js +0 -9947
- package/dist/es/index-6ce8276f.js.map +0 -1
@@ -1,29 +1,29 @@
|
|
1
1
|
import { Task } from '@lit/task';
|
2
2
|
import { v4 as uuidv4 } from 'uuid';
|
3
3
|
|
4
|
-
import type {
|
4
|
+
import type { PlayerEmbed, PlayerEmbedProps } from '..';
|
5
5
|
import type { ReactiveControllerHost } from 'lit';
|
6
6
|
import type { Environment } from '~/utils/env';
|
7
7
|
|
8
8
|
import { getEnvUrls } from '~/utils/env';
|
9
9
|
|
10
|
-
type
|
10
|
+
type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
|
11
11
|
|
12
12
|
type FetchTaskDeps = [
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
PlayerEmbedProps['env'],
|
14
|
+
PlayerEmbedProps['apiKey'],
|
15
|
+
PlayerEmbedProps['data'],
|
16
|
+
PlayerEmbedProps['vouchId'],
|
17
|
+
PlayerEmbedProps['templateId']
|
18
18
|
];
|
19
19
|
|
20
|
-
type FilterTaskDeps = [
|
20
|
+
type FilterTaskDeps = [PlayerEmbedProps['data'], PlayerEmbedProps['questions']];
|
21
21
|
|
22
22
|
class FetcherController {
|
23
|
-
host:
|
23
|
+
host: PlayerEmbedHost;
|
24
24
|
|
25
25
|
private _fetching = false;
|
26
|
-
private _vouch:
|
26
|
+
private _vouch: PlayerEmbedProps['data'];
|
27
27
|
|
28
28
|
set fetching(value) {
|
29
29
|
if (this._fetching !== value) {
|
@@ -97,7 +97,7 @@ class FetcherController {
|
|
97
97
|
return template;
|
98
98
|
};
|
99
99
|
|
100
|
-
constructor(host:
|
100
|
+
constructor(host: PlayerEmbedHost) {
|
101
101
|
this.host = host;
|
102
102
|
new Task<FetchTaskDeps, void>(
|
103
103
|
this.host,
|
package/src/components/{Embed/controllers/tracking.ts → PlayerEmbed/controllers/tracking/index.ts}
RENAMED
@@ -1,16 +1,13 @@
|
|
1
|
-
import {
|
2
|
-
import { v4 as uuidv4 } from 'uuid';
|
3
|
-
|
4
|
-
import type { Embed } from '..';
|
1
|
+
import type { PlayerEmbed } from '../..';
|
5
2
|
import type { VideoEventDetail } from '@vouchfor/media-player';
|
6
3
|
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
7
4
|
|
8
|
-
import
|
5
|
+
import { findVouchId, getReportingMetadata, getUids } from './utils';
|
9
6
|
import { getEnvUrls } from '~/utils/env';
|
10
7
|
|
11
8
|
const MINIMUM_SEND_THRESHOLD = 1;
|
12
9
|
|
13
|
-
type
|
10
|
+
type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
|
14
11
|
|
15
12
|
type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
|
16
13
|
type TrackingPayload = {
|
@@ -20,6 +17,13 @@ type TrackingPayload = {
|
|
20
17
|
streamEnd?: number;
|
21
18
|
};
|
22
19
|
|
20
|
+
type BatchEvent = {
|
21
|
+
event: TrackingEvent;
|
22
|
+
payload: TrackingPayload & {
|
23
|
+
time: string;
|
24
|
+
};
|
25
|
+
};
|
26
|
+
|
23
27
|
type TimeMap = {
|
24
28
|
[key: string]: number;
|
25
29
|
};
|
@@ -29,12 +33,9 @@ type BooleanMap = {
|
|
29
33
|
};
|
30
34
|
|
31
35
|
class TrackingController implements ReactiveController {
|
32
|
-
host:
|
33
|
-
|
34
|
-
private _tabId: string | undefined = undefined;
|
35
|
-
private _clientId: string | undefined = undefined;
|
36
|
-
private _visitorId: string | undefined = undefined;
|
36
|
+
host: PlayerEmbedHost;
|
37
37
|
|
38
|
+
private _batchedEvents: BatchEvent[] = [];
|
38
39
|
private _hasPlayed = false;
|
39
40
|
private _hasLoaded: BooleanMap = {};
|
40
41
|
private _answersViewed: BooleanMap = {};
|
@@ -42,125 +43,53 @@ class TrackingController implements ReactiveController {
|
|
42
43
|
private _streamLatestTime: TimeMap = {};
|
43
44
|
private _currentlyPlayingVideo: VideoEventDetail | null = null;
|
44
45
|
|
45
|
-
constructor(host:
|
46
|
+
constructor(host: PlayerEmbedHost) {
|
46
47
|
this.host = host;
|
47
48
|
host.addController(this);
|
48
49
|
}
|
49
50
|
|
50
|
-
private
|
51
|
-
|
52
|
-
return payload.vouchId;
|
53
|
-
}
|
54
|
-
if (this.host.vouch) {
|
55
|
-
return this.host.vouch.id;
|
56
|
-
}
|
57
|
-
return null;
|
58
|
-
}
|
59
|
-
|
60
|
-
private _createVisitor = (visitorId: string) => {
|
61
|
-
const { publicApiUrl } = getEnvUrls(this.host.env);
|
62
|
-
window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
|
63
|
-
navigator.sendBeacon(`${publicApiUrl}/api/visitor`, JSON.stringify({ visitorId }));
|
64
|
-
return visitorId;
|
65
|
-
};
|
66
|
-
|
67
|
-
private _getUids() {
|
68
|
-
if (typeof window === 'undefined') {
|
69
|
-
return {
|
70
|
-
client: null,
|
71
|
-
tab: null,
|
72
|
-
request: uuidv4()
|
73
|
-
};
|
74
|
-
}
|
75
|
-
|
76
|
-
// Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
|
77
|
-
const visitorId =
|
78
|
-
this._visitorId || window.localStorage?.getItem?.('vouch-uid-visitor') || this._createVisitor(uuidv4());
|
79
|
-
// Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
|
80
|
-
const clientId = this._clientId || window.localStorage?.getItem?.('vouch-uid-client') || uuidv4();
|
81
|
-
// Persisted in session storage, so we can search for everything the user has done in a specific tab
|
82
|
-
const tabId = this._tabId || window.sessionStorage?.getItem?.('vouch-uid-tab') || uuidv4();
|
83
|
-
// Not persisted, allows us to search for any logs related to a single FE request
|
84
|
-
// E.g. BE should pass this request ID through all other services to be able to group logs
|
85
|
-
const requestId = uuidv4();
|
86
|
-
|
87
|
-
// Cache and persist uids
|
88
|
-
if (visitorId !== this._visitorId) {
|
89
|
-
this._visitorId = visitorId;
|
90
|
-
window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
|
91
|
-
}
|
92
|
-
|
93
|
-
if (clientId !== this._clientId) {
|
94
|
-
this._clientId = clientId;
|
95
|
-
window.localStorage?.setItem?.('vouch-uid-client', clientId);
|
96
|
-
}
|
51
|
+
private _createTrackingEvent = (event: TrackingEvent, payload?: TrackingPayload) => {
|
52
|
+
const vouchId = findVouchId(payload, this.host.vouch);
|
97
53
|
|
98
|
-
if (
|
99
|
-
|
100
|
-
window.sessionStorage?.setItem?.('vouch-uid-tab', tabId);
|
54
|
+
if (!vouchId || this.host.disableTracking) {
|
55
|
+
return;
|
101
56
|
}
|
102
57
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
private _getReportingMetadata = () => {
|
112
|
-
const [country, region] = Intl.DateTimeFormat().resolvedOptions().timeZone?.split?.('/') ?? [];
|
113
|
-
|
114
|
-
const utmParams: any = {};
|
115
|
-
[...new URLSearchParams(location.search).entries()].forEach(([key, value]) => {
|
116
|
-
if (/utm/.test(key)) {
|
117
|
-
const param = key.toLowerCase().replace(/[-_][a-z0-9]/g, (group) => group.slice(-1).toUpperCase());
|
118
|
-
utmParams[param] = value;
|
58
|
+
this._batchedEvents.push({
|
59
|
+
event,
|
60
|
+
payload: {
|
61
|
+
...payload,
|
62
|
+
vouchId,
|
63
|
+
time: new Date().toISOString()
|
119
64
|
}
|
120
65
|
});
|
121
|
-
|
122
|
-
return {
|
123
|
-
source: this.host.trackingSource,
|
124
|
-
time: new Date(),
|
125
|
-
region,
|
126
|
-
country,
|
127
|
-
screenHeight: window.screen.height,
|
128
|
-
screenWidth: window.screen.width,
|
129
|
-
referrer: document.referrer,
|
130
|
-
currentUrl: location.href,
|
131
|
-
embedVersion: packageJson.version,
|
132
|
-
templateVersion: TEMPLATE_VERSION,
|
133
|
-
...utmParams
|
134
|
-
};
|
135
66
|
};
|
136
67
|
|
137
|
-
private _sendTrackingEvent = (
|
138
|
-
|
139
|
-
|
140
|
-
if (!vouchId || this.host.disableTracking) {
|
68
|
+
private _sendTrackingEvent = () => {
|
69
|
+
if (this._batchedEvents.length <= 0) {
|
141
70
|
return;
|
142
71
|
}
|
143
72
|
|
144
73
|
const { publicApiUrl } = getEnvUrls(this.host.env);
|
145
|
-
const { client, tab, request, visitor } = this.
|
74
|
+
const { client, tab, request, visitor } = getUids(this.host.env);
|
146
75
|
|
147
76
|
navigator.sendBeacon(
|
148
|
-
`${publicApiUrl}/api/
|
77
|
+
`${publicApiUrl}/api/batchevents`,
|
149
78
|
JSON.stringify({
|
150
|
-
event,
|
151
79
|
payload: {
|
152
|
-
|
153
|
-
vouchId
|
80
|
+
events: this._batchedEvents
|
154
81
|
},
|
155
82
|
context: {
|
156
83
|
'x-uid-client': client,
|
157
84
|
'x-uid-tab': tab,
|
158
85
|
'x-uid-request': request,
|
159
86
|
'x-uid-visitor': visitor,
|
160
|
-
'x-reporting-metadata': this.
|
87
|
+
'x-reporting-metadata': getReportingMetadata(this.host.trackingSource)
|
161
88
|
}
|
162
89
|
})
|
163
90
|
);
|
91
|
+
|
92
|
+
this._batchedEvents = [];
|
164
93
|
};
|
165
94
|
|
166
95
|
private _streamEnded = () => {
|
@@ -170,7 +99,7 @@ class TrackingController implements ReactiveController {
|
|
170
99
|
if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
|
171
100
|
// Send a video streamed event any time the stream ends to capture the time between starting
|
172
101
|
// the video and the video stopping for any reason (pausing, deleting the embed node or closing the browser)
|
173
|
-
this.
|
102
|
+
this._createTrackingEvent('VIDEO_STREAMED', {
|
174
103
|
answerId: id,
|
175
104
|
streamStart: this._streamStartTime[key],
|
176
105
|
streamEnd: this._streamLatestTime[key]
|
@@ -190,7 +119,7 @@ class TrackingController implements ReactiveController {
|
|
190
119
|
|
191
120
|
// Only send loaded event once per session
|
192
121
|
if (!this._hasLoaded[vouchId]) {
|
193
|
-
this.
|
122
|
+
this._createTrackingEvent('VOUCH_LOADED', { vouchId });
|
194
123
|
this._hasLoaded[vouchId] = true;
|
195
124
|
}
|
196
125
|
};
|
@@ -198,26 +127,21 @@ class TrackingController implements ReactiveController {
|
|
198
127
|
private _handlePlay = () => {
|
199
128
|
// Only send the video played event once per session
|
200
129
|
if (!this._hasPlayed) {
|
201
|
-
this.
|
130
|
+
this._createTrackingEvent('VIDEO_PLAYED', {
|
202
131
|
streamStart: this.host.currentTime
|
203
132
|
});
|
204
133
|
this._hasPlayed = true;
|
205
134
|
}
|
206
135
|
};
|
207
136
|
|
208
|
-
private _handleVideoPlay = ({ detail: { id, key
|
137
|
+
private _handleVideoPlay = ({ detail: { id, key } }: CustomEvent<VideoEventDetail>) => {
|
209
138
|
// Only increment play count once per session
|
210
139
|
if (!this._answersViewed[key]) {
|
211
|
-
this.
|
140
|
+
this._createTrackingEvent('VOUCH_RESPONSE_VIEWED', {
|
212
141
|
answerId: id
|
213
142
|
});
|
214
143
|
this._answersViewed[key] = true;
|
215
144
|
}
|
216
|
-
|
217
|
-
if (!this._streamStartTime[key]) {
|
218
|
-
this._streamStartTime[key] = node.currentTime;
|
219
|
-
this._streamLatestTime[key] = node.currentTime;
|
220
|
-
}
|
221
145
|
};
|
222
146
|
|
223
147
|
private _handleVideoTimeUpdate = ({ detail: { id, key, node } }: CustomEvent<VideoEventDetail>) => {
|
@@ -229,12 +153,17 @@ class TrackingController implements ReactiveController {
|
|
229
153
|
) {
|
230
154
|
this._currentlyPlayingVideo = { id, key, node };
|
231
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
|
+
}
|
232
161
|
}
|
233
162
|
};
|
234
163
|
|
235
164
|
private _handleVideoPause = ({ detail: { id, key } }: CustomEvent<VideoEventDetail>) => {
|
236
165
|
if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
|
237
|
-
this.
|
166
|
+
this._createTrackingEvent('VIDEO_STREAMED', {
|
238
167
|
answerId: id,
|
239
168
|
streamStart: this._streamStartTime[key],
|
240
169
|
streamEnd: this._streamLatestTime[key]
|
@@ -246,9 +175,7 @@ class TrackingController implements ReactiveController {
|
|
246
175
|
|
247
176
|
private _pageUnloading = () => {
|
248
177
|
this._streamEnded();
|
249
|
-
|
250
|
-
// time in stream ended so that there is no times to send and the pause event does nothing
|
251
|
-
this.host.pause();
|
178
|
+
this._sendTrackingEvent();
|
252
179
|
};
|
253
180
|
|
254
181
|
private _handleVisibilityChange = () => {
|
@@ -277,7 +204,9 @@ class TrackingController implements ReactiveController {
|
|
277
204
|
}
|
278
205
|
|
279
206
|
hostDisconnected() {
|
280
|
-
|
207
|
+
// Send events if DOM node is destroyed
|
208
|
+
this._pageUnloading();
|
209
|
+
|
281
210
|
if ('onvisibilitychange' in document) {
|
282
211
|
document.removeEventListener('visibilitychange', this._handleVisibilityChange);
|
283
212
|
} else {
|
@@ -292,3 +221,4 @@ class TrackingController implements ReactiveController {
|
|
292
221
|
}
|
293
222
|
|
294
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/video-utils';
|
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 };
|
@@ -5,7 +5,6 @@ import { createRef, ref } from 'lit/directives/ref.js';
|
|
5
5
|
|
6
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,7 +13,10 @@ import { TrackingController } from './controllers/tracking';
|
|
14
13
|
|
15
14
|
import '@vouchfor/media-player';
|
16
15
|
|
17
|
-
type
|
16
|
+
type PlayerEmbedProps = Pick<
|
17
|
+
MediaPlayerProps,
|
18
|
+
'data' | 'aspectRatio' | 'language' | 'preload' | 'autoplay' | 'controls'
|
19
|
+
> & {
|
18
20
|
env: Environment;
|
19
21
|
apiKey: string;
|
20
22
|
disableTracking?: boolean;
|
@@ -25,8 +27,8 @@ type EmbedProps = Pick<MediaPlayerProps, 'data' | 'aspectRatio' | 'preload' | 'a
|
|
25
27
|
questions?: number[];
|
26
28
|
};
|
27
29
|
|
28
|
-
@customElement('vouch-embed')
|
29
|
-
class
|
30
|
+
@customElement('vouch-embed-player')
|
31
|
+
class PlayerEmbed extends LitElement {
|
30
32
|
static styles = [
|
31
33
|
css`
|
32
34
|
:host {
|
@@ -35,22 +37,21 @@ class Embed extends LitElement {
|
|
35
37
|
`
|
36
38
|
];
|
37
39
|
|
38
|
-
|
40
|
+
@property({ type: Object }) data: PlayerEmbedProps['data'];
|
41
|
+
@property({ type: String }) vouchId: PlayerEmbedProps['vouchId'];
|
42
|
+
@property({ type: String }) templateId: PlayerEmbedProps['templateId'];
|
43
|
+
@property({ type: Array }) questions: PlayerEmbedProps['questions'];
|
39
44
|
|
40
|
-
@property({ type:
|
41
|
-
@property({ type: String })
|
42
|
-
@property({ type:
|
43
|
-
@property({ type:
|
45
|
+
@property({ type: String }) env: PlayerEmbedProps['env'] = 'prod';
|
46
|
+
@property({ type: String }) apiKey: PlayerEmbedProps['apiKey'] = '';
|
47
|
+
@property({ type: Boolean }) disableTracking: PlayerEmbedProps['disableTracking'] = false;
|
48
|
+
@property({ type: String }) trackingSource: PlayerEmbedProps['trackingSource'] = 'embedded_player';
|
44
49
|
|
45
|
-
@property({ type:
|
46
|
-
@property({ type: String })
|
47
|
-
@property({ type: Boolean })
|
48
|
-
@property({ type:
|
49
|
-
|
50
|
-
@property({ type: Array }) controls: EmbedProps['controls'];
|
51
|
-
@property({ type: String }) preload: EmbedProps['preload'] = 'auto';
|
52
|
-
@property({ type: Boolean }) autoplay: EmbedProps['autoplay'] = false;
|
53
|
-
@property({ type: Number }) aspectRatio: EmbedProps['aspectRatio'] = 0;
|
50
|
+
@property({ type: Array }) controls: PlayerEmbedProps['controls'];
|
51
|
+
@property({ type: String }) preload: PlayerEmbedProps['preload'] = 'auto';
|
52
|
+
@property({ type: Boolean }) autoplay: PlayerEmbedProps['autoplay'] = false;
|
53
|
+
@property({ type: Number }) aspectRatio: PlayerEmbedProps['aspectRatio'] = 0;
|
54
|
+
@property({ type: String }) language?: MediaPlayerProps['language'];
|
54
55
|
|
55
56
|
private eventController = new EventForwardController(this, [
|
56
57
|
'durationchange',
|
@@ -85,17 +86,23 @@ class Embed extends LitElement {
|
|
85
86
|
// @ts-ignore
|
86
87
|
private _trackingController = new TrackingController(this);
|
87
88
|
|
88
|
-
@state() vouch:
|
89
|
+
@state() vouch: PlayerEmbedProps['data'];
|
89
90
|
@state() template: TemplateInstance | undefined;
|
90
91
|
|
91
92
|
get fetching() {
|
92
93
|
return this._fetcherController.fetching;
|
93
94
|
}
|
94
95
|
|
96
|
+
private _mediaPlayerRef = createRef<MediaPlayer>();
|
97
|
+
|
95
98
|
get waiting() {
|
96
99
|
return this._mediaPlayerRef.value?.waiting;
|
97
100
|
}
|
98
101
|
|
102
|
+
get initialised() {
|
103
|
+
return this._mediaPlayerRef.value?.initialised;
|
104
|
+
}
|
105
|
+
|
99
106
|
get seeking() {
|
100
107
|
return this._mediaPlayerRef.value?.seeking;
|
101
108
|
}
|
@@ -180,12 +187,42 @@ class Embed extends LitElement {
|
|
180
187
|
this._mediaPlayerRef.value?.pause();
|
181
188
|
}
|
182
189
|
|
190
|
+
reset(time = 0, play = false) {
|
191
|
+
this._mediaPlayerRef.value?.reset(time, play);
|
192
|
+
}
|
193
|
+
|
183
194
|
setScene(index: number) {
|
184
195
|
this._mediaPlayerRef.value?.setScene(index);
|
185
196
|
}
|
186
197
|
|
198
|
+
private _renderStyles() {
|
199
|
+
if (!this.aspectRatio) {
|
200
|
+
return html`
|
201
|
+
<style>
|
202
|
+
:host {
|
203
|
+
width: 100%;
|
204
|
+
height: 100%;
|
205
|
+
}
|
206
|
+
</style>
|
207
|
+
`;
|
208
|
+
}
|
209
|
+
|
210
|
+
if (typeof this.aspectRatio === 'number') {
|
211
|
+
return html`
|
212
|
+
<style>
|
213
|
+
:host {
|
214
|
+
aspect-ratio: ${this.aspectRatio};
|
215
|
+
}
|
216
|
+
</style>
|
217
|
+
`;
|
218
|
+
}
|
219
|
+
|
220
|
+
return null;
|
221
|
+
}
|
222
|
+
|
187
223
|
render() {
|
188
224
|
return html`
|
225
|
+
${this._renderStyles()}
|
189
226
|
<vmp-media-player
|
190
227
|
${ref(this._mediaPlayerRef)}
|
191
228
|
${this.eventController.register()}
|
@@ -195,6 +232,7 @@ class Embed extends LitElement {
|
|
195
232
|
.template=${this.template}
|
196
233
|
aspectRatio=${ifDefined(this.aspectRatio)}
|
197
234
|
preload=${ifDefined(this.preload)}
|
235
|
+
language=${ifDefined(this.language)}
|
198
236
|
.controls=${this.controls}
|
199
237
|
></vmp-media-player>
|
200
238
|
`;
|
@@ -203,15 +241,15 @@ class Embed extends LitElement {
|
|
203
241
|
|
204
242
|
declare global {
|
205
243
|
interface HTMLElementTagNameMap {
|
206
|
-
'vouch-embed':
|
244
|
+
'vouch-embed-player': PlayerEmbed;
|
207
245
|
}
|
208
246
|
|
209
247
|
namespace JSX {
|
210
248
|
interface IntrinsicElements {
|
211
|
-
'vouch-embed':
|
249
|
+
'vouch-embed-player': PlayerEmbed;
|
212
250
|
}
|
213
251
|
}
|
214
252
|
}
|
215
253
|
|
216
|
-
export {
|
217
|
-
export type {
|
254
|
+
export { PlayerEmbed };
|
255
|
+
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
|
+
});
|