@vouchfor/embeds 0.0.0-experiment.f9b576b → 0.0.0-experiment.fcbbe7e

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. package/dist/es/embeds.js +1144 -13
  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/PlayerEmbed/controllers/fetcher.d.ts +14 -0
  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/es/src/utils/env.d.ts +12 -0
  15. package/dist/es/src/utils/events.d.ts +2 -0
  16. package/dist/iife/dialog-embed/embed.iife.js +4042 -0
  17. package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
  18. package/dist/iife/embeds.iife.js +3061 -370
  19. package/dist/iife/embeds.iife.js.map +1 -1
  20. package/dist/iife/player-embed/embed.iife.js +3904 -0
  21. package/dist/iife/player-embed/embed.iife.js.map +1 -0
  22. package/package.json +52 -34
  23. package/src/components/DialogEmbed/Dialog.stories.ts +91 -0
  24. package/src/components/DialogEmbed/DialogOverlay.ts +131 -0
  25. package/src/components/DialogEmbed/DialogPortal.ts +126 -0
  26. package/src/components/DialogEmbed/index.ts +97 -0
  27. package/src/components/PlayerEmbed/MultiEmbed.stories.ts +135 -0
  28. package/src/components/PlayerEmbed/PlayerEmbed.stories.ts +93 -0
  29. package/src/components/PlayerEmbed/controllers/event-forwarder.ts +48 -0
  30. package/src/components/PlayerEmbed/controllers/fetcher.ts +152 -0
  31. package/src/components/PlayerEmbed/controllers/tracking/index.ts +224 -0
  32. package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
  33. package/src/components/PlayerEmbed/index.ts +262 -0
  34. package/src/components/PlayerEmbed/tests/PlayerEmbed.spec.ts +87 -0
  35. package/src/components/PlayerEmbed/tests/data.ts +227 -0
  36. package/src/components/PlayerEmbed/tests/media-data.ts +22 -0
  37. package/src/index.ts +2 -1
  38. package/src/utils/env.ts +64 -0
  39. package/src/utils/events.ts +13 -0
  40. package/dist/es/components/InlineEmbed.d.ts +0 -12
  41. package/dist/es/components/index.d.ts +0 -2
  42. package/dist/es/index.d.ts +0 -1
  43. package/src/components/InlineEmbed.stories.ts +0 -119
  44. package/src/components/InlineEmbed.ts +0 -18
  45. package/src/components/index.ts +0 -2
@@ -0,0 +1,135 @@
1
+ import { html } from 'lit';
2
+ import { ifDefined } from 'lit/directives/if-defined.js';
3
+
4
+ import type { PlayerEmbedProps } from '.';
5
+ import type { Meta, StoryObj } from '@storybook/web-components';
6
+
7
+ import '.';
8
+
9
+ type MultiEmbedArgs = PlayerEmbedProps & {
10
+ apiKey1?: string;
11
+ apiKey2?: string;
12
+ apiKey3?: string;
13
+ vouchId1?: string;
14
+ vouchId2?: string;
15
+ vouchId3?: string;
16
+ showVouch?: boolean;
17
+ };
18
+
19
+ const _MultiEmbed = ({
20
+ vouchId1,
21
+ vouchId2,
22
+ vouchId3,
23
+ templateId,
24
+ questions,
25
+ preload,
26
+ autoplay,
27
+ env,
28
+ apiKey1,
29
+ apiKey2,
30
+ apiKey3,
31
+ controls,
32
+ aspectRatio
33
+ }: MultiEmbedArgs) => {
34
+ return html`
35
+ <div style="height: 33vh">
36
+ <vouch-embed-player
37
+ env=${ifDefined(env)}
38
+ apiKey=${ifDefined(apiKey1)}
39
+ vouchId=${ifDefined(vouchId1)}
40
+ templateId=${ifDefined(templateId)}
41
+ .questions=${questions}
42
+ .controls=${controls}
43
+ ?autoplay=${autoplay}
44
+ preload=${ifDefined(preload)}
45
+ aspectRatio=${ifDefined(aspectRatio)}
46
+ @error=${console.log}
47
+ ></vouch-embed-player>
48
+ </div>
49
+ <div style="height: 33vh">
50
+ <vouch-embed-player
51
+ env=${ifDefined(env)}
52
+ apiKey=${ifDefined(apiKey2)}
53
+ vouchId=${ifDefined(vouchId2)}
54
+ templateId=${ifDefined(templateId)}
55
+ .questions=${questions}
56
+ .controls=${controls}
57
+ ?autoplay=${autoplay}
58
+ preload=${ifDefined(preload)}
59
+ aspectRatio=${ifDefined(aspectRatio)}
60
+ @error=${console.log}
61
+ ></vouch-embed-player>
62
+ </div>
63
+ <div style="height: 33vh">
64
+ <vouch-embed-player
65
+ env=${ifDefined(env)}
66
+ apiKey=${ifDefined(apiKey3)}
67
+ vouchId=${ifDefined(vouchId3)}
68
+ templateId=${ifDefined(templateId)}
69
+ .questions=${questions}
70
+ .controls=${controls}
71
+ ?autoplay=${autoplay}
72
+ preload=${ifDefined(preload)}
73
+ aspectRatio=${ifDefined(aspectRatio)}
74
+ @error=${console.log}
75
+ ></vouch-embed-player>
76
+ </div>
77
+ `;
78
+ };
79
+
80
+ // More on how to set up stories at: https://storybook.js.org/docs/web-components/writing-stories/introduction
81
+ const meta = {
82
+ title: 'Embeds',
83
+ tags: ['autodocs'],
84
+ render: (args) => _MultiEmbed(args),
85
+ component: 'vouch-embed-player'
86
+ } satisfies Meta<PlayerEmbedProps>;
87
+
88
+ type Story = StoryObj<MultiEmbedArgs>;
89
+
90
+ const MultiPlayer: Story = {
91
+ args: {
92
+ env: 'dev',
93
+ apiKey1: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
94
+ vouchId1: '6JQEIPeStt',
95
+ apiKey2: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
96
+ vouchId2: '6JQEIPeStt',
97
+ apiKey3: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
98
+ vouchId3: '6JQEIPeStt',
99
+ templateId: '357fc118-e179-4171-9446-ff2b8e9d1b29',
100
+ questions: [],
101
+ aspectRatio: 0,
102
+ preload: 'none',
103
+ autoplay: false,
104
+ controls: [
105
+ 'progress',
106
+ 'play-large',
107
+ 'navigation',
108
+ 'play',
109
+ 'volume',
110
+ 'current-time',
111
+ 'duration',
112
+ 'speed',
113
+ 'captions',
114
+ 'fullscreen',
115
+ 'preview',
116
+ 'languages'
117
+ ]
118
+ },
119
+ argTypes: {
120
+ env: {
121
+ control: 'radio',
122
+ options: ['local', 'dev', 'staging', 'prod']
123
+ },
124
+ preload: {
125
+ control: 'radio',
126
+ options: ['auto', 'none']
127
+ }
128
+ },
129
+ parameters: {
130
+ layout: 'fullscreen'
131
+ }
132
+ };
133
+
134
+ export default meta;
135
+ export { MultiPlayer };
@@ -0,0 +1,93 @@
1
+ import { html } from 'lit';
2
+ import { ifDefined } from 'lit/directives/if-defined.js';
3
+
4
+ import type { PlayerEmbedProps } from '.';
5
+ import type { Meta, StoryObj } from '@storybook/web-components';
6
+
7
+ import '.';
8
+
9
+ type PlayerEmbedArgs = PlayerEmbedProps & {
10
+ showVouch?: boolean;
11
+ };
12
+
13
+ const _PlayerEmbed = ({
14
+ vouchId,
15
+ templateId,
16
+ questions,
17
+ preload,
18
+ autoplay,
19
+ env,
20
+ apiKey,
21
+ controls,
22
+ aspectRatio
23
+ }: PlayerEmbedArgs) => {
24
+ return html`
25
+ <div style="height: 100vh">
26
+ <vouch-embed-player
27
+ env=${ifDefined(env)}
28
+ apiKey=${ifDefined(apiKey)}
29
+ vouchId=${ifDefined(vouchId)}
30
+ templateId=${ifDefined(templateId)}
31
+ .questions=${questions}
32
+ .controls=${controls}
33
+ ?autoplay=${autoplay}
34
+ preload=${ifDefined(preload)}
35
+ aspectRatio=${ifDefined(aspectRatio)}
36
+ @error=${console.log}
37
+ ></vouch-embed-player>
38
+ </div>
39
+ `;
40
+ };
41
+
42
+ // More on how to set up stories at: https://storybook.js.org/docs/web-components/writing-stories/introduction
43
+ const meta = {
44
+ title: 'Embeds',
45
+ tags: ['autodocs'],
46
+ render: (args) => _PlayerEmbed(args),
47
+ component: 'vouch-embed-player'
48
+ } satisfies Meta<PlayerEmbedProps>;
49
+
50
+ type Story = StoryObj<PlayerEmbedArgs>;
51
+
52
+ const Player: Story = {
53
+ args: {
54
+ env: 'dev',
55
+ apiKey: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
56
+ vouchId: '6JQEIPeStt',
57
+ templateId: '357fc118-e179-4171-9446-ff2b8e9d1b29',
58
+ questions: [],
59
+ aspectRatio: 0,
60
+ preload: 'none',
61
+ autoplay: false,
62
+ controls: [
63
+ 'progress',
64
+ 'play-large',
65
+ 'navigation',
66
+ 'play',
67
+ 'volume',
68
+ 'current-time',
69
+ 'duration',
70
+ 'speed',
71
+ 'captions',
72
+ 'fullscreen',
73
+ 'preview',
74
+ 'languages'
75
+ ]
76
+ },
77
+ argTypes: {
78
+ env: {
79
+ control: 'radio',
80
+ options: ['local', 'dev', 'staging', 'prod']
81
+ },
82
+ preload: {
83
+ control: 'radio',
84
+ options: ['auto', 'none']
85
+ }
86
+ },
87
+ parameters: {
88
+ layout: 'fullscreen'
89
+ }
90
+ };
91
+
92
+ export default meta;
93
+ export { Player };
@@ -0,0 +1,48 @@
1
+ import { createRef, ref } from 'lit/directives/ref.js';
2
+
3
+ import type { PlayerEmbed } from '..';
4
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
5
+ import type { DirectiveResult } from 'lit/directive.js';
6
+ import type { Ref } from 'lit/directives/ref.js';
7
+
8
+ import { forwardEvent } from '~/utils/events';
9
+
10
+ type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
11
+
12
+ class EventForwardController implements ReactiveController {
13
+ host: PlayerEmbedHost;
14
+
15
+ private _events: string[] = [];
16
+ private _cleanup: (() => void)[] = [];
17
+ private _forwardElementRef: Ref<HTMLElement> = createRef();
18
+
19
+ constructor(host: PlayerEmbedHost, events: string[]) {
20
+ this.host = host;
21
+ this._events = events;
22
+ host.addController(this);
23
+ }
24
+
25
+ register(): DirectiveResult {
26
+ return ref(this._forwardElementRef);
27
+ }
28
+
29
+ hostConnected() {
30
+ // Guaranteed to have the element ref in the RAF callback because of how the lit lifecycle works
31
+ requestAnimationFrame(() => {
32
+ this._events.forEach((event) => {
33
+ if (this._forwardElementRef.value) {
34
+ this._cleanup.push(forwardEvent(event, this._forwardElementRef.value, this.host));
35
+ }
36
+ });
37
+ });
38
+ }
39
+
40
+ hostDisconnected() {
41
+ this._cleanup.forEach((fn) => {
42
+ fn();
43
+ });
44
+ this._cleanup = [];
45
+ }
46
+ }
47
+
48
+ export { EventForwardController };
@@ -0,0 +1,152 @@
1
+ import { Task } from '@lit/task';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+
4
+ import type { PlayerEmbed, PlayerEmbedProps } from '..';
5
+ import type { ReactiveControllerHost } from 'lit';
6
+ import type { Environment } from '~/utils/env';
7
+
8
+ import { getEnvUrls } from '~/utils/env';
9
+
10
+ type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
11
+
12
+ type FetchTaskDeps = [
13
+ PlayerEmbedProps['env'],
14
+ PlayerEmbedProps['apiKey'],
15
+ PlayerEmbedProps['data'],
16
+ PlayerEmbedProps['vouchId'],
17
+ PlayerEmbedProps['templateId']
18
+ ];
19
+
20
+ type FilterTaskDeps = [PlayerEmbedProps['data'], PlayerEmbedProps['questions']];
21
+
22
+ class FetcherController {
23
+ host: PlayerEmbedHost;
24
+
25
+ private _fetching = false;
26
+ private _vouch: PlayerEmbedProps['data'];
27
+
28
+ set fetching(value) {
29
+ if (this._fetching !== value) {
30
+ this._fetching = value;
31
+ this.host.requestUpdate();
32
+ }
33
+ }
34
+ get fetching() {
35
+ return this._fetching;
36
+ }
37
+
38
+ private getVouch = async (env: Environment, apiKey: string, vouchId: string) => {
39
+ const { embedApiUrl } = getEnvUrls(env);
40
+
41
+ const cacheCheck = uuidv4();
42
+ const res = await fetch(`${embedApiUrl}/vouches/${vouchId}`, {
43
+ method: 'GET',
44
+ headers: [
45
+ ['X-Api-Key', apiKey],
46
+ ['X-Cache-Check', cacheCheck]
47
+ ]
48
+ });
49
+
50
+ const vouch = await res.json();
51
+ this.host.dispatchEvent(new CustomEvent('vouch:loaded', { detail: vouch?.id }));
52
+
53
+ // HACK: we're currently using API Gateway caching on the embed API without any invalidation logic,
54
+ // so to ensure that the cache stays up to date, whenever we detect a cache hit we trigger another
55
+ // API call with the `Cache-Control` header which will re-fill the cache
56
+ const resCacheCheck = res?.headers?.get('X-Cache-Check');
57
+ if (resCacheCheck !== cacheCheck) {
58
+ fetch(`${embedApiUrl}/vouches/${vouchId}`, {
59
+ method: 'GET',
60
+ headers: [
61
+ ['X-Api-Key', apiKey],
62
+ ['Cache-Control', 'max-age=0']
63
+ ]
64
+ });
65
+ }
66
+
67
+ return vouch;
68
+ };
69
+
70
+ private getTemplate = async (env: Environment, apiKey: string, templateId: string) => {
71
+ const { embedApiUrl } = getEnvUrls(env);
72
+
73
+ const cacheCheck = uuidv4();
74
+ const res = await fetch(`${embedApiUrl}/templates/${templateId}`, {
75
+ method: 'GET',
76
+ headers: [
77
+ ['X-Api-Key', apiKey],
78
+ ['X-Cache-Check', cacheCheck]
79
+ ]
80
+ });
81
+ const template = await res.json();
82
+
83
+ // HACK: we're currently using API Gateway caching on the embed API without any invalidation logic,
84
+ // so to ensure that the cache stays up to date, whenever we detect a cache hit we trigger another
85
+ // API call with the `Cache-Control` header which will re-fill the cache
86
+ const resCacheCheck = res?.headers?.get('X-Cache-Check');
87
+ if (resCacheCheck !== cacheCheck) {
88
+ fetch(`${embedApiUrl}/templates/${templateId}`, {
89
+ method: 'GET',
90
+ headers: [
91
+ ['X-Api-Key', apiKey],
92
+ ['Cache-Control', 'max-age=0']
93
+ ]
94
+ });
95
+ }
96
+
97
+ return template;
98
+ };
99
+
100
+ constructor(host: PlayerEmbedHost) {
101
+ this.host = host;
102
+ new Task<FetchTaskDeps, void>(
103
+ this.host,
104
+ async ([env, apiKey, data, vouchId, templateId]: FetchTaskDeps) => {
105
+ try {
106
+ host.vouch = undefined;
107
+ host.template = undefined;
108
+
109
+ if (data) {
110
+ let template;
111
+ if (templateId) {
112
+ this.fetching = true;
113
+ template = await this.getTemplate(env, apiKey, templateId);
114
+ }
115
+ this._vouch = data;
116
+ host.template = template ?? data?.settings?.template?.instance;
117
+ } else if (vouchId) {
118
+ this.fetching = true;
119
+
120
+ const [vouch, template] = await Promise.all([
121
+ this.getVouch(env, apiKey, vouchId),
122
+ templateId ? this.getTemplate(env, apiKey, templateId) : null
123
+ ]);
124
+ this._vouch = vouch;
125
+ host.template = template ?? vouch?.settings?.template?.instance;
126
+ }
127
+ } finally {
128
+ this.fetching = false;
129
+ }
130
+ },
131
+ () => [host.env, host.apiKey, host.data, host.vouchId, host.templateId]
132
+ );
133
+
134
+ // This second task is to be able to filter the vouch without fetching it again if only the questions changed
135
+ new Task<FilterTaskDeps, void>(
136
+ this.host,
137
+ ([vouch, questions]: FilterTaskDeps) => {
138
+ host.vouch = vouch
139
+ ? {
140
+ ...vouch,
141
+ questions: {
142
+ items: vouch?.questions.items.filter((_, index) => !questions?.length || questions?.includes(index + 1))
143
+ }
144
+ }
145
+ : undefined;
146
+ },
147
+ () => [this._vouch, host.questions]
148
+ );
149
+ }
150
+ }
151
+
152
+ export { FetcherController };
@@ -0,0 +1,224 @@
1
+ import type { PlayerEmbed } from '../..';
2
+ import type { MediaEventDetail } from '@vouchfor/media-player';
3
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
4
+
5
+ import { findVouchId, getReportingMetadata, getUids } from './utils';
6
+ import { getEnvUrls } from '~/utils/env';
7
+
8
+ const MINIMUM_SEND_THRESHOLD = 1;
9
+
10
+ type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
11
+
12
+ type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
13
+ type TrackingPayload = {
14
+ vouchId?: string;
15
+ answerId?: string;
16
+ streamStart?: number;
17
+ streamEnd?: number;
18
+ };
19
+
20
+ type BatchEvent = {
21
+ event: TrackingEvent;
22
+ payload: TrackingPayload & {
23
+ time: string;
24
+ };
25
+ };
26
+
27
+ type TimeMap = {
28
+ [key: string]: number;
29
+ };
30
+
31
+ type BooleanMap = {
32
+ [key: string]: boolean;
33
+ };
34
+
35
+ class TrackingController implements ReactiveController {
36
+ host: PlayerEmbedHost;
37
+
38
+ private _batchedEvents: BatchEvent[] = [];
39
+ private _hasPlayed = false;
40
+ private _hasLoaded: BooleanMap = {};
41
+ private _answersViewed: BooleanMap = {};
42
+ private _streamStartTime: TimeMap = {};
43
+ private _streamLatestTime: TimeMap = {};
44
+ private _currentlyPlayingVideo: MediaEventDetail | null = null;
45
+
46
+ constructor(host: PlayerEmbedHost) {
47
+ this.host = host;
48
+ host.addController(this);
49
+ }
50
+
51
+ private _createTrackingEvent = (event: TrackingEvent, payload?: TrackingPayload) => {
52
+ const vouchId = findVouchId(payload, this.host.vouch);
53
+
54
+ if (!vouchId || this.host.disableTracking) {
55
+ return;
56
+ }
57
+
58
+ this._batchedEvents.push({
59
+ event,
60
+ payload: {
61
+ ...payload,
62
+ vouchId,
63
+ time: new Date().toISOString()
64
+ }
65
+ });
66
+ };
67
+
68
+ private _sendTrackingEvent = () => {
69
+ if (this._batchedEvents.length <= 0) {
70
+ return;
71
+ }
72
+
73
+ const { publicApiUrl } = getEnvUrls(this.host.env);
74
+ const { client, tab, request, visitor } = getUids(this.host.env);
75
+
76
+ navigator.sendBeacon(
77
+ `${publicApiUrl}/api/batchevents`,
78
+ JSON.stringify({
79
+ payload: {
80
+ events: this._batchedEvents
81
+ },
82
+ context: {
83
+ 'x-uid-client': client,
84
+ 'x-uid-tab': tab,
85
+ 'x-uid-request': request,
86
+ 'x-uid-visitor': visitor,
87
+ 'x-reporting-metadata': getReportingMetadata(this.host.trackingSource)
88
+ }
89
+ })
90
+ );
91
+
92
+ this._batchedEvents = [];
93
+ };
94
+
95
+ private _streamEnded = () => {
96
+ if (this._currentlyPlayingVideo) {
97
+ const { id, key } = this._currentlyPlayingVideo;
98
+ // Don't send a tracking event when seeking backwards
99
+ if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
100
+ // Send a video streamed event any time the stream ends to capture the time between starting
101
+ // the video and the video stopping for any reason (pausing, deleting the embed node or closing the browser)
102
+ this._createTrackingEvent('VIDEO_STREAMED', {
103
+ answerId: id,
104
+ streamStart: this._streamStartTime[key],
105
+ streamEnd: this._streamLatestTime[key]
106
+ });
107
+ }
108
+
109
+ // Make sure these events are only sent once by deleting the start and latest times
110
+ delete this._streamStartTime[key];
111
+ delete this._streamLatestTime[key];
112
+ }
113
+ };
114
+
115
+ private _handleVouchLoaded = ({ detail: vouchId }: CustomEvent<string>) => {
116
+ if (!vouchId) {
117
+ return;
118
+ }
119
+
120
+ // Only send loaded event once per session
121
+ if (!this._hasLoaded[vouchId]) {
122
+ this._createTrackingEvent('VOUCH_LOADED', { vouchId });
123
+ this._hasLoaded[vouchId] = true;
124
+ }
125
+ };
126
+
127
+ private _handlePlay = () => {
128
+ // Only send the video played event once per session
129
+ if (!this._hasPlayed) {
130
+ this._createTrackingEvent('VIDEO_PLAYED', {
131
+ streamStart: this.host.currentTime
132
+ });
133
+ this._hasPlayed = true;
134
+ }
135
+ };
136
+
137
+ private _handleVideoPlay = ({ detail: { id, key } }: CustomEvent<MediaEventDetail>) => {
138
+ // Only increment play count once per session
139
+ if (!this._answersViewed[key]) {
140
+ this._createTrackingEvent('VOUCH_RESPONSE_VIEWED', {
141
+ answerId: id
142
+ });
143
+ this._answersViewed[key] = true;
144
+ }
145
+ };
146
+
147
+ private _handleVideoTimeUpdate = ({ detail: { id, key, node } }: CustomEvent<MediaEventDetail>) => {
148
+ if (
149
+ // We only want to count any time that the video is actually playing
150
+ !this.host.paused
151
+ // Only update the latest time if this event fires for the currently active video
152
+ // id === this.host.scene?.video?.id
153
+ ) {
154
+ this._currentlyPlayingVideo = { id, key, node };
155
+ this._streamLatestTime[key] = node.currentTime;
156
+
157
+ if (!this._streamStartTime[key]) {
158
+ this._streamStartTime[key] = node.currentTime;
159
+ this._streamLatestTime[key] = node.currentTime;
160
+ }
161
+ }
162
+ };
163
+
164
+ private _handleVideoPause = ({ detail: { id, key } }: CustomEvent<MediaEventDetail>) => {
165
+ if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
166
+ this._createTrackingEvent('VIDEO_STREAMED', {
167
+ answerId: id,
168
+ streamStart: this._streamStartTime[key],
169
+ streamEnd: this._streamLatestTime[key]
170
+ });
171
+ }
172
+ delete this._streamStartTime[key];
173
+ delete this._streamLatestTime[key];
174
+ };
175
+
176
+ private _pageUnloading = () => {
177
+ this._streamEnded();
178
+ this._sendTrackingEvent();
179
+ };
180
+
181
+ private _handleVisibilityChange = () => {
182
+ if (document.visibilityState === 'hidden') {
183
+ this._pageUnloading();
184
+ }
185
+ };
186
+
187
+ private _handlePageHide = () => {
188
+ this._pageUnloading();
189
+ };
190
+
191
+ hostConnected() {
192
+ requestAnimationFrame(() => {
193
+ if ('onvisibilitychange' in document) {
194
+ document.addEventListener('visibilitychange', this._handleVisibilityChange);
195
+ } else {
196
+ window.addEventListener('pagehide', this._handlePageHide);
197
+ }
198
+ this.host.addEventListener('vouch:loaded', this._handleVouchLoaded);
199
+ this.host.mediaPlayer?.addEventListener('play', this._handlePlay);
200
+ this.host.mediaPlayer?.addEventListener('video:play', this._handleVideoPlay);
201
+ this.host.mediaPlayer?.addEventListener('video:pause', this._handleVideoPause);
202
+ this.host.mediaPlayer?.addEventListener('video:timeupdate', this._handleVideoTimeUpdate);
203
+ });
204
+ }
205
+
206
+ hostDisconnected() {
207
+ // Send events if DOM node is destroyed
208
+ this._pageUnloading();
209
+
210
+ if ('onvisibilitychange' in document) {
211
+ document.removeEventListener('visibilitychange', this._handleVisibilityChange);
212
+ } else {
213
+ window.removeEventListener('pagehide', this._handlePageHide);
214
+ }
215
+ this.host.removeEventListener('vouch:loaded', this._handleVouchLoaded);
216
+ this.host.mediaPlayer?.removeEventListener('play', this._handlePlay);
217
+ this.host.mediaPlayer?.removeEventListener('video:play', this._handleVideoPlay);
218
+ this.host.mediaPlayer?.removeEventListener('video:pause', this._handleVideoPause);
219
+ this.host.mediaPlayer?.removeEventListener('video:timeupdate', this._handleVideoTimeUpdate);
220
+ }
221
+ }
222
+
223
+ export { TrackingController };
224
+ export type { TrackingEvent, TrackingPayload };