UNPKG

@deriv-com/analytics

Version:

The analytics package contains all the utility functions used for tracking user events and sending them to the respective platform such as Rudderstack.

145 lines (144 loc) 9.44 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(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 (g && (g = 0, op[0] && (_ = 0)), _) 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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Growthbook = void 0; var growthbook_1 = require("@growthbook/growthbook"); var analytics_js_1 = require("@rudderstack/analytics-js"); var Growthbook = /** @class */ (function () { // we have to pass settings due the specific framework implementation function Growthbook(clientKey, decryptionKey, growthbookOptions) { if (growthbookOptions === void 0) { growthbookOptions = {}; } var _this = this; this.analytics = new analytics_js_1.RudderAnalytics(); this.isLoaded = false; this.status = undefined; this.setAttributes = function (_a) { var id = _a.id, country = _a.country, user_language = _a.user_language, device_language = _a.device_language, device_type = _a.device_type, utm_source = _a.utm_source, utm_medium = _a.utm_medium, utm_campaign = _a.utm_campaign, is_authorised = _a.is_authorised, url = _a.url, domain = _a.domain, utm_content = _a.utm_content, residence_country = _a.residence_country, loggedIn = _a.loggedIn, network_type = _a.network_type, network_downlink = _a.network_downlink, user_id = _a.user_id, anonymous_id = _a.anonymous_id, account_mode = _a.account_mode; var CURRENT_ATTRIBUTES = _this.GrowthBook.getAttributes(); _this.GrowthBook.setAttributes(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, CURRENT_ATTRIBUTES), { id: id }), (user_id !== undefined && { user_id: user_id })), (anonymous_id !== undefined && { anonymous_id: anonymous_id })), (country !== undefined && { country: country })), (residence_country !== undefined && { residence_country: residence_country })), (user_language !== undefined && { user_language: user_language })), (device_language !== undefined && { device_language: device_language })), (device_type !== undefined && { device_type: device_type })), (utm_source !== undefined && { utm_source: utm_source })), (utm_medium !== undefined && { utm_medium: utm_medium })), (utm_campaign !== undefined && { utm_campaign: utm_campaign })), (is_authorised !== undefined && { is_authorised: is_authorised })), (url !== undefined && { url: url })), (domain !== undefined && { domain: domain })), (utm_content !== undefined && { utm_content: utm_content })), (loggedIn !== undefined && { loggedIn: loggedIn })), (network_type !== undefined && { network_type: network_type })), (network_downlink !== undefined && { network_downlink: network_downlink })), (account_mode !== undefined && { account_mode: account_mode }))); }; this.getFeatureValue = function (key, defaultValue) { return _this.GrowthBook.getFeatureValue(key, defaultValue); }; this.getStatus = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.waitForIsLoaded()]; case 1: _a.sent(); return [2 /*return*/, { isLoaded: this.isLoaded, status: this.status, }]; } }); }); }; this.getFeatureState = function (id) { return _this.GrowthBook.evalFeature(id); }; this.setUrl = function (href) { return _this.GrowthBook.setURL(href); }; this.isOn = function (key) { return _this.GrowthBook.isOn(key); }; this.init = function () { return __awaiter(_this, void 0, void 0, function () { var status; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.GrowthBook.init({ timeout: 2000, streaming: true }).catch(function (err) { console.error(err); })]; case 1: status = _a.sent(); this.status = status; this.isLoaded = true; return [2 /*return*/]; } }); }); }; this.GrowthBook = new growthbook_1.GrowthBook(__assign({ apiHost: 'https://cdn.growthbook.io', clientKey: clientKey, decryptionKey: decryptionKey, antiFlicker: false, navigateDelay: 0, antiFlickerTimeout: 3500, subscribeToChanges: true, enableDevMode: window === null || window === void 0 ? void 0 : window.location.hostname.includes('localhost'), trackingCallback: function (experiment, result) { if (window.dataLayer) { window.dataLayer.push({ event: 'experiment_viewed', event_category: 'experiment', rudder_anonymous_id: _this.analytics.getAnonymousId(), experiment_id: experiment.key, variation_id: result.variationId, }); } _this.analytics.track('experiment_viewed', { experimentId: experiment.key, variationId: result.variationId, }); } }, growthbookOptions)); this.init(); } Growthbook.prototype.reapplyExperiment = function (url) { var currentUrl = url !== null && url !== void 0 ? url : window.location.href; this.GrowthBook.setURL(currentUrl); console.log('Route changed, new URL:', currentUrl); }; // Utility function to wait for isLoaded to become true Growthbook.prototype.waitForIsLoaded = function () { var _this = this; return new Promise(function (resolve) { var checkInterval = setInterval(function () { if (_this.isLoaded) { clearInterval(checkInterval); resolve(); } }, 100); }); }; // for make instance by singleton Growthbook.getGrowthBookInstance = function (clientKey, decryptionKey, growthbookOptions) { if (!Growthbook._instance) { Growthbook._instance = new Growthbook(clientKey, decryptionKey !== null && decryptionKey !== void 0 ? decryptionKey : '', growthbookOptions); return Growthbook._instance; } return Growthbook._instance; }; return Growthbook; }()); exports.Growthbook = Growthbook;