ngx-tag-commander-lib
Version:
This service lets you integrate Tag Commander in your AngularX (2+) applications easily. - [Official website](https://www.commandersact.com/fr/produits/tagcommander/)
378 lines (369 loc) • 12.4 kB
JavaScript
import { CommonModule } from '@angular/common';
import { Router, RoutesRecognized } from '@angular/router';
import { Injectable, NgModule, Directive, ElementRef, Input, defineInjectable, inject } from '@angular/core';
import { CustomNGXLoggerService, NgxLoggerLevel, LoggerModule } from 'ngx-logger';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @return {?}
*/
function _window() {
return window;
}
class WindowRef {
/**
* @return {?}
*/
get nativeWindow() {
return _window();
}
}
WindowRef.decorators = [
{ type: Injectable }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class TagCommanderService {
/**
* @param {?} winRef
* @param {?} customLogger
* @param {?} router
*/
constructor(winRef, customLogger, router) {
this.winRef = winRef;
this.customLogger = customLogger;
this.router = router;
this._tcContainers = [];
this._trackRoutes = false;
this.logger = customLogger.create({ level: NgxLoggerLevel.DEBUG });
this.router.events.subscribe(_data => {
if (_data instanceof RoutesRecognized && this._trackRoutes) {
if (_data.state.root.firstChild.data.tcInclude === undefined) {
/** @type {?} */
let data = _data.state.root.firstChild.data.tcInclude;
data.forEach(container => {
this.reloadContainer(container['ids'], container['idc'], container['options']);
});
}
}
});
}
// /**
// * the script URI correspond to the tag-commander script URL, it can either be a CDN URL or the path of your script
// * @param {string} id the id the script node will have
// * @param {string} uri the source of the script
// * @param {string} node the node on witch the script will be placed, it can either be head or body
// */
/**
* @param {?} id
* @param {?} uri
* @param {?} node
* @return {?}
*/
addContainer(id, uri, node) {
this._tcContainers.push({ 'id': id, 'uri': uri });
/** @type {?} */
let tagContainer = document.createElement('script');
tagContainer.setAttribute('type', 'text/javascript');
tagContainer.setAttribute('src', uri);
tagContainer.setAttribute('id', id);
if (typeof node !== 'string') {
this.logger.warn('you didn\'t specify where you wanted to place the script, it will be placed in the head by default');
document.querySelector('head').appendChild(tagContainer);
}
else if (node.toLowerCase() === 'head' || node.toLowerCase() === 'body') {
document.querySelector(node.toLowerCase()).appendChild(tagContainer);
}
else {
this.logger.warn('you didn\'t correctily specify where you wanted to place the script, it will be placed in the head by default');
document.querySelector('head').appendChild(tagContainer);
}
}
// /**
// * The script URI correspond to the tag-commander script URL, it can either be a CDN URL or the path of your script
// * @param {string} id
// */
/**
* @param {?} id
* @return {?}
*/
removeContainer(id) {
/** @type {?} */
let container = document.getElementById(id);
/** @type {?} */
let containers = this._tcContainers.slice(0);
document.querySelector('head').removeChild(container);
for (let i = 0; i < containers.length; i++) {
if (containers[i].id === id) {
this._tcContainers.splice(i, 1);
}
}
}
// /**
// * will display the debug messages if true
// * @param {boolean} debug if set to true it will activate the debug msg default is false
// */
/**
* @param {?} debug
* @return {?}
*/
setDebug(debug) {
this.debug = debug;
if (debug) {
this.logger.updateConfig({ level: NgxLoggerLevel.DEBUG });
}
else {
this.logger.updateConfig({ level: NgxLoggerLevel.OFF });
}
}
// /**
// * allows the router to be tracked
// * @param {boolean} b will read routes if set to true
// */
/**
* @param {?} b
* @return {?}
*/
trackRoutes(b) {
this._trackRoutes = !!b;
}
// /**
// * set or update the value of the var
// * @param {string} tcKey
// * @param {*} tcVar
// */
/**
* @param {?} tcKey
* @param {?} tcVar
* @return {?}
*/
setTcVar(tcKey, tcVar) {
if (typeof this.winRef.nativeWindow.tc_vars !== 'undefined') {
this.winRef.nativeWindow.tc_vars[tcKey] = tcVar;
}
}
// /**
// * set your varibles for the different providers, when called the first time it
// * instantiate the external varible
// * @param {object} vars
// */
/**
* @param {?} vars
* @return {?}
*/
setTcVars(vars) {
this.logger.debug('setTcVars', vars);
/** @type {?} */
let listOfVars = Object.keys(vars);
for (var i = 0; i < listOfVars.length; i++) {
this.setTcVar(listOfVars[i], vars[listOfVars[i]]);
}
}
;
// /**
// * get the value of the var
// * @param {string} tcKey
// */
/**
* @param {?} tcKey
* @return {?}
*/
getTcVar(tcKey) {
this.logger.debug('getTcVars', tcKey);
return this.winRef.nativeWindow.tc_vars[tcKey] === null ? this.winRef.nativeWindow.tc_vars[tcKey] : false;
}
;
// /**
// * removes the var by specifying the key
// * @param {string} varKey
// */
/**
* @param {?} varKey
* @return {?}
*/
removeTcVar(varKey) {
this.logger.debug('removeTcVars', varKey);
delete this.winRef.nativeWindow.tc_vars[varKey];
}
;
// /**
// * will reload all the containers
// * @param {object} options can contain some options in a form of an object
// */
/**
* @param {?} options
* @return {?}
*/
reloadAllContainers(options) {
this.logger.debug('reloadAllContainers', options);
options = options || {};
this.logger.debug('Reload all containers ', typeof options === 'object' ? 'with options ' + options : '');
this.winRef.nativeWindow.container.reload(options);
}
;
// /**
// * will reload a specifique container
// * @param {number} ids
// * @param {number} idc
// * @param {object} options can contain some options in a form of an object
// */
/**
* @param {?} ids
* @param {?} idc
* @param {?} options
* @return {?}
*/
reloadContainer(ids, idc, options) {
/** @type {?} */
var options = options || {};
this.logger.debug('Reload container ids: ' + ids + ' idc: ' + idc, typeof options === 'object' ? 'with options: ' + options : '');
this.winRef.nativeWindow['container_' + ids + '_' + idc].reload(options);
}
;
// /**
// * will set an TagCommander event
// * @param {string} eventLabel the name of your event
// * @param {HTMLElement} element the HTMLelement on witch the event is attached
// * @param {object} data the data you want to transmit
// */
/**
* @param {?} eventLabel
* @param {?} element
* @param {?} data
* @return {?}
*/
captureEvent(eventLabel, element, data) {
this.logger.debug('captureEvent', eventLabel, element, data);
if (typeof this.winRef.nativeWindow.tC !== 'undefined') {
if (eventLabel in Object.keys(this.winRef.nativeWindow.tC.event)) {
this.winRef.nativeWindow.tC.event[eventLabel](element, data);
}
else {
this.logger.error('the key ' + eventLabel + ' you specified in the catpure event does not existe');
}
}
}
;
}
TagCommanderService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
TagCommanderService.ctorParameters = () => [
{ type: WindowRef },
{ type: CustomNGXLoggerService },
{ type: Router }
];
/** @nocollapse */ TagCommanderService.ngInjectableDef = defineInjectable({ factory: function TagCommanderService_Factory() { return new TagCommanderService(inject(WindowRef), inject(CustomNGXLoggerService), inject(Router)); }, token: TagCommanderService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class TcSetVarsDirective {
/**
* @param {?} el
* @param {?} tcService
*/
constructor(el, tcService) {
this.el = el;
this.tcService = tcService;
}
/**
* @return {?}
*/
ngAfterViewInit() {
this.tcService.setTcVars(this.tcSetVars);
}
}
TcSetVarsDirective.decorators = [
{ type: Directive, args: [{
selector: '[tcSetVars]'
},] }
];
/** @nocollapse */
TcSetVarsDirective.ctorParameters = () => [
{ type: ElementRef },
{ type: TagCommanderService }
];
TcSetVarsDirective.propDecorators = {
tcSetVars: [{ type: Input, args: ['tcSetVars',] }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
// usage [tcEvent]
class TcEventDirective {
/**
* @param {?} el
* @param {?} tcService
*/
constructor(el, tcService) {
this.el = el;
this.tcService = tcService;
}
/**
* @return {?}
*/
ngAfterViewInit() {
if (typeof this.tcEvent === 'object' && (this.tcEventLabel === undefined || this.tcEventObj === undefined)) {
try {
this.tcService.captureEvent(this.tcEvent.eventLabel, this.el, this.tcEvent.data);
}
catch (e) {
console.error(e);
}
}
else {
this.tcService.captureEvent(this.tcEventLabel, this.el, this.tcEventObj);
}
}
}
TcEventDirective.decorators = [
{ type: Directive, args: [{
selector: '[tcEvent]'
},] }
];
/** @nocollapse */
TcEventDirective.ctorParameters = () => [
{ type: ElementRef },
{ type: TagCommanderService }
];
TcEventDirective.propDecorators = {
tcEvent: [{ type: Input, args: ['tcEvent',] }],
tcEventLabel: [{ type: Input, args: ['tcEventLabel',] }],
tcEventObj: [{ type: Input, args: ['tcEventObj',] }]
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgxTagCommanderModule {
}
NgxTagCommanderModule.decorators = [
{ type: NgModule, args: [{
declarations: [TcSetVarsDirective, TcEventDirective],
imports: [
CommonModule,
LoggerModule.forRoot({ level: NgxLoggerLevel.DEBUG, serverLogLevel: NgxLoggerLevel.ERROR })
],
exports: [TcSetVarsDirective, TcEventDirective],
providers: [TagCommanderService]
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NgxTagCommanderModule, TagCommanderService, TcEventDirective, TcSetVarsDirective, WindowRef };
//# sourceMappingURL=ngx-tag-commander.js.map