@deriv-com/analytics 1.31.2 → 1.31.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/analytics.js CHANGED
@@ -57,7 +57,7 @@ var js_cookie_1 = __importDefault(require("js-cookie"));
57
57
  var utils_1 = require("@deriv-com/utils");
58
58
  function createAnalyticsInstance(options) {
59
59
  var _this = this;
60
- var _growthbook, _rudderstack, core_data = {}, tracking_config = {}, event_cache = [];
60
+ var _growthbook, _rudderstack, core_data = {}, tracking_config = {}, event_cache = [], _pending_identify_calls = [];
61
61
  var getClientCountry = function () { return __awaiter(_this, void 0, void 0, function () {
62
62
  var countryFromCloudflare, countryFromCookie, websiteStatus, countryFromWebsiteStatus;
63
63
  var _a;
@@ -85,21 +85,30 @@ function createAnalyticsInstance(options) {
85
85
  var growthbookKey = _a.growthbookKey, growthbookDecryptionKey = _a.growthbookDecryptionKey, rudderstackKey = _a.rudderstackKey, growthbookOptions = _a.growthbookOptions, _b = _a.disableRudderstackAMD, disableRudderstackAMD = _b === void 0 ? false : _b;
86
86
  return __awaiter(_this, void 0, void 0, function () {
87
87
  var country, _c, interval_1, error_1;
88
- var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
89
- var _z, _0;
90
- return __generator(this, function (_1) {
91
- switch (_1.label) {
88
+ var _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
89
+ var _0, _1;
90
+ return __generator(this, function (_2) {
91
+ switch (_2.label) {
92
92
  case 0:
93
- _1.trys.push([0, 3, , 4]);
93
+ _2.trys.push([0, 3, , 4]);
94
94
  _c = ((_d = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _d === void 0 ? void 0 : _d.country);
95
95
  if (_c) return [3 /*break*/, 2];
96
96
  return [4 /*yield*/, getClientCountry()];
97
97
  case 1:
98
- _c = (_1.sent());
99
- _1.label = 2;
98
+ _c = (_2.sent());
99
+ _2.label = 2;
100
100
  case 2:
101
101
  country = _c;
102
- _rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey, disableRudderstackAMD);
102
+ _rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey, disableRudderstackAMD, function () {
103
+ _pending_identify_calls.forEach(function (userId) {
104
+ if (userId && !isUUID(userId)) {
105
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(userId, {
106
+ language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en',
107
+ });
108
+ }
109
+ });
110
+ _pending_identify_calls = [];
111
+ });
103
112
  if ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && Object.keys(growthbookOptions.attributes).length > 0)
104
113
  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) && {
105
114
  user_language: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.user_language,
@@ -119,19 +128,20 @@ function createAnalyticsInstance(options) {
119
128
  network_rtt: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_rtt,
120
129
  })), (((_r = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _r === void 0 ? void 0 : _r.network_downlink) && {
121
130
  network_downlink: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_downlink,
122
- })), (((_s = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _s === void 0 ? void 0 : _s.user_id) && {
123
- user_id: (_t = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _t === void 0 ? void 0 : _t.user_id,
131
+ })), (((_s = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _s === void 0 ? void 0 : _s.user_id) &&
132
+ !isUUID((_t = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _t === void 0 ? void 0 : _t.user_id) && {
133
+ user_id: (_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.user_id,
124
134
  })), ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && {
125
135
  anonymous_id: _rudderstack.getAnonymousId(),
126
- })), (((_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.account_currency) && {
136
+ })), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_currency) && {
127
137
  account_currency: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_currency,
128
- })), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_mode) && {
138
+ })), (((_w = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _w === void 0 ? void 0 : _w.account_mode) && {
129
139
  account_mode: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_mode,
130
140
  }));
131
141
  growthbookOptions !== null && growthbookOptions !== void 0 ? growthbookOptions : (growthbookOptions = {});
132
- (_w = growthbookOptions.attributes) !== null && _w !== void 0 ? _w : (growthbookOptions.attributes = {});
133
- (_x = (_z = growthbookOptions.attributes).id) !== null && _x !== void 0 ? _x : (_z.id = _rudderstack.getAnonymousId());
134
- (_y = (_0 = growthbookOptions.attributes).country) !== null && _y !== void 0 ? _y : (_0.country = country);
142
+ (_x = growthbookOptions.attributes) !== null && _x !== void 0 ? _x : (growthbookOptions.attributes = {});
143
+ (_y = (_0 = growthbookOptions.attributes).id) !== null && _y !== void 0 ? _y : (_0.id = _rudderstack.getAnonymousId());
144
+ (_z = (_1 = growthbookOptions.attributes).country) !== null && _z !== void 0 ? _z : (_1.country = country);
135
145
  if (growthbookKey) {
136
146
  _growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions);
137
147
  interval_1 = setInterval(function () {
@@ -143,7 +153,7 @@ function createAnalyticsInstance(options) {
143
153
  }
144
154
  return [3 /*break*/, 4];
145
155
  case 3:
146
- error_1 = _1.sent();
156
+ error_1 = _2.sent();
147
157
  console.log('Error in initializing analytics', error_1);
148
158
  return [3 /*break*/, 4];
149
159
  case 4: return [2 /*return*/];
@@ -158,29 +168,14 @@ function createAnalyticsInstance(options) {
158
168
  var user_identity = user_id !== null && user_id !== void 0 ? user_id : getId();
159
169
  // Check if we have Growthbook instance
160
170
  if (_growthbook) {
161
- var config = {
162
- country: country,
163
- residence_country: residence_country,
164
- user_language: user_language,
165
- device_language: device_language,
166
- device_type: device_type,
167
- utm_source: utm_source,
168
- utm_medium: utm_medium,
169
- utm_campaign: utm_campaign,
170
- is_authorised: is_authorised,
171
- url: url,
172
- domain: domain,
173
- loggedIn: loggedIn,
174
- user_id: user_id,
175
- anonymous_id: anonymous_id,
176
- };
171
+ 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 });
177
172
  if (user_identity) {
178
173
  config.id = user_identity;
179
174
  config.user_id = user_identity;
180
175
  }
181
176
  _growthbook.setAttributes(config);
182
177
  }
183
- 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 && { 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 }));
178
+ 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 }));
184
179
  };
185
180
  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; };
186
181
  var getFeatureValue = function (id, defaultValue) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureValue(id, defaultValue); };
@@ -192,7 +187,10 @@ function createAnalyticsInstance(options) {
192
187
  }); }); };
193
188
  var isFeatureOn = function (key) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.isOn(key); };
194
189
  var setUrl = function (href) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.setUrl(href); };
195
- var getId = function () { return (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || ''; };
190
+ var getId = function () {
191
+ var userId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || '';
192
+ return userId && !isUUID(userId) ? userId : '';
193
+ };
196
194
  /**
197
195
  * Pushes page view event to Rudderstack
198
196
  *
@@ -202,12 +200,19 @@ function createAnalyticsInstance(options) {
202
200
  if (platform === void 0) { platform = 'Deriv App'; }
203
201
  if (!_rudderstack)
204
202
  return;
205
- _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, getId(), properties);
203
+ var userId = getId();
204
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, userId, properties);
206
205
  };
207
206
  var identifyEvent = function (user_id) {
208
207
  var stored_user_id = user_id || getId();
209
- if (_rudderstack) {
210
- _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' });
208
+ if ((_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.has_initialized) && stored_user_id && !isUUID(stored_user_id)) {
209
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.identifyEvent(stored_user_id, {
210
+ language: (core_data === null || core_data === void 0 ? void 0 : core_data.user_language) || 'en',
211
+ });
212
+ return;
213
+ }
214
+ if (stored_user_id && !isUUID(stored_user_id)) {
215
+ _pending_identify_calls.push(stored_user_id);
211
216
  }
212
217
  };
213
218
  var reset = function () {
@@ -223,14 +228,19 @@ function createAnalyticsInstance(options) {
223
228
  delete event_cache[index];
224
229
  });
225
230
  }
231
+ var userId = getId();
232
+ var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
226
233
  if (event in tracking_config) {
227
- tracking_config[event] && (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, __assign(__assign({}, core_data), analytics_data)));
234
+ tracking_config[event] && (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload));
235
+ }
236
+ else {
237
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload);
228
238
  }
229
- else
230
- _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, __assign(__assign({}, core_data), analytics_data));
231
239
  }
232
240
  else {
233
- event_cache.push({ event: event, payload: __assign(__assign({}, core_data), analytics_data) });
241
+ var userId = getId();
242
+ var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
243
+ event_cache.push({ event: event, payload: payload });
234
244
  }
235
245
  };
236
246
  var getInstances = function () { return ({ ab: _growthbook, tracking: _rudderstack }); };
@@ -256,3 +266,7 @@ function createAnalyticsInstance(options) {
256
266
  }
257
267
  exports.createAnalyticsInstance = createAnalyticsInstance;
258
268
  exports.Analytics = createAnalyticsInstance();
269
+ var isUUID = function (str) {
270
+ var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
271
+ return uuidRegex.test(str);
272
+ };
@@ -7,8 +7,9 @@ export declare class RudderStack {
7
7
  current_page: string;
8
8
  rudderstack_anonymous_cookie_key: string;
9
9
  private static _instance;
10
- constructor(RUDDERSTACK_KEY: string, disableAMD?: boolean);
11
- static getRudderStackInstance: (RUDDERSTACK_KEY: string, disableAMD?: boolean) => RudderStack;
10
+ private onLoadedCallback?;
11
+ constructor(RUDDERSTACK_KEY: string, disableAMD?: boolean, onLoaded?: () => void);
12
+ static getRudderStackInstance: (RUDDERSTACK_KEY: string, disableAMD?: boolean, onLoaded?: () => void) => RudderStack;
12
13
  getAnonymousId: () => string | undefined;
13
14
  setCookieIfNotExists: () => void;
14
15
  /**
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
15
  };
@@ -8,7 +19,7 @@ var analytics_js_1 = require("@rudderstack/analytics-js");
8
19
  var uuid_1 = require("uuid");
9
20
  var js_cookie_1 = __importDefault(require("js-cookie"));
10
21
  var RudderStack = /** @class */ (function () {
11
- function RudderStack(RUDDERSTACK_KEY, disableAMD) {
22
+ function RudderStack(RUDDERSTACK_KEY, disableAMD, onLoaded) {
12
23
  if (disableAMD === void 0) { disableAMD = false; }
13
24
  var _this = this;
14
25
  this.analytics = new analytics_js_1.RudderAnalytics();
@@ -91,14 +102,16 @@ var RudderStack = /** @class */ (function () {
91
102
  _this.setCookieIfNotExists();
92
103
  _this.analytics.load(RUDDERSTACK_KEY, 'https://deriv-dataplane.rudderstack.com', {
93
104
  externalAnonymousIdCookieName: _this.rudderstack_anonymous_cookie_key,
94
- });
95
- _this.analytics.ready(function () {
96
- if (disableAMD) {
97
- window.define = _define_1;
98
- }
99
- _this.has_initialized = true;
100
- _this.has_identified = !!(_this.getUserId() || _this.getAnonymousId());
101
- _this.handleCachedEvents();
105
+ onLoaded: function () {
106
+ var _a;
107
+ if (disableAMD) {
108
+ window.define = _define_1;
109
+ }
110
+ _this.has_initialized = true;
111
+ _this.has_identified = !!(_this.getUserId() || _this.getAnonymousId());
112
+ _this.handleCachedEvents();
113
+ (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
114
+ },
102
115
  });
103
116
  }
104
117
  };
@@ -119,7 +132,8 @@ var RudderStack = /** @class */ (function () {
119
132
  this.pageView = function (current_page, platform, user_id, properties) {
120
133
  if (platform === void 0) { platform = 'Deriv App'; }
121
134
  if (_this.has_initialized && _this.has_identified && current_page !== _this.current_page) {
122
- _this.analytics.page(platform, current_page, { user_id: user_id }, properties);
135
+ var pageProperties = user_id ? __assign({ user_id: user_id }, properties) : properties;
136
+ _this.analytics.page(platform, current_page, pageProperties);
123
137
  _this.current_page = current_page;
124
138
  }
125
139
  };
@@ -153,12 +167,13 @@ var RudderStack = /** @class */ (function () {
153
167
  }
154
168
  }
155
169
  };
170
+ this.onLoadedCallback = onLoaded;
156
171
  this.init(RUDDERSTACK_KEY, disableAMD);
157
172
  }
158
- RudderStack.getRudderStackInstance = function (RUDDERSTACK_KEY, disableAMD) {
173
+ RudderStack.getRudderStackInstance = function (RUDDERSTACK_KEY, disableAMD, onLoaded) {
159
174
  if (disableAMD === void 0) { disableAMD = false; }
160
175
  if (!RudderStack._instance) {
161
- RudderStack._instance = new RudderStack(RUDDERSTACK_KEY, disableAMD);
176
+ RudderStack._instance = new RudderStack(RUDDERSTACK_KEY, disableAMD, onLoaded);
162
177
  return RudderStack._instance;
163
178
  }
164
179
  return RudderStack._instance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deriv-com/analytics",
3
- "version": "1.31.2",
3
+ "version": "1.31.4",
4
4
  "description": "The analytics package contains all the utility functions used for tracking user events and sending them to the respective platform such as Rudderstack.",
5
5
  "keywords": [
6
6
  "rudderstack",