@botonic/react
Version:
Build Chatbots using React
66 lines • 3 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { useContext } from 'react';
import { v7 as uuidv7 } from 'uuid';
import { WebchatContext } from './context';
export var EventAction;
(function (EventAction) {
EventAction["FeedbackKnowledgebase"] = "feedback_knowledgebase";
})(EventAction || (EventAction = {}));
var FeedbackOption;
(function (FeedbackOption) {
FeedbackOption["ThumbsUp"] = "thumbsUp";
FeedbackOption["ThumbsDown"] = "thumbsDown";
})(FeedbackOption || (FeedbackOption = {}));
export function useTracking() {
const { webchatState, trackEvent } = useContext(WebchatContext);
const getRequest = () => {
const request = {
session: Object.assign({}, webchatState.session),
getUserCountry: () => { var _a; return ((_a = webchatState.session.user) === null || _a === void 0 ? void 0 : _a.country) || ''; },
getUserLocale: () => { var _a; return ((_a = webchatState.session.user) === null || _a === void 0 ? void 0 : _a.locale) || ''; },
getSystemLocale: () => {
var _a;
return ((_a = webchatState.session.user) === null || _a === void 0 ? void 0 : _a.system_locale) || '';
},
};
return request;
};
const trackKnowledgebaseFeedback = ({ messageId, isUseful, botInteractionId, inferenceId, }) => __awaiter(this, void 0, void 0, function* () {
if (!trackEvent) {
return;
}
const request = getRequest();
// inferenceId and botInteractionId are strings, but in local development they are undefined
const event = {
action: EventAction.FeedbackKnowledgebase,
knowledgebaseInferenceId: inferenceId,
feedbackBotInteractionId: botInteractionId,
feedbackTargetId: messageId,
feedbackGroupId: uuidv7(),
possibleOptions: [FeedbackOption.ThumbsDown, FeedbackOption.ThumbsUp],
possibleValues: [0, 1],
option: isUseful ? FeedbackOption.ThumbsUp : FeedbackOption.ThumbsDown,
value: isUseful ? 1 : 0,
};
const { action } = event, eventArgs = __rest(event, ["action"]);
yield trackEvent(request, action, eventArgs);
});
const trackCustomEvent = (event) => __awaiter(this, void 0, void 0, function* () {
if (!trackEvent) {
return;
}
const request = getRequest();
const { action } = event, eventArgs = __rest(event, ["action"]);
yield trackEvent(request, action, eventArgs);
});
const trackFeedbackEvent = (event) => __awaiter(this, void 0, void 0, function* () {
if (!trackEvent) {
return;
}
const request = getRequest();
const { action } = event, eventArgs = __rest(event, ["action"]);
yield trackEvent(request, action, eventArgs);
});
return { trackKnowledgebaseFeedback, trackCustomEvent, trackFeedbackEvent };
}
//# sourceMappingURL=tracking.js.map