UNPKG

@inyourarea/aard-client-js

Version:

A JavaScript tracking library for the Aard Platform at InYourArea.co.uk. Record and track activity across the different services of the platform.

209 lines 9.99 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { createClient } from './client'; import { addRestoreFragmentPlugin } from './transformers'; import { DEFAULT_ENVIRONMENT, DEFAULT_BASE_URL_CREATOR } from './recorder.const'; // TODO: Added the queue to this class in a rush. Try to redesign it to split responsabilities and not put too much logic here. var AardRecorder = /** @class */ (function () { function AardRecorder(_a) { var _this = this; var projectName = _a.projectName, apiKey = _a.apiKey, _b = _a.session, session = _b === void 0 ? true : _b, fetchFn = _a.fetchFn, maxRetries = _a.maxRetries, _c = _a.environment, environment = _c === void 0 ? DEFAULT_ENVIRONMENT : _c, _d = _a.baseUrlCreator, baseUrlCreator = _d === void 0 ? DEFAULT_BASE_URL_CREATOR : _d, _e = _a.transformers, transformers = _e === void 0 ? [] : _e, debug = _a.debug; this.fragments = new Map(); this.eventQueue = []; this.busyCount = 0; /** * Returns the internal client used to communicate with the Aard client. * The client is an object that expose the following functions: * - trasnformEvent(event, transformers) transforms an event with the provided transformers * - saveFragment(dimensions) saves the provided dimensions as a fragment on the Aard server and returns its hash * - sendEvent(dimensions) sends an event to the Aard server */ this.getClient = function () { return _this.client; }; if (!projectName) { throw new Error('ProjectName is mandatory'); } var baseUrl = baseUrlCreator(environment); this.projectName = projectName; this.session = session; this.transformers = new Set(transformers); this.client = createClient({ apiKey: apiKey, baseUrl: baseUrl, fetchFn: fetchFn, maxRetries: maxRetries, debug: debug, }); } /** * Register a fragmet on Aard server and store its hash with the provided arbitrary name * The hash will be added to every event, in order to have the fragment merged to the event server side. */ AardRecorder.prototype.registerFragment = function (name, dimensionsObject) { return __awaiter(this, void 0, void 0, function () { var hash; return __generator(this, function (_a) { switch (_a.label) { case 0: this.busyCount++; return [4 /*yield*/, this.client.saveFragment(dimensionsObject)]; case 1: hash = _a.sent(); if (hash) { this.fragments.set(name, hash); } this.busyCount--; this.tryConsumeQueue(); return [2 /*return*/]; } }); }); }; /** * Adds a fragment hash to the recorder without registering any fragment on the server */ AardRecorder.prototype.addFragmentHash = function (name, hash) { this.fragments.set(name, hash); }; /** * Returns a map of fragment names and their hash */ AardRecorder.prototype.getFragments = function () { return this.fragments; }; /** * Returns a the fragment hash stored with the provided name */ AardRecorder.prototype.getFragment = function (name) { return this.fragments.get(name); }; /** * Returns an array with all the fragment hashes stored in the recoder */ AardRecorder.prototype.getFragmentsHashes = function () { return Array.from(this.fragments.values()); }; /** * Removes the fragment hash stored with the provided name */ AardRecorder.prototype.removeFragment = function (name) { this.fragments.delete(name); }; /** * Adds a transformer to the recorder. * This transformer will be applied to every event. */ AardRecorder.prototype.addTransformer = function (transformer) { this.transformers.add(transformer); }; /** * Removes a transformer from the recorder. */ AardRecorder.prototype.removeTransformer = function (transformer) { this.transformers.delete(transformer); }; /** * Returns a list of all the stored transformers. */ AardRecorder.prototype.getTransformersList = function () { return Array.from(this.transformers.values()); }; /** * Sends an event to the Aard server. * It applies the following modifications to the event: * - applies all the transformer provided to this method * - applies all the transformer stored in the recoder * - adds the restore fragment plugin for every fragment hash stored in the recorder */ AardRecorder.prototype.sendEvent = function (name, _a, transformers) { var dimensions = _a.dimensions, plugins = _a.plugins, _b = _a.unit, unit = _b === void 0 ? 'Unit' : _b, _c = _a.value, value = _c === void 0 ? 1 : _c; return this.addEventToQueue(name, { dimensions: dimensions, plugins: plugins, unit: unit, value: value, }, transformers); }; /** * Same as sendEvent, but specifically for the pageview event. */ AardRecorder.prototype.sendPageView = function (_a, transformers) { var dimensions = _a.dimensions, _b = _a.plugins, plugins = _b === void 0 ? [] : _b; return this.addEventToQueue('pageview', { dimensions: dimensions, plugins: plugins, unit: 'Unit', value: 1, }, transformers); }; AardRecorder.prototype.transformEvent = function (event, extraTransformers) { if (extraTransformers === void 0) { extraTransformers = []; } var transformersList = this.getTransformersList().concat(this.getFragmentsHashes() .map(function (hash) { return addRestoreFragmentPlugin(hash); }), extraTransformers); return this.client.transformEvent(event, transformersList); }; AardRecorder.prototype.addEventToQueue = function (name, _a, transformers) { var dimensions = _a.dimensions, _b = _a.plugins, plugins = _b === void 0 ? [] : _b, _c = _a.unit, unit = _c === void 0 ? 'Unit' : _c, _d = _a.value, value = _d === void 0 ? 1 : _d; var _e = this, projectName = _e.projectName, session = _e.session; var currentDate = new Date(); var event = { projectName: projectName, metricName: name, dimensions: dimensions, plugins: plugins, timestamp: currentDate.valueOf() + (currentDate.getTimezoneOffset() * 60000), timezone: currentDate.getTimezoneOffset() / 60 * -1, session: session, unit: unit, value: value, }; this.eventQueue.push([event, transformers || []]); this.tryConsumeQueue(); }; AardRecorder.prototype.tryConsumeQueue = function () { if (!this.busyCount) { var nextQueueItem = this.eventQueue.shift(); if (nextQueueItem) { var event_1 = nextQueueItem[0], transformers = nextQueueItem[1]; var transformedEvent = this.transformEvent(event_1, transformers); this.client.sendEvent(transformedEvent); this.tryConsumeQueue(); } } }; return AardRecorder; }()); export default AardRecorder; //# sourceMappingURL=recorder.js.map