UNPKG

@objectiv/tracker-angular

Version:

Objectiv Angular framework analytics tracker for the open analytics taxonomy

236 lines (226 loc) 8.69 kB
import { Tracker, makeBrowserTrackerDefaultTransport, makeBrowserTrackerDefaultQueue, makeBrowserTrackerDefaultPluginsList, tagChildren, tagChild, tagContent, tagExpandable, tagInput, tagLink, tagLocation, tagMediaPlayer, tagNavigation, tagOverlay, tagPressable, tagRootLocation, makeId, startAutoTracking } from '@objectiv/tracker-browser'; import { TrackerPlatform } from '@objectiv/tracker-core'; import { __decorate, __awaiter } from 'tslib'; import { ElementRef, Input, Directive, Pipe, InjectionToken, APP_INITIALIZER, NgModule } from '@angular/core'; /* * Copyright 2022 Objectiv B.V. */ /** * Angular Tracker is identical to BrowserTracker, exception made for its platform. */ class AngularTracker extends Tracker { constructor(trackerConfig, ...contextConfigs) { var _a, _b; let config = trackerConfig; // Set the platform config.platform = TrackerPlatform.ANGULAR; // Either `transport` or `endpoint` must be provided if (!config.transport && !trackerConfig.endpoint) { throw new Error('Either `transport` or `endpoint` must be provided'); } // `transport` and `endpoint` must not be provided together if (config.transport && trackerConfig.endpoint) { throw new Error('Please provider either `transport` or `endpoint`, not both at same time'); } // Automatically create a default Transport for the given `endpoint` with a sensible setup if (trackerConfig.endpoint) { config = Object.assign(Object.assign({}, config), { transport: makeBrowserTrackerDefaultTransport(), queue: (_a = config.queue) !== null && _a !== void 0 ? _a : makeBrowserTrackerDefaultQueue(config) }); } // Add default plugins for Angular config.plugins = [...makeBrowserTrackerDefaultPluginsList(trackerConfig), ...((_b = config.plugins) !== null && _b !== void 0 ? _b : [])]; // Initialize core Tracker super(config, ...contextConfigs); // Store original config for comparison with other instances of Browser Tracker this.trackerConfig = trackerConfig; } } /* * Copyright 2021-2022 Objectiv B.V. */ /** * Allows calling Browser Tracker Location Taggers and Children Taggers directly from templates */ let ObjectivTrackerDirective = class ObjectivTrackerDirective { constructor(element) { this.element = element; } ngOnInit() { var _a; let locationTaggingAttributes; let childrenTaggingAttributes; // Children / Child Tagger if (this.tagChildren) { childrenTaggingAttributes = tagChildren(this.tagChildren); } else if (this.tagChild) { childrenTaggingAttributes = tagChild(this.tagChild); } // Location Taggers if (this.tagContent) { locationTaggingAttributes = tagContent(this.tagContent); } else if (this.tagExpandable) { locationTaggingAttributes = tagExpandable(this.tagExpandable); } else if (this.tagInput) { locationTaggingAttributes = tagInput(this.tagInput); } else if (this.tagLink) { locationTaggingAttributes = tagLink(this.tagLink); } else if (this.tagLocation) { locationTaggingAttributes = tagLocation(this.tagLocation); } else if (this.tagMediaPlayer) { locationTaggingAttributes = tagMediaPlayer(this.tagMediaPlayer); } else if (this.tagNavigation) { locationTaggingAttributes = tagNavigation(this.tagNavigation); } else if (this.tagOverlay) { locationTaggingAttributes = tagOverlay(this.tagOverlay); } else if (this.tagPressable) { locationTaggingAttributes = tagPressable(this.tagPressable); } else if (this.tagRootLocation) { locationTaggingAttributes = tagRootLocation(this.tagRootLocation); } // Merge Location Tagging Attributes and Children Tagging Attributes const taggingAttributes = Object.assign(Object.assign(Object.assign({}, ((_a = this.applyTaggingAttributes) !== null && _a !== void 0 ? _a : {})), (locationTaggingAttributes !== null && locationTaggingAttributes !== void 0 ? locationTaggingAttributes : {})), (childrenTaggingAttributes !== null && childrenTaggingAttributes !== void 0 ? childrenTaggingAttributes : {})); // Set all attributes on the nativeElement for (let [key, value] of Object.entries(taggingAttributes)) { if (value !== undefined) { this.element.nativeElement.setAttribute(key, value); } } } }; ObjectivTrackerDirective.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input() ], ObjectivTrackerDirective.prototype, "applyTaggingAttributes", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagChild", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagChildren", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagContent", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagExpandable", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagInput", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagLink", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagLocation", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagMediaPlayer", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagNavigation", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagOverlay", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagPressable", void 0); __decorate([ Input() ], ObjectivTrackerDirective.prototype, "tagRootLocation", void 0); ObjectivTrackerDirective = __decorate([ Directive({ selector: '[applyTaggingAttributes], [tagChild], [tagChildren], [tagContent], [tagExpandable], [tagInput], [tagLink], [tagLocation], [tagMediaPlayer], [tagNavigation], [tagOverlay], [tagPressable], [tagRootLocation]', }) ], ObjectivTrackerDirective); /* * Copyright 2021-2022 Objectiv B.V. */ /** * A PipeTransform to convert the given string in an id-like string using Core Tracker makeId */ let MakeId = class MakeId { transform(input, normalize = true) { return makeId(input, normalize); } }; MakeId = __decorate([ Pipe({ name: 'makeId', }) ], MakeId); /* * Copyright 2021-2022 Objectiv B.V. */ const OBJECTIV_TRACKER_CONFIG_TOKEN = new InjectionToken('objectiv-tracker-config', { factory: () => ({ applicationId: '' }), }); /* * Copyright 2021-2022 Objectiv B.V. */ /** * DI Configuration to attach Tracker Initialization. */ const OBJECTIV_TRACKER_INITIALIZER_PROVIDER = { provide: APP_INITIALIZER, multi: true, useFactory: ObjectivTrackerInitializer, deps: [OBJECTIV_TRACKER_CONFIG_TOKEN], }; /** * Simply create a new AngularTracker instance and starts the auto-tracking MutationObserver. */ function ObjectivTrackerInitializer(trackerConfig) { return () => __awaiter(this, void 0, void 0, function* () { const newTracker = new AngularTracker(trackerConfig); startAutoTracking(trackerConfig); return newTracker; }); } /* * Copyright 2021-2022 Objectiv B.V. */ var ObjectivTrackerModule_1; /** * Configures Objectiv Tracker. * This module is meant to be set as a dependency of the highest level module of the application, such as AppModule. */ let ObjectivTrackerModule = ObjectivTrackerModule_1 = class ObjectivTrackerModule { static forRoot(trackerConfig) { return { ngModule: ObjectivTrackerModule_1, providers: [ { provide: OBJECTIV_TRACKER_CONFIG_TOKEN, useValue: trackerConfig, }, OBJECTIV_TRACKER_INITIALIZER_PROVIDER, ], }; } }; ObjectivTrackerModule = ObjectivTrackerModule_1 = __decorate([ NgModule({ imports: [], declarations: [ObjectivTrackerDirective, MakeId], exports: [ObjectivTrackerDirective, MakeId], }) ], ObjectivTrackerModule); /* * Copyright 2021-2022 Objectiv B.V. */ /** * Generated bundle index. Do not edit. */ export { AngularTracker, MakeId, OBJECTIV_TRACKER_CONFIG_TOKEN, OBJECTIV_TRACKER_INITIALIZER_PROVIDER, ObjectivTrackerDirective, ObjectivTrackerInitializer, ObjectivTrackerModule }; //# sourceMappingURL=objectiv-tracker-angular.js.map