@spartacus/tracking
Version:
Spartacus tracking and personalization library
73 lines (67 loc) • 3.38 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, NgModule } from '@angular/core';
import * as i1 from '@spartacus/core';
import { provideDefaultConfig } from '@spartacus/core';
/**
* Default Google Tag Manager collector.
*/
class GtmCollectorService {
constructor(winRef) {
this.winRef = winRef;
}
/**
* If the `TmsCollectorConfig.dataLayerProperty` is not specified, it uses the default `dataLayer`
*/
init(config, windowObject) {
var _a, _b;
const dataLayerProperty = (_a = config.dataLayerProperty) !== null && _a !== void 0 ? _a : 'dataLayer';
windowObject[dataLayerProperty] = (_b = windowObject[dataLayerProperty]) !== null && _b !== void 0 ? _b : [];
if (config.gtmId) {
(function (w, d, s, l, i) {
var _a;
w[l] = w[l] || [];
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
const f = d.getElementsByTagName(s)[0];
const j = d.createElement(s);
const dl = l !== 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
(_a = f.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(j, f);
})(windowObject, this.winRef.document, 'script', dataLayerProperty, config.gtmId);
}
}
pushEvent(config, windowObject, event) {
var _a;
const dataLayerProperty = (_a = config.dataLayerProperty) !== null && _a !== void 0 ? _a : 'dataLayer';
windowObject[dataLayerProperty].push(event);
}
}
GtmCollectorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmCollectorService, deps: [{ token: i1.WindowRef }], target: i0.ɵɵFactoryTarget.Injectable });
GtmCollectorService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmCollectorService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmCollectorService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: function () { return [{ type: i1.WindowRef }]; } });
const defaultGoogleTagManagerConfig = {
tagManager: {
gtm: {
collector: GtmCollectorService,
},
},
};
class GtmModule {
}
GtmModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
GtmModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmModule });
GtmModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmModule, providers: [provideDefaultConfig(defaultGoogleTagManagerConfig)] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: GtmModule, decorators: [{
type: NgModule,
args: [{
providers: [provideDefaultConfig(defaultGoogleTagManagerConfig)],
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { GtmCollectorService, GtmModule };
//# sourceMappingURL=spartacus-tracking-tms-gtm.js.map