UNPKG

featurehub-repository

Version:

Core package of API that exposes FeatureHub feature flags, values and configuration to client applications written in Typescript or Javascript.

83 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleAnalyticsCollector = void 0; const models_1 = require("./models/models"); class BrowserGoogleAnalyticsApiClient { cid(other) { return other.get('cid'); } postBatchUpdate(batchData) { const req = new XMLHttpRequest(); req.open('POST', 'https://www.google-analytics.com/batch'); req.send(batchData); } } class NodejsGoogleAnalyticsApiClient { cid(other) { return other.get('cid') || process.env.GA_CID; } postBatchUpdate(batchData) { const req = require('https').request({ host: 'www.google-analytics.com', path: 'batch' }); req.write(batchData); req.end(); } } class GoogleAnalyticsCollector { constructor(uaKey, cid, apiClient) { if (uaKey == null) { throw new Error('UA must be set'); } this.uaKey = uaKey; this._cid = cid; if (apiClient) { this.apiClient = apiClient; } else { if (Object.prototype.toString.call(global.process) !== '[object process]') { this.apiClient = new NodejsGoogleAnalyticsApiClient(); } else { this.apiClient = new BrowserGoogleAnalyticsApiClient(); } } } set cid(value) { this._cid = value; } logEvent(action, other, featureStateAtCurrentTime) { var _a; const finalCid = (_a = this._cid) !== null && _a !== void 0 ? _a : this.apiClient.cid(other); if (finalCid === undefined) { return; } const ev = (other !== undefined && other !== null && other.get('gaValue') !== undefined) ? ('&ev=' + encodeURI(other.get('gaValue'))) : ''; const baseForEachLine = 'v=1&tid=' + this.uaKey + '&cid=' + finalCid + '&t=event&ec=FeatureHub%20Event&ea=' + encodeURI(action) + ev + '&el='; let postString = ''; featureStateAtCurrentTime.forEach((f) => { let line = null; if (f.getType() === models_1.FeatureValueType.Boolean) { line = f.getBoolean() === true ? 'on' : 'off'; } else if (f.getType() === models_1.FeatureValueType.String) { line = f.getString(); } else if (f.getType() === models_1.FeatureValueType.Number) { line = f.getNumber().toString(); } if (line !== null && line !== undefined) { line = encodeURI(f.getKey() + ' : ' + line); postString = postString + baseForEachLine + line + '\n'; } }); if (postString.length > 0) { this.apiClient.postBatchUpdate(postString); } } } exports.GoogleAnalyticsCollector = GoogleAnalyticsCollector; //# sourceMappingURL=analytics.js.map