UNPKG

@grafana/faro-core

Version:
91 lines 3.78 kB
import { TransportItemType } from '../../transports'; import { dateNow, genShortID, Observable, stringifyObjectValues } from '../../utils'; import { ItemBuffer } from '../ItemBuffer'; import {} from '../measurements/types'; import {} from '../types'; import { userActionEventName, UserActionImportance } from './const'; import { UserActionState } from './types'; export default class UserAction extends Observable { constructor({ name, parentId, trigger, transports, attributes, trackUserActionsExcludeItem, importance = UserActionImportance.Normal, pushEvent, }) { super(); this.name = name; this.attributes = attributes; this.id = genShortID(); this.trigger = trigger; this.parentId = parentId !== null && parentId !== void 0 ? parentId : this.id; this.trackUserActionsExcludeItem = trackUserActionsExcludeItem; this.importance = importance; this._pushEvent = pushEvent; this._itemBuffer = new ItemBuffer(); this._transports = transports; this._state = UserActionState.Started; this._start(); } addItem(item) { if (this._state === UserActionState.Started) { this._itemBuffer.addItem(item); return true; } return false; } _start() { this._state = UserActionState.Started; if (this._state === UserActionState.Started) { this.startTime = dateNow(); } } halt() { if (this._state !== UserActionState.Started) { return; } this._state = UserActionState.Halted; this.notify(this._state); } cancel() { if (this._state === UserActionState.Started) { // Empty the buffer this._itemBuffer.flushBuffer(); } this._state = UserActionState.Cancelled; this.notify(this._state); } end() { if (this._state === UserActionState.Cancelled) { return; } const endTime = dateNow(); const duration = endTime - this.startTime; this._state = UserActionState.Ended; this._itemBuffer.flushBuffer((item) => { if (isExcludeFromUserAction(item, this.trackUserActionsExcludeItem)) { this._transports.execute(item); return; } const userActionItem = Object.assign(Object.assign({}, item), { payload: Object.assign(Object.assign({}, item.payload), { action: { parentId: this.id, name: this.name, } }) }); this._transports.execute(userActionItem); }); this._state = UserActionState.Ended; this.notify(this._state); this._pushEvent(userActionEventName, Object.assign({ userActionName: this.name, userActionStartTime: this.startTime.toString(), userActionEndTime: endTime.toString(), userActionDuration: duration.toString(), userActionTrigger: this.trigger, userActionImportance: this.importance }, stringifyObjectValues(this.attributes)), undefined, { timestampOverwriteMs: this.startTime, customPayloadTransformer: (payload) => { payload.action = { id: this.id, name: this.name, }; return payload; }, }); } getState() { return this._state; } } function isExcludeFromUserAction(item, trackUserActionsExcludeItem) { return ((trackUserActionsExcludeItem === null || trackUserActionsExcludeItem === void 0 ? void 0 : trackUserActionsExcludeItem(item)) || (item.type === TransportItemType.MEASUREMENT && item.payload.type === 'web-vitals')); } //# sourceMappingURL=userAction.js.map