@benshi.ai/js-sdk
Version:
Benshi SDK
165 lines • 8.09 kB
JavaScript
"use strict";
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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var logging_1 = require("../logging");
var typings_1 = require("../modules/Navigation/typings");
var typings_2 = require("./impressions/typings");
var utils_1 = require("../utils");
var BsCore = /** @class */ (function () {
function BsCore(sender, impressionDetector, debugging, defaultBlock, deviceId) {
this.searchId = '';
this.impressionsDetectors = {};
this.user_id = '';
this.sender = sender;
this.impressionDetectorClass = impressionDetector;
this.deviceId = deviceId;
this.defaultBlock = defaultBlock;
this.debug = (0, logging_1.debug)(debugging);
}
BsCore.createInstance = function (sender, impressionDetectorClass, debugging, defaultBlock, deviceId) {
if (!!BsCore.instance) {
return BsCore.instance;
}
BsCore.instance = new BsCore(sender, impressionDetectorClass, debugging, defaultBlock, deviceId);
return BsCore.instance;
};
BsCore.getInstance = function () {
if (!BsCore.instance) {
throw new Error('bslog-instance-not-created');
}
return BsCore.instance;
};
BsCore.prototype.login = function (userId) {
this.user_id = userId;
};
BsCore.prototype.logout = function (userId) {
this.user_id = userId;
};
BsCore.prototype.resolveModule = function (module) {
// logPageEvents are triggered automatically by the system. We do not
// know whether the web application calls setCurrentBlock before or after that:
var delta = 50;
if (module === typings_1.ContentBlock.Core) {
// in this case the event could be triggered by several modules
// so the SDK must decide which one must be included in the event
// sent to server (i.e.: page or media events)
if (this.currentBlock) {
return this.currentBlock;
}
else {
return this.defaultBlock;
}
}
else {
// specific module (i.e.: triggered by the ELearning module)
return module;
}
};
BsCore.prototype.resolveUserId = function (overWrittedUserId, user_id, device_id) {
if (overWrittedUserId && overWrittedUserId.length !== 0) {
return overWrittedUserId;
}
return user_id || device_id;
};
BsCore.prototype.trackEvent = function (eventTeaser, moduleName, overWrittedUserId, sendNow) {
return __awaiter(this, void 0, void 0, function () {
var event;
return __generator(this, function (_a) {
event = __assign(__assign({}, eventTeaser), { block: this.resolveModule(moduleName), ol: true, ts: (0, utils_1.toISOLocal)(new Date()) });
this.sender.add(this.resolveUserId(overWrittedUserId, this.user_id, this.deviceId), this.deviceId, event, sendNow);
return [2 /*return*/];
});
});
};
BsCore.prototype.startTrackingImpressions = function (impressionHandler, containerClassname, itemClassname, searchId) {
this.impressionsDetectors[containerClassname] = new this.impressionDetectorClass({ intersectionThreshold: 0.5 });
this.impressionsDetectors[containerClassname].on(typings_2.ImpressionEventType.Impression, impressionHandler);
this.impressionsDetectors[containerClassname].start(containerClassname, itemClassname, { search_id: searchId });
};
BsCore.prototype.stopTrackingImpressions = function (containerClassname) {
if (!containerClassname) {
Object.values(this.impressionsDetectors).forEach(function (detector) { return detector.stop(); });
return;
}
if (!this.impressionsDetectors[containerClassname]) {
return;
}
this.impressionsDetectors[containerClassname].stop();
};
BsCore.prototype.restartTrackingImpressions = function (containerClassname, searchId) {
if (!containerClassname || !this.impressionsDetectors[containerClassname]) {
console.error('Impossible to restart impression to unknown containerClassname');
throw new Error('unknown-container');
}
this.impressionsDetectors[containerClassname].restart({ search_id: searchId });
};
BsCore.prototype.whoami = function () {
return this.user_id;
};
BsCore.prototype.generateSearchId = function (isNewSearch) {
if (isNewSearch) {
this.searchId = "".concat(this.deviceId).concat(this.user_id).concat(Date.now());
}
return this.searchId;
};
BsCore.prototype.setCurrentBlock = function (block) {
var _this = this;
// the reason of this delay is that the page event is triggered
// 10 miliseconds after it really happens to wait for the title
setTimeout(function () {
_this.currentBlock = block;
}, 15);
};
BsCore.prototype.setTitle = function (title) {
// do nothing yet
};
return BsCore;
}());
exports.default = BsCore;
//# sourceMappingURL=BsCore.js.map