|
@@ -1,9 +1,9 @@
|
|
|
-import { IBaseAction, ICustomAction, TrackActionOption, TrackEntityType } from '@medici/types';
|
|
|
+import { EventReturnType, EventReturnBooleanType, IBaseAction, ICustomAction, TrackActionOption, TrackEntityType } from '@medici/types';
|
|
|
import type { SDKInitConfig, PlatformType, SDKConfigType } from './sdk';
|
|
|
import type { IHistory } from './history';
|
|
|
import { collect } from './collector';
|
|
|
// import cloneDeep from 'lodash-es/cloneDeep';
|
|
|
-import { cloneDeep } from '@medici/utils';
|
|
|
+import { cloneDeep, getNetworkType } from '@medici/utils';
|
|
|
|
|
|
import { SDK } from './sdk';
|
|
|
|
|
@@ -42,7 +42,8 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
module: params.module,
|
|
|
type: params.type,
|
|
|
url: params.url || this._history.playload.url,
|
|
|
- eventType: params.eventType,
|
|
|
+ // eventType: params.eventType,
|
|
|
+ event: params.event,
|
|
|
referrer: params.referrer || this._history.currentRef,
|
|
|
requestData: params.requestData,
|
|
|
};
|
|
@@ -51,6 +52,8 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
}
|
|
|
if (params.networkType) {
|
|
|
entity['networkType'] = params.networkType;
|
|
|
+ } else {
|
|
|
+ entity['networkType'] = getNetworkType();
|
|
|
}
|
|
|
if (params.userId) {
|
|
|
entity['userId'] = params.userId;
|
|
@@ -58,21 +61,16 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
return entity;
|
|
|
}
|
|
|
|
|
|
- public trackView(url: string, referrer: string, uuid: string): Promise<XMLHttpRequestResponseType> {
|
|
|
+ public trackView(url: string, referrer: string, uuid: string): EventReturnType {
|
|
|
console.log('BaseTrack-trackView', url, referrer, uuid);
|
|
|
if (!this._stopTrack) {
|
|
|
this._history.playload.url = url;
|
|
|
const historyPlayload = cloneDeep(this._history.playload);
|
|
|
- // const payload = Object.assign(historyPlayload, {
|
|
|
- // ...this._config,
|
|
|
- // referrer: this._history.currentRef,
|
|
|
- // url: url,
|
|
|
- // });
|
|
|
const payload: TrackEntityType = {
|
|
|
module: 'pageview',
|
|
|
url: url,
|
|
|
type: 'pageview',
|
|
|
- eventType: 'pageview',
|
|
|
+ event: 'pageview',
|
|
|
language: historyPlayload.language,
|
|
|
userId: this._config.user,
|
|
|
};
|
|
@@ -81,7 +79,7 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public trackEvent(event_name: string, event_data: string, url?: string, uuid?: string): Promise<XMLHttpRequestResponseType> {
|
|
|
+ public trackEvent(event_name: string, event_data: string, url?: string, uuid?: string): EventReturnType {
|
|
|
if (!this._stopTrack) {
|
|
|
console.log('BaseTrack-TrackEvent', event_name, event_data, url, uuid);
|
|
|
this._history.playload.url = url;
|
|
@@ -98,7 +96,7 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
module: event_name,
|
|
|
url: url,
|
|
|
type: 'event',
|
|
|
- eventType: event_data,
|
|
|
+ event: event_data,
|
|
|
language: historyPlayload.language,
|
|
|
userId: this._config.user,
|
|
|
};
|
|
@@ -107,38 +105,68 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public track(trackActionName: string, trackActionOption?: TrackActionOption): Promise<XMLHttpRequestResponseType> {
|
|
|
+ trackBeacon(trackActionName: string, trackActionOption?: TrackActionOption): EventReturnBooleanType {
|
|
|
if (!this._stopTrack) {
|
|
|
const historyPlayload = cloneDeep(this._history.playload);
|
|
|
const trackObj = cloneDeep(trackActionOption);
|
|
|
- delete trackObj.eventType;
|
|
|
+ delete trackObj.event;
|
|
|
+ delete trackObj.maxWaitTime;
|
|
|
+
|
|
|
+ const payload: TrackEntityType = {
|
|
|
+ module: trackActionName,
|
|
|
+ type: 'track',
|
|
|
+ event: trackActionOption.event,
|
|
|
+ requestData: Object.assign({ screen: historyPlayload.screen }, trackObj, this._config),
|
|
|
+ language: historyPlayload.language,
|
|
|
+ networkType: trackActionOption.networkType,
|
|
|
+ userId: this._config.user,
|
|
|
+ };
|
|
|
+ // this.toTrackEntity(payload);
|
|
|
+ if ('sendBeacon' in navigator) {
|
|
|
+ const blob = new Blob([JSON.stringify(payload)], {
|
|
|
+ type: 'application/json; charset=UTF-8',
|
|
|
+ });
|
|
|
+
|
|
|
+ const isSend = navigator.sendBeacon(this.trackUrl, blob);
|
|
|
+ return Promise.resolve(isSend);
|
|
|
+ }
|
|
|
+ return Promise.resolve(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public track(trackActionName: string, trackActionOption?: TrackActionOption): EventReturnType {
|
|
|
+ if (!this._stopTrack) {
|
|
|
+ const historyPlayload = cloneDeep(this._history.playload);
|
|
|
+ const trackObj = cloneDeep(trackActionOption);
|
|
|
+ delete trackObj.event;
|
|
|
delete trackObj.maxWaitTime;
|
|
|
|
|
|
const payload: TrackEntityType = {
|
|
|
module: trackActionName,
|
|
|
type: 'track',
|
|
|
- eventType: trackActionOption.eventType,
|
|
|
+ event: trackActionOption.event,
|
|
|
requestData: Object.assign({ screen: historyPlayload.screen }, trackObj, this._config),
|
|
|
language: historyPlayload.language,
|
|
|
networkType: trackActionOption.networkType,
|
|
|
userId: this._config.user,
|
|
|
};
|
|
|
+ console.log('track-payload', payload);
|
|
|
// this.toTrackEntity(payload);
|
|
|
return collect(this.trackUrl, 'track', this.toTrackEntity(payload));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public startTrack(trackActionName: string, trackActionOption?: TrackActionOption): Promise<XMLHttpRequestResponseType> {
|
|
|
+ public startTrack(trackActionName: string, trackActionOption?: TrackActionOption): EventReturnType {
|
|
|
if (!this._stopTrack) {
|
|
|
const historyPlayload = cloneDeep(this._history.playload);
|
|
|
const trackObj = cloneDeep(trackActionOption);
|
|
|
- delete trackObj.eventType;
|
|
|
+ delete trackObj.event;
|
|
|
delete trackObj.maxWaitTime;
|
|
|
|
|
|
const payload: TrackEntityType = {
|
|
|
module: trackActionName,
|
|
|
type: 'start_track',
|
|
|
- eventType: trackActionOption.eventType,
|
|
|
+ event: trackActionOption.event,
|
|
|
requestData: Object.assign({}, trackObj, this._config),
|
|
|
language: historyPlayload.language,
|
|
|
networkType: trackActionOption.networkType,
|
|
@@ -153,7 +181,7 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public endTrack(trackActionName: string, trackActionOption?: TrackActionOption): Promise<XMLHttpRequestResponseType> {
|
|
|
+ public endTrack(trackActionName: string, trackActionOption?: TrackActionOption): EventReturnType {
|
|
|
this._timeOut && clearTimeout(this._timeOut);
|
|
|
if (!this._stopTrack) {
|
|
|
const historyPlayload = cloneDeep(this._history.playload);
|
|
@@ -164,7 +192,7 @@ export class BaseTrack implements IBaseAction, ICustomAction {
|
|
|
const payload: TrackEntityType = {
|
|
|
module: trackActionName,
|
|
|
type: 'end_track',
|
|
|
- eventType: trackActionOption.eventType,
|
|
|
+ event: trackActionOption.event,
|
|
|
requestData: Object.assign({}, trackObj, this._config),
|
|
|
language: historyPlayload.language,
|
|
|
networkType: trackActionOption.networkType,
|