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.

273 lines (272 loc) 21.5 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 }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Analytics = exports.createAnalyticsInstance = void 0; var growthbook_1 = require("./growthbook"); var rudderstack_1 = require("./rudderstack"); var js_cookie_1 = __importDefault(require("js-cookie")); var utils_1 = require("@deriv-com/utils"); function createAnalyticsInstance(options) { var _this = this; var _growthbook, _rudderstack, core_data = {}, tracking_config = {}, event_cache = [], _pending_identify_calls = []; var getClientCountry = function () { return __awaiter(_this, void 0, void 0, function () { var countryFromCloudflare, countryFromCookie, websiteStatus, countryFromWebsiteStatus; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, utils_1.CountryUtils.getCountry()]; case 1: countryFromCloudflare = _b.sent(); countryFromCookie = js_cookie_1.default.get('clients_country'); websiteStatus = js_cookie_1.default.get('website_status'); countryFromWebsiteStatus = ''; if (websiteStatus) { try { countryFromWebsiteStatus = ((_a = JSON.parse(websiteStatus)) === null || _a === void 0 ? void 0 : _a.clients_country) || ''; } catch (e) { console.error('Failed to parse cookie: ', e); } } return [2 /*return*/, countryFromCookie || countryFromWebsiteStatus || countryFromCloudflare]; } }); }); }; var initialise = function (_a) { var growthbookKey = _a.growthbookKey, growthbookDecryptionKey = _a.growthbookDecryptionKey, rudderstackKey = _a.rudderstackKey, growthbookOptions = _a.growthbookOptions, _b = _a.disableRudderstackAMD, disableRudderstackAMD = _b === void 0 ? false : _b; return __awaiter(_this, void 0, void 0, function () { var country, _c, interval_1, error_1; var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z; var _0, _1; return __generator(this, function (_2) { switch (_2.label) { case 0: _2.trys.push([0, 3, , 4]); _c = ((_d = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _d === void 0 ? void 0 : _d.country); if (_c) return [3 /*break*/, 2]; return [4 /*yield*/, getClientCountry()]; case 1: _c = (_2.sent()); _2.label = 2; case 2: country = _c; _rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey, disableRudderstackAMD, function () { _pending_identify_calls.forEach(function (userId) { if (userId && !isUUID(userId)) { _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(userId, { language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en', }); } }); _pending_identify_calls = []; }); if ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && Object.keys(growthbookOptions.attributes).length > 0) core_data = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, core_data), { country: country }), (((_e = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _e === void 0 ? void 0 : _e.user_language) && { user_language: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.user_language, })), (((_f = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _f === void 0 ? void 0 : _f.account_type) && { account_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_type, })), (((_g = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _g === void 0 ? void 0 : _g.app_id) && { app_id: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.app_id })), (((_h = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _h === void 0 ? void 0 : _h.residence_country) && { residence_country: (_j = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _j === void 0 ? void 0 : _j.residence_country, })), (((_k = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _k === void 0 ? void 0 : _k.device_type) && { device_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.device_type, })), (((_l = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _l === void 0 ? void 0 : _l.url) && { url: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.url })), ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && { loggedIn: !!((_m = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _m === void 0 ? void 0 : _m.loggedIn), })), (((_o = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _o === void 0 ? void 0 : _o.email_hash) && { email_hash: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.email_hash, })), (((_p = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _p === void 0 ? void 0 : _p.network_type) && { network_type: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_type, })), (((_q = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _q === void 0 ? void 0 : _q.network_rtt) && { network_rtt: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_rtt, })), (((_r = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _r === void 0 ? void 0 : _r.network_downlink) && { network_downlink: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_downlink, })), (((_s = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _s === void 0 ? void 0 : _s.user_id) && !isUUID((_t = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _t === void 0 ? void 0 : _t.user_id) && { user_id: (_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.user_id, })), ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && { anonymous_id: _rudderstack.getAnonymousId(), })), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_currency) && { account_currency: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_currency, })), (((_w = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _w === void 0 ? void 0 : _w.account_mode) && { account_mode: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_mode, })); growthbookOptions !== null && growthbookOptions !== void 0 ? growthbookOptions : (growthbookOptions = {}); (_x = growthbookOptions.attributes) !== null && _x !== void 0 ? _x : (growthbookOptions.attributes = {}); (_y = (_0 = growthbookOptions.attributes).id) !== null && _y !== void 0 ? _y : (_0.id = _rudderstack.getAnonymousId()); (_z = (_1 = growthbookOptions.attributes).country) !== null && _z !== void 0 ? _z : (_1.country = country); if (growthbookKey) { _growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions); interval_1 = setInterval(function () { if (Object.keys(tracking_config).length > 0) clearInterval(interval_1); else tracking_config = getFeatureValue('tracking-buttons-config', {}); }, 1000); } return [3 /*break*/, 4]; case 3: error_1 = _2.sent(); console.log('Error in initializing analytics', error_1); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; var setAttributes = function (_a) { var country = _a.country, user_language = _a.user_language, device_language = _a.device_language, device_type = _a.device_type, account_type = _a.account_type, user_id = _a.user_id, anonymous_id = _a.anonymous_id, app_id = _a.app_id, utm_source = _a.utm_source, utm_medium = _a.utm_medium, utm_campaign = _a.utm_campaign, is_authorised = _a.is_authorised, residence_country = _a.residence_country, url = _a.url, domain = _a.domain, geo_location = _a.geo_location, loggedIn = _a.loggedIn, network_downlink = _a.network_downlink, network_rtt = _a.network_rtt, network_type = _a.network_type, account_currency = _a.account_currency, account_mode = _a.account_mode; if (!_rudderstack) return; var user_identity = user_id !== null && user_id !== void 0 ? user_id : getId(); // Check if we have Growthbook instance and update its attributes if (_growthbook) { var config = __assign(__assign({ country: country, residence_country: residence_country, user_language: user_language, device_language: device_language, device_type: device_type, utm_source: utm_source, utm_medium: utm_medium, utm_campaign: utm_campaign, is_authorised: is_authorised, url: url, domain: domain, loggedIn: loggedIn }, (user_id && !isUUID(user_id) && { user_id: user_id })), { anonymous_id: anonymous_id }); if (user_identity) { config.id = user_identity; config.user_id = user_identity; } _growthbook.setAttributes(config); } core_data = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, core_data), (country !== undefined && { country: country })), (geo_location !== undefined && { geo_location: geo_location })), (user_language !== undefined && { user_language: user_language })), (account_type !== undefined && { account_type: account_type })), (app_id !== undefined && { app_id: app_id })), (residence_country !== undefined && { residence_country: residence_country })), (device_type !== undefined && { device_type: device_type })), (url !== undefined && { url: url })), (loggedIn !== undefined && { loggedIn: loggedIn })), (network_downlink !== undefined && { network_downlink: network_downlink })), (network_rtt !== undefined && { network_rtt: network_rtt })), (network_type !== undefined && { network_type: network_type })), (user_id !== undefined && !isUUID(user_id) && { user_id: user_id })), (anonymous_id !== undefined && { anonymous_id: anonymous_id })), (account_currency !== undefined && { account_currency: account_currency })), (account_mode !== undefined && { account_mode: account_mode })); }; var getFeatureState = function (id) { var _a, _b; return (_b = (_a = _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureState(id)) === null || _a === void 0 ? void 0 : _a.experimentResult) === null || _b === void 0 ? void 0 : _b.name; }; var getFeatureValue = function (id, defaultValue) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureValue(id, defaultValue); }; var getGrowthbookStatus = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (_growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getStatus())]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; var isFeatureOn = function (key) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.isOn(key); }; var setUrl = function (href) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.setUrl(href); }; var getId = function () { var userId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || ''; return userId && !isUUID(userId) ? userId : ''; }; /** * Pushes page view event to Rudderstack * * @param curret_page The name or URL of the current page to track the page view event */ var pageView = function (current_page, platform, properties) { if (platform === void 0) { platform = 'Deriv App'; } if (!_rudderstack) return; var userId = getId(); _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, userId, properties); }; var identifyEvent = function (user_id) { var stored_user_id = user_id || getId(); if ((_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.has_initialized) && stored_user_id && !isUUID(stored_user_id)) { _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(stored_user_id, { language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en', }); return; } if (stored_user_id && !isUUID(stored_user_id)) { _pending_identify_calls.push(stored_user_id); } }; var reset = function () { if (!_rudderstack) return; _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.reset(); }; var trackEvent = function (event, analytics_data) { if (navigator.onLine && _rudderstack) { if (event_cache.length > 0) { event_cache.forEach(function (cache, index) { _rudderstack.track(cache.event, cache.payload); delete event_cache[index]; }); } var userId = getId(); var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId })); if (event in tracking_config) { tracking_config[event] && (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload)); } else { _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload); } } else { var userId = getId(); var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId })); event_cache.push({ event: event, payload: payload }); } }; var getInstances = function () { return ({ ab: _growthbook, tracking: _rudderstack }); }; var AnalyticsInstance = { initialise: initialise, setAttributes: setAttributes, identifyEvent: identifyEvent, getFeatureState: getFeatureState, getFeatureValue: getFeatureValue, getGrowthbookStatus: getGrowthbookStatus, isFeatureOn: isFeatureOn, setUrl: setUrl, getId: getId, trackEvent: trackEvent, getInstances: getInstances, pageView: pageView, reset: reset, }; if (typeof window !== 'undefined') { window.AnalyticsInstance = AnalyticsInstance; } return AnalyticsInstance; } exports.createAnalyticsInstance = createAnalyticsInstance; exports.Analytics = createAnalyticsInstance(); var isUUID = function (str) { var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; return uuidRegex.test(str); };