UNPKG

4.49 kBJavaScriptView Raw
1import { ɵɵdefineInjectable, ɵɵinject, Injectable } from '@angular/core';
2import { Angulartics2 } from 'angulartics2';
3
4class GoogleTagManagerDefaults {
5 constructor() {
6 this.userId = null;
7 }
8}
9class Angulartics2GoogleTagManager {
10 constructor(angulartics2) {
11 this.angulartics2 = angulartics2;
12 // The dataLayer needs to be initialized
13 if (typeof dataLayer !== 'undefined' && dataLayer) {
14 dataLayer = window.dataLayer = window.dataLayer || [];
15 }
16 const defaults = new GoogleTagManagerDefaults();
17 // Set the default settings for this module
18 this.angulartics2.settings.gtm = Object.assign(Object.assign({}, defaults), this.angulartics2.settings.gtm);
19 this.angulartics2.setUsername
20 .subscribe((x) => this.setUsername(x));
21 }
22 startTracking() {
23 this.angulartics2.pageTrack
24 .pipe(this.angulartics2.filterDeveloperMode())
25 .subscribe((x) => this.pageTrack(x.path));
26 this.angulartics2.eventTrack
27 .pipe(this.angulartics2.filterDeveloperMode())
28 .subscribe((x) => this.eventTrack(x.action, x.properties));
29 this.angulartics2.exceptionTrack
30 .pipe(this.angulartics2.filterDeveloperMode())
31 .subscribe((x) => this.exceptionTrack(x));
32 }
33 pageTrack(path) {
34 this.pushLayer({
35 event: 'Page View',
36 'content-name': path,
37 userId: this.angulartics2.settings.gtm.userId
38 });
39 }
40 /**
41 * Send Data Layer
42 *
43 * @layer data layer object
44 */
45 pushLayer(layer) {
46 if (typeof dataLayer !== 'undefined' && dataLayer) {
47 dataLayer.push(layer);
48 }
49 }
50 /**
51 * Send interactions to the dataLayer, i.e. for event tracking in Google Analytics
52 *
53 * @param action associated with the event
54 */
55 eventTrack(action, properties) {
56 // TODO: make interface
57 // @param {string} properties.category
58 // @param {string} [properties.label]
59 // @param {number} [properties.value]
60 // @param {boolean} [properties.noninteraction]
61 // Set a default GTM category
62 properties = properties || {};
63 this.pushLayer(Object.assign({ event: properties.event || 'interaction', target: properties.category || 'Event', action, label: properties.label, value: properties.value, interactionType: properties.noninteraction, userId: this.angulartics2.settings.gtm.userId }, properties.gtmCustom));
64 }
65 /**
66 * Exception Track Event in GTM
67 *
68 */
69 exceptionTrack(properties) {
70 // TODO: make interface
71 // @param {Object} properties
72 // @param {string} properties.appId
73 // @param {string} properties.appName
74 // @param {string} properties.appVersion
75 // @param {string} [properties.description]
76 // @param {boolean} [properties.fatal]
77 if (!properties || !properties.appId || !properties.appName || !properties.appVersion) {
78 console.error('Must be setted appId, appName and appVersion.');
79 return;
80 }
81 if (properties.fatal === undefined) {
82 console.log('No "fatal" provided, sending with fatal=true');
83 properties.exFatal = true;
84 }
85 properties.exDescription = properties.event ? properties.event.stack : properties.description;
86 this.eventTrack(`Exception thrown for ${properties.appName} <${properties.appId}@${properties.appVersion}>`, {
87 category: 'Exception',
88 label: properties.exDescription,
89 });
90 }
91 /**
92 * Set userId for use with Universal Analytics User ID feature
93 *
94 * @param userId used to identify user cross-device in Google Analytics
95 */
96 setUsername(userId) {
97 this.angulartics2.settings.gtm.userId = userId;
98 }
99}
100Angulartics2GoogleTagManager.ɵprov = ɵɵdefineInjectable({ factory: function Angulartics2GoogleTagManager_Factory() { return new Angulartics2GoogleTagManager(ɵɵinject(Angulartics2)); }, token: Angulartics2GoogleTagManager, providedIn: "root" });
101Angulartics2GoogleTagManager.decorators = [
102 { type: Injectable, args: [{ providedIn: 'root' },] }
103];
104Angulartics2GoogleTagManager.ctorParameters = () => [
105 { type: Angulartics2 }
106];
107
108/**
109 * Generated bundle index. Do not edit.
110 */
111
112export { Angulartics2GoogleTagManager, GoogleTagManagerDefaults };
113//# sourceMappingURL=angulartics2-gtm.js.map