@vouchfor/embeds 0.0.0-experiment.eb79200 → 0.0.0-experiment.ed7c00c
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/es/embeds.js +1025 -1084
- package/dist/es/embeds.js.map +1 -1
- package/dist/es/src/components/DialogEmbed/DialogOverlay.d.ts +20 -0
- package/dist/es/src/components/DialogEmbed/DialogPortal.d.ts +36 -0
- package/dist/es/src/components/DialogEmbed/index.d.ts +38 -0
- package/dist/es/{components/Embed → src/components/PlayerEmbed}/controllers/event-forwarder.d.ts +6 -5
- package/dist/es/src/components/PlayerEmbed/controllers/fetcher.d.ts +14 -0
- package/dist/es/src/components/PlayerEmbed/controllers/tracking/index.d.ts +36 -0
- package/dist/es/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +17 -0
- package/dist/es/src/components/PlayerEmbed/index.d.ts +75 -0
- package/dist/es/src/components/PlayerEmbed/tests/data.d.ts +4 -0
- package/dist/es/src/components/PlayerEmbed/tests/media-data.d.ts +19 -0
- package/dist/es/src/index.d.ts +2 -0
- package/dist/es/src/utils/env.d.ts +12 -0
- package/dist/iife/dialog-embed/embed.iife.js +4042 -0
- package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
- package/dist/iife/embeds.iife.js +3006 -445
- package/dist/iife/embeds.iife.js.map +1 -1
- package/dist/iife/player-embed/embed.iife.js +3904 -0
- package/dist/iife/player-embed/embed.iife.js.map +1 -0
- package/package.json +46 -33
- package/src/components/DialogEmbed/Dialog.stories.ts +91 -0
- package/src/components/DialogEmbed/DialogOverlay.ts +131 -0
- package/src/components/DialogEmbed/DialogPortal.ts +126 -0
- package/src/components/DialogEmbed/index.ts +97 -0
- package/src/components/PlayerEmbed/MultiEmbed.stories.ts +135 -0
- package/src/components/{Embed/Embed.stories.ts → PlayerEmbed/PlayerEmbed.stories.ts} +41 -30
- package/src/components/{Embed → PlayerEmbed}/controllers/event-forwarder.ts +6 -5
- package/src/components/PlayerEmbed/controllers/fetcher.ts +152 -0
- package/src/components/PlayerEmbed/controllers/tracking/index.ts +224 -0
- package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
- package/src/components/PlayerEmbed/index.ts +262 -0
- package/src/components/PlayerEmbed/tests/PlayerEmbed.spec.ts +87 -0
- package/src/components/PlayerEmbed/tests/data.ts +227 -0
- package/src/components/PlayerEmbed/tests/media-data.ts +22 -0
- package/src/index.ts +2 -1
- package/src/utils/env.ts +18 -32
- package/dist/es/components/Embed/controllers/fetcher.d.ts +0 -8
- package/dist/es/components/Embed/controllers/tracking.d.ts +0 -28
- package/dist/es/components/Embed/index.d.ts +0 -65
- package/dist/es/index.d.ts +0 -1
- package/dist/es/utils/env.d.ts +0 -18
- package/src/components/Embed/controllers/fetcher.ts +0 -43
- package/src/components/Embed/controllers/tracking.ts +0 -268
- package/src/components/Embed/index.ts +0 -204
- /package/dist/es/{utils → src/utils}/events.d.ts +0 -0
package/src/utils/env.ts
CHANGED
@@ -1,15 +1,11 @@
|
|
1
|
-
type Environment = 'dev' | 'staging' | 'prod';
|
1
|
+
type Environment = 'local' | 'dev' | 'staging' | 'prod';
|
2
2
|
|
3
3
|
type GetEnvUrlsReturn = {
|
4
|
-
marketingUrl: string;
|
5
4
|
videoUrl: string;
|
6
5
|
publicApiUrl: string;
|
7
6
|
embedApiUrl: string;
|
8
|
-
publicRecorderUrl: string;
|
9
7
|
};
|
10
8
|
|
11
|
-
const marketingUrl = 'https://vouchfor.com';
|
12
|
-
|
13
9
|
const devVideoUrl = 'https://d2rxhdlm2q91uk.cloudfront.net';
|
14
10
|
const stagingVideoUrl = 'https://d1ix11aj5kfygl.cloudfront.net';
|
15
11
|
const prodVideoUrl = 'https://d157jlwnudd93d.cloudfront.net';
|
@@ -18,61 +14,51 @@ const devPublicApiUrl = 'https://bshyfw4h5a.execute-api.ap-southeast-2.amazonaws
|
|
18
14
|
const stagingPublicApiUrl = 'https://gyzw7rpbq3.execute-api.ap-southeast-2.amazonaws.com/staging';
|
19
15
|
const prodPublicApiUrl = 'https://vfcjuim1l3.execute-api.ap-southeast-2.amazonaws.com/prod';
|
20
16
|
|
21
|
-
const
|
22
|
-
const
|
23
|
-
const
|
24
|
-
|
25
|
-
const devPublicRecorderUrl = 'https://dev.vouchfor.com';
|
26
|
-
const stagingPublicRecorderUrl = 'https://staging.vouchfor.com';
|
27
|
-
const prodPublicRecorderUrl = 'https://app.vouchfor.com';
|
17
|
+
const localEmbedApiUrl = 'http://localhost:6060/v2';
|
18
|
+
const devEmbedApiUrl = 'https://embed-dev.vouchfor.com/v2';
|
19
|
+
const stagingEmbedApiUrl = 'https://embed-staging.vouchfor.com/v2';
|
20
|
+
const prodEmbedApiUrl = 'https://embed.vouchfor.com/v2';
|
28
21
|
|
29
22
|
// We are handling the case where env is an unknown string so the ts error is a lie
|
30
23
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
31
24
|
// @ts-ignore
|
32
25
|
function getEnvUrls(env: Environment): GetEnvUrlsReturn {
|
33
|
-
if (!['dev', 'staging', 'prod'].includes(env)) {
|
26
|
+
if (!['local', 'dev', 'staging', 'prod'].includes(env)) {
|
34
27
|
throw new Error(`Unknown environment: ${env}`);
|
35
28
|
}
|
36
29
|
|
30
|
+
if (env === 'local') {
|
31
|
+
return {
|
32
|
+
videoUrl: devVideoUrl,
|
33
|
+
publicApiUrl: devPublicApiUrl,
|
34
|
+
embedApiUrl: localEmbedApiUrl
|
35
|
+
};
|
36
|
+
}
|
37
|
+
|
37
38
|
if (env === 'dev') {
|
38
39
|
return {
|
39
|
-
marketingUrl,
|
40
40
|
videoUrl: devVideoUrl,
|
41
41
|
publicApiUrl: devPublicApiUrl,
|
42
|
-
embedApiUrl: devEmbedApiUrl
|
43
|
-
publicRecorderUrl: devPublicRecorderUrl
|
42
|
+
embedApiUrl: devEmbedApiUrl
|
44
43
|
};
|
45
44
|
}
|
46
45
|
|
47
46
|
if (env === 'staging') {
|
48
47
|
return {
|
49
|
-
marketingUrl,
|
50
48
|
videoUrl: stagingVideoUrl,
|
51
49
|
publicApiUrl: stagingPublicApiUrl,
|
52
|
-
embedApiUrl: stagingEmbedApiUrl
|
53
|
-
publicRecorderUrl: stagingPublicRecorderUrl
|
50
|
+
embedApiUrl: stagingEmbedApiUrl
|
54
51
|
};
|
55
52
|
}
|
56
53
|
|
57
54
|
if (env === 'prod') {
|
58
55
|
return {
|
59
|
-
marketingUrl,
|
60
56
|
videoUrl: prodVideoUrl,
|
61
57
|
publicApiUrl: prodPublicApiUrl,
|
62
|
-
embedApiUrl: prodEmbedApiUrl
|
63
|
-
publicRecorderUrl: prodPublicRecorderUrl
|
58
|
+
embedApiUrl: prodEmbedApiUrl
|
64
59
|
};
|
65
60
|
}
|
66
61
|
}
|
67
62
|
|
68
|
-
export {
|
69
|
-
marketingUrl,
|
70
|
-
devEmbedApiUrl,
|
71
|
-
stagingEmbedApiUrl,
|
72
|
-
prodEmbedApiUrl,
|
73
|
-
devPublicRecorderUrl,
|
74
|
-
stagingPublicRecorderUrl,
|
75
|
-
prodPublicRecorderUrl,
|
76
|
-
getEnvUrls
|
77
|
-
};
|
63
|
+
export { devEmbedApiUrl, stagingEmbedApiUrl, prodEmbedApiUrl, getEnvUrls };
|
78
64
|
export type { Environment };
|
@@ -1,28 +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 _streamedTime;
|
13
|
-
private _streamedPrevTimestamp;
|
14
|
-
constructor(host: EmbedHost);
|
15
|
-
private _findVouchId;
|
16
|
-
private _createVisitor;
|
17
|
-
private _getUids;
|
18
|
-
private _getReportingMetadata;
|
19
|
-
private _sendTrackingEvent;
|
20
|
-
private _handleVouchLoaded;
|
21
|
-
private _handlePlay;
|
22
|
-
private _handleVideoPlay;
|
23
|
-
private _handleVideoTimeUpdate;
|
24
|
-
private _handleVideoPause;
|
25
|
-
hostConnected(): void;
|
26
|
-
hostDisconnected(): void;
|
27
|
-
}
|
28
|
-
export { TrackingController };
|
@@ -1,65 +0,0 @@
|
|
1
|
-
import { LitElement } from 'lit';
|
2
|
-
import type { Scene } 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' | 'resolution' | 'aspectRatio' | 'preload' | 'autoplay' | 'controls'> & {
|
6
|
-
env: Environment;
|
7
|
-
apiKey: string;
|
8
|
-
vouchHashId?: string;
|
9
|
-
enableTracking?: boolean;
|
10
|
-
trackingSource?: string;
|
11
|
-
};
|
12
|
-
declare class Embed extends LitElement {
|
13
|
-
private _mediaPlayerRef;
|
14
|
-
propData: EmbedProps['data'];
|
15
|
-
vouchHashId: EmbedProps['vouchHashId'];
|
16
|
-
env: EmbedProps['env'];
|
17
|
-
apiKey: EmbedProps['apiKey'];
|
18
|
-
preload: EmbedProps['preload'];
|
19
|
-
autoplay: EmbedProps['autoplay'];
|
20
|
-
enableTracking: EmbedProps['enableTracking'];
|
21
|
-
trackingSource: EmbedProps['trackingSource'];
|
22
|
-
resolution: EmbedProps['resolution'];
|
23
|
-
aspectRatio: EmbedProps['aspectRatio'];
|
24
|
-
controls: EmbedProps['controls'];
|
25
|
-
data: MediaPlayerProps['data'];
|
26
|
-
instance: MediaPlayerProps['instance'];
|
27
|
-
fetching: boolean;
|
28
|
-
private eventController;
|
29
|
-
private trackingController;
|
30
|
-
private fetcherController;
|
31
|
-
get waiting(): boolean | undefined;
|
32
|
-
get seeking(): boolean | undefined;
|
33
|
-
get paused(): boolean | undefined;
|
34
|
-
get captions(): boolean | undefined;
|
35
|
-
get fullscreen(): boolean | undefined;
|
36
|
-
get duration(): number | undefined;
|
37
|
-
set currentTime(value: number);
|
38
|
-
get currentTime(): number;
|
39
|
-
set playbackRate(value: number);
|
40
|
-
get playbackRate(): number;
|
41
|
-
set volume(value: number);
|
42
|
-
get volume(): number;
|
43
|
-
set muted(value: boolean);
|
44
|
-
get muted(): boolean;
|
45
|
-
get scene(): Scene | null;
|
46
|
-
get scenes(): Scene[];
|
47
|
-
get videoState(): import("@vouchfor/media-player/dist/src/components/MediaPlayer/controllers/scenes").VideoStateMap | undefined;
|
48
|
-
get mediaPlayer(): MediaPlayer | undefined;
|
49
|
-
play(): void;
|
50
|
-
pause(): void;
|
51
|
-
setScene(index: number): void;
|
52
|
-
render(): import("lit-html").TemplateResult<1>;
|
53
|
-
}
|
54
|
-
declare global {
|
55
|
-
interface HTMLElementTagNameMap {
|
56
|
-
'vouch-embed': Embed;
|
57
|
-
}
|
58
|
-
namespace JSX {
|
59
|
-
interface IntrinsicElements {
|
60
|
-
'vouch-embed': Embed;
|
61
|
-
}
|
62
|
-
}
|
63
|
-
}
|
64
|
-
export { Embed };
|
65
|
-
export type { EmbedProps };
|
package/dist/es/index.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export { Embed } from './components/Embed';
|
package/dist/es/utils/env.d.ts
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
type Environment = 'dev' | 'staging' | 'prod';
|
2
|
-
type GetEnvUrlsReturn = {
|
3
|
-
marketingUrl: string;
|
4
|
-
videoUrl: string;
|
5
|
-
publicApiUrl: string;
|
6
|
-
embedApiUrl: string;
|
7
|
-
publicRecorderUrl: string;
|
8
|
-
};
|
9
|
-
declare const marketingUrl = "https://vouchfor.com";
|
10
|
-
declare const devEmbedApiUrl = "http://localhost:6060/v1";
|
11
|
-
declare const stagingEmbedApiUrl = "https://embed-staging.vouchfor.com/v1";
|
12
|
-
declare const prodEmbedApiUrl = "https://embed.vouchfor.com/v1";
|
13
|
-
declare const devPublicRecorderUrl = "https://dev.vouchfor.com";
|
14
|
-
declare const stagingPublicRecorderUrl = "https://staging.vouchfor.com";
|
15
|
-
declare const prodPublicRecorderUrl = "https://app.vouchfor.com";
|
16
|
-
declare function getEnvUrls(env: Environment): GetEnvUrlsReturn;
|
17
|
-
export { marketingUrl, devEmbedApiUrl, stagingEmbedApiUrl, prodEmbedApiUrl, devPublicRecorderUrl, stagingPublicRecorderUrl, prodPublicRecorderUrl, getEnvUrls };
|
18
|
-
export type { Environment };
|
@@ -1,43 +0,0 @@
|
|
1
|
-
import { Task } from '@lit/task';
|
2
|
-
|
3
|
-
import type { Embed, EmbedProps } from '..';
|
4
|
-
import type { ReactiveControllerHost } from 'lit';
|
5
|
-
|
6
|
-
import { getEnvUrls } from '~/utils/env';
|
7
|
-
|
8
|
-
type EmbedHost = ReactiveControllerHost & Embed;
|
9
|
-
|
10
|
-
type TaskDeps = [EmbedProps['env'], EmbedProps['apiKey'], EmbedProps['vouchHashId']];
|
11
|
-
|
12
|
-
class FetcherController {
|
13
|
-
host: EmbedHost;
|
14
|
-
|
15
|
-
constructor(host: EmbedHost) {
|
16
|
-
this.host = host;
|
17
|
-
new Task<TaskDeps, void>(
|
18
|
-
this.host,
|
19
|
-
async ([env, apiKey, vouchHashId]: TaskDeps) => {
|
20
|
-
try {
|
21
|
-
if (vouchHashId) {
|
22
|
-
host.fetching = true;
|
23
|
-
|
24
|
-
const { embedApiUrl } = getEnvUrls(env);
|
25
|
-
|
26
|
-
const data = await fetch(`${embedApiUrl}/vouches/${vouchHashId}`, {
|
27
|
-
method: 'GET',
|
28
|
-
headers: [['X-Api-Key', apiKey]]
|
29
|
-
}).then((response) => response.json());
|
30
|
-
|
31
|
-
host.data = data;
|
32
|
-
host.instance = data?.settings?.template?.instance;
|
33
|
-
}
|
34
|
-
} finally {
|
35
|
-
host.fetching = false;
|
36
|
-
}
|
37
|
-
},
|
38
|
-
() => [host.env, host.apiKey, host.vouchHashId]
|
39
|
-
);
|
40
|
-
}
|
41
|
-
}
|
42
|
-
|
43
|
-
export { FetcherController };
|
@@ -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
|
-
const STREAMED_THROTTLE = 2000;
|
10
|
-
|
11
|
-
type EmbedHost = ReactiveControllerHost & Embed;
|
12
|
-
|
13
|
-
type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
|
14
|
-
type TrackingPayload = {
|
15
|
-
vouchId: string;
|
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 _streamedTime: TimeMap = {};
|
40
|
-
private _streamedPrevTimestamp: TimeMap = {};
|
41
|
-
|
42
|
-
constructor(host: EmbedHost) {
|
43
|
-
this.host = host;
|
44
|
-
host.addController(this);
|
45
|
-
}
|
46
|
-
|
47
|
-
private _findVouchId() {
|
48
|
-
if (this.host.data) {
|
49
|
-
if ('uuid' in this.host.data) {
|
50
|
-
return this.host.data.uuid;
|
51
|
-
}
|
52
|
-
return this.host.data.id;
|
53
|
-
}
|
54
|
-
}
|
55
|
-
|
56
|
-
private _createVisitor = (visitorId: string) => {
|
57
|
-
const { publicApiUrl } = getEnvUrls(this.host.env);
|
58
|
-
window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
|
59
|
-
navigator.sendBeacon(`${publicApiUrl}/api/visitor`, JSON.stringify({ visitorId }));
|
60
|
-
return visitorId;
|
61
|
-
};
|
62
|
-
|
63
|
-
private _getUids() {
|
64
|
-
if (typeof window === 'undefined') {
|
65
|
-
return {
|
66
|
-
client: null,
|
67
|
-
tab: null,
|
68
|
-
request: uuidv4()
|
69
|
-
};
|
70
|
-
}
|
71
|
-
|
72
|
-
// Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
|
73
|
-
const visitorId =
|
74
|
-
this._visitorId || window.localStorage?.getItem?.('vouch-uid-visitor') || this._createVisitor(uuidv4());
|
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 clientId = this._clientId || window.localStorage?.getItem?.('vouch-uid-client') || uuidv4();
|
77
|
-
// Persisted in session storage, so we can search for everything the user has done in a specific tab
|
78
|
-
const tabId = this._tabId || window.sessionStorage?.getItem?.('vouch-uid-tab') || uuidv4();
|
79
|
-
// Not persisted, allows us to search for any logs related to a single FE request
|
80
|
-
// E.g. BE should pass this request ID through all other services to be able to group logs
|
81
|
-
const requestId = uuidv4();
|
82
|
-
|
83
|
-
// Cache and persist uids
|
84
|
-
if (visitorId !== this._visitorId) {
|
85
|
-
this._visitorId = visitorId;
|
86
|
-
window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
|
87
|
-
}
|
88
|
-
|
89
|
-
if (clientId !== this._clientId) {
|
90
|
-
this._clientId = clientId;
|
91
|
-
window.localStorage?.setItem?.('vouch-uid-client', clientId);
|
92
|
-
}
|
93
|
-
|
94
|
-
if (tabId !== this._tabId) {
|
95
|
-
this._tabId = tabId;
|
96
|
-
window.sessionStorage?.setItem?.('vouch-uid-tab', tabId);
|
97
|
-
}
|
98
|
-
|
99
|
-
return {
|
100
|
-
client: clientId,
|
101
|
-
tab: tabId,
|
102
|
-
request: requestId,
|
103
|
-
visitor: visitorId
|
104
|
-
};
|
105
|
-
}
|
106
|
-
|
107
|
-
private _getReportingMetadata = () => {
|
108
|
-
const [country, region] = Intl.DateTimeFormat().resolvedOptions().timeZone?.split?.('/') ?? [];
|
109
|
-
|
110
|
-
const utmParams: any = {};
|
111
|
-
[...new URLSearchParams(location.search).entries()].forEach(([key, value]) => {
|
112
|
-
if (/utm/.test(key)) {
|
113
|
-
const param = key.toLowerCase().replace(/[-_][a-z0-9]/g, (group) => group.slice(-1).toUpperCase());
|
114
|
-
utmParams[param] = value;
|
115
|
-
}
|
116
|
-
});
|
117
|
-
|
118
|
-
return {
|
119
|
-
// Source might be embeds, could be playlink etc.
|
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 { publicApiUrl } = getEnvUrls(this.host.env);
|
134
|
-
const { client, tab, request, visitor } = this._getUids();
|
135
|
-
|
136
|
-
// Don't send tracking if we don't have a source
|
137
|
-
if (this.host.enableTracking && this.host.trackingSource) {
|
138
|
-
navigator.sendBeacon(
|
139
|
-
`${publicApiUrl}/api/events`,
|
140
|
-
JSON.stringify({
|
141
|
-
event,
|
142
|
-
payload,
|
143
|
-
context: {
|
144
|
-
'x-uid-client': client,
|
145
|
-
'x-uid-tab': tab,
|
146
|
-
'x-uid-request': request,
|
147
|
-
'x-uid-visitor': visitor,
|
148
|
-
'x-reporting-metadata': this._getReportingMetadata()
|
149
|
-
}
|
150
|
-
})
|
151
|
-
);
|
152
|
-
}
|
153
|
-
};
|
154
|
-
|
155
|
-
private _handleVouchLoaded = ({ detail: vouchId }: CustomEvent<string>) => {
|
156
|
-
if (!vouchId) {
|
157
|
-
return;
|
158
|
-
}
|
159
|
-
|
160
|
-
// Only send loaded event once per session
|
161
|
-
if (!this._hasLoaded[vouchId]) {
|
162
|
-
this._sendTrackingEvent('VOUCH_LOADED', {
|
163
|
-
vouchId
|
164
|
-
});
|
165
|
-
this._hasLoaded[vouchId] = true;
|
166
|
-
}
|
167
|
-
};
|
168
|
-
|
169
|
-
private _handlePlay = () => {
|
170
|
-
const vouchId = this._findVouchId();
|
171
|
-
|
172
|
-
if (!vouchId) {
|
173
|
-
return;
|
174
|
-
}
|
175
|
-
|
176
|
-
// Only send the video played event once per session
|
177
|
-
if (!this._hasPlayed) {
|
178
|
-
this._sendTrackingEvent('VIDEO_PLAYED', {
|
179
|
-
vouchId,
|
180
|
-
streamStart: this.host.currentTime
|
181
|
-
});
|
182
|
-
this._hasPlayed = true;
|
183
|
-
}
|
184
|
-
};
|
185
|
-
|
186
|
-
private _handleVideoPlay = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
|
187
|
-
const vouchId = this._findVouchId();
|
188
|
-
if (!vouchId) {
|
189
|
-
return;
|
190
|
-
}
|
191
|
-
// Only increment play count once per session
|
192
|
-
if (!this._answersViewed[id]) {
|
193
|
-
this._sendTrackingEvent('VOUCH_RESPONSE_VIEWED', {
|
194
|
-
vouchId,
|
195
|
-
answerId: id
|
196
|
-
});
|
197
|
-
this._answersViewed[id] = true;
|
198
|
-
}
|
199
|
-
this._streamedTime[id] = node.currentTime;
|
200
|
-
this._streamedPrevTimestamp[id] = Date.now();
|
201
|
-
};
|
202
|
-
|
203
|
-
private _handleVideoTimeUpdate = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
|
204
|
-
const vouchId = this._findVouchId();
|
205
|
-
if (!vouchId) {
|
206
|
-
return;
|
207
|
-
}
|
208
|
-
const currentTimestamp = Date.now();
|
209
|
-
if (
|
210
|
-
!this.host.paused &&
|
211
|
-
// Only fire the video seeked event when this video is the active one
|
212
|
-
id === this.host.scene?.video?.id &&
|
213
|
-
// Throttle the frequency that we send streamed events while playing
|
214
|
-
currentTimestamp - this._streamedPrevTimestamp[id] > STREAMED_THROTTLE
|
215
|
-
) {
|
216
|
-
this._sendTrackingEvent('VIDEO_STREAMED', {
|
217
|
-
vouchId,
|
218
|
-
answerId: id,
|
219
|
-
streamStart: this._streamedTime[id],
|
220
|
-
streamEnd: node.currentTime
|
221
|
-
});
|
222
|
-
this._streamedTime[id] = node.currentTime;
|
223
|
-
this._streamedPrevTimestamp[id] = currentTimestamp;
|
224
|
-
}
|
225
|
-
};
|
226
|
-
|
227
|
-
private _handleVideoPause = ({ detail: { id, node } }: CustomEvent<VideoEventDetail>) => {
|
228
|
-
const vouchId = this._findVouchId();
|
229
|
-
if (!vouchId) {
|
230
|
-
return;
|
231
|
-
}
|
232
|
-
// Send a video streamed event any time the video pauses then reset the streamed state
|
233
|
-
// We do this to capture the last bit of time that the video was played between the previous
|
234
|
-
// stream event and the video being paused manually or stopping because it ended
|
235
|
-
this._sendTrackingEvent('VIDEO_STREAMED', {
|
236
|
-
vouchId,
|
237
|
-
answerId: id,
|
238
|
-
streamStart: this._streamedTime[id],
|
239
|
-
streamEnd: node.currentTime
|
240
|
-
});
|
241
|
-
delete this._streamedTime[id];
|
242
|
-
delete this._streamedPrevTimestamp[id];
|
243
|
-
};
|
244
|
-
|
245
|
-
hostConnected() {
|
246
|
-
if (this.host.enableTracking && this.host.trackingSource) {
|
247
|
-
requestAnimationFrame(() => {
|
248
|
-
this.host.addEventListener('vouch:loaded', this._handleVouchLoaded);
|
249
|
-
this.host.mediaPlayer?.addEventListener('play', this._handlePlay);
|
250
|
-
this.host.mediaPlayer?.addEventListener('video:play', this._handleVideoPlay);
|
251
|
-
this.host.mediaPlayer?.addEventListener('video:pause', this._handleVideoPause);
|
252
|
-
this.host.mediaPlayer?.addEventListener('video:timeupdate', this._handleVideoTimeUpdate);
|
253
|
-
});
|
254
|
-
}
|
255
|
-
}
|
256
|
-
|
257
|
-
hostDisconnected() {
|
258
|
-
if (this.host.enableTracking && this.host.trackingSource) {
|
259
|
-
this.host.removeEventListener('vouch:loaded', this._handleVouchLoaded);
|
260
|
-
this.host.mediaPlayer?.removeEventListener('play', this._handlePlay);
|
261
|
-
this.host.mediaPlayer?.removeEventListener('video:play', this._handleVideoPlay);
|
262
|
-
this.host.mediaPlayer?.removeEventListener('video:pause', this._handleVideoPause);
|
263
|
-
this.host.mediaPlayer?.removeEventListener('video:timeupdate', this._handleVideoTimeUpdate);
|
264
|
-
}
|
265
|
-
}
|
266
|
-
}
|
267
|
-
|
268
|
-
export { TrackingController };
|