@vouchfor/embeds 0.0.0-experiment.7e7b0a8 → 0.0.0-experiment.860a574

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. package/dist/es/browser-87d44538.js +733 -0
  2. package/dist/es/browser-87d44538.js.map +1 -0
  3. package/dist/es/embeds.js +12 -1510
  4. package/dist/es/embeds.js.map +1 -1
  5. package/dist/es/index-7de167be.js +16482 -0
  6. package/dist/es/index-7de167be.js.map +1 -0
  7. package/dist/es/src/components/DialogEmbed/DialogOverlay.d.ts +19 -0
  8. package/dist/es/src/components/DialogEmbed/DialogPortal.d.ts +35 -0
  9. package/dist/es/src/components/DialogEmbed/index.d.ts +36 -0
  10. package/dist/es/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  11. package/dist/es/{components/Embed → src/components/PlayerEmbed}/controllers/fetcher.d.ts +5 -4
  12. package/dist/es/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  13. package/dist/es/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  14. package/dist/es/{components/Embed → src/components/PlayerEmbed}/index.d.ts +27 -20
  15. package/dist/es/src/components/PlayerEmbed/tests/data.d.ts +3 -0
  16. package/dist/es/src/index.d.ts +2 -0
  17. package/dist/iife/dialog-embed/browser-bb22ee61.js +433 -0
  18. package/dist/iife/dialog-embed/browser-bb22ee61.js.map +1 -0
  19. package/dist/iife/dialog-embed/embed.iife.js +1722 -0
  20. package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
  21. package/dist/iife/dialog-embed/embed.js +5 -0
  22. package/dist/iife/dialog-embed/embed.js.map +1 -0
  23. package/dist/iife/dialog-embed/index-cdd85bb3.js +33284 -0
  24. package/dist/iife/dialog-embed/index-cdd85bb3.js.map +1 -0
  25. package/dist/iife/dialog-embed/src/components/DialogEmbed/DialogOverlay.d.ts +19 -0
  26. package/dist/iife/dialog-embed/src/components/DialogEmbed/DialogPortal.d.ts +35 -0
  27. package/dist/iife/dialog-embed/src/components/DialogEmbed/index.d.ts +36 -0
  28. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  29. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/fetcher.d.ts +14 -0
  30. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  31. package/dist/iife/dialog-embed/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  32. package/dist/iife/dialog-embed/src/components/PlayerEmbed/index.d.ts +73 -0
  33. package/dist/iife/dialog-embed/src/components/PlayerEmbed/tests/data.d.ts +3 -0
  34. package/dist/iife/dialog-embed/src/index.d.ts +2 -0
  35. package/dist/iife/dialog-embed/src/utils/env.d.ts +12 -0
  36. package/dist/iife/dialog-embed/src/utils/events.d.ts +2 -0
  37. package/dist/iife/embeds.iife.js +645 -357
  38. package/dist/iife/embeds.iife.js.map +1 -1
  39. package/dist/iife/player-embed/browser-bbe0a31c.js +433 -0
  40. package/dist/iife/player-embed/browser-bbe0a31c.js.map +1 -0
  41. package/dist/iife/player-embed/embed.iife.js +1584 -0
  42. package/dist/iife/player-embed/embed.iife.js.map +1 -0
  43. package/dist/iife/player-embed/embed.js +5 -0
  44. package/dist/iife/player-embed/embed.js.map +1 -0
  45. package/dist/iife/player-embed/index-ba069531.js +32839 -0
  46. package/dist/iife/player-embed/index-ba069531.js.map +1 -0
  47. package/dist/iife/player-embed/src/components/DialogEmbed/DialogOverlay.d.ts +19 -0
  48. package/dist/iife/player-embed/src/components/DialogEmbed/DialogPortal.d.ts +35 -0
  49. package/dist/iife/player-embed/src/components/DialogEmbed/index.d.ts +36 -0
  50. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +15 -0
  51. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/fetcher.d.ts +14 -0
  52. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
  53. package/dist/iife/player-embed/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
  54. package/dist/iife/player-embed/src/components/PlayerEmbed/index.d.ts +73 -0
  55. package/dist/iife/player-embed/src/components/PlayerEmbed/tests/data.d.ts +3 -0
  56. package/dist/iife/player-embed/src/index.d.ts +2 -0
  57. package/dist/iife/player-embed/src/utils/env.d.ts +12 -0
  58. package/dist/iife/player-embed/src/utils/events.d.ts +2 -0
  59. package/package.json +13 -5
  60. package/src/components/DialogEmbed/Dialog.stories.ts +91 -0
  61. package/src/components/DialogEmbed/DialogOverlay.ts +131 -0
  62. package/src/components/DialogEmbed/DialogPortal.ts +126 -0
  63. package/src/components/DialogEmbed/index.ts +97 -0
  64. package/src/components/{Embed/Embed.stories.ts → PlayerEmbed/PlayerEmbed.stories.ts} +27 -14
  65. package/src/components/{Embed → PlayerEmbed}/controllers/event-forwarder.ts +6 -5
  66. package/src/components/{Embed → PlayerEmbed}/controllers/fetcher.ts +36 -17
  67. package/src/components/PlayerEmbed/controllers/tracking/index.ts +224 -0
  68. package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
  69. package/src/components/{Embed → PlayerEmbed}/index.ts +76 -28
  70. package/src/components/PlayerEmbed/tests/PlayerEmbed.spec.ts +80 -0
  71. package/src/components/PlayerEmbed/tests/data.ts +183 -0
  72. package/src/index.ts +2 -1
  73. package/dist/es/components/Embed/controllers/tracking.d.ts +0 -29
  74. package/dist/es/index.d.ts +0 -1
  75. package/src/components/Embed/controllers/tracking.ts +0 -268
  76. /package/dist/es/{utils → src/utils}/env.d.ts +0 -0
  77. /package/dist/es/{utils → src/utils}/events.d.ts +0 -0
@@ -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