Skip to content

Commit

Permalink
Akmal / add rudderstack metrics for reports package (deriv-com#10183)
Browse files Browse the repository at this point in the history
* feat: add rudderstack metrics for reports package

* feat: add account_type and device_type to each call

* fix: prettier suggestions

* feat: replace null with undefined to match rudderstack sdk

* feat: unit test cases for formatDate

* fix: incorrect test case and function refactor

* feat: add rudderstack config

* fix: resolve merge issues

* fix: merge conflict

* chore: resolve conflicts

* chore: fix broken test cases
  • Loading branch information
akmal-deriv authored Oct 18, 2023
1 parent b6acdcb commit bf89ae2
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 63 deletions.
1 change: 1 addition & 0 deletions packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@deriv/shared": "^1.0.0",
"rudder-sdk-js": "^2.35.0"
}
}
36 changes: 36 additions & 0 deletions packages/analytics/src/Constants/rudderstack-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export const getRudderstackConfig = () => ({
action_names: {
choose_report_type: 'choose_report_type',
close: 'close',
close_contract: 'close_contract',
filter_dates: 'filter_dates',
filter_growth_rate: 'filter_growth_rate',
filter_trade_type: 'filter_trade_type',
filter_transaction_type: 'filter_transaction_type',
open_contract_details: 'open_contract_details',
open: 'open',
},
event_names: {
chart_types: 'ce_chart_types_form',
drawing_tools: 'ce_drawing_tools_form',
indicators_types: 'ce_indicators_types_form',
market_types: 'ce_market_types_form',
recent_positions: 'ce_recent_positions_form',
reports: 'ce_reports_form',
template: 'ce_template_form',
trade_types: 'ce_trade_types_form',
} ,
form_names: {
default: 'default',
},
form_sources: {
deriv_trader: 'deriv_trader',
other: 'other',
},
subform_names: {
contract_details: 'contract_details_form',
open_positions: 'open_positions_form',
statement: 'statement_form',
trade_table: 'trade_table_form',
},
} as const);
1 change: 1 addition & 0 deletions packages/analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as RudderStack } from './rudderstack';
export type { TEvents } from './rudderstack';
export * from './Constants/rudderstack-events';
60 changes: 58 additions & 2 deletions packages/analytics/src/rudderstack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as RudderAnalytics from 'rudder-sdk-js';
import { isMobile } from '@deriv/shared';

type SignupProvider = 'email' | 'phone' | 'google' | 'facebook' | 'apple';

Expand Down Expand Up @@ -133,14 +134,68 @@ type IdentifyAction = {
language: string;
};

type ReportsFormAction =
| {
action: 'choose_report_type';
form_name: string;
subform_name: 'open_positions_form' | 'statement_form' | 'trade_table_form';
trade_type_filter?: string;
growth_type_filter?: string;
start_date_filter?: string;
end_date_filter?: string;
transaction_type_filter?: string;
}
| {
action: 'filter_trade_type';
form_name: string;
subform_name: 'open_positions_form';
trade_type_filter: string;
}
| {
action: 'filter_growth_rate';
form_name: string;
subform_name: 'open_positions_form';
growth_type_filter: string;
}
| {
action: 'filter_dates';
form_name: string;
subform_name: 'trade_table_form' | 'statement_form';
start_date_filter?: string;
end_date_filter?: string;
}
| {
action: 'filter_transaction_type';
form_name: string;
subform_name: 'statement_form';
transaction_type_filter: string;
}
| {
action: 'open';
form_name: string;
subform_name: string;
form_source: string;
}
| {
action: 'close';
form_name: string;
subform_name: string;
}
| {
action: 'open_contract_details';
form_name: string;
form_source: string;
};

export type TEvents = {
ce_chart_types_form: ChartTypesFormAction;
ce_indicators_types_form: IndicatorsTypesFormAction;
ce_market_types_form: MarketTypesFormAction;
ce_virtual_signup_form: VirtualSignupFormAction;
ce_real_account_signup_form: RealAccountSignupFormAction;
ce_virtual_signup_email_confirmation: VirtualSignupEmailConfirmationAction;
ce_reports_form: ReportsFormAction;
ce_trade_types_form: TradeTypesFormAction;
ce_virtual_signup_email_confirmation: VirtualSignupEmailConfirmationAction;
ce_virtual_signup_form: VirtualSignupFormAction;
identify: IdentifyAction;
};

Expand Down Expand Up @@ -212,6 +267,7 @@ export class RudderStack {
RudderAnalytics.track(event, {
...payload,
account_type: this.account_type,
device_type: isMobile() ? 'mobile' : 'desktop',
});
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/Stores/contract-replay-store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { action, observable, makeObservable, override } from 'mobx';
import { routes, isEmptyObject, isForwardStarting, WS, contractCancelled, contractSold } from '@deriv/shared';
import { Money } from '@deriv/components';
import { RudderStack, getRudderstackConfig } from '@deriv/analytics';
import { localize } from '@deriv/translations';
import ContractStore from './contract-store';
import BaseStore from './base-store';
Expand Down Expand Up @@ -261,6 +262,8 @@ export default class ContractReplayStore extends BaseStore {
}

handleSell(response) {
const { action_names, event_names, form_names, subform_names } = getRudderstackConfig();

if (response.error) {
// If unable to sell due to error, give error via pop up if not in contract mode
this.is_sell_requested = false;
Expand All @@ -278,6 +281,12 @@ export default class ContractReplayStore extends BaseStore {
this.root_store.notifications.addNotificationMessage(
contractSold(this.root_store.client.currency, response.sell.sold_for, Money)
);

RudderStack.track(event_names.reports, {
action: action_names.close_contract,
form_name: form_names.default,
subform_name: subform_names.contract_details,
});
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/Stores/portfolio-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
TURBOS,
} from '@deriv/shared';
import { Money } from '@deriv/components';
import { RudderStack, getRudderstackConfig } from '@deriv/analytics';
import { ChartBarrierStore } from './chart-barrier-store';
import { setLimitOrderBarriers } from './Helpers/limit-orders';

Expand Down Expand Up @@ -324,6 +325,7 @@ export default class PortfolioStore extends BaseStore {
}

handleSell(response) {
const { action_names, event_names, form_names, subform_names } = getRudderstackConfig();
if (response.error) {
// If unable to sell due to error, give error via pop up if not in contract mode
const i = this.getPositionIndexById(response.echo_req.sell);
Expand All @@ -345,6 +347,12 @@ export default class PortfolioStore extends BaseStore {
this.root_store.notifications.addNotificationMessage(
contractSold(this.root_store.client.currency, response.sell.sold_for, Money)
);

RudderStack.track(event_names.reports, {
action: action_names.close_contract,
form_name: form_names.default,
subform_name: subform_names.open_positions,
});
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/reports/build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ module.exports = function (env) {
'@deriv/components': '@deriv/components',
'@deriv/translations': '@deriv/translations',
'@deriv/deriv-charts': '@deriv/deriv-charts',
'@deriv/analytics': '@deriv/analytics',
},
/^@deriv\/shared\/.+$/,
/^@deriv\/components\/.+$/,
/^@deriv\/translations\/.+$/,
/^@deriv\/account\/.+$/,
/^@deriv\/analytics\/.+$/,
],
target: 'web',
plugins: plugins(base, false),
Expand Down
1 change: 1 addition & 0 deletions packages/reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"webpack-node-externals": "^2.5.2"
},
"dependencies": {
"@deriv/analytics": "^1.0.0",
"@deriv/components": "^1.0.0",
"@deriv/deriv-api": "^1.0.13",
"@deriv/shared": "^1.0.0",
Expand Down
Loading

0 comments on commit bf89ae2

Please sign in to comment.