@vouchfor/embeds 0.0.0-experiment.93ea548 → 0.0.0-experiment.9821b79
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/browser-8d4ae80d.js +433 -0
 - package/dist/es/browser-8d4ae80d.js.map +1 -0
 - package/dist/es/embeds.js +7 -1521
 - package/dist/es/embeds.js.map +1 -1
 - package/dist/es/index-74d4cefa.js +9980 -0
 - package/dist/es/index-74d4cefa.js.map +1 -0
 - package/dist/es/{components → src/components}/Embed/controllers/fetcher.d.ts +1 -0
 - package/dist/es/{components/Embed/controllers/tracking.d.ts → src/components/Embed/controllers/tracking/index.d.ts} +17 -11
 - package/dist/es/src/components/Embed/controllers/tracking/utils.d.ts +17 -0
 - package/dist/es/{components → src/components}/Embed/index.d.ts +6 -1
 - package/dist/es/src/components/Embed/tests/data.d.ts +3 -0
 - package/dist/iife/embeds.iife.js +455 -313
 - package/dist/iife/embeds.iife.js.map +1 -1
 - package/package.json +6 -3
 - package/src/components/Embed/Embed.stories.ts +15 -2
 - package/src/components/Embed/controllers/fetcher.ts +27 -8
 - package/src/components/Embed/controllers/tracking/index.ts +224 -0
 - package/src/components/Embed/controllers/tracking/utils.ts +95 -0
 - package/src/components/Embed/index.ts +47 -6
 - package/src/components/Embed/tests/Embed.spec.ts +78 -0
 - package/src/components/Embed/tests/data.ts +183 -0
 - package/src/components/Embed/controllers/tracking.ts +0 -306
 - /package/dist/es/{index.d.ts → src/index.d.ts} +0 -0
 - /package/dist/es/{utils → src/utils}/env.d.ts +0 -0
 - /package/dist/es/{utils → src/utils}/events.d.ts +0 -0
 
    
        package/package.json
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            {
         
     | 
| 
       2 
2 
     | 
    
         
             
              "name": "@vouchfor/embeds",
         
     | 
| 
       3 
     | 
    
         
            -
              "version": "0.0.0-experiment. 
     | 
| 
      
 3 
     | 
    
         
            +
              "version": "0.0.0-experiment.9821b79",
         
     | 
| 
       4 
4 
     | 
    
         
             
              "license": "MIT",
         
     | 
| 
       5 
5 
     | 
    
         
             
              "author": "Aaron Williams",
         
     | 
| 
       6 
6 
     | 
    
         
             
              "main": "dist/es/embeds.js",
         
     | 
| 
         @@ -28,7 +28,8 @@ 
     | 
|
| 
       28 
28 
     | 
    
         
             
                "size": "size-limit",
         
     | 
| 
       29 
29 
     | 
    
         
             
                "storybook": "yarn prebuild && storybook dev -p 6007",
         
     | 
| 
       30 
30 
     | 
    
         
             
                "prebuild": "yarn build:deps && yarn generate:manifest",
         
     | 
| 
       31 
     | 
    
         
            -
                "test": " 
     | 
| 
      
 31 
     | 
    
         
            +
                "test": "rm -rf test/lib && yarn prebuild && vite build --mode test && web-test-runner",
         
     | 
| 
      
 32 
     | 
    
         
            +
                "test:ci": "yarn test --config web-test-runner.ci.config.js"
         
     | 
| 
       32 
33 
     | 
    
         
             
              },
         
     | 
| 
       33 
34 
     | 
    
         
             
              "lint-staged": {
         
     | 
| 
       34 
35 
     | 
    
         
             
                "**/*.{ts,tsx,js}": "eslint --fix --quiet",
         
     | 
| 
         @@ -36,7 +37,7 @@ 
     | 
|
| 
       36 
37 
     | 
    
         
             
              },
         
     | 
| 
       37 
38 
     | 
    
         
             
              "dependencies": {
         
     | 
| 
       38 
39 
     | 
    
         
             
                "@lit/task": "^1.0.0",
         
     | 
| 
       39 
     | 
    
         
            -
                "@vouchfor/media-player": "0.0.0-experiment. 
     | 
| 
      
 40 
     | 
    
         
            +
                "@vouchfor/media-player": "0.0.0-experiment.9821b79",
         
     | 
| 
       40 
41 
     | 
    
         
             
                "uuid": "^9.0.1"
         
     | 
| 
       41 
42 
     | 
    
         
             
              },
         
     | 
| 
       42 
43 
     | 
    
         
             
              "peerDependencies": {
         
     | 
| 
         @@ -66,9 +67,11 @@ 
     | 
|
| 
       66 
67 
     | 
    
         
             
                "prettier": "^3.0.3",
         
     | 
| 
       67 
68 
     | 
    
         
             
                "react": "^18.2.0",
         
     | 
| 
       68 
69 
     | 
    
         
             
                "react-dom": "^18.2.0",
         
     | 
| 
      
 70 
     | 
    
         
            +
                "sinon": "^17.0.1",
         
     | 
| 
       69 
71 
     | 
    
         
             
                "storybook": "^7.4.5",
         
     | 
| 
       70 
72 
     | 
    
         
             
                "typescript": "^5.1.3",
         
     | 
| 
       71 
73 
     | 
    
         
             
                "vite": "^4.4.9",
         
     | 
| 
      
 74 
     | 
    
         
            +
                "vite-plugin-commonjs": "^0.10.0",
         
     | 
| 
       72 
75 
     | 
    
         
             
                "vite-plugin-dts": "^3.6.0",
         
     | 
| 
       73 
76 
     | 
    
         
             
                "web-component-analyzer": "^1.1.7"
         
     | 
| 
       74 
77 
     | 
    
         
             
              }
         
     | 
| 
         @@ -10,7 +10,17 @@ type EmbedArgs = EmbedProps & { 
     | 
|
| 
       10 
10 
     | 
    
         
             
              showVouch?: boolean;
         
     | 
| 
       11 
11 
     | 
    
         
             
            };
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
            const _Embed = ({ 
     | 
| 
      
 13 
     | 
    
         
            +
            const _Embed = ({
         
     | 
| 
      
 14 
     | 
    
         
            +
              vouchId,
         
     | 
| 
      
 15 
     | 
    
         
            +
              templateId,
         
     | 
| 
      
 16 
     | 
    
         
            +
              questions,
         
     | 
| 
      
 17 
     | 
    
         
            +
              preload,
         
     | 
| 
      
 18 
     | 
    
         
            +
              autoplay,
         
     | 
| 
      
 19 
     | 
    
         
            +
              env,
         
     | 
| 
      
 20 
     | 
    
         
            +
              apiKey,
         
     | 
| 
      
 21 
     | 
    
         
            +
              controls,
         
     | 
| 
      
 22 
     | 
    
         
            +
              aspectRatio
         
     | 
| 
      
 23 
     | 
    
         
            +
            }: EmbedArgs) => {
         
     | 
| 
       14 
24 
     | 
    
         
             
              return html`
         
     | 
| 
       15 
25 
     | 
    
         
             
                <div style="height: 100vh">
         
     | 
| 
       16 
26 
     | 
    
         
             
                  <vouch-embed
         
     | 
| 
         @@ -18,10 +28,12 @@ const _Embed = ({ vouchId, templateId, preload, autoplay, env, apiKey, controls, 
     | 
|
| 
       18 
28 
     | 
    
         
             
                    apiKey=${ifDefined(apiKey)}
         
     | 
| 
       19 
29 
     | 
    
         
             
                    vouchId=${ifDefined(vouchId)}
         
     | 
| 
       20 
30 
     | 
    
         
             
                    templateId=${ifDefined(templateId)}
         
     | 
| 
      
 31 
     | 
    
         
            +
                    .questions=${questions}
         
     | 
| 
       21 
32 
     | 
    
         
             
                    .controls=${controls}
         
     | 
| 
       22 
33 
     | 
    
         
             
                    ?autoplay=${autoplay}
         
     | 
| 
       23 
34 
     | 
    
         
             
                    preload=${ifDefined(preload)}
         
     | 
| 
       24 
35 
     | 
    
         
             
                    aspectRatio=${ifDefined(aspectRatio)}
         
     | 
| 
      
 36 
     | 
    
         
            +
                    @error=${console.log}
         
     | 
| 
       25 
37 
     | 
    
         
             
                  ></vouch-embed>
         
     | 
| 
       26 
38 
     | 
    
         
             
                </div>
         
     | 
| 
       27 
39 
     | 
    
         
             
              `;
         
     | 
| 
         @@ -39,10 +51,11 @@ type Story = StoryObj<EmbedArgs>; 
     | 
|
| 
       39 
51 
     | 
    
         | 
| 
       40 
52 
     | 
    
         
             
            const Embed: Story = {
         
     | 
| 
       41 
53 
     | 
    
         
             
              args: {
         
     | 
| 
       42 
     | 
    
         
            -
                env: ' 
     | 
| 
      
 54 
     | 
    
         
            +
                env: 'dev',
         
     | 
| 
       43 
55 
     | 
    
         
             
                apiKey: 'TVik9uTMgE-PD25UTHIS6gyl0hMBWC7AT4dkpdlLBT4VIfDWZJrQiCk6Ak7m1',
         
     | 
| 
       44 
56 
     | 
    
         
             
                vouchId: '6JQEIPeStt',
         
     | 
| 
       45 
57 
     | 
    
         
             
                templateId: '357fc118-e179-4171-9446-ff2b8e9d1b29',
         
     | 
| 
      
 58 
     | 
    
         
            +
                questions: [],
         
     | 
| 
       46 
59 
     | 
    
         
             
                aspectRatio: 0,
         
     | 
| 
       47 
60 
     | 
    
         
             
                preload: 'none',
         
     | 
| 
       48 
61 
     | 
    
         
             
                autoplay: false
         
     | 
| 
         @@ -9,7 +9,7 @@ import { getEnvUrls } from '~/utils/env'; 
     | 
|
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
            type EmbedHost = ReactiveControllerHost & Embed;
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
            type  
     | 
| 
      
 12 
     | 
    
         
            +
            type FetchTaskDeps = [
         
     | 
| 
       13 
13 
     | 
    
         
             
              EmbedProps['env'],
         
     | 
| 
       14 
14 
     | 
    
         
             
              EmbedProps['apiKey'],
         
     | 
| 
       15 
15 
     | 
    
         
             
              EmbedProps['data'],
         
     | 
| 
         @@ -17,10 +17,13 @@ type TaskDeps = [ 
     | 
|
| 
       17 
17 
     | 
    
         
             
              EmbedProps['templateId']
         
     | 
| 
       18 
18 
     | 
    
         
             
            ];
         
     | 
| 
       19 
19 
     | 
    
         | 
| 
      
 20 
     | 
    
         
            +
            type FilterTaskDeps = [EmbedProps['data'], EmbedProps['questions']];
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
       20 
22 
     | 
    
         
             
            class FetcherController {
         
     | 
| 
       21 
23 
     | 
    
         
             
              host: EmbedHost;
         
     | 
| 
       22 
24 
     | 
    
         | 
| 
       23 
25 
     | 
    
         
             
              private _fetching = false;
         
     | 
| 
      
 26 
     | 
    
         
            +
              private _vouch: EmbedProps['data'];
         
     | 
| 
       24 
27 
     | 
    
         | 
| 
       25 
28 
     | 
    
         
             
              set fetching(value) {
         
     | 
| 
       26 
29 
     | 
    
         
             
                if (this._fetching !== value) {
         
     | 
| 
         @@ -45,13 +48,13 @@ class FetcherController { 
     | 
|
| 
       45 
48 
     | 
    
         
             
                });
         
     | 
| 
       46 
49 
     | 
    
         | 
| 
       47 
50 
     | 
    
         
             
                const vouch = await res.json();
         
     | 
| 
       48 
     | 
    
         
            -
                this.host.dispatchEvent(new CustomEvent('vouch:loaded', { detail:  
     | 
| 
      
 51 
     | 
    
         
            +
                this.host.dispatchEvent(new CustomEvent('vouch:loaded', { detail: vouch?.id }));
         
     | 
| 
       49 
52 
     | 
    
         | 
| 
       50 
53 
     | 
    
         
             
                // HACK: we're currently using API Gateway caching on the embed API without any invalidation logic,
         
     | 
| 
       51 
54 
     | 
    
         
             
                // so to ensure that the cache stays up to date, whenever we detect a cache hit we trigger another
         
     | 
| 
       52 
55 
     | 
    
         
             
                // API call with the `Cache-Control` header which will re-fill the cache
         
     | 
| 
       53 
56 
     | 
    
         
             
                const resCacheCheck = res?.headers?.get('X-Cache-Check');
         
     | 
| 
       54 
     | 
    
         
            -
                if (resCacheCheck  
     | 
| 
      
 57 
     | 
    
         
            +
                if (resCacheCheck !== cacheCheck) {
         
     | 
| 
       55 
58 
     | 
    
         
             
                  fetch(`${embedApiUrl}/vouches/${vouchId}`, {
         
     | 
| 
       56 
59 
     | 
    
         
             
                    method: 'GET',
         
     | 
| 
       57 
60 
     | 
    
         
             
                    headers: [
         
     | 
| 
         @@ -81,7 +84,7 @@ class FetcherController { 
     | 
|
| 
       81 
84 
     | 
    
         
             
                // so to ensure that the cache stays up to date, whenever we detect a cache hit we trigger another
         
     | 
| 
       82 
85 
     | 
    
         
             
                // API call with the `Cache-Control` header which will re-fill the cache
         
     | 
| 
       83 
86 
     | 
    
         
             
                const resCacheCheck = res?.headers?.get('X-Cache-Check');
         
     | 
| 
       84 
     | 
    
         
            -
                if (resCacheCheck  
     | 
| 
      
 87 
     | 
    
         
            +
                if (resCacheCheck !== cacheCheck) {
         
     | 
| 
       85 
88 
     | 
    
         
             
                  fetch(`${embedApiUrl}/templates/${templateId}`, {
         
     | 
| 
       86 
89 
     | 
    
         
             
                    method: 'GET',
         
     | 
| 
       87 
90 
     | 
    
         
             
                    headers: [
         
     | 
| 
         @@ -96,9 +99,9 @@ class FetcherController { 
     | 
|
| 
       96 
99 
     | 
    
         | 
| 
       97 
100 
     | 
    
         
             
              constructor(host: EmbedHost) {
         
     | 
| 
       98 
101 
     | 
    
         
             
                this.host = host;
         
     | 
| 
       99 
     | 
    
         
            -
                new Task< 
     | 
| 
      
 102 
     | 
    
         
            +
                new Task<FetchTaskDeps, void>(
         
     | 
| 
       100 
103 
     | 
    
         
             
                  this.host,
         
     | 
| 
       101 
     | 
    
         
            -
                  async ([env, apiKey, data, vouchId, templateId]:  
     | 
| 
      
 104 
     | 
    
         
            +
                  async ([env, apiKey, data, vouchId, templateId]: FetchTaskDeps) => {
         
     | 
| 
       102 
105 
     | 
    
         
             
                    try {
         
     | 
| 
       103 
106 
     | 
    
         
             
                      host.vouch = undefined;
         
     | 
| 
       104 
107 
     | 
    
         
             
                      host.template = undefined;
         
     | 
| 
         @@ -109,7 +112,7 @@ class FetcherController { 
     | 
|
| 
       109 
112 
     | 
    
         
             
                          this.fetching = true;
         
     | 
| 
       110 
113 
     | 
    
         
             
                          template = await this.getTemplate(env, apiKey, templateId);
         
     | 
| 
       111 
114 
     | 
    
         
             
                        }
         
     | 
| 
       112 
     | 
    
         
            -
                         
     | 
| 
      
 115 
     | 
    
         
            +
                        this._vouch = data;
         
     | 
| 
       113 
116 
     | 
    
         
             
                        host.template = template ?? data?.settings?.template?.instance;
         
     | 
| 
       114 
117 
     | 
    
         
             
                      } else if (vouchId) {
         
     | 
| 
       115 
118 
     | 
    
         
             
                        this.fetching = true;
         
     | 
| 
         @@ -118,7 +121,7 @@ class FetcherController { 
     | 
|
| 
       118 
121 
     | 
    
         
             
                          this.getVouch(env, apiKey, vouchId),
         
     | 
| 
       119 
122 
     | 
    
         
             
                          templateId ? this.getTemplate(env, apiKey, templateId) : null
         
     | 
| 
       120 
123 
     | 
    
         
             
                        ]);
         
     | 
| 
       121 
     | 
    
         
            -
                         
     | 
| 
      
 124 
     | 
    
         
            +
                        this._vouch = vouch;
         
     | 
| 
       122 
125 
     | 
    
         
             
                        host.template = template ?? vouch?.settings?.template?.instance;
         
     | 
| 
       123 
126 
     | 
    
         
             
                      }
         
     | 
| 
       124 
127 
     | 
    
         
             
                    } finally {
         
     | 
| 
         @@ -127,6 +130,22 @@ class FetcherController { 
     | 
|
| 
       127 
130 
     | 
    
         
             
                  },
         
     | 
| 
       128 
131 
     | 
    
         
             
                  () => [host.env, host.apiKey, host.data, host.vouchId, host.templateId]
         
     | 
| 
       129 
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 
     | 
    
         
            +
                );
         
     | 
| 
       130 
149 
     | 
    
         
             
              }
         
     | 
| 
       131 
150 
     | 
    
         
             
            }
         
     | 
| 
       132 
151 
     | 
    
         | 
| 
         @@ -0,0 +1,224 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import type { Embed } from '../..';
         
     | 
| 
      
 2 
     | 
    
         
            +
            import type { VideoEventDetail } 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 EmbedHost = ReactiveControllerHost & Embed;
         
     | 
| 
      
 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: EmbedHost;
         
     | 
| 
      
 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: VideoEventDetail | null = null;
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
              constructor(host: EmbedHost) {
         
     | 
| 
      
 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<VideoEventDetail>) => {
         
     | 
| 
      
 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<VideoEventDetail>) => {
         
     | 
| 
      
 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<VideoEventDetail>) => {
         
     | 
| 
      
 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 };
         
     | 
| 
         @@ -0,0 +1,95 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import { TEMPLATE_VERSION } from '@vouchfor/canvas-video';
         
     | 
| 
      
 2 
     | 
    
         
            +
            import { v4 as uuidv4 } from 'uuid';
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            import type { TrackingPayload } from '.';
         
     | 
| 
      
 5 
     | 
    
         
            +
            import type { Vouch } from '@vouchfor/media-player';
         
     | 
| 
      
 6 
     | 
    
         
            +
            import type { Environment } from '~/utils/env';
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            import packageJson from '../../../../../package.json';
         
     | 
| 
      
 9 
     | 
    
         
            +
            import { getEnvUrls } from '~/utils/env';
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            function createVisitor(env: Environment) {
         
     | 
| 
      
 12 
     | 
    
         
            +
              const { publicApiUrl } = getEnvUrls(env);
         
     | 
| 
      
 13 
     | 
    
         
            +
              const visitorId = uuidv4();
         
     | 
| 
      
 14 
     | 
    
         
            +
              navigator.sendBeacon(`${publicApiUrl}/api/visitor`, JSON.stringify({ visitorId }));
         
     | 
| 
      
 15 
     | 
    
         
            +
              return visitorId;
         
     | 
| 
      
 16 
     | 
    
         
            +
            }
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            function getUids(env: Environment) {
         
     | 
| 
      
 19 
     | 
    
         
            +
              if (typeof window === 'undefined') {
         
     | 
| 
      
 20 
     | 
    
         
            +
                return {
         
     | 
| 
      
 21 
     | 
    
         
            +
                  client: null,
         
     | 
| 
      
 22 
     | 
    
         
            +
                  tab: null,
         
     | 
| 
      
 23 
     | 
    
         
            +
                  request: uuidv4()
         
     | 
| 
      
 24 
     | 
    
         
            +
                };
         
     | 
| 
      
 25 
     | 
    
         
            +
              }
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
              // Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
         
     | 
| 
      
 28 
     | 
    
         
            +
              let visitorId = window.localStorage?.getItem?.('vouch-uid-visitor');
         
     | 
| 
      
 29 
     | 
    
         
            +
              // Persisted for a user for the same device + browser, so we can e.g. search for all logs related to that browser
         
     | 
| 
      
 30 
     | 
    
         
            +
              let clientId = window.localStorage?.getItem?.('vouch-uid-client');
         
     | 
| 
      
 31 
     | 
    
         
            +
              // Persisted in session storage, so we can search for everything the user has done in a specific tab
         
     | 
| 
      
 32 
     | 
    
         
            +
              let tabId = window.sessionStorage?.getItem?.('vouch-uid-tab');
         
     | 
| 
      
 33 
     | 
    
         
            +
              // Not persisted, allows us to search for any logs related to a single FE request
         
     | 
| 
      
 34 
     | 
    
         
            +
              // E.g. BE should pass this request ID through all other services to be able to group logs
         
     | 
| 
      
 35 
     | 
    
         
            +
              const requestId = uuidv4();
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
              // Cache uids
         
     | 
| 
      
 38 
     | 
    
         
            +
              if (!visitorId) {
         
     | 
| 
      
 39 
     | 
    
         
            +
                visitorId = createVisitor(env);
         
     | 
| 
      
 40 
     | 
    
         
            +
                window.localStorage?.setItem?.('vouch-uid-visitor', visitorId);
         
     | 
| 
      
 41 
     | 
    
         
            +
              }
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
              if (!clientId) {
         
     | 
| 
      
 44 
     | 
    
         
            +
                clientId = uuidv4();
         
     | 
| 
      
 45 
     | 
    
         
            +
                window.localStorage?.setItem?.('vouch-uid-client', clientId);
         
     | 
| 
      
 46 
     | 
    
         
            +
              }
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
              if (!tabId) {
         
     | 
| 
      
 49 
     | 
    
         
            +
                tabId = uuidv4();
         
     | 
| 
      
 50 
     | 
    
         
            +
                window.sessionStorage?.setItem?.('vouch-uid-tab', tabId);
         
     | 
| 
      
 51 
     | 
    
         
            +
              }
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
              return {
         
     | 
| 
      
 54 
     | 
    
         
            +
                client: clientId,
         
     | 
| 
      
 55 
     | 
    
         
            +
                tab: tabId,
         
     | 
| 
      
 56 
     | 
    
         
            +
                request: requestId,
         
     | 
| 
      
 57 
     | 
    
         
            +
                visitor: visitorId
         
     | 
| 
      
 58 
     | 
    
         
            +
              };
         
     | 
| 
      
 59 
     | 
    
         
            +
            }
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
            function findVouchId(payload?: TrackingPayload, vouch?: Vouch) {
         
     | 
| 
      
 62 
     | 
    
         
            +
              if (payload && 'vouchId' in payload) {
         
     | 
| 
      
 63 
     | 
    
         
            +
                return payload.vouchId;
         
     | 
| 
      
 64 
     | 
    
         
            +
              }
         
     | 
| 
      
 65 
     | 
    
         
            +
              return vouch?.id ?? null;
         
     | 
| 
      
 66 
     | 
    
         
            +
            }
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            function getReportingMetadata(source = 'embedded_player') {
         
     | 
| 
      
 69 
     | 
    
         
            +
              const [country, region] = Intl.DateTimeFormat().resolvedOptions().timeZone?.split?.('/') ?? [];
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
              const utmParams: any = {};
         
     | 
| 
      
 72 
     | 
    
         
            +
              [...new URLSearchParams(location.search).entries()].forEach(([key, value]) => {
         
     | 
| 
      
 73 
     | 
    
         
            +
                if (/utm/.test(key)) {
         
     | 
| 
      
 74 
     | 
    
         
            +
                  const param = key.toLowerCase().replace(/[-_][a-z0-9]/g, (group) => group.slice(-1).toUpperCase());
         
     | 
| 
      
 75 
     | 
    
         
            +
                  utmParams[param] = value;
         
     | 
| 
      
 76 
     | 
    
         
            +
                }
         
     | 
| 
      
 77 
     | 
    
         
            +
              });
         
     | 
| 
      
 78 
     | 
    
         
            +
             
     | 
| 
      
 79 
     | 
    
         
            +
              return {
         
     | 
| 
      
 80 
     | 
    
         
            +
                source,
         
     | 
| 
      
 81 
     | 
    
         
            +
                time: new Date(),
         
     | 
| 
      
 82 
     | 
    
         
            +
                region,
         
     | 
| 
      
 83 
     | 
    
         
            +
                country,
         
     | 
| 
      
 84 
     | 
    
         
            +
                screenHeight: window.screen.height,
         
     | 
| 
      
 85 
     | 
    
         
            +
                screenWidth: window.screen.width,
         
     | 
| 
      
 86 
     | 
    
         
            +
                referrer: document.referrer,
         
     | 
| 
      
 87 
     | 
    
         
            +
                currentUrl: location.href,
         
     | 
| 
      
 88 
     | 
    
         
            +
                embedType: 'media-player-embed',
         
     | 
| 
      
 89 
     | 
    
         
            +
                embedVersion: packageJson.version,
         
     | 
| 
      
 90 
     | 
    
         
            +
                templateVersion: TEMPLATE_VERSION,
         
     | 
| 
      
 91 
     | 
    
         
            +
                ...utmParams
         
     | 
| 
      
 92 
     | 
    
         
            +
              };
         
     | 
| 
      
 93 
     | 
    
         
            +
            }
         
     | 
| 
      
 94 
     | 
    
         
            +
             
     | 
| 
      
 95 
     | 
    
         
            +
            export { getUids, findVouchId, getReportingMetadata };
         
     | 
| 
         @@ -1,9 +1,9 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            import { html, LitElement } from 'lit';
         
     | 
| 
      
 1 
     | 
    
         
            +
            import { css, html, LitElement } from 'lit';
         
     | 
| 
       2 
2 
     | 
    
         
             
            import { customElement, property, state } from 'lit/decorators.js';
         
     | 
| 
       3 
3 
     | 
    
         
             
            import { ifDefined } from 'lit/directives/if-defined.js';
         
     | 
| 
       4 
4 
     | 
    
         
             
            import { createRef, ref } from 'lit/directives/ref.js';
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
            import type { Scene, TemplateInstance } from '@vouchfor/canvas-video';
         
     | 
| 
      
 6 
     | 
    
         
            +
            import type { Scene, Scenes, TemplateInstance } from '@vouchfor/canvas-video';
         
     | 
| 
       7 
7 
     | 
    
         
             
            import type { MediaPlayer, MediaPlayerProps } from '@vouchfor/media-player';
         
     | 
| 
       8 
8 
     | 
    
         
             
            import type { Ref } from 'lit/directives/ref.js';
         
     | 
| 
       9 
9 
     | 
    
         
             
            import type { Environment } from '~/utils/env';
         
     | 
| 
         @@ -21,20 +21,31 @@ type EmbedProps = Pick<MediaPlayerProps, 'data' | 'aspectRatio' | 'preload' | 'a 
     | 
|
| 
       21 
21 
     | 
    
         
             
              trackingSource?: string;
         
     | 
| 
       22 
22 
     | 
    
         
             
              vouchId?: string;
         
     | 
| 
       23 
23 
     | 
    
         
             
              templateId?: string;
         
     | 
| 
      
 24 
     | 
    
         
            +
              // Index of the questions to include starting from 1
         
     | 
| 
      
 25 
     | 
    
         
            +
              questions?: number[];
         
     | 
| 
       24 
26 
     | 
    
         
             
            };
         
     | 
| 
       25 
27 
     | 
    
         | 
| 
       26 
28 
     | 
    
         
             
            @customElement('vouch-embed')
         
     | 
| 
       27 
29 
     | 
    
         
             
            class Embed extends LitElement {
         
     | 
| 
      
 30 
     | 
    
         
            +
              static styles = [
         
     | 
| 
      
 31 
     | 
    
         
            +
                css`
         
     | 
| 
      
 32 
     | 
    
         
            +
                  :host {
         
     | 
| 
      
 33 
     | 
    
         
            +
                    display: flex;
         
     | 
| 
      
 34 
     | 
    
         
            +
                  }
         
     | 
| 
      
 35 
     | 
    
         
            +
                `
         
     | 
| 
      
 36 
     | 
    
         
            +
              ];
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
       28 
38 
     | 
    
         
             
              private _mediaPlayerRef: Ref<MediaPlayer> = createRef();
         
     | 
| 
       29 
39 
     | 
    
         | 
| 
       30 
     | 
    
         
            -
              @property({ type: Object 
     | 
| 
      
 40 
     | 
    
         
            +
              @property({ type: Object }) data: EmbedProps['data'];
         
     | 
| 
       31 
41 
     | 
    
         
             
              @property({ type: String }) vouchId: EmbedProps['vouchId'];
         
     | 
| 
       32 
42 
     | 
    
         
             
              @property({ type: String }) templateId: EmbedProps['templateId'];
         
     | 
| 
      
 43 
     | 
    
         
            +
              @property({ type: Array }) questions: EmbedProps['questions'];
         
     | 
| 
       33 
44 
     | 
    
         | 
| 
       34 
45 
     | 
    
         
             
              @property({ type: String }) env: EmbedProps['env'] = 'prod';
         
     | 
| 
       35 
46 
     | 
    
         
             
              @property({ type: String }) apiKey: EmbedProps['apiKey'] = '';
         
     | 
| 
       36 
47 
     | 
    
         
             
              @property({ type: Boolean }) disableTracking: EmbedProps['disableTracking'] = false;
         
     | 
| 
       37 
     | 
    
         
            -
              @property({ type: String }) trackingSource: EmbedProps['trackingSource'] = ' 
     | 
| 
      
 48 
     | 
    
         
            +
              @property({ type: String }) trackingSource: EmbedProps['trackingSource'] = 'embedded_player';
         
     | 
| 
       38 
49 
     | 
    
         | 
| 
       39 
50 
     | 
    
         
             
              @property({ type: Array }) controls: EmbedProps['controls'];
         
     | 
| 
       40 
51 
     | 
    
         
             
              @property({ type: String }) preload: EmbedProps['preload'] = 'auto';
         
     | 
| 
         @@ -149,6 +160,10 @@ class Embed extends LitElement { 
     | 
|
| 
       149 
160 
     | 
    
         
             
                return this._mediaPlayerRef.value?.scenes ?? [];
         
     | 
| 
       150 
161 
     | 
    
         
             
              }
         
     | 
| 
       151 
162 
     | 
    
         | 
| 
      
 163 
     | 
    
         
            +
              get sceneConfig(): Scenes | null {
         
     | 
| 
      
 164 
     | 
    
         
            +
                return this._mediaPlayerRef.value?.sceneConfig ?? null;
         
     | 
| 
      
 165 
     | 
    
         
            +
              }
         
     | 
| 
      
 166 
     | 
    
         
            +
             
     | 
| 
       152 
167 
     | 
    
         
             
              get videoState() {
         
     | 
| 
       153 
168 
     | 
    
         
             
                return this._mediaPlayerRef.value?.videoState;
         
     | 
| 
       154 
169 
     | 
    
         
             
              }
         
     | 
| 
         @@ -169,9 +184,35 @@ class Embed extends LitElement { 
     | 
|
| 
       169 
184 
     | 
    
         
             
                this._mediaPlayerRef.value?.setScene(index);
         
     | 
| 
       170 
185 
     | 
    
         
             
              }
         
     | 
| 
       171 
186 
     | 
    
         | 
| 
      
 187 
     | 
    
         
            +
              private _renderStyles() {
         
     | 
| 
      
 188 
     | 
    
         
            +
                if (!this.aspectRatio) {
         
     | 
| 
      
 189 
     | 
    
         
            +
                  return html`
         
     | 
| 
      
 190 
     | 
    
         
            +
                    <style>
         
     | 
| 
      
 191 
     | 
    
         
            +
                      :host {
         
     | 
| 
      
 192 
     | 
    
         
            +
                        width: 100%;
         
     | 
| 
      
 193 
     | 
    
         
            +
                        height: 100%;
         
     | 
| 
      
 194 
     | 
    
         
            +
                      }
         
     | 
| 
      
 195 
     | 
    
         
            +
                    </style>
         
     | 
| 
      
 196 
     | 
    
         
            +
                  `;
         
     | 
| 
      
 197 
     | 
    
         
            +
                }
         
     | 
| 
      
 198 
     | 
    
         
            +
             
     | 
| 
      
 199 
     | 
    
         
            +
                if (typeof this.aspectRatio === 'number') {
         
     | 
| 
      
 200 
     | 
    
         
            +
                  return html`
         
     | 
| 
      
 201 
     | 
    
         
            +
                    <style>
         
     | 
| 
      
 202 
     | 
    
         
            +
                      :host {
         
     | 
| 
      
 203 
     | 
    
         
            +
                        aspect-ratio: ${this.aspectRatio};
         
     | 
| 
      
 204 
     | 
    
         
            +
                      }
         
     | 
| 
      
 205 
     | 
    
         
            +
                    </style>
         
     | 
| 
      
 206 
     | 
    
         
            +
                  `;
         
     | 
| 
      
 207 
     | 
    
         
            +
                }
         
     | 
| 
      
 208 
     | 
    
         
            +
             
     | 
| 
      
 209 
     | 
    
         
            +
                return null;
         
     | 
| 
      
 210 
     | 
    
         
            +
              }
         
     | 
| 
      
 211 
     | 
    
         
            +
             
     | 
| 
       172 
212 
     | 
    
         
             
              render() {
         
     | 
| 
       173 
213 
     | 
    
         
             
                return html`
         
     | 
| 
       174 
     | 
    
         
            -
                   
     | 
| 
      
 214 
     | 
    
         
            +
                  ${this._renderStyles()}
         
     | 
| 
      
 215 
     | 
    
         
            +
                  <vmp-media-player
         
     | 
| 
       175 
216 
     | 
    
         
             
                    ${ref(this._mediaPlayerRef)}
         
     | 
| 
       176 
217 
     | 
    
         
             
                    ${this.eventController.register()}
         
     | 
| 
       177 
218 
     | 
    
         
             
                    ?autoplay=${this.autoplay}
         
     | 
| 
         @@ -181,7 +222,7 @@ class Embed extends LitElement { 
     | 
|
| 
       181 
222 
     | 
    
         
             
                    aspectRatio=${ifDefined(this.aspectRatio)}
         
     | 
| 
       182 
223 
     | 
    
         
             
                    preload=${ifDefined(this.preload)}
         
     | 
| 
       183 
224 
     | 
    
         
             
                    .controls=${this.controls}
         
     | 
| 
       184 
     | 
    
         
            -
                  ></vmp- 
     | 
| 
      
 225 
     | 
    
         
            +
                  ></vmp-media-player>
         
     | 
| 
       185 
226 
     | 
    
         
             
                `;
         
     | 
| 
       186 
227 
     | 
    
         
             
              }
         
     | 
| 
       187 
228 
     | 
    
         
             
            }
         
     | 
| 
         @@ -0,0 +1,78 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            /* eslint-disable @typescript-eslint/ban-ts-comment */
         
     | 
| 
      
 2 
     | 
    
         
            +
            import { expect, fixture, waitUntil } from '@open-wc/testing';
         
     | 
| 
      
 3 
     | 
    
         
            +
            import { html } from 'lit';
         
     | 
| 
      
 4 
     | 
    
         
            +
            import sinon from 'sinon';
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            import type { Embed } from '../index.js';
         
     | 
| 
      
 7 
     | 
    
         
            +
            import type { VideoMap } from '@vouchfor/media-player';
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            import { data } from './data.js';
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            // Can't use typescript aliases with esbuild file transforms apparently
         
     | 
| 
      
 12 
     | 
    
         
            +
            // No idea what a good way to actually build before testing is, the examples give nothing
         
     | 
| 
      
 13 
     | 
    
         
            +
            // https://modern-web.dev/guides/test-runner/typescript/
         
     | 
| 
      
 14 
     | 
    
         
            +
            import '../../../test/lib/embeds.js';
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            function getVideo(videos: VideoMap) {
         
     | 
| 
      
 17 
     | 
    
         
            +
              return Object.values(videos)[0];
         
     | 
| 
      
 18 
     | 
    
         
            +
            }
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
            function playerLoaded(player: Embed) {
         
     | 
| 
      
 21 
     | 
    
         
            +
              return waitUntil(
         
     | 
| 
      
 22 
     | 
    
         
            +
                () => {
         
     | 
| 
      
 23 
     | 
    
         
            +
                  return player.mediaPlayer?.initialised;
         
     | 
| 
      
 24 
     | 
    
         
            +
                },
         
     | 
| 
      
 25 
     | 
    
         
            +
                'Player has not loaded video',
         
     | 
| 
      
 26 
     | 
    
         
            +
                { timeout: 20000 }
         
     | 
| 
      
 27 
     | 
    
         
            +
              );
         
     | 
| 
      
 28 
     | 
    
         
            +
            }
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            describe('Embeds', () => {
         
     | 
| 
      
 31 
     | 
    
         
            +
              it('passes', async () => {
         
     | 
| 
      
 32 
     | 
    
         
            +
                const player = await fixture<Embed>(html`<vouch-embed env="dev" .data=${data} aspectratio=${1}></vouch-embed>`);
         
     | 
| 
      
 33 
     | 
    
         
            +
                // @ts-ignore - accessing private property
         
     | 
| 
      
 34 
     | 
    
         
            +
                const sendTrackingSpy = sinon.spy(player._trackingController, '_sendTrackingEvent');
         
     | 
| 
      
 35 
     | 
    
         
            +
                // @ts-ignore - accessing private property
         
     | 
| 
      
 36 
     | 
    
         
            +
                const createTrackingSpy = sinon.spy(player._trackingController, '_createTrackingEvent');
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                await playerLoaded(player);
         
     | 
| 
      
 39 
     | 
    
         
            +
                // Have to mute the player because we can't programmatically play videos with sound
         
     | 
| 
      
 40 
     | 
    
         
            +
                player.muted = true;
         
     | 
| 
      
 41 
     | 
    
         
            +
                player.play();
         
     | 
| 
      
 42 
     | 
    
         
            +
                expect(player.paused).eq(false);
         
     | 
| 
      
 43 
     | 
    
         
            +
                await waitUntil(
         
     | 
| 
      
 44 
     | 
    
         
            +
                  () => {
         
     | 
| 
      
 45 
     | 
    
         
            +
                    // Video plays for 3 seconds
         
     | 
| 
      
 46 
     | 
    
         
            +
                    return (getVideo(player.mediaPlayer!.videos)?.node?.currentTime ?? 0) > 3;
         
     | 
| 
      
 47 
     | 
    
         
            +
                  },
         
     | 
| 
      
 48 
     | 
    
         
            +
                  'Video did not play for 3 seconds',
         
     | 
| 
      
 49 
     | 
    
         
            +
                  { timeout: 20000 }
         
     | 
| 
      
 50 
     | 
    
         
            +
                );
         
     | 
| 
      
 51 
     | 
    
         
            +
                expect(getVideo(player.mediaPlayer!.videos)?.node?.paused).eq(false);
         
     | 
| 
      
 52 
     | 
    
         
            +
                player.pause();
         
     | 
| 
      
 53 
     | 
    
         
            +
                expect(getVideo(player.mediaPlayer!.videos)?.node?.paused).eq(true);
         
     | 
| 
      
 54 
     | 
    
         
            +
                expect(sendTrackingSpy.callCount).to.be.eq(0);
         
     | 
| 
      
 55 
     | 
    
         
            +
                // Destroy node because events are sent when node is removed from the document
         
     | 
| 
      
 56 
     | 
    
         
            +
                player.remove();
         
     | 
| 
      
 57 
     | 
    
         
            +
                expect(sendTrackingSpy.callCount).to.be.eq(1);
         
     | 
| 
      
 58 
     | 
    
         
            +
                expect(createTrackingSpy.args[0]).to.eql([
         
     | 
| 
      
 59 
     | 
    
         
            +
                  'VIDEO_PLAYED',
         
     | 
| 
      
 60 
     | 
    
         
            +
                  {
         
     | 
| 
      
 61 
     | 
    
         
            +
                    streamStart: 0
         
     | 
| 
      
 62 
     | 
    
         
            +
                  }
         
     | 
| 
      
 63 
     | 
    
         
            +
                ]);
         
     | 
| 
      
 64 
     | 
    
         
            +
                expect(createTrackingSpy.args[1]).to.eql([
         
     | 
| 
      
 65 
     | 
    
         
            +
                  'VOUCH_RESPONSE_VIEWED',
         
     | 
| 
      
 66 
     | 
    
         
            +
                  {
         
     | 
| 
      
 67 
     | 
    
         
            +
                    answerId: '5c66bb3a-ed68-41a0-a601-a49865104418'
         
     | 
| 
      
 68 
     | 
    
         
            +
                  }
         
     | 
| 
      
 69 
     | 
    
         
            +
                ]);
         
     | 
| 
      
 70 
     | 
    
         
            +
                expect(createTrackingSpy.args[2][0]).to.eq('VIDEO_STREAMED');
         
     | 
| 
      
 71 
     | 
    
         
            +
                // Remove streamStart and streamEnd as these are non-deterministic
         
     | 
| 
      
 72 
     | 
    
         
            +
                expect({ ...createTrackingSpy.args[2][1], streamStart: undefined, streamEnd: undefined }).to.eql({
         
     | 
| 
      
 73 
     | 
    
         
            +
                  answerId: '5c66bb3a-ed68-41a0-a601-a49865104418',
         
     | 
| 
      
 74 
     | 
    
         
            +
                  streamStart: undefined,
         
     | 
| 
      
 75 
     | 
    
         
            +
                  streamEnd: undefined
         
     | 
| 
      
 76 
     | 
    
         
            +
                });
         
     | 
| 
      
 77 
     | 
    
         
            +
              });
         
     | 
| 
      
 78 
     | 
    
         
            +
            });
         
     |