@vouchfor/embeds 0.0.0-experiment.f97b5b2 → 0.0.0-experiment.ff9bc1d

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. package/dist/es/embeds.js +1015 -1376
  2. package/dist/es/embeds.js.map +1 -1
  3. package/dist/es/src/components/DialogEmbed/DialogOverlay.d.ts +20 -0
  4. package/dist/es/src/components/DialogEmbed/DialogPortal.d.ts +36 -0
  5. package/dist/es/src/components/DialogEmbed/index.d.ts +38 -0
  6. package/dist/es/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  7. package/dist/es/{components/Embed → src/components/PlayerEmbed}/controllers/fetcher.d.ts +5 -4
  8. package/dist/es/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  9. package/dist/es/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  10. package/dist/es/src/components/PlayerEmbed/index.d.ts +77 -0
  11. package/dist/es/src/components/PlayerEmbed/tests/data.d.ts +4 -0
  12. package/dist/es/src/components/PlayerEmbed/tests/media-data.d.ts +19 -0
  13. package/dist/es/src/index.d.ts +2 -0
  14. package/dist/iife/dialog-embed/embed.iife.js +1757 -0
  15. package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
  16. package/dist/iife/embeds.iife.js +769 -442
  17. package/dist/iife/embeds.iife.js.map +1 -1
  18. package/dist/iife/player-embed/embed.iife.js +1619 -0
  19. package/dist/iife/player-embed/embed.iife.js.map +1 -0
  20. package/package.json +44 -31
  21. package/src/components/DialogEmbed/Dialog.stories.ts +91 -0
  22. package/src/components/DialogEmbed/DialogOverlay.ts +131 -0
  23. package/src/components/DialogEmbed/DialogPortal.ts +126 -0
  24. package/src/components/DialogEmbed/index.ts +97 -0
  25. package/src/components/PlayerEmbed/MultiEmbed.stories.ts +135 -0
  26. package/src/components/{Embed/Embed.stories.ts → PlayerEmbed/PlayerEmbed.stories.ts} +42 -15
  27. package/src/components/{Embed → PlayerEmbed}/controllers/event-forwarder.ts +6 -5
  28. package/src/components/{Embed → PlayerEmbed}/controllers/fetcher.ts +36 -17
  29. package/src/components/PlayerEmbed/controllers/tracking/index.ts +224 -0
  30. package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
  31. package/src/components/{Embed → PlayerEmbed}/index.ts +93 -29
  32. package/src/components/PlayerEmbed/tests/PlayerEmbed.spec.ts +87 -0
  33. package/src/components/PlayerEmbed/tests/data.ts +227 -0
  34. package/src/components/PlayerEmbed/tests/media-data.ts +22 -0
  35. package/src/index.ts +2 -1
  36. package/dist/es/components/Embed/controllers/tracking.d.ts +0 -28
  37. package/dist/es/components/Embed/index.d.ts +0 -66
  38. package/dist/es/index.d.ts +0 -1
  39. package/src/components/Embed/controllers/tracking.ts +0 -271
  40. /package/dist/es/{utils → src/utils}/env.d.ts +0 -0
  41. /package/dist/es/{utils → src/utils}/events.d.ts +0 -0
@@ -1,66 +0,0 @@
1
- import { LitElement } from 'lit';
2
- import type { Scene, TemplateInstance } from '@vouchfor/canvas-video';
3
- import type { MediaPlayer, MediaPlayerProps } from '@vouchfor/media-player';
4
- import type { Environment } from '../../utils/env';
5
- type EmbedProps = Pick<MediaPlayerProps, 'data' | 'aspectRatio' | 'preload' | 'autoplay' | 'controls'> & {
6
- env: Environment;
7
- apiKey: string;
8
- enableTracking?: boolean;
9
- trackingSource?: string;
10
- vouchId?: string;
11
- templateId?: string;
12
- };
13
- declare class Embed extends LitElement {
14
- private _mediaPlayerRef;
15
- data: EmbedProps['data'];
16
- vouchId: EmbedProps['vouchId'];
17
- templateId: EmbedProps['templateId'];
18
- env: EmbedProps['env'];
19
- apiKey: EmbedProps['apiKey'];
20
- enableTracking: EmbedProps['enableTracking'];
21
- trackingSource: EmbedProps['trackingSource'];
22
- controls: EmbedProps['controls'];
23
- preload: EmbedProps['preload'];
24
- autoplay: EmbedProps['autoplay'];
25
- aspectRatio: EmbedProps['aspectRatio'];
26
- private eventController;
27
- private _fetcherController;
28
- private _trackingController;
29
- vouch: EmbedProps['data'];
30
- template: TemplateInstance | undefined;
31
- get fetching(): boolean;
32
- get waiting(): boolean | undefined;
33
- get seeking(): boolean | undefined;
34
- get paused(): boolean | undefined;
35
- get captions(): boolean | undefined;
36
- get fullscreen(): boolean | undefined;
37
- get duration(): number | undefined;
38
- set currentTime(value: number);
39
- get currentTime(): number;
40
- set playbackRate(value: number);
41
- get playbackRate(): number;
42
- set volume(value: number);
43
- get volume(): number;
44
- set muted(value: boolean);
45
- get muted(): boolean;
46
- get scene(): Scene | null;
47
- get scenes(): Scene[];
48
- get videoState(): import("@vouchfor/media-player/dist/src/components/MediaPlayer/controllers/scenes").VideoStateMap | undefined;
49
- get mediaPlayer(): MediaPlayer | undefined;
50
- play(): void;
51
- pause(): void;
52
- setScene(index: number): void;
53
- render(): import("lit").TemplateResult<1>;
54
- }
55
- declare global {
56
- interface HTMLElementTagNameMap {
57
- 'vouch-embed': Embed;
58
- }
59
- namespace JSX {
60
- interface IntrinsicElements {
61
- 'vouch-embed': Embed;
62
- }
63
- }
64
- }
65
- export { Embed };
66
- export type { EmbedProps };
@@ -1 +0,0 @@
1
- export { Embed } from './components/Embed';
@@ -1,271 +0,0 @@
1
- import { v4 as uuidv4 } from 'uuid';
2
-
3
- import type { Embed } from '..';
4
- import type { VideoEventDetail } from '@vouchfor/media-player';
5
- import type { ReactiveController, ReactiveControllerHost } from 'lit';
6
-
7
- import { getEnvUrls } from '~/utils/env';
8
-
9
- const STREAMED_THROTTLE = 10000;
10
-
11
- type EmbedHost = ReactiveControllerHost & Embed;
12
-
13
- type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
14
- type TrackingPayload = {
15
- vouchId: string;
16
- answerId?: string;
17
- streamStart?: number;
18
- streamEnd?: number;
19
- };
20
-
21
- type TimeMap = {
22
- [key: string]: number;
23
- };
24
-
25
- type BooleanMap = {
26
- [key: string]: boolean;
27
- };
28
-
29
- class TrackingController implements ReactiveController {
30
- host: EmbedHost;
31
-
32
- private _tabId: string | undefined = undefined;
33
- private _clientId: string | undefined = undefined;
34
- private _visitorId: string | undefined = undefined;
35
-
36
- private _hasPlayed = false;
37
- private _hasLoaded: BooleanMap = {};
38
- private _answersViewed: BooleanMap = {};
39
- private _streamedTime: TimeMap = {};
40
- private _streamedPrevTimestamp: TimeMap = {};
41
-
42
- constructor(host: EmbedHost) {
43
- this.host = host;
44
- host.addController(this);
45
- }
46
-
47
- private _findVouchId() {
48
- if (this.host.vouch) {
49
- if ('uuid' in this.host.vouch) {
50
- return this.host.vouch.uuid;
51
- }
52
- return this.host.vouch.id;
53
- }
54
- }
55
-
56
- private _createVisitor = (visitorId: string) => {
57
- const { publicApiUrl } = getEnvUrls(this.host.env);
58
- window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
59
- navigator.sendBeacon(`${publicApiUrl}/api/visitor`, JSON.stringify({ visitorId }));
60
- return visitorId;
61
- };
62
-
63
- private _getUids() {
64
- if (typeof window === 'undefined') {
65
- return {
66
- client: null,
67
- tab: null,
68
- request: uuidv4()
69
- };
70
- }
71
-
72
- // Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
73
- const visitorId =
74
- this._visitorId || window.localStorage?.getItem?.('vouch-uid-visitor') || this._createVisitor(uuidv4());
75
- // Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
76
- const clientId = this._clientId || window.localStorage?.getItem?.('vouch-uid-client') || uuidv4();
77
- // Persisted in session storage, so we can search for everything the user has done in a specific tab
78
- const tabId = this._tabId || window.sessionStorage?.getItem?.('vouch-uid-tab') || uuidv4();
79
- // Not persisted, allows us to search for any logs related to a single FE request
80
- // E.g. BE should pass this request ID through all other services to be able to group logs
81
- const requestId = uuidv4();
82
-
83
- // Cache and persist uids
84
- if (visitorId !== this._visitorId) {
85
- this._visitorId = visitorId;
86
- window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
87
- }
88
-
89
- if (clientId !== this._clientId) {
90
- this._clientId = clientId;
91
- window.localStorage?.setItem?.('vouch-uid-client', clientId);
92
- }
93
-
94
- if (tabId !== this._tabId) {
95
- this._tabId = tabId;
96
- window.sessionStorage?.setItem?.('vouch-uid-tab', tabId);
97
- }
98
-
99
- return {
100
- client: clientId,
101
- tab: tabId,
102
- request: requestId,
103
- visitor: visitorId
104
- };
105
- }
106
-
107
- private _getReportingMetadata = () => {
108
- const [country, region] = Intl.DateTimeFormat().resolvedOptions().timeZone?.split?.('/') ?? [];
109
-
110
- const utmParams: any = {};
111
- [...new URLSearchParams(location.search).entries()].forEach(([key, value]) => {
112
- if (/utm/.test(key)) {
113
- const param = key.toLowerCase().replace(/[-_][a-z0-9]/g, (group) => group.slice(-1).toUpperCase());
114
- utmParams[param] = value;
115
- }
116
- });
117
-
118
- return {
119
- source: this.host.trackingSource,
120
- time: new Date(),
121
- region,
122
- country,
123
- screenHeight: window.screen.height,
124
- screenWidth: window.screen.width,
125
- referrer: document.referrer,
126
- currentUrl: location.href,
127
- ...utmParams
128
- };
129
- };
130
-
131
- private _sendTrackingEvent = (event: TrackingEvent, payload: TrackingPayload) => {
132
- const { publicApiUrl } = getEnvUrls(this.host.env);
133
- const { client, tab, request, visitor } = this._getUids();
134
-
135
- if (this.host.enableTracking) {
136
- navigator.sendBeacon(
137
- `${publicApiUrl}/api/events`,
138
- JSON.stringify({
139
- event,
140
- payload,
141
- context: {
142
- 'x-uid-client': client,
143
- 'x-uid-tab': tab,
144
- 'x-uid-request': request,
145
- 'x-uid-visitor': visitor,
146
- 'x-reporting-metadata': this._getReportingMetadata()
147
- }
148
- })
149
- );
150
- }
151
- };
152
-
153
- private _handleVouchLoaded = ({ detail: vouchId }: CustomEvent<string>) => {
154
- if (!vouchId) {
155
- return;
156
- }
157
-
158
- // Only send loaded event once per session
159
- if (!this._hasLoaded[vouchId]) {
160
- this._sendTrackingEvent('VOUCH_LOADED', {
161
- vouchId
162
- });
163
- this._hasLoaded[vouchId] = true;
164
- }
165
- };
166
-
167
- private _handlePlay = () => {
168
- const vouchId = this._findVouchId();
169
-
170
- if (!vouchId) {
171
- return;
172
- }
173
-
174
- // Only send the video played event once per session
175
- if (!this._hasPlayed) {
176
- this._sendTrackingEvent('VIDEO_PLAYED', {
177
- vouchId,
178
- streamStart: this.host.currentTime
179
- });
180
- this._hasPlayed = true;
181
- }
182
- };
183
-
184
- private _handleVideoPlay = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
185
- const vouchId = this._findVouchId();
186
-
187
- if (!vouchId) {
188
- return;
189
- }
190
- // Only increment play count once per session
191
- if (!this._answersViewed[id]) {
192
- this._sendTrackingEvent('VOUCH_RESPONSE_VIEWED', {
193
- vouchId,
194
- answerId: id
195
- });
196
- this._answersViewed[id] = true;
197
- }
198
- this._streamedTime[id] = node.currentTime;
199
- this._streamedPrevTimestamp[id] = Date.now();
200
- };
201
-
202
- private _handleVideoTimeUpdate = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
203
- const vouchId = this._findVouchId();
204
-
205
- if (!vouchId) {
206
- return;
207
- }
208
- const currentTimestamp = Date.now();
209
- if (
210
- node.currentTime &&
211
- !node.paused &&
212
- !this.host.paused &&
213
- // Only fire the video seeked event when this video is the active one
214
- id === this.host.scene?.video?.id &&
215
- // Throttle the frequency that we send streamed events while playing
216
- currentTimestamp - this._streamedPrevTimestamp[id] > STREAMED_THROTTLE
217
- ) {
218
- this._sendTrackingEvent('VIDEO_STREAMED', {
219
- vouchId,
220
- answerId: id,
221
- streamStart: this._streamedTime[id],
222
- streamEnd: node.currentTime
223
- });
224
- this._streamedTime[id] = node.currentTime;
225
- this._streamedPrevTimestamp[id] = currentTimestamp;
226
- }
227
- };
228
-
229
- private _handleVideoPause = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
230
- const vouchId = this._findVouchId();
231
-
232
- if (!vouchId) {
233
- return;
234
- }
235
-
236
- // Don't send a tracking event if the video pauses when seeking backwards
237
- if (node.currentTime > this._streamedTime[id]) {
238
- // Send a video streamed event any time the video pauses then reset the streamed state
239
- // We do this to capture the last bit of time that the video was played between the previous
240
- // stream event and the video being paused manually or stopping because it ended
241
- this._sendTrackingEvent('VIDEO_STREAMED', {
242
- vouchId,
243
- answerId: id,
244
- streamStart: this._streamedTime[id],
245
- streamEnd: node.currentTime
246
- });
247
- }
248
- delete this._streamedTime[id];
249
- delete this._streamedPrevTimestamp[id];
250
- };
251
-
252
- hostConnected() {
253
- requestAnimationFrame(() => {
254
- this.host.addEventListener('vouch:loaded', this._handleVouchLoaded);
255
- this.host.mediaPlayer?.addEventListener('play', this._handlePlay);
256
- this.host.mediaPlayer?.addEventListener('video:play', this._handleVideoPlay);
257
- this.host.mediaPlayer?.addEventListener('video:pause', this._handleVideoPause);
258
- this.host.mediaPlayer?.addEventListener('video:timeupdate', this._handleVideoTimeUpdate);
259
- });
260
- }
261
-
262
- hostDisconnected() {
263
- this.host.removeEventListener('vouch:loaded', this._handleVouchLoaded);
264
- this.host.mediaPlayer?.removeEventListener('play', this._handlePlay);
265
- this.host.mediaPlayer?.removeEventListener('video:play', this._handleVideoPlay);
266
- this.host.mediaPlayer?.removeEventListener('video:pause', this._handleVideoPause);
267
- this.host.mediaPlayer?.removeEventListener('video:timeupdate', this._handleVideoTimeUpdate);
268
- }
269
- }
270
-
271
- export { TrackingController };
File without changes
File without changes