UNPKG

nativescript

Version:

Command-line interface for building NativeScript projects

138 lines • 6.77 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleAnalyticsProvider = void 0; const uuid_1 = require("uuid"); const ua = require("universal-analytics"); const decorators_1 = require("../../common/decorators"); const _ = require("lodash"); const yok_1 = require("../../common/yok"); class GoogleAnalyticsProvider { constructor(clientId, $staticConfig, $analyticsSettingsService, $logger, $proxyService, $config, analyticsLoggingService) { this.clientId = clientId; this.$staticConfig = $staticConfig; this.$analyticsSettingsService = $analyticsSettingsService; this.$logger = $logger; this.$proxyService = $proxyService; this.$config = $config; this.analyticsLoggingService = analyticsLoggingService; } async trackHit(trackInfo) { const sessionId = (0, uuid_1.v4)(); try { await this.track(this.$config.GA_TRACKING_ID, trackInfo, sessionId); } catch (e) { this.analyticsLoggingService.logData({ type: "Error" /* FileLogMessageType.Error */, message: `Unable to track information ${JSON.stringify(trackInfo)}. Error is: ${e}`, }); this.$logger.trace("Analytics exception: ", e); } } getVisitor(gaTrackingId, proxy) { this.analyticsLoggingService.logData({ message: `Initializing Google Analytics visitor for id: ${gaTrackingId} with clientId: ${this.clientId}.`, }); const visitor = ua({ tid: gaTrackingId, cid: this.clientId, headers: { ["User-Agent"]: this.$analyticsSettingsService.getUserAgentString(`tnsCli/${this.$staticConfig.version}`), }, requestOptions: { proxy, }, https: true, }); this.analyticsLoggingService.logData({ message: `Successfully initialized Google Analytics visitor for id: ${gaTrackingId} with clientId: ${this.clientId}.`, }); return visitor; } async track(gaTrackingId, trackInfo, sessionId) { const proxySettings = await this.$proxyService.getCache(); const proxy = proxySettings && proxySettings.proxy; const visitor = this.getVisitor(gaTrackingId, proxy); await this.setCustomDimensions(visitor, trackInfo.customDimensions, sessionId); switch (trackInfo.googleAnalyticsDataType) { case "pageview" /* GoogleAnalyticsDataType.Page */: await this.trackPageView(visitor, trackInfo); break; case "event" /* GoogleAnalyticsDataType.Event */: await this.trackEvent(visitor, trackInfo); break; } } async setCustomDimensions(visitor, customDimensions, sessionId) { const defaultValues = { ["cd1" /* GoogleAnalyticsCustomDimensions.cliVersion */]: this.$staticConfig.version, ["cd6" /* GoogleAnalyticsCustomDimensions.nodeVersion */]: process.version, ["cd3" /* GoogleAnalyticsCustomDimensions.clientID */]: this.clientId, ["cd2" /* GoogleAnalyticsCustomDimensions.projectType */]: null, ["cd9" /* GoogleAnalyticsCustomDimensions.isShared */]: null, ["cd4" /* GoogleAnalyticsCustomDimensions.sessionID */]: sessionId, ["cd5" /* GoogleAnalyticsCustomDimensions.client */]: "Unknown" /* AnalyticsClients.Unknown */, }; customDimensions = _.merge(defaultValues, customDimensions); _.each(customDimensions, (value, key) => { this.analyticsLoggingService.logData({ message: `Setting custom dimension ${key} to value ${value}`, }); visitor.set(key, value); }); } trackEvent(visitor, trackInfo) { return new Promise((resolve, reject) => { visitor.event(trackInfo.category, trackInfo.action, trackInfo.label, trackInfo.value, { p: this.currentPage }, (err) => { if (err) { this.analyticsLoggingService.logData({ message: `Unable to track event with category: '${trackInfo.category}', action: '${trackInfo.action}', label: '${trackInfo.label}', ` + `value: '${trackInfo.value}' attached page: ${this.currentPage}. Error is: ${err}.`, type: "Error" /* FileLogMessageType.Error */, }); reject(err); return; } this.analyticsLoggingService.logData({ message: `Tracked event with category: '${trackInfo.category}', action: '${trackInfo.action}', label: '${trackInfo.label}', value: '${trackInfo.value}' attached page: ${this.currentPage}.`, }); resolve(); }); }); } trackPageView(visitor, trackInfo) { return new Promise((resolve, reject) => { this.currentPage = trackInfo.path; const pageViewData = { dp: trackInfo.path, dt: trackInfo.title, }; visitor.pageview(pageViewData, (err) => { if (err) { this.analyticsLoggingService.logData({ message: `Unable to track pageview with path '${trackInfo.path}' and title: '${trackInfo.title}' Error is: ${err}.`, type: "Error" /* FileLogMessageType.Error */, }); reject(err); return; } this.analyticsLoggingService.logData({ message: `Tracked pageview with path '${trackInfo.path}' and title: '${trackInfo.title}'.`, }); resolve(); }); }); } } exports.GoogleAnalyticsProvider = GoogleAnalyticsProvider; __decorate([ (0, decorators_1.cache)() ], GoogleAnalyticsProvider.prototype, "getVisitor", null); yok_1.injector.register("googleAnalyticsProvider", GoogleAnalyticsProvider); //# sourceMappingURL=google-analytics-provider.js.map