@benshi.ai/js-sdk
Version:
Benshi SDK
277 lines • 13.3 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var typings_ti_1 = __importDefault(require("../ECommerce/typings-ti"));
var oxygen_typings_ti_1 = __importDefault(require("../ECommerce/oxygen.typings-ti"));
var blood_typings_ti_1 = __importDefault(require("../ECommerce/blood.typings-ti"));
var medicalEquipment_typings_ti_1 = __importDefault(require("../ECommerce/medicalEquipment.typings-ti"));
var typings_ti_2 = __importDefault(require("./typings-ti"));
var typings_1 = require("./typings");
var typings_2 = require("./typings");
var injector_1 = require("../../core/injector");
var coreInstanceGetter_1 = require("../../coreInstanceGetter");
var typings_3 = require("../ECommerce/typings");
var utils_1 = require("../../utils");
var catalogRepository;
var moduleName = typings_1.ContentBlock.Core;
/**
* `title` may be overwritten by the developer
* This variable stores the known title, that is,
* the one provided by the BsAppStateMonitor or by developer
*/
var lastKnownTitle;
/**
* @internal
*
* @param properties
* @param sendNow
*/
var logAppEvent = function (properties, sendNow) {
if (sendNow === void 0) { sendNow = false; }
(0, injector_1.injectEvent)(properties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.App, moduleName, '', sendNow);
};
/**
* @internal
*
* @param userId
* @param properties
* @param sendNow
*/
var logIdentifyEvent = function (properties, sendNow) {
if (sendNow === void 0) { sendNow = false; }
(0, injector_1.injectEvent)(properties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.Identify, moduleName, '', sendNow);
};
/**
* Whenever the user interacts with a media element (Video, Audio, Image), log that
* action with this method
*
* @param {MediaProperties} properties
*
* @param {MediaData} mediaData Information related to media features to be injected
* into the media catalog
*
* @param sendNow
*/
var logMediaEvent = function (properties, mediaData, sendNow) {
if (sendNow === void 0) { sendNow = false; }
return __awaiter(void 0, void 0, void 0, function () {
var internalMediaProperties;
return __generator(this, function (_a) {
properties.time = properties.type === typings_1.ImageType.Image ? 0 : properties.time;
internalMediaProperties = __assign(__assign({}, properties), { id_source: properties.id, id: "".concat(properties.type, "_").concat(properties.id) });
(0, injector_1.injectEvent)(internalMediaProperties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.Media, properties.contentBlock || typings_1.ContentBlock.Core, '', sendNow);
if (mediaData !== undefined && Object.keys(mediaData).length !== 0) {
catalogRepository.injectMedia(properties.id, properties.type, mediaData);
}
return [2 /*return*/];
});
});
};
/**
* SDK tracks automatically page changes by inspecting the URL. However,
* depending on the app implementation, it may happen that the URL does not
* change when a new view is presented to the user. For these cases, use this method
*
* @param properties
* @param sendNow
*/
var logPageEvent = function (properties, sendNow) {
if (sendNow === void 0) { sendNow = false; }
if (properties.duration === 0) {
// avoid noise due internal URLs that changes so fast
return;
}
properties.title = lastKnownTitle ? lastKnownTitle : properties.title;
(0, injector_1.injectEvent)(properties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.Page, moduleName, '', sendNow);
lastKnownTitle = undefined;
};
/**
* Log generic action regarding navigation, like view an item or a list, that are not
* included in other specific modules like `eCommerce` or `eLearning`. It also allows
* to log navigation to external links
*
* @param properties
* @param sendNow
*/
var logTrackEvent = function (properties, sendNow) {
if (sendNow === void 0) { sendNow = false; }
return __awaiter(void 0, void 0, void 0, function () {
var internalProperties;
return __generator(this, function (_a) {
internalProperties = __assign(__assign({}, properties), { type: typings_1.TrackTypes.ReferenceGuide });
(0, injector_1.injectEvent)(internalProperties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.Track, moduleName, '', sendNow);
return [2 /*return*/];
});
});
};
/**
* @internal
*
* @param properties
* @param sendNow
*/
var logPushNotificationEvent = function (properties, sendNow) {
if (sendNow === void 0) { sendNow = false; }
(0, injector_1.injectEvent)(properties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.NudgeResponse, moduleName, '', sendNow);
};
var logRateEvent = function (properties, sendNow) {
if (sendNow === void 0) { sendNow = false; }
var RATE_MIN_VALUE = 0;
var RATE_MAX_VALUE = 5;
if (properties.rate_value < RATE_MIN_VALUE || properties.rate_value > RATE_MAX_VALUE) {
throw new Error('invalid range for value');
}
(0, injector_1.injectEvent)(properties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.Rate, moduleName, '', sendNow);
};
/**
* This function logs into the platform a new search performed by the user,
* including both, the parameters and the resulting ids
*
* @param {SearchProperties} properties
* @param {boolean} isNewSearch Whenever the user performs a new search,
* call the function with `true` in this parameter; however, when the user
* clicks on the links to go to next paginated page or load more items,
* related to the same search, just pass `false`
*
* @returns Search ID, which must be included in subsequences Item events
*/
var logSearchEvent = function (properties, searchId, sendNow) {
if (sendNow === void 0) { sendNow = false; }
var contentBlock = properties.contentBlock, restOfProperties = __rest(properties, ["contentBlock"]);
var ERROR_REPEATEAD_IDS = 'repeated-ids';
var formattedResultsIds = [];
if (!properties.results_ids) {
throw new Error('missing-results-ids');
}
if (properties.results_ids.length !== 0 && typeof properties.results_ids[0] === 'string') {
formattedResultsIds = properties.results_ids
.map(function (id) { return ({ id: id, type: typings_3.ItemType.Drug }); });
}
else {
formattedResultsIds = properties.results_ids;
}
if ((0, utils_1.hasRepeatedIds)(formattedResultsIds)) {
throw new Error(ERROR_REPEATEAD_IDS);
}
var internalProperties = __assign(__assign({}, restOfProperties), { results_list: formattedResultsIds, id: searchId });
delete internalProperties.results_ids;
(0, injector_1.injectEvent)(internalProperties, [typings_ti_2.default, typings_ti_1.default, blood_typings_ti_1.default, oxygen_typings_ti_1.default, medicalEquipment_typings_ti_1.default], typings_2.NavigationTypes.Search, properties.contentBlock || typings_1.ContentBlock.Core, '', sendNow);
};
/**
* SDK has some autonomy to trigger events automatically. For instance,
* it detects URL changes and notify Benshi.ai backend about that changes
* and the duration. However, it is not able to find out which block that URL
* belongs to. The goal of this method is to solve that. So, call it whenever
* the user goes to another section within the site. For example, if the user
* jumps from the e-commerce section to e-learning, call in this way:
*
* ```
* Navigation.setCurrentBlock(ContentBlock.Elearning)
* ```
*
* @param {ContentBlock} block the new section where the user has just jumped
*/
var setCurrentBlock = function (block) {
var core = (0, coreInstanceGetter_1.getCoreInstance)();
core.setCurrentBlock(block);
};
/**
* There are scenarious where the browser page title is not available,
* for instance when the web is embedded within a webview. Use this function
* whenever the new page is loaded to let the SDK know the title.
*
* Note that only is necessary when the standard `document.title` is not
* available for any reason
*
*
* @param title
*/
var setTitle = function (title) {
// the timeout is for waiting for the page-changed event, if exists,
// see BsSystem for more details
setTimeout(function () {
lastKnownTitle = title;
}, 15);
};
/**
* Private function to inject CurrencyRepository
*
* @ignore
*/
var init = function (injectedCatalogRepository) {
catalogRepository = injectedCatalogRepository;
};
exports.default = {
logAppEvent: logAppEvent,
logIdentifyEvent: logIdentifyEvent,
logMediaEvent: logMediaEvent,
logPageEvent: logPageEvent,
logPushNotificationEvent: logPushNotificationEvent,
logRateEvent: logRateEvent,
logSearchEvent: logSearchEvent,
logTrackEvent: logTrackEvent,
setCurrentBlock: setCurrentBlock,
setTitle: setTitle,
init: init
};
//# sourceMappingURL=index.js.map