featurehub-javascript-client-sdk
Version:
FeatureHub client/browser SDK
67 lines • 2.59 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleAnalyticsCollector = void 0;
const models_1 = require("./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 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 {
this.apiClient = GoogleAnalyticsCollector.googleAnalyticsClientProvider();
}
}
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) => {
var _a;
let line = undefined;
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 = (_a = f.getNumber()) === null || _a === void 0 ? void 0 : _a.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;
GoogleAnalyticsCollector.googleAnalyticsClientProvider = () => new BrowserGoogleAnalyticsApiClient();
//# sourceMappingURL=analytics.js.map
;