@cognigy/rest-api-client
Version:
Cognigy REST-Client
109 lines • 4.35 kB
JavaScript
import { __awaiter } from "tslib";
/* Custom modules */
import { createNodeDescriptor } from "../../createNodeDescriptor";
/* Npm modules */
import * as crypto from "crypto";
/**
* Node name: 'trackGoal'
*
* Purpose:
* Track a goal with steps
* @param goalReferenceId the reference id of the goal to track
*/
export const TRACK_GOAL = createNodeDescriptor({
type: "trackGoal",
defaultLabel: "Track Goal",
summary: 'UI__NODE_EDITOR__TRACK_GOAL__SUMMARY',
appearance: {
showIcon: false
},
fields: [
{
key: "goal",
type: "goalAndStepsSelect",
label: "UI__NODE_EDITOR__GOALS__CHOOSE_GOAL__LABEL",
defaultValue: {
goalReferenceId: "",
stepIds: []
},
params: {
required: true
}
},
],
preview: {
key: "goal",
type: "text"
},
tags: ["analytics"],
function: ({ cognigy, config, projectId, organisationId, }) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const { api } = cognigy;
if (!(config === null || config === void 0 ? void 0 : config.goal)) {
api.log("error", `Invalid config for 'trackGoal' Node`);
api.logDebugError(`Invalid config for 'trackGoal' Node`);
return;
}
const sessionState = api.getSessionStateCopy();
const { selectedSteps, referenceId, version, name, description, goalId, } = config.goal;
const activeCycleIds = ((_a = sessionState.analytics) === null || _a === void 0 ? void 0 : _a.goalCycleIds) || {};
let cycleId = activeCycleIds[goalId];
const hasStartStep = selectedSteps.some(step => step.type === "start");
if (!cycleId && !hasStartStep) {
api.log("warn", `No start step found for goal ${name} (${goalId}). Ignoring goal tracking.`);
api.logDebugError(`No start step found for goal <b>${name}</b>.<br>Ignoring goal tracking.`);
return;
}
// Create a new cycle id if there is a start step
if (hasStartStep) {
cycleId = crypto.randomUUID();
activeCycleIds[goalId] = cycleId;
const sessionStateAnalyticsUpdate = Object.assign(Object.assign({}, sessionState.analytics), { goalCycleIds: activeCycleIds });
api.setSessionState("analytics", sessionStateAnalyticsUpdate);
}
const { sessionId, endpointUrlToken, endpointName, channel, endpointType, localeName, localeReferenceId, traceId, snapshotId, snapshotName, contactId, cxOneData } = api.getMetadata();
for (const step of selectedSteps) {
const goalDefinition = {
goalId,
name,
description,
referenceId,
version,
steps: [{
_id: step.stepId,
name: step.name,
description: step.description,
order: step.order,
type: step.type,
metrics: step.metrics,
}],
};
const payload = {
data: {
traceId: traceId,
disableSensitiveLogging: false,
},
analyticsdata: Object.assign(Object.assign({ projectId,
organisationId,
sessionId,
version,
referenceId, timestamp: new Date(), goalCycleId: cycleId, goalId, stepId: step.stepId, stepType: step.type, endpointUrlToken,
endpointName,
endpointType,
channel,
localeName,
localeReferenceId,
snapshotId,
snapshotName,
contactId }, (cxOneData ? { cxOneData } : {})), { goalDefinition }),
};
yield api.sendTrackGoal(payload);
let msg = `<b>Goal Name:</b> ${name}<br><b>Step Name:</b> ${step.name}<br>`;
if (step.type) {
msg = msg + `<b>Step Type:</b> ${step.type}<br>`;
}
api.logDebugMessage(msg);
}
})
});
//# sourceMappingURL=trackGoal.js.map