@vouchfor/embeds 0.0.0-experiment.113f809 → 0.0.0-experiment.116e6a5
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.
- package/dist/es/embeds.js +899 -1470
- 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 +37 -0
- package/dist/es/src/components/PlayerEmbed/controllers/event-forwarder.d.ts +16 -0
- package/dist/es/src/components/{Embed → PlayerEmbed}/controllers/fetcher.d.ts +6 -5
- package/dist/es/src/components/{Embed/controllers/tracking.d.ts → PlayerEmbed/controllers/tracking/index.d.ts} +17 -12
- package/dist/es/src/components/PlayerEmbed/controllers/tracking/utils.d.ts +18 -0
- package/dist/es/src/components/PlayerEmbed/index.d.ts +76 -0
- package/dist/es/src/components/PlayerEmbed/tests/data.d.ts +5 -0
- package/dist/es/src/components/PlayerEmbed/tests/media-data.d.ts +19 -0
- package/dist/es/src/index.d.ts +2 -1
- package/dist/iife/dialog-embed/embed.iife.js +4078 -0
- package/dist/iife/dialog-embed/embed.iife.js.map +1 -0
- package/dist/iife/embeds.iife.js +2992 -459
- package/dist/iife/embeds.iife.js.map +1 -1
- package/dist/iife/player-embed/embed.iife.js +3940 -0
- package/dist/iife/player-embed/embed.iife.js.map +1 -0
- package/package.json +51 -37
- 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} +33 -17
- package/src/components/{Embed → PlayerEmbed}/controllers/event-forwarder.ts +6 -5
- package/src/components/{Embed → PlayerEmbed}/controllers/fetcher.ts +11 -11
- package/src/components/{Embed/controllers/tracking.ts → PlayerEmbed/controllers/tracking/index.ts} +55 -123
- package/src/components/PlayerEmbed/controllers/tracking/utils.ts +95 -0
- package/src/components/{Embed → PlayerEmbed}/index.ts +100 -44
- 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/dist/es/src/components/Embed/index.d.ts +0 -69
|
@@ -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 };
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { html } from 'lit';
|
|
2
2
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type { PlayerEmbedProps } from '.';
|
|
5
5
|
import type { Meta, StoryObj } from '@storybook/web-components';
|
|
6
6
|
|
|
7
|
-
import '
|
|
7
|
+
import '.';
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type PlayerEmbedArgs = PlayerEmbedProps & {
|
|
10
10
|
showVouch?: boolean;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
const
|
|
13
|
+
const _PlayerEmbed = ({
|
|
14
14
|
vouchId,
|
|
15
15
|
templateId,
|
|
16
16
|
questions,
|
|
@@ -19,11 +19,12 @@ const _Embed = ({
|
|
|
19
19
|
env,
|
|
20
20
|
apiKey,
|
|
21
21
|
controls,
|
|
22
|
-
aspectRatio
|
|
23
|
-
|
|
22
|
+
aspectRatio,
|
|
23
|
+
senderId
|
|
24
|
+
}: PlayerEmbedArgs) => {
|
|
24
25
|
return html`
|
|
25
26
|
<div style="height: 100vh">
|
|
26
|
-
<vouch-embed
|
|
27
|
+
<vouch-embed-player
|
|
27
28
|
env=${ifDefined(env)}
|
|
28
29
|
apiKey=${ifDefined(apiKey)}
|
|
29
30
|
vouchId=${ifDefined(vouchId)}
|
|
@@ -33,32 +34,47 @@ const _Embed = ({
|
|
|
33
34
|
?autoplay=${autoplay}
|
|
34
35
|
preload=${ifDefined(preload)}
|
|
35
36
|
aspectRatio=${ifDefined(aspectRatio)}
|
|
37
|
+
senderId=${ifDefined(senderId)}
|
|
36
38
|
@error=${console.log}
|
|
37
|
-
></vouch-embed>
|
|
39
|
+
></vouch-embed-player>
|
|
38
40
|
</div>
|
|
39
41
|
`;
|
|
40
42
|
};
|
|
41
43
|
|
|
42
44
|
// More on how to set up stories at: https://storybook.js.org/docs/web-components/writing-stories/introduction
|
|
43
45
|
const meta = {
|
|
44
|
-
title: '
|
|
46
|
+
title: 'Embeds',
|
|
45
47
|
tags: ['autodocs'],
|
|
46
|
-
render: (args) =>
|
|
47
|
-
component: 'vouch-embed'
|
|
48
|
-
} satisfies Meta<
|
|
48
|
+
render: (args) => _PlayerEmbed(args),
|
|
49
|
+
component: 'vouch-embed-player'
|
|
50
|
+
} satisfies Meta<PlayerEmbedProps>;
|
|
49
51
|
|
|
50
|
-
type Story = StoryObj<
|
|
52
|
+
type Story = StoryObj<PlayerEmbedArgs>;
|
|
51
53
|
|
|
52
|
-
const
|
|
54
|
+
const Player: Story = {
|
|
53
55
|
args: {
|
|
54
|
-
env: '
|
|
56
|
+
env: 'dev',
|
|
55
57
|
apiKey: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
|
|
56
58
|
vouchId: '6JQEIPeStt',
|
|
57
59
|
templateId: '357fc118-e179-4171-9446-ff2b8e9d1b29',
|
|
58
60
|
questions: [],
|
|
59
61
|
aspectRatio: 0,
|
|
60
62
|
preload: 'none',
|
|
61
|
-
autoplay: false
|
|
63
|
+
autoplay: false,
|
|
64
|
+
controls: [
|
|
65
|
+
'progress',
|
|
66
|
+
'play-large',
|
|
67
|
+
'navigation',
|
|
68
|
+
'play',
|
|
69
|
+
'volume',
|
|
70
|
+
'current-time',
|
|
71
|
+
'duration',
|
|
72
|
+
'speed',
|
|
73
|
+
'captions',
|
|
74
|
+
'fullscreen',
|
|
75
|
+
'preview',
|
|
76
|
+
'languages'
|
|
77
|
+
]
|
|
62
78
|
},
|
|
63
79
|
argTypes: {
|
|
64
80
|
env: {
|
|
@@ -76,4 +92,4 @@ const Embed: Story = {
|
|
|
76
92
|
};
|
|
77
93
|
|
|
78
94
|
export default meta;
|
|
79
|
-
export {
|
|
95
|
+
export { Player };
|
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import { createRef, ref } from 'lit/directives/ref.js';
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { PlayerEmbed } from '..';
|
|
4
4
|
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
5
|
+
import type { DirectiveResult } from 'lit/directive.js';
|
|
5
6
|
import type { Ref } from 'lit/directives/ref.js';
|
|
6
7
|
|
|
7
8
|
import { forwardEvent } from '~/utils/events';
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
+
type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
|
|
10
11
|
|
|
11
12
|
class EventForwardController implements ReactiveController {
|
|
12
|
-
host:
|
|
13
|
+
host: PlayerEmbedHost;
|
|
13
14
|
|
|
14
15
|
private _events: string[] = [];
|
|
15
16
|
private _cleanup: (() => void)[] = [];
|
|
16
17
|
private _forwardElementRef: Ref<HTMLElement> = createRef();
|
|
17
18
|
|
|
18
|
-
constructor(host:
|
|
19
|
+
constructor(host: PlayerEmbedHost, events: string[]) {
|
|
19
20
|
this.host = host;
|
|
20
21
|
this._events = events;
|
|
21
22
|
host.addController(this);
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
register() {
|
|
25
|
+
register(): DirectiveResult {
|
|
25
26
|
return ref(this._forwardElementRef);
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import { Task } from '@lit/task';
|
|
2
2
|
import { v4 as uuidv4 } from 'uuid';
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type { PlayerEmbed, PlayerEmbedProps } from '..';
|
|
5
5
|
import type { ReactiveControllerHost } from 'lit';
|
|
6
6
|
import type { Environment } from '~/utils/env';
|
|
7
7
|
|
|
8
8
|
import { getEnvUrls } from '~/utils/env';
|
|
9
9
|
|
|
10
|
-
type
|
|
10
|
+
type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
|
|
11
11
|
|
|
12
12
|
type FetchTaskDeps = [
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
PlayerEmbedProps['env'],
|
|
14
|
+
PlayerEmbedProps['apiKey'],
|
|
15
|
+
PlayerEmbedProps['data'],
|
|
16
|
+
PlayerEmbedProps['vouchId'],
|
|
17
|
+
PlayerEmbedProps['templateId']
|
|
18
18
|
];
|
|
19
19
|
|
|
20
|
-
type FilterTaskDeps = [
|
|
20
|
+
type FilterTaskDeps = [PlayerEmbedProps['data'], PlayerEmbedProps['questions']];
|
|
21
21
|
|
|
22
22
|
class FetcherController {
|
|
23
|
-
host:
|
|
23
|
+
host: PlayerEmbedHost;
|
|
24
24
|
|
|
25
25
|
private _fetching = false;
|
|
26
|
-
private _vouch:
|
|
26
|
+
private _vouch: PlayerEmbedProps['data'];
|
|
27
27
|
|
|
28
28
|
set fetching(value) {
|
|
29
29
|
if (this._fetching !== value) {
|
|
@@ -97,7 +97,7 @@ class FetcherController {
|
|
|
97
97
|
return template;
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
-
constructor(host:
|
|
100
|
+
constructor(host: PlayerEmbedHost) {
|
|
101
101
|
this.host = host;
|
|
102
102
|
new Task<FetchTaskDeps, void>(
|
|
103
103
|
this.host,
|
package/src/components/{Embed/controllers/tracking.ts → PlayerEmbed/controllers/tracking/index.ts}
RENAMED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import type { Embed } from '..';
|
|
4
|
-
import type { VideoEventDetail } from '@vouchfor/media-player';
|
|
1
|
+
import type { PlayerEmbed } from '../..';
|
|
2
|
+
import type { MediaEventDetail } from '@vouchfor/media-player';
|
|
5
3
|
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
6
4
|
|
|
7
|
-
import
|
|
5
|
+
import { findVouchId, getReportingMetadata, getUids } from './utils';
|
|
8
6
|
import { getEnvUrls } from '~/utils/env';
|
|
9
7
|
|
|
10
8
|
const MINIMUM_SEND_THRESHOLD = 1;
|
|
11
9
|
|
|
12
|
-
type
|
|
10
|
+
type PlayerEmbedHost = ReactiveControllerHost & PlayerEmbed;
|
|
13
11
|
|
|
14
12
|
type TrackingEvent = 'VOUCH_LOADED' | 'VOUCH_RESPONSE_VIEWED' | 'VIDEO_PLAYED' | 'VIDEO_STREAMED';
|
|
15
13
|
type TrackingPayload = {
|
|
@@ -17,6 +15,14 @@ type TrackingPayload = {
|
|
|
17
15
|
answerId?: string;
|
|
18
16
|
streamStart?: number;
|
|
19
17
|
streamEnd?: number;
|
|
18
|
+
senderId?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type BatchEvent = {
|
|
22
|
+
event: TrackingEvent;
|
|
23
|
+
payload: TrackingPayload & {
|
|
24
|
+
time: string;
|
|
25
|
+
};
|
|
20
26
|
};
|
|
21
27
|
|
|
22
28
|
type TimeMap = {
|
|
@@ -28,137 +34,64 @@ type BooleanMap = {
|
|
|
28
34
|
};
|
|
29
35
|
|
|
30
36
|
class TrackingController implements ReactiveController {
|
|
31
|
-
host:
|
|
32
|
-
|
|
33
|
-
private _tabId: string | undefined = undefined;
|
|
34
|
-
private _clientId: string | undefined = undefined;
|
|
35
|
-
private _visitorId: string | undefined = undefined;
|
|
37
|
+
host: PlayerEmbedHost;
|
|
36
38
|
|
|
39
|
+
private _batchedEvents: BatchEvent[] = [];
|
|
37
40
|
private _hasPlayed = false;
|
|
38
41
|
private _hasLoaded: BooleanMap = {};
|
|
39
42
|
private _answersViewed: BooleanMap = {};
|
|
40
43
|
private _streamStartTime: TimeMap = {};
|
|
41
44
|
private _streamLatestTime: TimeMap = {};
|
|
42
|
-
private _currentlyPlayingVideo:
|
|
45
|
+
private _currentlyPlayingVideo: MediaEventDetail | null = null;
|
|
43
46
|
|
|
44
|
-
constructor(host:
|
|
47
|
+
constructor(host: PlayerEmbedHost) {
|
|
45
48
|
this.host = host;
|
|
46
49
|
host.addController(this);
|
|
47
50
|
}
|
|
48
51
|
|
|
49
|
-
private
|
|
50
|
-
|
|
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
|
-
};
|
|
52
|
+
private _createTrackingEvent = (event: TrackingEvent, payload?: TrackingPayload) => {
|
|
53
|
+
const vouchId = findVouchId(payload, this.host.vouch);
|
|
65
54
|
|
|
66
|
-
|
|
67
|
-
|
|
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);
|
|
55
|
+
if (!vouchId || this.host.disableTracking) {
|
|
56
|
+
return;
|
|
100
57
|
}
|
|
101
58
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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;
|
|
59
|
+
this._batchedEvents.push({
|
|
60
|
+
event,
|
|
61
|
+
payload: {
|
|
62
|
+
...payload,
|
|
63
|
+
senderId: this.host.senderId,
|
|
64
|
+
vouchId,
|
|
65
|
+
time: new Date().toISOString()
|
|
118
66
|
}
|
|
119
67
|
});
|
|
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
68
|
};
|
|
133
69
|
|
|
134
|
-
private _sendTrackingEvent = (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (!vouchId || this.host.disableTracking) {
|
|
70
|
+
private _sendTrackingEvent = () => {
|
|
71
|
+
if (this._batchedEvents.length <= 0) {
|
|
138
72
|
return;
|
|
139
73
|
}
|
|
140
74
|
|
|
141
75
|
const { publicApiUrl } = getEnvUrls(this.host.env);
|
|
142
|
-
const { client, tab, request, visitor } = this.
|
|
76
|
+
const { client, tab, request, visitor } = getUids(this.host.env);
|
|
143
77
|
|
|
144
78
|
navigator.sendBeacon(
|
|
145
|
-
`${publicApiUrl}/api/
|
|
79
|
+
`${publicApiUrl}/api/batchevents`,
|
|
146
80
|
JSON.stringify({
|
|
147
|
-
event,
|
|
148
81
|
payload: {
|
|
149
|
-
|
|
150
|
-
vouchId
|
|
82
|
+
events: this._batchedEvents
|
|
151
83
|
},
|
|
152
84
|
context: {
|
|
153
85
|
'x-uid-client': client,
|
|
154
86
|
'x-uid-tab': tab,
|
|
155
87
|
'x-uid-request': request,
|
|
156
88
|
'x-uid-visitor': visitor,
|
|
157
|
-
'x-reporting-metadata': this.
|
|
158
|
-
'x-embeds-version': packageJson.version
|
|
89
|
+
'x-reporting-metadata': getReportingMetadata(this.host.trackingSource)
|
|
159
90
|
}
|
|
160
91
|
})
|
|
161
92
|
);
|
|
93
|
+
|
|
94
|
+
this._batchedEvents = [];
|
|
162
95
|
};
|
|
163
96
|
|
|
164
97
|
private _streamEnded = () => {
|
|
@@ -168,7 +101,7 @@ class TrackingController implements ReactiveController {
|
|
|
168
101
|
if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
|
|
169
102
|
// Send a video streamed event any time the stream ends to capture the time between starting
|
|
170
103
|
// the video and the video stopping for any reason (pausing, deleting the embed node or closing the browser)
|
|
171
|
-
this.
|
|
104
|
+
this._createTrackingEvent('VIDEO_STREAMED', {
|
|
172
105
|
answerId: id,
|
|
173
106
|
streamStart: this._streamStartTime[key],
|
|
174
107
|
streamEnd: this._streamLatestTime[key]
|
|
@@ -188,7 +121,7 @@ class TrackingController implements ReactiveController {
|
|
|
188
121
|
|
|
189
122
|
// Only send loaded event once per session
|
|
190
123
|
if (!this._hasLoaded[vouchId]) {
|
|
191
|
-
this.
|
|
124
|
+
this._createTrackingEvent('VOUCH_LOADED', { vouchId });
|
|
192
125
|
this._hasLoaded[vouchId] = true;
|
|
193
126
|
}
|
|
194
127
|
};
|
|
@@ -196,43 +129,41 @@ class TrackingController implements ReactiveController {
|
|
|
196
129
|
private _handlePlay = () => {
|
|
197
130
|
// Only send the video played event once per session
|
|
198
131
|
if (!this._hasPlayed) {
|
|
199
|
-
this.
|
|
200
|
-
streamStart: this.host.currentTime
|
|
201
|
-
});
|
|
132
|
+
this._createTrackingEvent('VIDEO_PLAYED', { streamStart: this.host.currentTime });
|
|
202
133
|
this._hasPlayed = true;
|
|
203
134
|
}
|
|
204
135
|
};
|
|
205
136
|
|
|
206
|
-
private _handleVideoPlay = ({ detail: { id, key
|
|
137
|
+
private _handleVideoPlay = ({ detail: { id, key } }: CustomEvent<MediaEventDetail>) => {
|
|
207
138
|
// Only increment play count once per session
|
|
208
139
|
if (!this._answersViewed[key]) {
|
|
209
|
-
this.
|
|
140
|
+
this._createTrackingEvent('VOUCH_RESPONSE_VIEWED', {
|
|
210
141
|
answerId: id
|
|
211
142
|
});
|
|
212
143
|
this._answersViewed[key] = true;
|
|
213
144
|
}
|
|
214
|
-
|
|
215
|
-
if (!this._streamStartTime[key]) {
|
|
216
|
-
this._streamStartTime[key] = node.currentTime;
|
|
217
|
-
this._streamLatestTime[key] = node.currentTime;
|
|
218
|
-
}
|
|
219
145
|
};
|
|
220
146
|
|
|
221
|
-
private _handleVideoTimeUpdate = ({ detail: { id, key, node } }: CustomEvent<
|
|
147
|
+
private _handleVideoTimeUpdate = ({ detail: { id, key, node } }: CustomEvent<MediaEventDetail>) => {
|
|
222
148
|
if (
|
|
223
149
|
// We only want to count any time that the video is actually playing
|
|
224
|
-
!this.host.paused
|
|
150
|
+
!this.host.paused
|
|
225
151
|
// Only update the latest time if this event fires for the currently active video
|
|
226
|
-
id === this.host.scene?.video?.id
|
|
152
|
+
// id === this.host.scene?.video?.id
|
|
227
153
|
) {
|
|
228
154
|
this._currentlyPlayingVideo = { id, key, node };
|
|
229
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
|
+
}
|
|
230
161
|
}
|
|
231
162
|
};
|
|
232
163
|
|
|
233
|
-
private _handleVideoPause = ({ detail: { id, key } }: CustomEvent<
|
|
164
|
+
private _handleVideoPause = ({ detail: { id, key } }: CustomEvent<MediaEventDetail>) => {
|
|
234
165
|
if (this._streamLatestTime[key] > this._streamStartTime[key] + MINIMUM_SEND_THRESHOLD) {
|
|
235
|
-
this.
|
|
166
|
+
this._createTrackingEvent('VIDEO_STREAMED', {
|
|
236
167
|
answerId: id,
|
|
237
168
|
streamStart: this._streamStartTime[key],
|
|
238
169
|
streamEnd: this._streamLatestTime[key]
|
|
@@ -244,9 +175,7 @@ class TrackingController implements ReactiveController {
|
|
|
244
175
|
|
|
245
176
|
private _pageUnloading = () => {
|
|
246
177
|
this._streamEnded();
|
|
247
|
-
|
|
248
|
-
// time in stream ended so that there is no times to send and the pause event does nothing
|
|
249
|
-
this.host.pause();
|
|
178
|
+
this._sendTrackingEvent();
|
|
250
179
|
};
|
|
251
180
|
|
|
252
181
|
private _handleVisibilityChange = () => {
|
|
@@ -275,7 +204,9 @@ class TrackingController implements ReactiveController {
|
|
|
275
204
|
}
|
|
276
205
|
|
|
277
206
|
hostDisconnected() {
|
|
278
|
-
|
|
207
|
+
// Send events if DOM node is destroyed
|
|
208
|
+
this._pageUnloading();
|
|
209
|
+
|
|
279
210
|
if ('onvisibilitychange' in document) {
|
|
280
211
|
document.removeEventListener('visibilitychange', this._handleVisibilityChange);
|
|
281
212
|
} else {
|
|
@@ -290,3 +221,4 @@ class TrackingController implements ReactiveController {
|
|
|
290
221
|
}
|
|
291
222
|
|
|
292
223
|
export { TrackingController };
|
|
224
|
+
export type { TrackingEvent, TrackingPayload };
|