UNPKG

@robotical/appv2-analytics-gatherer

Version:

A tool that gathers data from the Apps and sends it to the Analytics server

133 lines (132 loc) 5.46 kB
import Logger from '../services/Logger'; import HeartbeatsHub from '../services/HeartbeatsHub'; import { startSession } from '../core/sessions'; import { trackConsentRatio } from '../core/consent'; import { HeartbeatTypeEnum } from '../types/types'; import { captureScreenVisit } from '../core/screenVisits'; import { captureActivityVisit } from '../core/activityVisits'; const SHOW_LOGS = true; const TAG = 'AnalyticsFacade'; class AnalyticsFacade { static setRobotId(robotId) { Logger.info(SHOW_LOGS, TAG, `Setting robot id to: ${robotId}`); this.robotId = robotId; } static setRaftType(raftType) { Logger.info(SHOW_LOGS, TAG, `Setting raft type to: ${raftType}`); this.raftTypeE = raftType; } static async setConsent(consent) { Logger.info(SHOW_LOGS, TAG, `Setting consent to: ${consent}`); this.consent = consent; const wasConsentTracked = await trackConsentRatio(consent); if (!wasConsentTracked) { Logger.error(SHOW_LOGS, TAG, 'Failed to track consent'); } } static setConsentWithoutTracking(consent) { Logger.info(SHOW_LOGS, TAG, `Setting consent to: ${consent}`); this.consent = consent; } static async startSession(sessionId, deviceId, robotId, raftType) { if (!this.consent) { Logger.info(SHOW_LOGS, TAG, 'Cannot start session without consent'); return false; } Logger.info(SHOW_LOGS, TAG, `Starting session with id: ${sessionId}`); this.sessionId = sessionId; this.deviceId = deviceId; this.robotId = robotId; this.raftTypeE = raftType; const wasSessionStarted = await startSession(sessionId, deviceId, robotId, raftType); if (!wasSessionStarted) { Logger.error(SHOW_LOGS, TAG, `Failed to start session with id: ${sessionId}`); return false; } await HeartbeatsHub.addHeartbeat({ interval: 60000, type: HeartbeatTypeEnum.SESSION, sessionId, }); return true; } static async logScreenVisit(screenName) { Logger.info(SHOW_LOGS, TAG, `Viewing screen: ${screenName}`); if (!this._areNecessaryFieldsSet()) { return null; } HeartbeatsHub.stopAllHeartbeatsOfType(HeartbeatTypeEnum.SCREEN); const heartbeatId = await HeartbeatsHub.addHeartbeat({ interval: 60000, type: HeartbeatTypeEnum.SCREEN, screen: screenName, sessionId: this.sessionId, }); if (!heartbeatId) { Logger.error(SHOW_LOGS, TAG, `Failed to add heartbeat for screen view: ${screenName}`); return null; } Logger.info(SHOW_LOGS, TAG, `Heartbeat added for screen view: ${screenName}`); const wasScreenVisitCaptured = await captureScreenVisit(screenName, this.sessionId, this.deviceId, this.raftTypeE, this.robotId); if (!wasScreenVisitCaptured) { Logger.error(SHOW_LOGS, TAG, `Failed to capture screen visit: ${screenName}`); HeartbeatsHub.stopHeartbeat(heartbeatId); return null; } return () => HeartbeatsHub.stopHeartbeat(heartbeatId); } static async logActivityVisit(activityName) { Logger.info(SHOW_LOGS, TAG, `Visiting activity: ${activityName}`); if (!this._areNecessaryFieldsSet()) { return null; } HeartbeatsHub.stopAllHeartbeatsOfType(HeartbeatTypeEnum.ACTIVITY); const heartbeatId = await HeartbeatsHub.addHeartbeat({ interval: 60000, type: HeartbeatTypeEnum.ACTIVITY, sessionId: this.sessionId, activity: activityName, }); if (!heartbeatId) { Logger.error(SHOW_LOGS, TAG, `Failed to add heartbeat for activity visit: ${activityName}`); return null; } Logger.info(SHOW_LOGS, TAG, `Heartbeat added for activity visit: ${activityName}`); const wasActivityVisitCaptured = await captureActivityVisit(activityName, this.sessionId, this.deviceId, this.raftTypeE, this.robotId); if (!wasActivityVisitCaptured) { Logger.error(SHOW_LOGS, TAG, `Failed to capture activity visit: ${activityName}`); HeartbeatsHub.stopHeartbeat(heartbeatId); return null; } return () => HeartbeatsHub.stopHeartbeat(heartbeatId); } static async stopAllActivityHeartbeats() { Logger.info(SHOW_LOGS, TAG, 'Stopping all activity heartbeats'); HeartbeatsHub.stopAllHeartbeatsOfType(HeartbeatTypeEnum.ACTIVITY); } ; static _areNecessaryFieldsSet() { if (!this.consent) { Logger.warn(SHOW_LOGS, TAG, 'Consent not given. Cannot log events without consent'); return false; } if (!this.sessionId) { Logger.error(SHOW_LOGS, TAG, 'Session id is not set'); return false; } if (!this.deviceId) { Logger.error(SHOW_LOGS, TAG, 'Device id is not set'); return false; } if (!this.robotId) { Logger.error(SHOW_LOGS, TAG, 'Robot id is not set'); return false; } if (!this.raftTypeE) { Logger.error(SHOW_LOGS, TAG, 'Raft type is not set'); return false; } return true; } } export default AnalyticsFacade;