{"version":3,"file":"angulartics2-gst.js","sources":["ng://angulartics2/gst/gst.ts","ng://angulartics2/gst/angulartics2-gst.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\n\nimport { Angulartics2, GoogleGlobalSiteTagSettings, UserTimings } from 'angulartics2';\nimport { EventGst, UserTimingsGst } from './gst-interfaces';\n\ndeclare var gtag: any;\ndeclare var ga: any;\n\nexport class GoogleGlobalSiteTagDefaults implements GoogleGlobalSiteTagSettings {\n trackingIds: string[] = [];\n\n constructor() {\n if (typeof ga !== 'undefined' && ga) {\n // See: https://developers.google.com/analytics/devguides/collection/analyticsjs/ga-object-methods-reference\n ga(() => {\n ga.getAll().forEach((tracker: any) => {\n const id = tracker.get('trackingId');\n // If set both in forRoot and HTML page, we want to avoid duplicates\n if (id !== undefined && this.trackingIds.indexOf(id) === -1) {\n this.trackingIds.push(id);\n }\n });\n });\n }\n }\n}\n\n@Injectable({ providedIn: 'root' })\nexport class Angulartics2GoogleGlobalSiteTag {\n private dimensionsAndMetrics: { [key: string]: any } = {};\n\n constructor(protected angulartics2: Angulartics2) {\n const defaults = new GoogleGlobalSiteTagDefaults();\n // Set the default settings for this module\n this.angulartics2.settings.gst = { ...defaults, ...this.angulartics2.settings.gst };\n }\n\n startTracking(): void {\n this.angulartics2.pageTrack\n .pipe(this.angulartics2.filterDeveloperMode())\n .subscribe((x) => this.pageTrack(x.path));\n this.angulartics2.eventTrack\n .pipe(this.angulartics2.filterDeveloperMode())\n .subscribe((x) => this.eventTrack(x.action, x.properties));\n this.angulartics2.exceptionTrack\n .pipe(this.angulartics2.filterDeveloperMode())\n .subscribe((x: any) => this.exceptionTrack(x));\n this.angulartics2.userTimings\n .pipe(this.angulartics2.filterDeveloperMode())\n .subscribe(x => this.userTimings(this.convertTimings(x)));\n this.angulartics2.setUsername\n .pipe(this.angulartics2.filterDeveloperMode())\n .subscribe((x: string) => this.setUsername(x));\n this.angulartics2.setUserProperties\n .pipe(this.angulartics2.filterDeveloperMode())\n .subscribe((x: any) => this.setUserProperties(x));\n }\n\n /**\n * Manually track page view, see:\n *\n * https://developers.google.com/analytics/devguides/collection/gtagjs/single-page-applications#tracking_virtual_pageviews\n *\n * @param path relative url\n */\n pageTrack(path: string) {\n if (typeof gtag !== 'undefined' && gtag) {\n const params: any = {\n page_path: path,\n page_location: window.location.protocol + '//' + window.location.host + path,\n ...this.dimensionsAndMetrics\n };\n\n // Custom map must be reset with all config to stay valid.\n\n if (this.angulartics2.settings.gst.customMap) {\n params.custom_map = this.angulartics2.settings.gst.customMap;\n }\n if (this.angulartics2.settings.gst.userId) {\n params.user_id = this.angulartics2.settings.gst.userId;\n }\n if (this.angulartics2.settings.gst.anonymizeIp) {\n params.anonymize_ip = this.angulartics2.settings.gst.anonymizeIp;\n }\n\n for (const id of this.angulartics2.settings.gst.trackingIds) {\n gtag('config', id, params);\n }\n }\n }\n\n /**\n * Send interactions to gtag, i.e. for event tracking in Google Analytics. See:\n *\n * https://developers.google.com/analytics/devguides/collection/gtagjs/events\n *\n * @param action associated with the event\n */\n eventTrack(action: string, properties: Partial = {}) {\n this.eventTrackInternal(action, {\n event_category: properties.category || 'interaction',\n event_label: properties.label,\n value: properties.value,\n non_interaction: properties.noninteraction,\n ...properties.gstCustom\n });\n }\n\n /**\n * Exception Track Event in GST. See:\n *\n * https://developers.google.com/analytics/devguides/collection/gtagjs/exceptions\n *\n */\n exceptionTrack(properties: any) {\n // TODO: make interface\n // @param {Object} properties\n // @param {string} [properties.description]\n // @param {boolean} [properties.fatal]\n if (properties.fatal === undefined) {\n console.log('No \"fatal\" provided, sending with fatal=true');\n properties.fatal = true;\n }\n\n properties.exDescription = properties.event ? properties.event.stack : properties.description;\n\n this.eventTrack('exception', {\n gstCustom: {\n description: properties.exDescription,\n fatal: properties.fatal,\n ...properties.gstCustom\n }\n });\n }\n\n /**\n * User Timings Event in GST.\n *\n * @param properties Comprised of the mandatory fields:\n * - name (string)\n * - value (number - integer)\n * Properties can also have the optional fields:\n * - category (string)\n * - label (string)\n *\n * @link https://developers.google.com/analytics/devguides/collection/gtagjs/user-timings\n */\n userTimings(properties: UserTimingsGst) {\n if (!properties) {\n console.error('User timings - \"properties\" parameter is required to be set.');\n return;\n }\n\n this.eventTrackInternal('timing_complete', {\n name: properties.name,\n value: properties.value,\n event_category: properties.category,\n event_label: properties.label\n });\n }\n\n private convertTimings(properties: UserTimings): UserTimingsGst {\n return {\n name: properties.timingVar,\n value: properties.timingValue,\n category: properties.timingCategory,\n label: properties.timingLabel\n };\n }\n\n setUsername(userId: string | { userId: string | number }) {\n this.angulartics2.settings.gst.userId = userId;\n if (typeof gtag !== 'undefined' && gtag) {\n gtag('set', { user_id: typeof userId === 'string' || !userId ? userId : userId.userId });\n }\n }\n\n setUserProperties(properties: any) {\n this.setDimensionsAndMetrics(properties);\n }\n\n private setDimensionsAndMetrics(properties: { [key: string]: any }) {\n // We want the dimensions and metrics to accumulate, so we merge with previous value\n this.dimensionsAndMetrics = {\n ...this.dimensionsAndMetrics,\n ...properties\n };\n\n // Remove properties that are null or undefined\n Object.keys(this.dimensionsAndMetrics).forEach(key => {\n const val = this.dimensionsAndMetrics[key];\n if (val === undefined || val === null) {\n delete this.dimensionsAndMetrics[key];\n }\n });\n\n if (typeof gtag !== 'undefined' && gtag) {\n gtag('set', this.dimensionsAndMetrics);\n }\n }\n\n private eventTrackInternal(action: string, properties: any = {}) {\n this.cleanProperties(properties);\n if (typeof gtag !== 'undefined' && gtag) {\n gtag('event', action, properties);\n }\n }\n\n private cleanProperties(properties: { [key: string]: any }): void {\n // GA requires that eventValue be an non-negative integer, see:\n // https://developers.google.com/analytics/devguides/collection/gtagjs/events\n if (properties.value) {\n const parsed = parseInt(properties.value, 10);\n properties.value = isNaN(parsed) ? 0 : parsed;\n }\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './gst';\n"],"names":[],"mappings":";;;;MAQa,2BAA2B;IAGtC;QAFA,gBAAW,GAAa,EAAE,CAAC;QAGzB,IAAI,OAAO,E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