import { IBaseAction, ICustomAction, TrackActionOption } from '@medici/types'; import type { SDKInitConfig, PlatformType, SDKConfigType } from './sdk'; import type { IHistory } from './history'; import { collect } from './collector'; import { cloneDeep } from 'lodash-es'; import { SDK } from './sdk'; export class BaseTrack implements IBaseAction, ICustomAction { protected _platform: PlatformType; protected _config: SDKInitConfig; protected _history: IHistory; protected _endPoint: string; protected _trackEndPoint: string; protected _appId: string; private _timeOut: ReturnType; constructor(params: SDKConfigType) { this._config = params.config; this._platform = params.platform; this._appId = params.appId; this._endPoint = params.endPoint; this._trackEndPoint = params.trackEndPoint; } get _stopTrack() { return SDK._stopTrack; } public trackView(url: string, referrer: string, uuid: string): Promise { 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, }); console.log('send-playload', payload); return collect(this._endPoint, 'pageview', payload); } } public trackEvent(event_name: string, event_data: string, url?: string, uuid?: string): Promise { if (!this._stopTrack) { console.log('BaseTrack-TrackEvent', event_name, event_data, url, uuid); this._history.playload.url = url; const historyPlayload = cloneDeep(this._history.playload); const payload = Object.assign(historyPlayload, { referrer: this._history.currentRef, event_name: event_name, url: url, event_data: event_data, ...this._config, }); console.log('send-playload', payload); return collect(this._endPoint, 'event', payload); } } public sendEvent(value: string, type: string, url?: string, uuid?: string): Promise { if (!this._stopTrack) { console.log('BaseTrack-sendEvent', value, type, url, uuid); const payload = Object.assign(this._history.playload, { referrer: this._history.currentRef, url: url, event_name: type, event_data: value, ...this._config, }); return collect(this._endPoint, 'event', payload); } } public track(trackActionName: string, trackActionOption?: TrackActionOption): Promise { if (!this._stopTrack) { const historyPlayload = cloneDeep(this._history.playload); const payload = Object.assign(historyPlayload, { referrer: this._history.currentRef, ...trackActionOption, trackActionName, ...this._config, }); return collect(this._trackEndPoint, 'track', payload); } } public startTrack(trackActionName: string, trackActionOption?: TrackActionOption): Promise { if (!this._stopTrack) { const historyPlayload = cloneDeep(this._history.playload); const payload = Object.assign(historyPlayload, { referrer: this._history.currentRef, ...trackActionOption, trackActionName, ...this._config, }); if (trackActionOption.maxWaitTime) { this._timeOut = setTimeout(() => { this.endTrack(trackActionName, trackActionOption); }, trackActionOption.maxWaitTime); } return collect(this._trackEndPoint, 'startTrack', payload); } } public endTrack(trackActionName: string, trackActionOption?: TrackActionOption): Promise { clearTimeout(this._timeOut); if (!this._stopTrack) { const historyPlayload = cloneDeep(this._history.playload); const payload = Object.assign(historyPlayload, { referrer: this._history.currentRef, ...trackActionOption, trackActionName, ...this._config, }); return collect(this._trackEndPoint, 'endTrack', payload); } } }