@trimble-oss/trimble-id
Version:
Trimble Identity SDK for JavaScript/ TypeScript
121 lines (111 loc) • 4.9 kB
JavaScript
;
// implements IHttpClientProvider
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
// AMD
define(['./HttpClient'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory({
HttpClient: require('./HttpClient'),
});
} else {
// Browser globals (Note: root is window)
root.AnalyticsHttpClient = factory(root);
}
}(this, function (imports) {
/**
* A Http client for sending event hits to GA
*/
class AnalyticsHttpClient {
static baseAddress = "https://www.google-analytics.com"
static measurementId = "G-9DHNE789XG"
static apiKey = "7wd60wEeSnufb8FK6naHOQ"
static cid = uuidv4()
static sdkName = "@trimble-oss/trimble-id"
static sdkVersion = "0.0.7"
/**
* The method to send other type of events
* @param {string} name : Name of an event
* @param {object} params : Additional parameters of an event
*/
static sendEvent(name, params) {
return new Promise((resolve, reject) => {
try {
const relativeAddress = `/mp/collect?measurement_id=${this.measurementId}&api_secret=${this.apiKey}`;
const address = this.baseAddress + relativeAddress;
const requestSettings = {
headers: {
"Content-Type": 'application/x-www-form-urlencoded',
Accept: 'application/json',
}
};
let body = {
"client_id": this.cid,
"events": [{
"name": name,
"params": params
}]
}
new imports.HttpClient().httpPost(address, JSON.stringify(body), requestSettings)
.then(() => resolve())
.catch(() => resolve());
} catch (e) {
resolve();
}
});
}
static sendCustomEvent(name, eventType, application_id, clientName = null, clientVersion = null, exception_message = null) {
const params = {
"event_type": eventType,
"client_name": clientName || this.sdkName,
"client_version": clientVersion || this.sdkVersion,
"language": "js",
"application_id": application_id
};
if (exception_message) params.exception_message = String(exception_message);
this.sendEvent(name + "_" + eventType.toLowerCase(), params);
}
/**
* Send method events
* @param {string} name : Name of an event
* @param {string} application_id : Consumer key for the calling application
* @param {string|null} clientName : Name of the client SDK
* @param {string|null} clientVersion : Version of the client SDK
*/
static sendMethodEvent(name, application_id, clientName = null, clientVersion = null) {
this.sendCustomEvent(name, "METHOD_USAGE", application_id, clientName, clientVersion);
}
/**
* Send initialization events
* @param {string} name : Name of an event
* @param {string} application_id : Consumer key for the calling application
* @param {string|null} clientName : Name of the client SDK
* @param {string|null} clientVersion : Version of the client SDK
*/
static sendInitEvent(name, application_id, clientName = null, clientVersion = null) {
this.sendCustomEvent(name, "INIT", application_id, clientName, clientVersion);
}
/**
* Send exception events
* @param {string} name : Name of an event
* @param {string} exception_message : Exception message
* @param {string} application_id : Consumer key for the calling application
* @param {string|null} clientName : Name of the client SDK
* @param {string|null} clientVersion : Version of the client SDK
*/
static sendExceptionEvent(name, exception_message, application_id, clientName = null, clientVersion = null) {
this.sendCustomEvent(name, "EXCEPTION", application_id, clientName, clientVersion, exception_message);
}
}
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
return AnalyticsHttpClient;
}));