UNPKG

@spartacus/tracking

Version:

Spartacus tracking and personalization library

103 lines (94 loc) 6.95 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('rxjs/operators'), require('@spartacus/tracking/personalization/root'), require('@spartacus/core')) : typeof define === 'function' && define.amd ? define('@spartacus/tracking/personalization/core', ['exports', '@angular/core', 'rxjs', 'rxjs/operators', '@spartacus/tracking/personalization/root', '@spartacus/core'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.spartacus = global.spartacus || {}, global.spartacus.tracking = global.spartacus.tracking || {}, global.spartacus.tracking.personalization = global.spartacus.tracking.personalization || {}, global.spartacus.tracking.personalization.core = {}), global.ng.core, global.rxjs, global.rxjs.operators, global.spartacus.tracking.personalization.root, global.core)); }(this, (function (exports, i0, rxjs, operators, i1, i2) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n['default'] = e; return Object.freeze(n); } var i0__namespace = /*#__PURE__*/_interopNamespace(i0); var i1__namespace = /*#__PURE__*/_interopNamespace(i1); var i2__namespace = /*#__PURE__*/_interopNamespace(i2); var PersonalizationCoreModule = /** @class */ (function () { function PersonalizationCoreModule() { } return PersonalizationCoreModule; }()); PersonalizationCoreModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationCoreModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule }); PersonalizationCoreModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationCoreModule }); PersonalizationCoreModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationCoreModule }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationCoreModule, decorators: [{ type: i0.NgModule, args: [{}] }] }); var PersonalizationContextService = /** @class */ (function () { function PersonalizationContextService(config, cmsService) { this.config = config; this.cmsService = cmsService; } PersonalizationContextService.prototype.getPersonalizationContext = function () { var _this = this; var _a; if (!((_a = this.config.personalization) === null || _a === void 0 ? void 0 : _a.context)) { if (i0.isDevMode()) { console.warn("There is no context configured in Personalization."); } return rxjs.EMPTY; } else { var context_1 = this.config.personalization.context; return this.cmsService.getCurrentPage().pipe(operators.filter(Boolean), operators.map(function (page) { var _a; return (_a = page.slots) === null || _a === void 0 ? void 0 : _a[context_1.slotPosition]; }), operators.filter(Boolean), operators.map(function (slot) { var _a, _b, _c; var scriptComponent = (_a = slot.components) === null || _a === void 0 ? void 0 : _a.find(function (i) { return i.uid === context_1.componentId; }); return _this.buildPersonalizationContext((_c = (_b = scriptComponent === null || scriptComponent === void 0 ? void 0 : scriptComponent.properties) === null || _b === void 0 ? void 0 : _b.script) === null || _c === void 0 ? void 0 : _c.data); })); } }; PersonalizationContextService.prototype.buildPersonalizationContext = function (data) { if (data) { var context = JSON.parse(atob(data)); context.actions.forEach(function (action) { Object.keys(action).forEach(function (key) { action[key] = atob(action[key]); }); }); for (var i = 0; i < context.segments.length; i++) { context.segments[i] = atob(context.segments[i]); } return context; } }; return PersonalizationContextService; }()); PersonalizationContextService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationContextService, deps: [{ token: i1__namespace.PersonalizationConfig }, { token: i2__namespace.CmsService }], target: i0__namespace.ɵɵFactoryTarget.Injectable }); PersonalizationContextService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationContextService, providedIn: 'root' }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0__namespace, type: PersonalizationContextService, decorators: [{ type: i0.Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: function () { return [{ type: i1__namespace.PersonalizationConfig }, { type: i2__namespace.CmsService }]; } }); /** * Generated bundle index. Do not edit. */ exports.PersonalizationContextService = PersonalizationContextService; exports.PersonalizationCoreModule = PersonalizationCoreModule; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=spartacus-tracking-personalization-core.umd.js.map