@vkontakte/videoplayer-statistics 1.0.3-dev.fd0f81b.0 → 1.0.6
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/Api.d.ts +10 -0
- package/ApiTransport.d.ts +3 -0
- package/OneStat.d.ts +39 -1
- package/index.cjs.js +3 -3
- package/index.d.ts +3 -3
- package/index.esm.js +3 -3
- package/package.json +3 -2
- package/types.d.ts +29 -0
package/Api.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ContentType, Quality, ScreenDestination, StatType } from './types';
|
|
1
2
|
import { IApiTransport } from './ApiTransport';
|
|
2
3
|
export interface ILogItem {
|
|
3
4
|
operation: string;
|
|
@@ -9,6 +10,15 @@ export interface ILogCustomData {
|
|
|
9
10
|
* Id видео
|
|
10
11
|
*/
|
|
11
12
|
vid: number;
|
|
13
|
+
quality?: Quality;
|
|
14
|
+
dst?: ScreenDestination;
|
|
15
|
+
ct?: ContentType;
|
|
16
|
+
aid?: string;
|
|
17
|
+
place?: string;
|
|
18
|
+
cdn_host?: string;
|
|
19
|
+
stat_type?: StatType;
|
|
20
|
+
vk_playlist_id?: string;
|
|
21
|
+
vk_app_id?: string;
|
|
12
22
|
/**
|
|
13
23
|
* Параметр события (имеет разный смысл в каждом из событий)
|
|
14
24
|
*/
|
package/ApiTransport.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { ISubject, IError } from '@vkontakte/videoplayer-shared';
|
|
1
2
|
export interface IApiTransportParams {
|
|
2
3
|
apiKey: string;
|
|
3
4
|
apiBaseUrl: string;
|
|
5
|
+
error$: ISubject<IError>;
|
|
4
6
|
}
|
|
5
7
|
export interface IApiTransport {
|
|
6
8
|
sendBeacon(method: string, params: Record<string, unknown>, sessionKey?: string): boolean;
|
|
@@ -9,6 +11,7 @@ export interface IApiTransport {
|
|
|
9
11
|
export default class ApiTransport implements IApiTransport {
|
|
10
12
|
private apiKey;
|
|
11
13
|
private apiBaseUrl;
|
|
14
|
+
private params;
|
|
12
15
|
constructor(params: IApiTransportParams);
|
|
13
16
|
sendBeacon(method: string, params: Record<string, unknown>, sessionKey?: string): boolean;
|
|
14
17
|
sendRequest<T = unknown>(method: string, params: Record<string, unknown>, sessionKey?: string): Promise<T>;
|
package/OneStat.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IObservable, IError } from '@vkontakte/videoplayer-shared';
|
|
2
|
+
import { ApiEnv, ContentType, Quality, ScreenDestination } from './types';
|
|
2
3
|
export interface IOneStat {
|
|
3
4
|
/**
|
|
4
5
|
* Лучше авторизоваться сразу, чтобы избежать ситуации:
|
|
@@ -7,6 +8,10 @@ export interface IOneStat {
|
|
|
7
8
|
* 3. Запрос пытается уйти через sendBeacon и падает без авторизации
|
|
8
9
|
*/
|
|
9
10
|
authorize(): Promise<unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Обновляет общие параметры событий
|
|
13
|
+
*/
|
|
14
|
+
updateContext(context: Partial<IStatContext>): void;
|
|
10
15
|
/**
|
|
11
16
|
* Ставит отправку событий на паузу. Они будут накапливаться до вызова `resume()`
|
|
12
17
|
*/
|
|
@@ -57,6 +62,16 @@ export interface IOneStat {
|
|
|
57
62
|
* произошла ошибка
|
|
58
63
|
*/
|
|
59
64
|
logError(payload: IErrorPayload): void;
|
|
65
|
+
logAdSlotRequest(): void;
|
|
66
|
+
logAdStarted(sectionType: string): void;
|
|
67
|
+
logAdPaused(): void;
|
|
68
|
+
logAdResumed(): void;
|
|
69
|
+
logAdEnded(): void;
|
|
70
|
+
logAdSkipped(): void;
|
|
71
|
+
logAdClicked(): void;
|
|
72
|
+
experimental: {
|
|
73
|
+
error$: IObservable<IError>;
|
|
74
|
+
};
|
|
60
75
|
}
|
|
61
76
|
export interface IPausePayload {
|
|
62
77
|
/**
|
|
@@ -88,6 +103,18 @@ export interface IStatContext {
|
|
|
88
103
|
* Ид видео
|
|
89
104
|
*/
|
|
90
105
|
movieId: number;
|
|
106
|
+
quality?: Quality;
|
|
107
|
+
destination?: ScreenDestination;
|
|
108
|
+
contentType?: ContentType;
|
|
109
|
+
albumId?: string;
|
|
110
|
+
vkPlaylistId?: string;
|
|
111
|
+
vkAppId?: string;
|
|
112
|
+
/**
|
|
113
|
+
* Раздел сайта на котором играет видео: Лента, плеер, личные сообщения, ...
|
|
114
|
+
*/
|
|
115
|
+
place?: string;
|
|
116
|
+
cdnHostname?: string;
|
|
117
|
+
autoplay?: boolean;
|
|
91
118
|
}
|
|
92
119
|
export interface IConstructorParams {
|
|
93
120
|
apiEnv: ApiEnv;
|
|
@@ -100,8 +127,12 @@ export interface IWatchCoveragePayload {
|
|
|
100
127
|
export declare class OneStat implements IOneStat {
|
|
101
128
|
private api;
|
|
102
129
|
private logger;
|
|
130
|
+
experimental: {
|
|
131
|
+
error$: IObservable<IError>;
|
|
132
|
+
};
|
|
103
133
|
private statContext;
|
|
104
134
|
constructor(statContext: IStatContext, params: IConstructorParams);
|
|
135
|
+
updateContext(newContext: Partial<IStatContext>): void;
|
|
105
136
|
authorize(authToken?: string): Promise<unknown>;
|
|
106
137
|
pause(): void;
|
|
107
138
|
resume(): void;
|
|
@@ -114,6 +145,13 @@ export declare class OneStat implements IOneStat {
|
|
|
114
145
|
logError(payload: IErrorPayload): void;
|
|
115
146
|
logWatchCoverageRecord(payload: IWatchCoveragePayload): void;
|
|
116
147
|
logWatchCoverageLive(payload: IWatchCoveragePayload): void;
|
|
148
|
+
logAdSlotRequest(): void;
|
|
149
|
+
logAdStarted(sectionType: string): void;
|
|
150
|
+
logAdPaused(): void;
|
|
151
|
+
logAdResumed(): void;
|
|
152
|
+
logAdEnded(): void;
|
|
153
|
+
logAdSkipped(): void;
|
|
154
|
+
logAdClicked(): void;
|
|
117
155
|
private log;
|
|
118
156
|
private crateLogItem;
|
|
119
157
|
}
|
package/index.cjs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vkontakte/videoplayer-statistics v1.0.
|
|
3
|
-
*
|
|
2
|
+
* @vkontakte/videoplayer-statistics v1.0.6
|
|
3
|
+
* Thu, 09 Dec 2021 12:00:52 GMT
|
|
4
4
|
*/
|
|
5
|
-
"use strict";function e(
|
|
5
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@vkontakte/videoplayer-shared");function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=e(require("lodash/debounce"));function o(t,e,s,o){return new(s||(s=Promise))((function(i,r){function a(t){try{h(o.next(t))}catch(t){r(t)}}function n(t){try{h(o.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,n)}h((o=o.apply(t,e||[])).next())}))}const i=(()=>{try{const t=Date.now().toString(),e=window.localStorage;e.setItem(t,t);const s=e.getItem(t)===t;return e.removeItem(t),s?e:null}catch(t){return null}})();var r,a,n,h,u,p={get:t=>function(t){const e=i?i.getItem("_one-stat_"+t):null;if(null===e)return null;try{return JSON.parse(e)}catch(t){return null}}(t)||null,set(t,e){!function(t,e){try{i&&i.setItem("_one-stat_"+t,JSON.stringify(e))}catch(t){}}(t,e)},remove(t){!function(t){i&&i.removeItem("_one-stat_"+t)}(t)}};exports.ApiEnv=void 0,(r=exports.ApiEnv||(exports.ApiEnv={})).PROD="prod",r.VIDEOTEST="videotest",r.TEST="test",exports.Quality=void 0,(a=exports.Quality||(exports.Quality={})).Q144P="144",a.Q240P="240",a.Q360P="360",a.Q480P="480",a.Q720P="720",a.Q1080P="1080",a.Q1440P="1440",a.Q2160P="2160",a.Q4320P="4320",a.UNKNOWN="unknown",exports.ScreenDestination=void 0,(n=exports.ScreenDestination||(exports.ScreenDestination={})).CHROMECAST="chromecast",n.APPLE_TV="appletv",n.PIP="pip",n.HEADPHONES="externalHeadphones",n.SCREEN="",exports.ContentType=void 0,(h=exports.ContentType||(exports.ContentType={})).MP4="mp4",h.DASH="dash",h.HLS="hls",h.WEBM="webm",function(t){t.AUTO="auto",t.MANUAL=""}(u||(u={}));const d={[exports.ApiEnv.PROD]:"https://api.ok.ru",[exports.ApiEnv.VIDEOTEST]:"https://videotestapi.ok.ru/api",[exports.ApiEnv.TEST]:"https://apitest.ok.ru"};var l;!function(t){t[t.PARAM_SESSION_EXPIRED=102]="PARAM_SESSION_EXPIRED",t[t.PARAM_SESSION_KEY=103]="PARAM_SESSION_KEY",t[t.PARAM_SIGNATURE=104]="PARAM_SIGNATURE",t[t.AUTH_LOGIN=401]="AUTH_LOGIN"}(l||(l={}));class c{constructor(t){this.params=t;const e=p.get("uuid");e?this.uuid=String(e):(this.uuid=function(){const t="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),e=new Array(36);let s,o,i=0;for(o=0;o<36;o++)8===o||13===o||18===o||23===o?e[o]="-":14===o?e[o]="4":(i<=2&&(i=33554432+16777216*Math.random()|0),s=15&i,i>>=4,e[o]=t[19===o?3&s|8:s]);return e.join("")}(),p.set("uuid",this.uuid))}authorize(t){return o(this,void 0,void 0,(function*(){return this.authToken=null!=t?t:yield this.refreshAuthToken(),this._authorize()}))}logBeacon(t){const e="log.externalLog",s=this.createLogParams(t),o=this.sessionKey;void 0!==o&&this.params.apiTransport.sendBeacon(e,s,o)||this.params.apiTransport.sendRequest(e,s,o)}logRequest(t){var e,s;return o(this,void 0,void 0,(function*(){const o="log.externalLog",i=this.createLogParams(t),r=null!==(e=this.sessionKey)&&void 0!==e?e:yield this._authorize();try{return yield this.params.apiTransport.sendRequest(o,i,r)}catch(t){switch(null===(s=t)||void 0===s?void 0:s.error_code){case l.PARAM_SESSION_EXPIRED:case l.PARAM_SESSION_KEY:case l.PARAM_SIGNATURE:{const t=yield this._authorize();return this.params.apiTransport.sendRequest(o,i,t)}case l.AUTH_LOGIN:{this.authToken=yield this.refreshAuthToken();const t=yield this._authorizeRequest();return this.params.apiTransport.sendRequest(o,i,t)}default:throw t}}}))}refreshAuthToken(){return o(this,void 0,void 0,(function*(){if(this.params.refreshAuthToken)return this.refreshAuthTokenPromise||(this.refreshAuthTokenPromise=this.params.refreshAuthToken().finally((()=>{this.refreshAuthTokenPromise=void 0}))),this.refreshAuthTokenPromise}))}createLogParams(t){return{collector:"ok.mobile.apps.video",data:JSON.stringify({application:"@vkontakte/videoplayer-statistics:1.0.6",platform:"WEB",items:t.map((t=>Object.assign(Object.assign({},t),{time:0,type:1})))})}}_authorize(){var t;return o(this,void 0,void 0,(function*(){try{return yield this._authorizeRequest()}catch(e){switch(null===(t=e)||void 0===t?void 0:t.error_code){case l.AUTH_LOGIN:return this.authToken=yield this.refreshAuthToken(),this._authorizeRequest()}throw e}}))}_authorizeRequest(){return o(this,void 0,void 0,(function*(){if(this.authorizePromise)return this.authorizePromise;this.sessionKey=void 0;const t="auth.anonymLogin",e={session_data:{version:2,device_id:this.uuid,client_version:"1.0.6".split("-")[0],client_type:"SDK_JS"}};return void 0!==this.authToken&&(e.session_data.auth_token=this.authToken,e.session_data.version=3),this.authorizePromise=this.params.apiTransport.sendRequest(t,e).then((s=>{if(!s)throw new Error(`Empty response. Method: ${t}, params: ${JSON.stringify(e)}`);if(!s.session_key)throw new Error(`Empty 'session_key' in response. Method: ${t}, params: ${JSON.stringify(e)}, response: ${JSON.stringify(s)}`);return this.sessionKey=s.session_key,this.sessionKey})).catch((t=>{throw this.sessionKey=void 0,t})).finally((()=>{this.authorizePromise=void 0})),this.authorizePromise}))}}class m{constructor(t){this.params=t,this.apiKey=t.apiKey,this.apiBaseUrl=t.apiBaseUrl,"/"!==this.apiBaseUrl[this.apiBaseUrl.length-1]&&(this.apiBaseUrl+="/")}sendBeacon(t,e,s){if(!window.Blob||!window.navigator.sendBeacon)return!1;const o=this._prepareQueryParams({method:t,queryParams:e,sessionKey:s}),i=new window.Blob([o.toString()],{type:"application/x-www-form-urlencoded"});return window.navigator.sendBeacon(`${this.apiBaseUrl}fb.do`,i)}sendRequest(t,e,s){return o(this,void 0,void 0,(function*(){const o=Date.now(),i=i=>{throw this.params.error$.next({id:"OneStat:ApiTransport:sendRequest",message:"Unhandled request error",thrown:i,data:{method:t,params:e,sessionKey:s,time:Date.now()-o}}),i};return fetch(`${this.apiBaseUrl}fb.do`,{method:"post",headers:{"Content-type":"application/x-www-form-urlencoded"},body:this._prepareQueryParams({method:t,queryParams:e,sessionKey:s})}).then((t=>Number(t.headers.get("content-length"))>0?t.json().then((t=>Object.prototype.hasOwnProperty.call(t,"error_msg")?Promise.reject(t):t),i):void 0),i)}))}_prepareQueryParams(t){const e=new URLSearchParams({format:"JSON",method:t.method,application_key:this.apiKey});return void 0!==t.sessionKey&&e.append("session_key",t.sessionKey),Object.entries(t.queryParams).forEach((([t,s])=>e.append(t,"string"==typeof s?s:JSON.stringify(s)))),e}}class g{constructor(t){this.requestQueue=[],this.isPaused=!1,this.debouncedFlush=s.default((()=>this.flush()),3e3,{maxWait:15e3}),this.api=t.api}log(t){this.requestQueue.push(t),this.isPaused||this.debouncedFlush()}flush(t=!1){0!==this.requestQueue.length&&(t?this.api.logBeacon(this.requestQueue):this.api.logRequest(this.requestQueue),this.requestQueue=[],this.debouncedFlush.cancel())}pause(){this.isPaused=!0,this.debouncedFlush.cancel()}resume(){this.isPaused=!1,this.debouncedFlush()}}exports.OneStat=class{constructor(e,s){this.statContext=e;const o=new t.Subject;this.experimental={error$:o};const i=new m({apiKey:"CIOPGQJGDIHBABABA",apiBaseUrl:d[s.apiEnv],error$:o});this.api=new c({apiTransport:i,refreshAuthToken:s.refreshAuthToken}),this.logger=new g({api:this.api}),window.addEventListener("beforeunload",(()=>this.logger.flush(!0)))}updateContext(t){this.statContext=Object.assign(Object.assign({},this.statContext),t)}authorize(t){return this.api.authorize(t)}pause(){this.logger.pause()}resume(){this.logger.resume()}logStarted(){this.log({operation:"play"})}logPlay(){this.log({operation:"play_toggle"})}logPause(t){this.log({operation:"pause",param:String(Math.round(t.position))})}logSeek(){this.log({operation:"seek"})}logFirstBytes(t){this.log({operation:"first_bytes",param:String(t.time)})}logFirstFrame(t){this.log({operation:"first_frame",param:String(t.time)})}logError(t){this.log({operation:"error",param:t.errorType})}logWatchCoverageRecord(t){this.log({operation:"watch_coverage_record",param:`${t.start}-${t.end}`})}logWatchCoverageLive(t){this.log({operation:"watch_coverage_live",param:`${t.start}-${t.end}`})}logAdSlotRequest(){this.log({operation:"adv",param:"slot_request"})}logAdStarted(t){this.log({operation:"adv",param:t})}logAdPaused(){this.log({operation:"adv",param:"pause"})}logAdResumed(){this.log({operation:"adv",param:"resume"})}logAdEnded(){this.log({operation:"adv",param:"ended"})}logAdSkipped(){this.log({operation:"adv",param:"skip"})}logAdClicked(){this.log({operation:"adv",param:"click"})}log(t){const e=this.crateLogItem(t);this.logger.log(e)}crateLogItem({operation:t,param:e}){const s=Date.now(),o={vid:this.statContext.movieId,quality:this.statContext.quality,dst:this.statContext.destination,ct:this.statContext.contentType,aid:this.statContext.albumId,place:this.statContext.place,cdn_host:this.statContext.cdnHostname,stat_type:!0===this.statContext.autoplay?u.AUTO:!1===this.statContext.autoplay?u.MANUAL:void 0,vk_playlist_id:this.statContext.vkPlaylistId,vk_app_id:this.statContext.vkAppId};return void 0!==e&&(o.param=e),{operation:t,timestamp:s,custom:o}}};
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { IOneStat, OneStat } from './OneStat';
|
|
2
|
-
import { ApiEnv } from './types';
|
|
3
|
-
export { OneStat, IOneStat, ApiEnv };
|
|
1
|
+
import { IOneStat, OneStat, IStatContext } from './OneStat';
|
|
2
|
+
import { ApiEnv, ContentType, ScreenDestination, Quality } from './types';
|
|
3
|
+
export { OneStat, IOneStat, IStatContext, ApiEnv, ContentType, ScreenDestination, Quality };
|
package/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vkontakte/videoplayer-statistics v1.0.
|
|
3
|
-
*
|
|
2
|
+
* @vkontakte/videoplayer-statistics v1.0.6
|
|
3
|
+
* Thu, 09 Dec 2021 12:00:52 GMT
|
|
4
4
|
*/
|
|
5
|
-
import e from"lodash/debounce";function t
|
|
5
|
+
import{Subject as t}from"@vkontakte/videoplayer-shared";import e from"lodash/debounce";function s(t,e,s,o){return new(s||(s=Promise))((function(i,r){function a(t){try{h(o.next(t))}catch(t){r(t)}}function n(t){try{h(o.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,n)}h((o=o.apply(t,e||[])).next())}))}const o=(()=>{try{const t=Date.now().toString(),e=window.localStorage;e.setItem(t,t);const s=e.getItem(t)===t;return e.removeItem(t),s?e:null}catch(t){return null}})();var i,r,a,n,h,u={get:t=>function(t){const e=o?o.getItem("_one-stat_"+t):null;if(null===e)return null;try{return JSON.parse(e)}catch(t){return null}}(t)||null,set(t,e){!function(t,e){try{o&&o.setItem("_one-stat_"+t,JSON.stringify(e))}catch(t){}}(t,e)},remove(t){!function(t){o&&o.removeItem("_one-stat_"+t)}(t)}};!function(t){t.PROD="prod",t.VIDEOTEST="videotest",t.TEST="test"}(i||(i={})),function(t){t.Q144P="144",t.Q240P="240",t.Q360P="360",t.Q480P="480",t.Q720P="720",t.Q1080P="1080",t.Q1440P="1440",t.Q2160P="2160",t.Q4320P="4320",t.UNKNOWN="unknown"}(r||(r={})),function(t){t.CHROMECAST="chromecast",t.APPLE_TV="appletv",t.PIP="pip",t.HEADPHONES="externalHeadphones",t.SCREEN=""}(a||(a={})),function(t){t.MP4="mp4",t.DASH="dash",t.HLS="hls",t.WEBM="webm"}(n||(n={})),function(t){t.AUTO="auto",t.MANUAL=""}(h||(h={}));const p={[i.PROD]:"https://api.ok.ru",[i.VIDEOTEST]:"https://videotestapi.ok.ru/api",[i.TEST]:"https://apitest.ok.ru"};var d;!function(t){t[t.PARAM_SESSION_EXPIRED=102]="PARAM_SESSION_EXPIRED",t[t.PARAM_SESSION_KEY=103]="PARAM_SESSION_KEY",t[t.PARAM_SIGNATURE=104]="PARAM_SIGNATURE",t[t.AUTH_LOGIN=401]="AUTH_LOGIN"}(d||(d={}));class l{constructor(t){this.params=t;const e=u.get("uuid");e?this.uuid=String(e):(this.uuid=function(){const t="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),e=new Array(36);let s,o,i=0;for(o=0;o<36;o++)8===o||13===o||18===o||23===o?e[o]="-":14===o?e[o]="4":(i<=2&&(i=33554432+16777216*Math.random()|0),s=15&i,i>>=4,e[o]=t[19===o?3&s|8:s]);return e.join("")}(),u.set("uuid",this.uuid))}authorize(t){return s(this,void 0,void 0,(function*(){return this.authToken=null!=t?t:yield this.refreshAuthToken(),this._authorize()}))}logBeacon(t){const e="log.externalLog",s=this.createLogParams(t),o=this.sessionKey;void 0!==o&&this.params.apiTransport.sendBeacon(e,s,o)||this.params.apiTransport.sendRequest(e,s,o)}logRequest(t){var e,o;return s(this,void 0,void 0,(function*(){const s="log.externalLog",i=this.createLogParams(t),r=null!==(e=this.sessionKey)&&void 0!==e?e:yield this._authorize();try{return yield this.params.apiTransport.sendRequest(s,i,r)}catch(t){switch(null===(o=t)||void 0===o?void 0:o.error_code){case d.PARAM_SESSION_EXPIRED:case d.PARAM_SESSION_KEY:case d.PARAM_SIGNATURE:{const t=yield this._authorize();return this.params.apiTransport.sendRequest(s,i,t)}case d.AUTH_LOGIN:{this.authToken=yield this.refreshAuthToken();const t=yield this._authorizeRequest();return this.params.apiTransport.sendRequest(s,i,t)}default:throw t}}}))}refreshAuthToken(){return s(this,void 0,void 0,(function*(){if(this.params.refreshAuthToken)return this.refreshAuthTokenPromise||(this.refreshAuthTokenPromise=this.params.refreshAuthToken().finally((()=>{this.refreshAuthTokenPromise=void 0}))),this.refreshAuthTokenPromise}))}createLogParams(t){return{collector:"ok.mobile.apps.video",data:JSON.stringify({application:"@vkontakte/videoplayer-statistics:1.0.6",platform:"WEB",items:t.map((t=>Object.assign(Object.assign({},t),{time:0,type:1})))})}}_authorize(){var t;return s(this,void 0,void 0,(function*(){try{return yield this._authorizeRequest()}catch(e){switch(null===(t=e)||void 0===t?void 0:t.error_code){case d.AUTH_LOGIN:return this.authToken=yield this.refreshAuthToken(),this._authorizeRequest()}throw e}}))}_authorizeRequest(){return s(this,void 0,void 0,(function*(){if(this.authorizePromise)return this.authorizePromise;this.sessionKey=void 0;const t="auth.anonymLogin",e={session_data:{version:2,device_id:this.uuid,client_version:"1.0.6".split("-")[0],client_type:"SDK_JS"}};return void 0!==this.authToken&&(e.session_data.auth_token=this.authToken,e.session_data.version=3),this.authorizePromise=this.params.apiTransport.sendRequest(t,e).then((s=>{if(!s)throw new Error(`Empty response. Method: ${t}, params: ${JSON.stringify(e)}`);if(!s.session_key)throw new Error(`Empty 'session_key' in response. Method: ${t}, params: ${JSON.stringify(e)}, response: ${JSON.stringify(s)}`);return this.sessionKey=s.session_key,this.sessionKey})).catch((t=>{throw this.sessionKey=void 0,t})).finally((()=>{this.authorizePromise=void 0})),this.authorizePromise}))}}class c{constructor(t){this.params=t,this.apiKey=t.apiKey,this.apiBaseUrl=t.apiBaseUrl,"/"!==this.apiBaseUrl[this.apiBaseUrl.length-1]&&(this.apiBaseUrl+="/")}sendBeacon(t,e,s){if(!window.Blob||!window.navigator.sendBeacon)return!1;const o=this._prepareQueryParams({method:t,queryParams:e,sessionKey:s}),i=new window.Blob([o.toString()],{type:"application/x-www-form-urlencoded"});return window.navigator.sendBeacon(`${this.apiBaseUrl}fb.do`,i)}sendRequest(t,e,o){return s(this,void 0,void 0,(function*(){const s=Date.now(),i=i=>{throw this.params.error$.next({id:"OneStat:ApiTransport:sendRequest",message:"Unhandled request error",thrown:i,data:{method:t,params:e,sessionKey:o,time:Date.now()-s}}),i};return fetch(`${this.apiBaseUrl}fb.do`,{method:"post",headers:{"Content-type":"application/x-www-form-urlencoded"},body:this._prepareQueryParams({method:t,queryParams:e,sessionKey:o})}).then((t=>Number(t.headers.get("content-length"))>0?t.json().then((t=>Object.prototype.hasOwnProperty.call(t,"error_msg")?Promise.reject(t):t),i):void 0),i)}))}_prepareQueryParams(t){const e=new URLSearchParams({format:"JSON",method:t.method,application_key:this.apiKey});return void 0!==t.sessionKey&&e.append("session_key",t.sessionKey),Object.entries(t.queryParams).forEach((([t,s])=>e.append(t,"string"==typeof s?s:JSON.stringify(s)))),e}}class m{constructor(t){this.requestQueue=[],this.isPaused=!1,this.debouncedFlush=e((()=>this.flush()),3e3,{maxWait:15e3}),this.api=t.api}log(t){this.requestQueue.push(t),this.isPaused||this.debouncedFlush()}flush(t=!1){0!==this.requestQueue.length&&(t?this.api.logBeacon(this.requestQueue):this.api.logRequest(this.requestQueue),this.requestQueue=[],this.debouncedFlush.cancel())}pause(){this.isPaused=!0,this.debouncedFlush.cancel()}resume(){this.isPaused=!1,this.debouncedFlush()}}class g{constructor(e,s){this.statContext=e;const o=new t;this.experimental={error$:o};const i=new c({apiKey:"CIOPGQJGDIHBABABA",apiBaseUrl:p[s.apiEnv],error$:o});this.api=new l({apiTransport:i,refreshAuthToken:s.refreshAuthToken}),this.logger=new m({api:this.api}),window.addEventListener("beforeunload",(()=>this.logger.flush(!0)))}updateContext(t){this.statContext=Object.assign(Object.assign({},this.statContext),t)}authorize(t){return this.api.authorize(t)}pause(){this.logger.pause()}resume(){this.logger.resume()}logStarted(){this.log({operation:"play"})}logPlay(){this.log({operation:"play_toggle"})}logPause(t){this.log({operation:"pause",param:String(Math.round(t.position))})}logSeek(){this.log({operation:"seek"})}logFirstBytes(t){this.log({operation:"first_bytes",param:String(t.time)})}logFirstFrame(t){this.log({operation:"first_frame",param:String(t.time)})}logError(t){this.log({operation:"error",param:t.errorType})}logWatchCoverageRecord(t){this.log({operation:"watch_coverage_record",param:`${t.start}-${t.end}`})}logWatchCoverageLive(t){this.log({operation:"watch_coverage_live",param:`${t.start}-${t.end}`})}logAdSlotRequest(){this.log({operation:"adv",param:"slot_request"})}logAdStarted(t){this.log({operation:"adv",param:t})}logAdPaused(){this.log({operation:"adv",param:"pause"})}logAdResumed(){this.log({operation:"adv",param:"resume"})}logAdEnded(){this.log({operation:"adv",param:"ended"})}logAdSkipped(){this.log({operation:"adv",param:"skip"})}logAdClicked(){this.log({operation:"adv",param:"click"})}log(t){const e=this.crateLogItem(t);this.logger.log(e)}crateLogItem({operation:t,param:e}){const s=Date.now(),o={vid:this.statContext.movieId,quality:this.statContext.quality,dst:this.statContext.destination,ct:this.statContext.contentType,aid:this.statContext.albumId,place:this.statContext.place,cdn_host:this.statContext.cdnHostname,stat_type:!0===this.statContext.autoplay?h.AUTO:!1===this.statContext.autoplay?h.MANUAL:void 0,vk_playlist_id:this.statContext.vkPlaylistId,vk_app_id:this.statContext.vkAppId};return void 0!==e&&(o.param=e),{operation:t,timestamp:s,custom:o}}}export{i as ApiEnv,n as ContentType,g as OneStat,r as Quality,a as ScreenDestination};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vkontakte/videoplayer-statistics",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"author": "vk.com",
|
|
5
5
|
"description": "Statistics library for vk.com videoplayer",
|
|
6
6
|
"homepage": "https://vk.com",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"**/*.d.ts"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"
|
|
17
|
+
"@vkontakte/videoplayer-shared": "1.0.5",
|
|
18
|
+
"lodash": "4.17.21"
|
|
18
19
|
}
|
|
19
20
|
}
|
package/types.d.ts
CHANGED
|
@@ -3,3 +3,32 @@ export declare enum ApiEnv {
|
|
|
3
3
|
VIDEOTEST = "videotest",
|
|
4
4
|
TEST = "test"
|
|
5
5
|
}
|
|
6
|
+
export declare enum Quality {
|
|
7
|
+
Q144P = "144",
|
|
8
|
+
Q240P = "240",
|
|
9
|
+
Q360P = "360",
|
|
10
|
+
Q480P = "480",
|
|
11
|
+
Q720P = "720",
|
|
12
|
+
Q1080P = "1080",
|
|
13
|
+
Q1440P = "1440",
|
|
14
|
+
Q2160P = "2160",
|
|
15
|
+
Q4320P = "4320",
|
|
16
|
+
UNKNOWN = "unknown"
|
|
17
|
+
}
|
|
18
|
+
export declare enum ScreenDestination {
|
|
19
|
+
CHROMECAST = "chromecast",
|
|
20
|
+
APPLE_TV = "appletv",
|
|
21
|
+
PIP = "pip",
|
|
22
|
+
HEADPHONES = "externalHeadphones",
|
|
23
|
+
SCREEN = ""
|
|
24
|
+
}
|
|
25
|
+
export declare enum ContentType {
|
|
26
|
+
MP4 = "mp4",
|
|
27
|
+
DASH = "dash",
|
|
28
|
+
HLS = "hls",
|
|
29
|
+
WEBM = "webm"
|
|
30
|
+
}
|
|
31
|
+
export declare enum StatType {
|
|
32
|
+
AUTO = "auto",
|
|
33
|
+
MANUAL = ""
|
|
34
|
+
}
|