@deriv-com/analytics 1.31.8 → 1.32.0

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.
@@ -1,6 +1,6 @@
1
1
  import { Growthbook, GrowthbookConfigs } from './growthbook';
2
2
  import { RudderStack } from './rudderstack';
3
- import { TCoreAttributes, TEvents, TGrowthbookOptions } from './types';
3
+ import { TCoreAttributes, TGrowthbookOptions, TAllEvents } from './types';
4
4
  declare global {
5
5
  interface Window {
6
6
  AnalyticsInstance: ReturnType<typeof createAnalyticsInstance>;
@@ -26,7 +26,7 @@ export declare function createAnalyticsInstance(options?: Options): {
26
26
  isFeatureOn: (key: string) => boolean;
27
27
  setUrl: (href: string) => Promise<void>;
28
28
  getId: () => string;
29
- trackEvent: <T extends keyof TEvents>(event: T, analytics_data: TEvents[T]) => void;
29
+ trackEvent: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, analytics_data: TAllEvents[T]) => void;
30
30
  getInstances: () => {
31
31
  ab: Growthbook;
32
32
  tracking: RudderStack;
@@ -47,7 +47,7 @@ export declare const Analytics: {
47
47
  isFeatureOn: (key: string) => boolean;
48
48
  setUrl: (href: string) => Promise<void>;
49
49
  getId: () => string;
50
- trackEvent: <T extends keyof TEvents>(event: T, analytics_data: TEvents[T]) => void;
50
+ trackEvent: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, analytics_data: TAllEvents[T]) => void;
51
51
  getInstances: () => {
52
52
  ab: Growthbook;
53
53
  tracking: RudderStack;
package/lib/analytics.js CHANGED
@@ -220,7 +220,22 @@ function createAnalyticsInstance(options) {
220
220
  return;
221
221
  _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.reset();
222
222
  };
223
+ // Runtime guard for V2 Events
224
+ var isV2Payload = function (payload) {
225
+ return 'event_metadata' in payload || 'cta_information' in payload || 'error' in payload;
226
+ };
223
227
  var trackEvent = function (event, analytics_data) {
228
+ var userId = getId();
229
+ var final_payload = {};
230
+ if (isV2Payload(analytics_data)) {
231
+ // --- V2 LOGIC: Nest Core Data ---
232
+ var v2_data = analytics_data;
233
+ final_payload = __assign(__assign({}, v2_data), { event_metadata: __assign(__assign(__assign({}, core_data), (userId && { user_id: userId })), v2_data.event_metadata) });
234
+ }
235
+ else {
236
+ // --- V1 LOGIC: Flatten Core Data (Backward Compatible) ---
237
+ final_payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
238
+ }
224
239
  if (navigator.onLine && _rudderstack) {
225
240
  if (event_cache.length > 0) {
226
241
  event_cache.forEach(function (cache, index) {
@@ -228,19 +243,15 @@ function createAnalyticsInstance(options) {
228
243
  delete event_cache[index];
229
244
  });
230
245
  }
231
- var userId = getId();
232
- var payload = __assign(__assign(__assign({}, core_data), analytics_data), (userId && { user_id: userId }));
233
246
  if (event in tracking_config) {
234
- tracking_config[event] && (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload));
247
+ tracking_config[event] && (_rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, final_payload));
235
248
  }
236
249
  else {
237
- _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, payload);
250
+ _rudderstack === null || _rudderstack === void 0 ? void 0 : _rudderstack.track(event, final_payload);
238
251
  }
239
252
  }
240
253
  else {
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 });
254
+ event_cache.push({ event: event, payload: final_payload });
244
255
  }
245
256
  };
246
257
  var getInstances = function () { return ({ ab: _growthbook, tracking: _rudderstack }); };
@@ -1,5 +1,5 @@
1
1
  import { RudderAnalytics } from '@rudderstack/analytics-js';
2
- import { TCoreAttributes, TEvents } from './types';
2
+ import { TCoreAttributes, TAllEvents } from './types';
3
3
  export declare class RudderStack {
4
4
  analytics: RudderAnalytics;
5
5
  has_identified: boolean;
@@ -43,11 +43,7 @@ export declare class RudderStack {
43
43
  */
44
44
  reset: () => void;
45
45
  /**
46
- * Pushes track events to Rudderstack. When this method is called before `identifyEvent` method is called, the events tracked will be associated with an anonymous ID.
47
- * Otherwise, if the events needs to be associated with a user ID, call `identifyEvent` with the user ID passed first before calling this method.
48
- *
49
- * @param event The event name to track
50
- * @param payload Additional information related to the event
46
+ * Pushes track events to Rudderstack.
51
47
  */
52
- track: <T extends keyof TEvents>(event: T, payload: TEvents[T] & Partial<TCoreAttributes>) => void;
48
+ track: <T extends keyof import("./types").TEvents | "ce_get_start_page">(event: T, payload: TAllEvents[T] & Partial<TCoreAttributes>) => void;
53
49
  }
@@ -150,11 +150,7 @@ var RudderStack = /** @class */ (function () {
150
150
  }
151
151
  };
152
152
  /**
153
- * Pushes track events to Rudderstack. When this method is called before `identifyEvent` method is called, the events tracked will be associated with an anonymous ID.
154
- * Otherwise, if the events needs to be associated with a user ID, call `identifyEvent` with the user ID passed first before calling this method.
155
- *
156
- * @param event The event name to track
157
- * @param payload Additional information related to the event
153
+ * Pushes track events to Rudderstack.
158
154
  */
159
155
  this.track = function (event, payload) {
160
156
  var clean_payload = Object.fromEntries(Object.entries(payload).filter(function (_a) {
@@ -163,6 +159,7 @@ var RudderStack = /** @class */ (function () {
163
159
  }));
164
160
  if (_this.has_initialized) {
165
161
  try {
162
+ // Cast to 'any' allows passing complex V2 objects that RudderStack's strictest types might reject
166
163
  _this.analytics.track(event, clean_payload);
167
164
  }
168
165
  catch (err) {
package/lib/types.d.ts CHANGED
@@ -166,6 +166,18 @@ type IndicatorsTypesFormAction = {
166
166
  search_string?: string;
167
167
  subform_name?: string;
168
168
  };
169
+ type DrawingToolsTypesFormAction = {
170
+ action: 'open' | 'add' | 'delete' | 'edit_px' | 'edit_color';
171
+ form_name?: string;
172
+ drawing_tool_name?: string;
173
+ pxthickness?: string;
174
+ color_name?: string;
175
+ };
176
+ type CrossHairFormAction = {
177
+ action: 'click';
178
+ form_name?: string;
179
+ cta_name?: 'enable' | 'disable';
180
+ };
169
181
  type MarketTypesFormAction = {
170
182
  action: 'open' | 'close' | 'choose_market_type' | 'search' | 'info_redirect' | 'add_to_favorites' | 'delete_from_favorites';
171
183
  form_name?: string;
@@ -218,7 +230,7 @@ type ReportsFormAction = {
218
230
  form_source: string;
219
231
  };
220
232
  type ChartTypesFormAction = {
221
- action?: 'open' | 'close' | 'choose_chart_type' | 'choose_time_interval';
233
+ action?: 'open' | 'close' | 'choose_chart_type' | 'choose_time_interval' | 'switch_toggle';
222
234
  form_name?: string;
223
235
  chart_type_name?: string;
224
236
  time_interval_name?: string;
@@ -316,6 +328,8 @@ export type TEvents = {
316
328
  ce_bot_form: BotForm;
317
329
  ce_contracts_set_up_form: ContractsSetupForm;
318
330
  ce_indicators_types_form: IndicatorsTypesFormAction;
331
+ ce_drawing_tools_form: DrawingToolsTypesFormAction;
332
+ ce_crosshair: CrossHairFormAction;
319
333
  ce_trade_types_form: TradeTypesForm;
320
334
  ce_chart_types_form: ChartTypesFormAction;
321
335
  ce_market_types_form: MarketTypesFormAction;
@@ -331,4 +345,28 @@ export type TEvents = {
331
345
  ce_cashier_deposit_onboarding_form: TCashierDepositOnboardingFormAction;
332
346
  ce_dtrader_trade_form: TDtraderTradeForm;
333
347
  };
348
+ export type TCtaInformation = {
349
+ cta_name: string;
350
+ section_name?: string;
351
+ container_name?: string;
352
+ };
353
+ export type TErrorInformation = {
354
+ error_message: string;
355
+ error_code?: string;
356
+ };
357
+ export type TV2EventMetadata = {
358
+ page_name?: string;
359
+ marketing_data?: Record<string, any>;
360
+ } & Partial<TCoreAttributes>;
361
+ export type TV2EventPayload = {
362
+ action: string;
363
+ form_name?: string;
364
+ cta_information?: TCtaInformation | null;
365
+ error?: TErrorInformation | null;
366
+ event_metadata: TV2EventMetadata;
367
+ };
368
+ export type TEventsV2 = {
369
+ ce_get_start_page: TV2EventPayload;
370
+ };
371
+ export type TAllEvents = TEvents & TEventsV2;
334
372
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deriv-com/analytics",
3
- "version": "1.31.8",
3
+ "version": "1.32.0",
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",