@benshi.ai/js-sdk
Version:
Benshi SDK
112 lines • 5.01 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var events_1 = require("events");
var ViewableImpressionObserver_1 = require("./ViewableImpressionObserver");
var typings_1 = require("./typings");
var ViewableImpressionEngine = /** @class */ (function (_super) {
__extends(ViewableImpressionEngine, _super);
// at least one second in the screen
function ViewableImpressionEngine(impressionObserver, options) {
var _this = _super.call(this) || this;
_this.impressionObserver = null;
_this.appData = {};
_this.alreadyVisibleImpressed = new Set();
_this.pendingImpressions = {};
_this.options = options;
_this.impressionObserver = impressionObserver;
_this.impressionObserver.on(ViewableImpressionObserver_1.ImpressionObservedTypes.View, function (id, eventData) {
_this.add(id, eventData);
});
_this.impressionObserver.on(ViewableImpressionObserver_1.ImpressionObservedTypes.Hide, function (id) {
_this.remove(id);
});
setInterval(function () { return _this.triggerPendingImpressions(); }, _this.options.triggerInterval);
return _this;
}
ViewableImpressionEngine.prototype.triggerPendingImpressions = function () {
var _this = this;
var currentTimestamp = Date.now();
var idsToTrigger = [];
for (var _i = 0, _a = Object.entries(this.pendingImpressions); _i < _a.length; _i++) {
var _b = _a[_i], id = _b[0], time_in = _b[1].time_in;
// a potential optimization of this would be to sort the
// list by date and stop checking when one does not
// satisfy the time constraint
if (currentTimestamp - time_in > this.options.keepVisibleTimeout) {
idsToTrigger.push(id);
}
}
idsToTrigger.forEach(function (id) {
_this.alreadyVisibleImpressed.add(id);
_this.emit(typings_1.ImpressionEventType.Impression, {
dataset: _this.pendingImpressions[id].data,
appData: _this.appData
});
delete _this.pendingImpressions[id];
});
};
ViewableImpressionEngine.prototype.start = function (containerClassname, itemClassname, appData) {
this.appData = appData;
this.containerClassname = containerClassname;
this.itemClassname = itemClassname;
this.impressionObserver.start(containerClassname, itemClassname);
};
ViewableImpressionEngine.prototype.stop = function () {
this.triggerPendingImpressions();
this.impressionObserver.stop();
this.alreadyVisibleImpressed.clear();
this.pendingImpressions = {};
};
ViewableImpressionEngine.prototype.restart = function (appData) {
this.triggerPendingImpressions();
this.appData = appData;
this.alreadyVisibleImpressed.clear();
this.pendingImpressions = {};
this.impressionObserver.stop();
this.impressionObserver.start(this.containerClassname, this.itemClassname);
};
ViewableImpressionEngine.prototype.add = function (id, data) {
if (this.alreadyVisibleImpressed.has(id)) {
// the item has been already tracked
return;
}
if (!this.pendingImpressions[id]) {
this.pendingImpressions[id] = {
time_in: Date.now(),
appData: this.appData,
data: data
};
}
};
ViewableImpressionEngine.prototype.remove = function (id) {
if (!this.pendingImpressions[id]) {
return;
}
// this check is needed in case the interval is still sleeping
if (Date.now() - this.pendingImpressions[id].time_in > this.options.keepVisibleTimeout) {
this.emit(typings_1.ImpressionEventType.Impression, {
dataset: this.pendingImpressions[id].data,
appData: this.appData
});
}
delete this.pendingImpressions[id];
};
return ViewableImpressionEngine;
}(events_1.EventEmitter));
exports.default = ViewableImpressionEngine;
//# sourceMappingURL=ViewableImpressionEngine.js.map