@vouchfor/embeds 0.0.0-experiment.607fdcd → 0.0.0-experiment.620e7b8

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