@vouchfor/embeds 0.0.0-experiment.a91d11e → 0.0.0-experiment.a9fe71a

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