UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

104 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TRACK_GOAL = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../createNodeDescriptor"); /* Npm modules */ const uuid_1 = require("uuid"); /** * Node name: 'trackGoal' * * Purpose: * Track a goal with steps * @param goalReferenceId the reference id of the goal to track */ exports.TRACK_GOAL = (0, createNodeDescriptor_1.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: async ({ cognigy, config, projectId, organisationId, }) => { 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, 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 = (0, uuid_1.v4)(); 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 } = api.getMetadata(); for (const step of selectedSteps) { const payload = { data: { traceId: traceId, disableSensitiveLogging: false, }, analyticsdata: { 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 } }; await 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