@deriv-com/analytics 1.34.0 → 1.35.1

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/posthog.js CHANGED
@@ -17,15 +17,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.PostHogAnalytics = void 0;
18
18
  var posthog_js_1 = __importDefault(require("posthog-js"));
19
19
  var uuid_1 = require("uuid");
20
- var js_cookie_1 = __importDefault(require("js-cookie"));
21
20
  var PostHogAnalytics = /** @class */ (function () {
22
- function PostHogAnalytics(POSTHOG_KEY, POSTHOG_HOST, disableAMD, onLoaded, config) {
23
- if (disableAMD === void 0) { disableAMD = false; }
21
+ function PostHogAnalytics(POSTHOG_KEY, POSTHOG_HOST, onLoaded, config) {
24
22
  var _this = this;
25
23
  this.has_identified = false;
26
24
  this.has_initialized = false;
27
25
  this.current_page = '';
28
- // Share the same anonymous ID cookie with RudderStack
29
26
  this.rudderstack_anonymous_cookie_key = 'rudder_anonymous_id';
30
27
  this.getAnonymousId = function () {
31
28
  var _a;
@@ -35,178 +32,78 @@ var PostHogAnalytics = /** @class */ (function () {
35
32
  var anonymous_id = _this.getAnonymousId();
36
33
  if (!anonymous_id) {
37
34
  var hostname_1 = window.location.hostname;
38
- // List of external domains where we should use the full hostname
39
35
  var external_domains = ['webflow.io'];
40
- // Check if the hostname ends with any of the external domains
41
36
  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
37
  var domain_name = is_external_domain ? hostname_1 : hostname_1.split('.').slice(-2).join('.');
44
- // Set cookie to expire in 2 years
45
38
  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);
46
39
  }
47
40
  };
48
- /**
49
- * @returns The user ID that was assigned to the user after calling identify event
50
- */
51
41
  this.getUserId = function () { return posthog_js_1.default.get_distinct_id(); };
52
- /** For caching mechanism, PostHog SDK, first page load */
53
- this.handleCachedEvents = function () {
54
- var allowedDomains = ['deriv.com', 'deriv.team', 'deriv.ae'];
55
- var domain = allowedDomains.some(function (d) { return window.location.hostname.includes(d); })
56
- ? ".".concat(allowedDomains.find(function (d) { return window.location.hostname.includes(d); }))
57
- : ".".concat(allowedDomains[0]);
58
- var storedEventsString = js_cookie_1.default.get('cached_analytics_events');
59
- var storedPagesString = js_cookie_1.default.get('cached_analytics_page_views');
60
- try {
61
- // Handle cached analytics events
62
- if (storedEventsString) {
63
- var storedEvents = JSON.parse(storedEventsString);
64
- if (Array.isArray(storedEvents) && storedEvents.length > 0) {
65
- storedEvents.forEach(function (event) {
66
- posthog_js_1.default.capture(event.name, event.properties);
67
- });
68
- // Clear the stored events cookie
69
- js_cookie_1.default.remove('cached_analytics_events', { domain: domain });
42
+ this.transformToPostHogPayload = function (payload, maxDepth) {
43
+ if (maxDepth === void 0) { maxDepth = 10; }
44
+ var transformed = {};
45
+ var flatten = function (obj, depth) {
46
+ if (depth === void 0) { depth = 0; }
47
+ if (depth > maxDepth)
48
+ return;
49
+ if (obj === null || obj === undefined)
50
+ return;
51
+ Object.keys(obj).forEach(function (key) {
52
+ var value = obj[key];
53
+ if (Array.isArray(value)) {
54
+ transformed[key] = value;
70
55
  }
71
- }
72
- // Handle cached page views
73
- if (storedPagesString) {
74
- var storedPages = JSON.parse(storedPagesString);
75
- if (Array.isArray(storedPages) && storedPages.length > 0) {
76
- storedPages.forEach(function (page) {
77
- posthog_js_1.default.capture('$pageview', __assign({ $current_url: window.location.href, page_name: page === null || page === void 0 ? void 0 : page.name }, page === null || page === void 0 ? void 0 : page.properties));
78
- });
79
- // Clear the stored page views cookie
80
- js_cookie_1.default.remove('cached_analytics_page_views', { domain: domain });
56
+ else if (typeof value === 'object' && value !== null) {
57
+ flatten(value, depth + 1);
81
58
  }
82
- }
83
- }
84
- catch (error) {
85
- // eslint-disable-next-line no-console
86
- console.log(error);
87
- }
88
- };
89
- /**
90
- * Transform V2 event payload to PostHog flat structure
91
- */
92
- this.transformToPostHogPayload = function (payload) {
93
- var transformed = {};
94
- // Flatten event_metadata
95
- if (payload.event_metadata) {
96
- Object.keys(payload.event_metadata).forEach(function (key) {
97
- // Handle nested marketing_data within event_metadata
98
- if (key === 'marketing_data' && typeof payload.event_metadata[key] === 'object') {
99
- var marketingData_1 = payload.event_metadata[key];
100
- Object.keys(marketingData_1).forEach(function (marketingKey) {
101
- // Flatten utm_data within marketing_data
102
- if (marketingKey === 'utm_data') {
103
- try {
104
- // Parse utm_data if it's a JSON string
105
- var utmData_1 = typeof marketingData_1[marketingKey] === 'string'
106
- ? JSON.parse(marketingData_1[marketingKey])
107
- : marketingData_1[marketingKey];
108
- // Flatten utm_data properties to root level
109
- if (typeof utmData_1 === 'object' && utmData_1 !== null) {
110
- Object.keys(utmData_1).forEach(function (utmKey) {
111
- transformed[utmKey] = utmData_1[utmKey];
112
- });
113
- }
59
+ else if (typeof value === 'string') {
60
+ var trimmed = value.trim();
61
+ if ((trimmed.startsWith('{') && trimmed.endsWith('}')) ||
62
+ (trimmed.startsWith('[') && trimmed.endsWith(']'))) {
63
+ try {
64
+ var parsed = JSON.parse(trimmed);
65
+ if (typeof parsed === 'object' && parsed !== null) {
66
+ flatten(parsed, depth + 1);
114
67
  }
115
- catch (error) {
116
- // If parsing fails, add the raw value
117
- console.error('Failed to parse utm_data:', error);
118
- transformed[marketingKey] = marketingData_1[marketingKey];
68
+ else {
69
+ transformed[key] = value;
119
70
  }
120
71
  }
121
- else {
122
- // Add other marketing_data properties directly
123
- transformed[marketingKey] = marketingData_1[marketingKey];
72
+ catch (_a) {
73
+ transformed[key] = value;
124
74
  }
125
- });
75
+ }
76
+ else {
77
+ transformed[key] = value;
78
+ }
126
79
  }
127
80
  else {
128
- transformed[key] = payload.event_metadata[key];
81
+ transformed[key] = value;
129
82
  }
130
83
  });
131
- }
132
- // Flatten cta_information
133
- if (payload.cta_information) {
134
- Object.keys(payload.cta_information).forEach(function (key) {
135
- transformed[key] = payload.cta_information[key];
136
- });
137
- }
138
- // Flatten error
139
- if (payload.error) {
140
- Object.keys(payload.error).forEach(function (key) {
141
- transformed[key] = payload.error[key];
142
- });
143
- }
144
- // Add top-level properties (excluding nested objects)
145
- Object.keys(payload).forEach(function (key) {
146
- if (!['event_metadata', 'cta_information', 'error'].includes(key)) {
147
- transformed[key] = payload[key];
148
- }
149
- });
150
- return transformed;
84
+ };
85
+ flatten(payload);
86
+ return Object.fromEntries(Object.entries(transformed).filter(function (_a) {
87
+ var _ = _a[0], value = _a[1];
88
+ return value !== undefined;
89
+ }));
151
90
  };
152
- /**
153
- * Initializes the PostHog SDK using proxy endpoint.
154
- * For local/staging environment, ensure that `POSTHOG_STAGING_KEY` is set.
155
- * For production environment, ensure that `POSTHOG_PRODUCTION_KEY` is set.
156
- *
157
- * Proxy Setup:
158
- * - Using https://ph.deriv.com as proxy endpoint (same for staging and production)
159
- * - Proxy forwards requests to PostHog Cloud
160
- * - X-headers are configured on the proxy server (not client-side)
161
- * - See: https://posthog.com/docs/advanced/proxy
162
- */
163
- this.init = function (POSTHOG_KEY, POSTHOG_HOST, disableAMD, config) {
164
- if (disableAMD === void 0) { disableAMD = false; }
165
- if (POSTHOG_KEY) {
166
- var _define_1;
167
- if (disableAMD) {
168
- _define_1 = window.define;
169
- window.define = undefined;
170
- }
171
- _this.setCookieIfNotExists();
172
- // Get the shared anonymous ID from cookie
173
- var anonymous_id_1 = _this.getAnonymousId();
174
- posthog_js_1.default.init(POSTHOG_KEY, __assign(__assign({
175
- // Use proxy endpoint for both staging and production
176
- // Default to https://ph.deriv.com, can be overridden via POSTHOG_HOST parameter
177
- api_host: POSTHOG_HOST || 'https://ph.deriv.com',
178
- // UI host should point to PostHog Cloud for dashboard links
179
- ui_host: 'https://us.posthog.com',
180
- // Use the same anonymous ID as RudderStack
181
- bootstrap: {
182
- distinctID: anonymous_id_1,
183
- },
184
- // Default configurations
185
- capture_pageview: false, capture_pageleave: false, autocapture: true }, config), { loaded: function (ph) {
186
- var _a;
187
- if (disableAMD) {
188
- window.define = _define_1;
189
- }
190
- _this.has_initialized = true;
191
- // Check if user is already identified
192
- _this.has_identified = posthog_js_1.default.get_distinct_id() !== anonymous_id_1;
193
- _this.handleCachedEvents();
194
- (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
195
- } }));
196
- }
91
+ this.init = function (POSTHOG_KEY, POSTHOG_HOST, config) {
92
+ if (!POSTHOG_KEY)
93
+ return;
94
+ _this.setCookieIfNotExists();
95
+ var anonymous_id = _this.getAnonymousId();
96
+ posthog_js_1.default.init(POSTHOG_KEY, __assign(__assign({ api_host: POSTHOG_HOST || 'https://ph.deriv.com', ui_host: 'https://us.posthog.com', bootstrap: { distinctID: anonymous_id }, capture_pageview: false, capture_pageleave: false, autocapture: true }, config), { loaded: function () {
97
+ var _a;
98
+ _this.has_initialized = true;
99
+ _this.has_identified = posthog_js_1.default.get_distinct_id() !== anonymous_id;
100
+ (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
101
+ } }));
197
102
  };
198
- /**
199
- *
200
- * @param user_id The user ID of the user to identify and associate all events with that particular user ID
201
- * @param payload Additional information passed to identify the user
202
- */
203
103
  this.identifyEvent = function (user_id, payload) {
204
104
  var currentUserId = _this.getUserId();
205
105
  var anonymousId = _this.getAnonymousId();
206
- // Only identify if not already identified or if it's still using anonymous ID
207
106
  if (!currentUserId || currentUserId === anonymousId) {
208
- // CRITICAL: Use alias to link anonymous ID with user ID before identifying
209
- // This preserves the pre-signup journey in PostHog
210
107
  if (anonymousId && currentUserId === anonymousId) {
211
108
  posthog_js_1.default.alias(user_id, anonymousId);
212
109
  }
@@ -214,56 +111,41 @@ var PostHogAnalytics = /** @class */ (function () {
214
111
  }
215
112
  _this.has_identified = true;
216
113
  };
217
- /**
218
- * Pushes page view event to PostHog
219
- *
220
- * @param current_page The name or URL of the current page to track the page view event
221
- */
222
114
  this.pageView = function (current_page, platform, user_id, properties) {
223
115
  if (platform === void 0) { platform = 'Deriv App'; }
224
- if (_this.has_initialized && current_page !== _this.current_page) {
225
- var pageProperties = user_id ? __assign({ user_id: user_id, platform: platform }, properties) : __assign({ platform: platform }, properties);
226
- posthog_js_1.default.capture('$pageview', __assign({ $current_url: window.location.href, page_name: current_page }, pageProperties));
227
- _this.current_page = current_page;
228
- }
116
+ if (!_this.has_initialized || current_page === _this.current_page)
117
+ return;
118
+ var pageProperties = __assign(__assign({ platform: platform }, (user_id && { user_id: user_id })), properties);
119
+ posthog_js_1.default.capture('$pageview', __assign({ $current_url: window.location.href, page_name: current_page }, pageProperties));
120
+ _this.current_page = current_page;
229
121
  };
230
- /**
231
- * Pushes reset event to PostHog
232
- */
233
122
  this.reset = function () {
234
- if (_this.has_initialized) {
235
- posthog_js_1.default.reset();
236
- _this.has_identified = false;
237
- }
123
+ if (!_this.has_initialized)
124
+ return;
125
+ posthog_js_1.default.reset();
126
+ _this.has_identified = false;
238
127
  };
239
- /**
240
- * Pushes track events to PostHog with automatic payload transformation
241
- */
242
128
  this.track = function (event, payload) {
243
- if (_this.has_initialized) {
244
- try {
245
- // Transform payload to flat structure for PostHog
246
- var transformedPayload = _this.transformToPostHogPayload(payload);
247
- // Clean undefined values
248
- var clean_payload = Object.fromEntries(Object.entries(transformedPayload).filter(function (_a) {
249
- var _ = _a[0], value = _a[1];
250
- return value !== undefined;
251
- }));
252
- posthog_js_1.default.capture(event, clean_payload);
253
- }
254
- catch (err) {
255
- console.error(err);
256
- }
129
+ if (!_this.has_initialized)
130
+ return;
131
+ try {
132
+ var transformedPayload = _this.transformToPostHogPayload(payload);
133
+ var clean_payload = Object.fromEntries(Object.entries(transformedPayload).filter(function (_a) {
134
+ var _ = _a[0], value = _a[1];
135
+ return value !== undefined;
136
+ }));
137
+ posthog_js_1.default.capture(event, clean_payload);
138
+ }
139
+ catch (err) {
140
+ console.warn('PostHog: Failed to track event', err);
257
141
  }
258
142
  };
259
143
  this.onLoadedCallback = onLoaded;
260
- this.init(POSTHOG_KEY, POSTHOG_HOST, disableAMD, config);
144
+ this.init(POSTHOG_KEY, POSTHOG_HOST, config);
261
145
  }
262
- PostHogAnalytics.getPostHogInstance = function (POSTHOG_KEY, POSTHOG_HOST, disableAMD, onLoaded, config) {
263
- if (disableAMD === void 0) { disableAMD = false; }
146
+ PostHogAnalytics.getPostHogInstance = function (POSTHOG_KEY, POSTHOG_HOST, onLoaded, config) {
264
147
  if (!PostHogAnalytics._instance) {
265
- PostHogAnalytics._instance = new PostHogAnalytics(POSTHOG_KEY, POSTHOG_HOST, disableAMD, onLoaded, config);
266
- return PostHogAnalytics._instance;
148
+ PostHogAnalytics._instance = new PostHogAnalytics(POSTHOG_KEY, POSTHOG_HOST, onLoaded, config);
267
149
  }
268
150
  return PostHogAnalytics._instance;
269
151
  };
@@ -8,42 +8,16 @@ export declare class RudderStack {
8
8
  rudderstack_anonymous_cookie_key: string;
9
9
  private static _instance;
10
10
  private onLoadedCallback?;
11
- constructor(RUDDERSTACK_KEY: string, disableAMD?: boolean, onLoaded?: () => void);
12
- static getRudderStackInstance: (RUDDERSTACK_KEY: string, disableAMD?: boolean, onLoaded?: () => void) => RudderStack;
11
+ constructor(RUDDERSTACK_KEY: string, onLoaded?: () => void);
12
+ static getRudderStackInstance: (RUDDERSTACK_KEY: string, onLoaded?: () => void) => RudderStack;
13
13
  getAnonymousId: () => string | undefined;
14
14
  setCookieIfNotExists: () => void;
15
- /**
16
- * @returns The user ID that was assigned to the user after calling identify event
17
- */
18
15
  getUserId: () => (string | null) | undefined;
19
- /** For caching mechanism, Rudderstack SDK, first page load */
20
- handleCachedEvents: () => void;
21
- /**
22
- * Initializes the Rudderstack SDK. Ensure that the appropriate environment variables are set before this is called.
23
- * For local/staging environment, ensure that `RUDDERSTACK_STAGING_KEY` and `RUDDERSTACK_URL` is set.
24
- * For production environment, ensure that `RUDDERSTACK_PRODUCTION_KEY` and `RUDDERSTACK_URL` is set.
25
- */
26
- init: (RUDDERSTACK_KEY: string, disableAMD?: boolean) => void;
27
- /**
28
- *
29
- * @param user_id The user ID of the user to identify and associate all events with that particular user ID
30
- * @param payload Additional information passed to identify the user
31
- */
16
+ init: (RUDDERSTACK_KEY: string) => void;
32
17
  identifyEvent: (user_id: string, payload: {
33
18
  language: string;
34
19
  }) => void;
35
- /**
36
- * Pushes page view event to Rudderstack
37
- *
38
- * @param curret_page The name or URL of the current page to track the page view event
39
- */
40
- pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: {}) => void;
41
- /**
42
- * Pushes reset event to rudderstack
43
- */
20
+ pageView: (current_page: string, platform: string | undefined, user_id: string, properties?: Record<string, unknown>) => void;
44
21
  reset: () => void;
45
- /**
46
- * Pushes track events to Rudderstack.
47
- */
48
22
  track: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, payload: TAllEvents[T] & Partial<TCoreAttributes>) => void;
49
23
  }
@@ -10,17 +10,12 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
13
  Object.defineProperty(exports, "__esModule", { value: true });
17
14
  exports.RudderStack = void 0;
18
15
  var analytics_js_1 = require("@rudderstack/analytics-js");
19
16
  var uuid_1 = require("uuid");
20
- var js_cookie_1 = __importDefault(require("js-cookie"));
21
17
  var RudderStack = /** @class */ (function () {
22
- function RudderStack(RUDDERSTACK_KEY, disableAMD, onLoaded) {
23
- if (disableAMD === void 0) { disableAMD = false; }
18
+ function RudderStack(RUDDERSTACK_KEY, onLoaded) {
24
19
  var _this = this;
25
20
  this.analytics = new analytics_js_1.RudderAnalytics();
26
21
  this.has_identified = false;
@@ -35,91 +30,27 @@ var RudderStack = /** @class */ (function () {
35
30
  var anonymous_id = _this.getAnonymousId();
36
31
  if (!anonymous_id) {
37
32
  var hostname_1 = window.location.hostname;
38
- // List of external domains where we should use the full hostname
39
33
  var external_domains = ['webflow.io'];
40
- // Check if the hostname ends with any of the external domains
41
34
  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
35
  var domain_name = is_external_domain ? hostname_1 : hostname_1.split('.').slice(-2).join('.');
44
- // Set cookie to expire in 2 years
45
36
  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);
46
37
  }
47
38
  };
48
- /**
49
- * @returns The user ID that was assigned to the user after calling identify event
50
- */
51
39
  this.getUserId = function () { return _this.analytics.getUserId(); };
52
- /** For caching mechanism, Rudderstack SDK, first page load */
53
- this.handleCachedEvents = function () {
54
- var allowedDomains = ['deriv.com', 'deriv.team', 'deriv.ae'];
55
- var domain = allowedDomains.some(function (d) { return window.location.hostname.includes(d); })
56
- ? ".".concat(allowedDomains.find(function (d) { return window.location.hostname.includes(d); }))
57
- : ".".concat(allowedDomains[0]);
58
- var storedEventsString = js_cookie_1.default.get('cached_analytics_events');
59
- var storedPagesString = js_cookie_1.default.get('cached_analytics_page_views');
60
- try {
61
- // Handle cached analytics events
62
- if (storedEventsString) {
63
- var storedEvents = JSON.parse(storedEventsString);
64
- if (Array.isArray(storedEvents) && storedEvents.length > 0) {
65
- storedEvents.forEach(function (event) {
66
- _this.analytics.track(event.name, event.properties);
67
- });
68
- // Clear the stored events cookie
69
- js_cookie_1.default.remove('cached_analytics_events', { domain: domain });
70
- }
71
- }
72
- // Handle cached page views
73
- if (storedPagesString) {
74
- var storedPages = JSON.parse(storedPagesString);
75
- if (Array.isArray(storedPages) && storedPages.length > 0) {
76
- storedPages.forEach(function (page) {
77
- _this.analytics.page(page === null || page === void 0 ? void 0 : page.name, page === null || page === void 0 ? void 0 : page.properties);
78
- });
79
- // Clear the stored page views cookie
80
- js_cookie_1.default.remove('cached_analytics_page_views', { domain: domain });
81
- }
82
- }
83
- }
84
- catch (error) {
85
- // eslint-disable-next-line no-console
86
- console.log(error);
87
- }
88
- };
89
- /**
90
- * Initializes the Rudderstack SDK. Ensure that the appropriate environment variables are set before this is called.
91
- * For local/staging environment, ensure that `RUDDERSTACK_STAGING_KEY` and `RUDDERSTACK_URL` is set.
92
- * For production environment, ensure that `RUDDERSTACK_PRODUCTION_KEY` and `RUDDERSTACK_URL` is set.
93
- */
94
- this.init = function (RUDDERSTACK_KEY, disableAMD) {
95
- if (disableAMD === void 0) { disableAMD = false; }
96
- if (RUDDERSTACK_KEY) {
97
- var _define_1;
98
- if (disableAMD) {
99
- _define_1 = window.define;
100
- window.define = undefined;
101
- }
102
- _this.setCookieIfNotExists();
103
- _this.analytics.load(RUDDERSTACK_KEY, 'https://deriv-dataplane.rudderstack.com', {
104
- externalAnonymousIdCookieName: _this.rudderstack_anonymous_cookie_key,
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();
112
- _this.handleCachedEvents();
113
- (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
114
- },
115
- });
116
- }
40
+ this.init = function (RUDDERSTACK_KEY) {
41
+ if (!RUDDERSTACK_KEY)
42
+ return;
43
+ _this.setCookieIfNotExists();
44
+ _this.analytics.load(RUDDERSTACK_KEY, 'https://deriv-dataplane.rudderstack.com', {
45
+ externalAnonymousIdCookieName: _this.rudderstack_anonymous_cookie_key,
46
+ onLoaded: function () {
47
+ var _a;
48
+ _this.has_initialized = true;
49
+ _this.has_identified = !!_this.getUserId();
50
+ (_a = _this.onLoadedCallback) === null || _a === void 0 ? void 0 : _a.call(_this);
51
+ },
52
+ });
117
53
  };
118
- /**
119
- *
120
- * @param user_id The user ID of the user to identify and associate all events with that particular user ID
121
- * @param payload Additional information passed to identify the user
122
- */
123
54
  this.identifyEvent = function (user_id, payload) {
124
55
  var currentUserId = _this.getUserId();
125
56
  if (!currentUserId) {
@@ -127,53 +58,40 @@ var RudderStack = /** @class */ (function () {
127
58
  }
128
59
  _this.has_identified = true;
129
60
  };
130
- /**
131
- * Pushes page view event to Rudderstack
132
- *
133
- * @param curret_page The name or URL of the current page to track the page view event
134
- */
135
61
  this.pageView = function (current_page, platform, user_id, properties) {
136
62
  if (platform === void 0) { platform = 'Deriv App'; }
137
- if (_this.has_initialized && current_page !== _this.current_page) {
138
- var pageProperties = user_id ? __assign({ user_id: user_id }, properties) : properties;
139
- _this.analytics.page(platform, current_page, pageProperties);
140
- _this.current_page = current_page;
141
- }
63
+ if (!_this.has_initialized || current_page === _this.current_page)
64
+ return;
65
+ var pageProperties = __assign(__assign({}, (user_id && { user_id: user_id })), properties);
66
+ _this.analytics.page(platform, current_page, pageProperties);
67
+ _this.current_page = current_page;
142
68
  };
143
- /**
144
- * Pushes reset event to rudderstack
145
- */
146
69
  this.reset = function () {
147
- if (_this.has_initialized) {
148
- _this.analytics.reset();
149
- _this.has_identified = false;
150
- }
70
+ if (!_this.has_initialized)
71
+ return;
72
+ _this.analytics.reset();
73
+ _this.has_identified = false;
151
74
  };
152
- /**
153
- * Pushes track events to Rudderstack.
154
- */
155
75
  this.track = function (event, payload) {
156
- var clean_payload = Object.fromEntries(Object.entries(payload).filter(function (_a) {
157
- var _ = _a[0], value = _a[1];
158
- return value !== undefined;
159
- }));
160
- if (_this.has_initialized) {
161
- try {
162
- _this.analytics.track(event, clean_payload);
163
- }
164
- catch (err) {
165
- console.error(err);
166
- }
76
+ if (!_this.has_initialized)
77
+ return;
78
+ try {
79
+ var clean_payload = Object.fromEntries(Object.entries(payload).filter(function (_a) {
80
+ var _ = _a[0], value = _a[1];
81
+ return value !== undefined;
82
+ }));
83
+ _this.analytics.track(event, clean_payload);
84
+ }
85
+ catch (err) {
86
+ console.warn('RudderStack: Failed to track event', err);
167
87
  }
168
88
  };
169
89
  this.onLoadedCallback = onLoaded;
170
- this.init(RUDDERSTACK_KEY, disableAMD);
90
+ this.init(RUDDERSTACK_KEY);
171
91
  }
172
- RudderStack.getRudderStackInstance = function (RUDDERSTACK_KEY, disableAMD, onLoaded) {
173
- if (disableAMD === void 0) { disableAMD = false; }
92
+ RudderStack.getRudderStackInstance = function (RUDDERSTACK_KEY, onLoaded) {
174
93
  if (!RudderStack._instance) {
175
- RudderStack._instance = new RudderStack(RUDDERSTACK_KEY, disableAMD, onLoaded);
176
- return RudderStack._instance;
94
+ RudderStack._instance = new RudderStack(RUDDERSTACK_KEY, onLoaded);
177
95
  }
178
96
  return RudderStack._instance;
179
97
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deriv-com/analytics",
3
- "version": "1.34.0",
3
+ "version": "1.35.1",
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",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "homepage": "https://github.com/binary-com/deriv-analytics",
33
33
  "devDependencies": {
34
- "@deriv-com/utils": "^0.0.57",
34
+ "@deriv-com/utils": "^0.0.58",
35
35
  "@semantic-release/changelog": "^6.0.3",
36
36
  "@semantic-release/github": "^10.0.6",
37
37
  "@semantic-release/npm": "^13.0.0",