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.

183 lines (182 loc) 8.84 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RudderStack = void 0; var analytics_js_1 = require("@rudderstack/analytics-js"); var uuid_1 = require("uuid"); var js_cookie_1 = __importDefault(require("js-cookie")); var RudderStack = /** @class */ (function () { function RudderStack(RUDDERSTACK_KEY, disableAMD, onLoaded) { if (disableAMD === void 0) { disableAMD = false; } var _this = this; this.analytics = new analytics_js_1.RudderAnalytics(); this.has_identified = false; this.has_initialized = false; this.current_page = ''; this.rudderstack_anonymous_cookie_key = 'rudder_anonymous_id'; this.getAnonymousId = function () { var _a; return (_a = document.cookie.match('(^|;)\\s*' + _this.rudderstack_anonymous_cookie_key + '\\s*=\\s*([^;]+)')) === null || _a === void 0 ? void 0 : _a.pop(); }; this.setCookieIfNotExists = function () { var anonymous_id = _this.getAnonymousId(); if (!anonymous_id) { var hostname_1 = window.location.hostname; // List of external domains where we should use the full hostname var external_domains = ['webflow.io']; // Check if the hostname ends with any of the external domains var is_external_domain = external_domains.some(function (domain) { return hostname_1.endsWith(domain); }); // If it's an external domain, use the full hostname, otherwise use the last two parts var domain_name = is_external_domain ? hostname_1 : hostname_1.split('.').slice(-2).join('.'); // Set cookie to expire in 2 years document.cookie = "".concat(_this.rudderstack_anonymous_cookie_key, "=").concat((0, uuid_1.v6)(), "; path=/; Domain=").concat(domain_name, "; max-age=").concat(2 * 365 * 24 * 60 * 60); } }; /** * @returns The user ID that was assigned to the user after calling identify event */ this.getUserId = function () { return _this.analytics.getUserId(); }; /** For caching mechanism, Rudderstack SDK, first page load */ this.handleCachedEvents = function () { var allowedDomains = ['deriv.com', 'deriv.team', 'deriv.ae']; var domain = allowedDomains.some(function (d) { return window.location.hostname.includes(d); }) ? ".".concat(allowedDomains.find(function (d) { return window.location.hostname.includes(d); })) : ".".concat(allowedDomains[0]); var storedEventsString = js_cookie_1.default.get('cached_analytics_events'); var storedPagesString = js_cookie_1.default.get('cached_analytics_page_views'); try { // Handle cached analytics events if (storedEventsString) { var storedEvents = JSON.parse(storedEventsString); if (Array.isArray(storedEvents) && storedEvents.length > 0) { storedEvents.forEach(function (event) { _this.analytics.track(event.name, event.properties); }); // Clear the stored events cookie js_cookie_1.default.remove('cached_analytics_events', { domain: domain }); } } // Handle cached page views if (storedPagesString) { var storedPages = JSON.parse(storedPagesString); if (Array.isArray(storedPages) && storedPages.length > 0) { storedPages.forEach(function (page) { _this.analytics.page(page === null || page === void 0 ? void 0 : page.name, page === null || page === void 0 ? void 0 : page.properties); }); // Clear the stored page views cookie js_cookie_1.default.remove('cached_analytics_page_views', { domain: domain }); } } } catch (error) { // eslint-disable-next-line no-console console.log(error); } }; /** * Initializes the Rudderstack SDK. Ensure that the appropriate environment variables are set before this is called. * For local/staging environment, ensure that `RUDDERSTACK_STAGING_KEY` and `RUDDERSTACK_URL` is set. * For production environment, ensure that `RUDDERSTACK_PRODUCTION_KEY` and `RUDDERSTACK_URL` is set. */ this.init = function (RUDDERSTACK_KEY, disableAMD) { if (disableAMD === void 0) { disableAMD = false; } if (RUDDERSTACK_KEY) { var _define_1; if (disableAMD) { _define_1 = window.define; window.define = undefined; } _this.setCookieIfNotExists(); _this.analytics.load(RUDDERSTACK_KEY, 'https://deriv-dataplane.rudderstack.com', { externalAnonymousIdCookieName: _this.rudderstack_anonymous_cookie_key, onLoaded: function () { var _a; if (disableAMD) { window.define = _define_1; } _this.has_initialized = true; _this.has_identified = !!_this.getUserId(); _this.handleCachedEvents(); (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this); }, }); } }; /** * * @param user_id The user ID of the user to identify and associate all events with that particular user ID * @param payload Additional information passed to identify the user */ this.identifyEvent = function (user_id, payload) { var currentUserId = _this.getUserId(); if (!currentUserId) { _this.analytics.identify(user_id, payload); } _this.has_identified = true; }; /** * Pushes page view event to Rudderstack * * @param curret_page The name or URL of the current page to track the page view event */ this.pageView = function (current_page, platform, user_id, properties) { if (platform === void 0) { platform = 'Deriv App'; } if (_this.has_initialized && current_page !== _this.current_page) { var pageProperties = user_id ? __assign({ user_id: user_id }, properties) : properties; _this.analytics.page(platform, current_page, pageProperties); _this.current_page = current_page; } }; /** * Pushes reset event to rudderstack */ this.reset = function () { if (_this.has_initialized) { _this.analytics.reset(); _this.has_identified = false; } }; /** * Pushes track events to Rudderstack. */ this.track = function (event, payload) { var clean_payload = Object.fromEntries(Object.entries(payload).filter(function (_a) { var _ = _a[0], value = _a[1]; return value !== undefined; })); if (_this.has_initialized) { try { _this.analytics.track(event, clean_payload); } catch (err) { console.error(err); } } }; this.onLoadedCallback = onLoaded; this.init(RUDDERSTACK_KEY, disableAMD); } RudderStack.getRudderStackInstance = function (RUDDERSTACK_KEY, disableAMD, onLoaded) { if (disableAMD === void 0) { disableAMD = false; } if (!RudderStack._instance) { RudderStack._instance = new RudderStack(RUDDERSTACK_KEY, disableAMD, onLoaded); return RudderStack._instance; } return RudderStack._instance; }; return RudderStack; }()); exports.RudderStack = RudderStack;