UNPKG

@objectiv/tracker-angular

Version:

Objectiv Angular framework analytics tracker for the open analytics taxonomy

268 lines (258 loc) 10.3 kB
import { __extends, __assign, __spread, __values, __read, __decorate, __awaiter, __generator } from 'tslib'; import { makeBrowserTrackerDefaultTransport, makeBrowserTrackerDefaultQueue, makeBrowserTrackerDefaultPluginsList, Tracker, 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 { 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. */ var AngularTracker = /** @class */ (function (_super) { __extends(AngularTracker, _super); function AngularTracker(trackerConfig) { var contextConfigs = []; for (var _i = 1; _i < arguments.length; _i++) { contextConfigs[_i - 1] = arguments[_i]; } var _a, _b; var _this = this; var 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 = __assign(__assign({}, config), { transport: makeBrowserTrackerDefaultTransport(), queue: (_a = config.queue) !== null && _a !== void 0 ? _a : makeBrowserTrackerDefaultQueue(config) }); } // Add default plugins for Angular config.plugins = __spread(makeBrowserTrackerDefaultPluginsList(trackerConfig), ((_b = config.plugins) !== null && _b !== void 0 ? _b : [])); // Initialize core Tracker _this = _super.apply(this, __spread([config], contextConfigs)) || this; // Store original config for comparison with other instances of Browser Tracker _this.trackerConfig = trackerConfig; return _this; } return AngularTracker; }(Tracker)); /* * Copyright 2021-2022 Objectiv B.V. */ /** * Allows calling Browser Tracker Location Taggers and Children Taggers directly from templates */ var ObjectivTrackerDirective = /** @class */ (function () { function ObjectivTrackerDirective(element) { this.element = element; } ObjectivTrackerDirective.prototype.ngOnInit = function () { var e_1, _a; var _b; var locationTaggingAttributes; var 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 var taggingAttributes = __assign(__assign(__assign({}, ((_b = this.applyTaggingAttributes) !== null && _b !== void 0 ? _b : {})), (locationTaggingAttributes !== null && locationTaggingAttributes !== void 0 ? locationTaggingAttributes : {})), (childrenTaggingAttributes !== null && childrenTaggingAttributes !== void 0 ? childrenTaggingAttributes : {})); try { // Set all attributes on the nativeElement for (var _c = __values(Object.entries(taggingAttributes)), _d = _c.next(); !_d.done; _d = _c.next()) { var _e = __read(_d.value, 2), key = _e[0], value = _e[1]; if (value !== undefined) { this.element.nativeElement.setAttribute(key, value); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_1) throw e_1.error; } } }; ObjectivTrackerDirective.ctorParameters = function () { return [ { 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); return ObjectivTrackerDirective; }()); /* * Copyright 2021-2022 Objectiv B.V. */ /** * A PipeTransform to convert the given string in an id-like string using Core Tracker makeId */ var MakeId = /** @class */ (function () { function MakeId() { } MakeId.prototype.transform = function (input, normalize) { if (normalize === void 0) { normalize = true; } return makeId(input, normalize); }; MakeId = __decorate([ Pipe({ name: 'makeId', }) ], MakeId); return MakeId; }()); /* * Copyright 2021-2022 Objectiv B.V. */ var OBJECTIV_TRACKER_CONFIG_TOKEN = new InjectionToken('objectiv-tracker-config', { factory: function () { return ({ applicationId: '' }); }, }); /* * Copyright 2021-2022 Objectiv B.V. */ /** * DI Configuration to attach Tracker Initialization. */ var 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) { var _this = this; return function () { return __awaiter(_this, void 0, void 0, function () { var newTracker; return __generator(this, function (_a) { newTracker = new AngularTracker(trackerConfig); startAutoTracking(trackerConfig); return [2 /*return*/, newTracker]; }); }); }; } /* * Copyright 2021-2022 Objectiv B.V. */ /** * Configures Objectiv Tracker. * This module is meant to be set as a dependency of the highest level module of the application, such as AppModule. */ var ObjectivTrackerModule = /** @class */ (function () { function ObjectivTrackerModule() { } ObjectivTrackerModule_1 = ObjectivTrackerModule; ObjectivTrackerModule.forRoot = function (trackerConfig) { return { ngModule: ObjectivTrackerModule_1, providers: [ { provide: OBJECTIV_TRACKER_CONFIG_TOKEN, useValue: trackerConfig, }, OBJECTIV_TRACKER_INITIALIZER_PROVIDER, ], }; }; var ObjectivTrackerModule_1; ObjectivTrackerModule = ObjectivTrackerModule_1 = __decorate([ NgModule({ imports: [], declarations: [ObjectivTrackerDirective, MakeId], exports: [ObjectivTrackerDirective, MakeId], }) ], ObjectivTrackerModule); return 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