@deriv-com/analytics 1.31.1 → 1.31.3

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
@@ -85,18 +85,18 @@ 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
102
  _rudderstack = rudderstack_1.RudderStack.getRudderStackInstance(rudderstackKey, disableRudderstackAMD);
@@ -119,19 +119,20 @@ function createAnalyticsInstance(options) {
119
119
  network_rtt: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.network_rtt,
120
120
  })), (((_r = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _r === void 0 ? void 0 : _r.network_downlink) && {
121
121
  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,
122
+ })), (((_s = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _s === void 0 ? void 0 : _s.user_id) &&
123
+ !isUUID((_t = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _t === void 0 ? void 0 : _t.user_id) && {
124
+ user_id: (_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.user_id,
124
125
  })), ((growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) && {
125
126
  anonymous_id: _rudderstack.getAnonymousId(),
126
- })), (((_u = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _u === void 0 ? void 0 : _u.account_currency) && {
127
+ })), (((_v = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _v === void 0 ? void 0 : _v.account_currency) && {
127
128
  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) && {
129
+ })), (((_w = growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes) === null || _w === void 0 ? void 0 : _w.account_mode) && {
129
130
  account_mode: growthbookOptions === null || growthbookOptions === void 0 ? void 0 : growthbookOptions.attributes.account_mode,
130
131
  }));
131
132
  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);
133
+ (_x = growthbookOptions.attributes) !== null && _x !== void 0 ? _x : (growthbookOptions.attributes = {});
134
+ (_y = (_0 = growthbookOptions.attributes).id) !== null && _y !== void 0 ? _y : (_0.id = _rudderstack.getAnonymousId());
135
+ (_z = (_1 = growthbookOptions.attributes).country) !== null && _z !== void 0 ? _z : (_1.country = country);
135
136
  if (growthbookKey) {
136
137
  _growthbook = growthbook_1.Growthbook.getGrowthBookInstance(growthbookKey, growthbookDecryptionKey, growthbookOptions);
137
138
  interval_1 = setInterval(function () {
@@ -143,7 +144,7 @@ function createAnalyticsInstance(options) {
143
144
  }
144
145
  return [3 /*break*/, 4];
145
146
  case 3:
146
- error_1 = _1.sent();
147
+ error_1 = _2.sent();
147
148
  console.log('Error in initializing analytics', error_1);
148
149
  return [3 /*break*/, 4];
149
150
  case 4: return [2 /*return*/];
@@ -158,29 +159,14 @@ function createAnalyticsInstance(options) {
158
159
  var user_identity = user_id !== null && user_id !== void 0 ? user_id : getId();
159
160
  // Check if we have Growthbook instance
160
161
  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
- };
162
+ 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
163
  if (user_identity) {
178
164
  config.id = user_identity;
179
165
  config.user_id = user_identity;
180
166
  }
181
167
  _growthbook.setAttributes(config);
182
168
  }
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 }));
169
+ 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
170
  };
185
171
  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
172
  var getFeatureValue = function (id, defaultValue) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.getFeatureValue(id, defaultValue); };
@@ -192,7 +178,10 @@ function createAnalyticsInstance(options) {
192
178
  }); }); };
193
179
  var isFeatureOn = function (key) { return _growthbook === null || _growthbook === void 0 ? void 0 : _growthbook.isOn(key); };
194
180
  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()) || ''; };
181
+ var getId = function () {
182
+ var userId = (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.getUserId()) || '';
183
+ return userId && !isUUID(userId) ? userId : '';
184
+ };
196
185
  /**
197
186
  * Pushes page view event to Rudderstack
198
187
  *
@@ -202,11 +191,12 @@ function createAnalyticsInstance(options) {
202
191
  if (platform === void 0) { platform = 'Deriv App'; }
203
192
  if (!_rudderstack)
204
193
  return;
205
- _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, getId(), properties);
194
+ var userId = getId();
195
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.pageView(current_page, platform, userId, properties);
206
196
  };
207
197
  var identifyEvent = function (user_id) {
208
198
  var stored_user_id = user_id || getId();
209
- if (_rudderstack) {
199
+ if (_rudderstack && stored_user_id && !isUUID(stored_user_id)) {
210
200
  _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' });
211
201
  }
212
202
  };
@@ -223,14 +213,19 @@ function createAnalyticsInstance(options) {
223
213
  delete event_cache[index];
224
214
  });
225
215
  }
216
+ var userId = getId();
217
+ var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
226
218
  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)));
219
+ tracking_config[event] && (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload));
220
+ }
221
+ else {
222
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload);
228
223
  }
229
- else
230
- _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, __assign(__assign({}, core_data), analytics_data));
231
224
  }
232
225
  else {
233
- event_cache.push({ event: event, payload: __assign(__assign({}, core_data), analytics_data) });
226
+ var userId = getId();
227
+ var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
228
+ event_cache.push({ event: event, payload: payload });
234
229
  }
235
230
  };
236
231
  var getInstances = function () { return ({ ab: _growthbook, tracking: _rudderstack }); };
@@ -256,3 +251,7 @@ function createAnalyticsInstance(options) {
256
251
  }
257
252
  exports.createAnalyticsInstance = createAnalyticsInstance;
258
253
  exports.Analytics = createAnalyticsInstance();
254
+ var isUUID = function (str) {
255
+ var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
256
+ return uuidRegex.test(str);
257
+ };
@@ -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
  };
@@ -23,7 +34,13 @@ var RudderStack = /** @class */ (function () {
23
34
  this.setCookieIfNotExists = function () {
24
35
  var anonymous_id = _this.getAnonymousId();
25
36
  if (!anonymous_id) {
26
- var domain_name = window.location.hostname.split('.').slice(-2).join('.');
37
+ var hostname_1 = window.location.hostname;
38
+ // List of external domains where we should use the full hostname
39
+ var external_domains = ['webflow.io'];
40
+ // Check if the hostname ends with any of the external domains
41
+ var is_external_domain = external_domains.some(function (domain) { return hostname_1.endsWith(domain); });
42
+ // If it's an external domain, use the full hostname, otherwise use the last two parts
43
+ var domain_name = is_external_domain ? hostname_1 : hostname_1.split('.').slice(-2).join('.');
27
44
  // Set cookie to expire in 2 years
28
45
  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);
29
46
  }
@@ -113,7 +130,8 @@ var RudderStack = /** @class */ (function () {
113
130
  this.pageView = function (current_page, platform, user_id, properties) {
114
131
  if (platform === void 0) { platform = 'Deriv App'; }
115
132
  if (_this.has_initialized && _this.has_identified && current_page !== _this.current_page) {
116
- _this.analytics.page(platform, current_page, { user_id: user_id }, properties);
133
+ var pageProperties = user_id ? __assign({ user_id: user_id }, properties) : properties;
134
+ _this.analytics.page(platform, current_page, pageProperties);
117
135
  _this.current_page = current_page;
118
136
  }
119
137
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deriv-com/analytics",
3
- "version": "1.31.1",
3
+ "version": "1.31.3",
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",