@vouchfor/embeds 0.0.0-experiment.a63622c → 0.0.0-experiment.a835fdb

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