UNPKG

@botonic/plugin-hubtype-analytics

Version:

Plugin for tracking in the Hubtype backend to see the results in the Hubtype Dashbord

71 lines 2.66 kB
import { __awaiter } from "tslib"; import axios from 'axios'; import { EventType } from './types'; import { createHtEvent } from './utils'; export default class BotonicPluginHubtypeAnalytics { constructor() { const hubtypeUrl = process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'; this.eventsBaseUrl = `${hubtypeUrl}/external/v2/conversational_apps`; } post() { return; } trackEvent(request, htEventProps) { return __awaiter(this, void 0, void 0, function* () { const requestData = this.getRequestData(request); const event = createHtEvent(requestData, htEventProps); return this.sendEvent(request, event); }); } getRequestData(request) { var _a; return { userId: this.isLambdaEvent(request) ? request.session.user.id : undefined, botInteractionId: (_a = request.input) === null || _a === void 0 ? void 0 : _a.bot_interaction_id, userLocale: request.getSystemLocale(), userCountry: request.getUserCountry(), systemLocale: request.getSystemLocale(), }; } isLambdaEvent(request) { var _a, _b; return ((_b = (_a = request.session) === null || _a === void 0 ? void 0 : _a.bot) === null || _b === void 0 ? void 0 : _b.id) !== undefined; } sendEvent(request, event) { if (event.type === EventType.BotEvent) { return this.sendBotEvent(request, event); } return this.sendWebEvent(request, event); } sendBotEvent(request, event) { const botId = request.session.bot.id; const url = `${this.eventsBaseUrl}/${botId}/bot_event/`; const config = { headers: { Authorization: `Bearer ${request.session._access_token}` }, }; return axios.post(url, event, config); } sendWebEvent(request, event) { if (this.isLambdaEvent(request)) { return this.sendWebEventByBotId(request, event); } return this.sendWebEventByProviderId(request, event); } sendWebEventByBotId(request, event) { const botId = request.session.bot.id; const url = `${this.eventsBaseUrl}/${botId}/web_event/`; return axios.post(url, event); } sendWebEventByProviderId(request, event) { const url = `${this.eventsBaseUrl}/web_event/`; const config = { params: { provider_id: request.session.user.id, }, }; return axios.post(url, event, config); } } export * from './types'; export * from './utils'; //# sourceMappingURL=index.js.map