UNPKG

@spartacus/tracking

Version:

Spartacus tracking and personalization library

70 lines (65 loc) 3.77 kB
import * as i0 from '@angular/core'; import { NgModule, isDevMode, Injectable } from '@angular/core'; import { EMPTY } from 'rxjs'; import { filter, map } from 'rxjs/operators'; import * as i1 from '@spartacus/tracking/personalization/root'; import * as i2 from '@spartacus/core'; class PersonalizationCoreModule { } PersonalizationCoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationCoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); PersonalizationCoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationCoreModule }); PersonalizationCoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationCoreModule }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationCoreModule, decorators: [{ type: NgModule, args: [{}] }] }); class PersonalizationContextService { constructor(config, cmsService) { this.config = config; this.cmsService = cmsService; } getPersonalizationContext() { var _a; if (!((_a = this.config.personalization) === null || _a === void 0 ? void 0 : _a.context)) { if (isDevMode()) { console.warn(`There is no context configured in Personalization.`); } return EMPTY; } else { const context = this.config.personalization.context; return this.cmsService.getCurrentPage().pipe(filter(Boolean), map((page) => { var _a; return (_a = page.slots) === null || _a === void 0 ? void 0 : _a[context.slotPosition]; }), filter(Boolean), map((slot) => { var _a, _b, _c; const scriptComponent = (_a = slot.components) === null || _a === void 0 ? void 0 : _a.find((i) => i.uid === context.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); })); } } buildPersonalizationContext(data) { if (data) { const context = JSON.parse(atob(data)); context.actions.forEach((action) => { Object.keys(action).forEach((key) => { action[key] = atob(action[key]); }); }); for (let i = 0; i < context.segments.length; i++) { context.segments[i] = atob(context.segments[i]); } return context; } } } PersonalizationContextService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationContextService, deps: [{ token: i1.PersonalizationConfig }, { token: i2.CmsService }], target: i0.ɵɵFactoryTarget.Injectable }); PersonalizationContextService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationContextService, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: PersonalizationContextService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: function () { return [{ type: i1.PersonalizationConfig }, { type: i2.CmsService }]; } }); /** * Generated bundle index. Do not edit. */ export { PersonalizationContextService, PersonalizationCoreModule }; //# sourceMappingURL=spartacus-tracking-personalization-core.js.map