Skip to content

Commit

Permalink
feat: add traking events for new dtrader (deriv-com#17370)
Browse files Browse the repository at this point in the history
* feat: add traking events for new dtrader

* fix: resolve commits

* refactor: refactor the rudderstack function in trader

* fix: stop event from getting fired in contracts page
  • Loading branch information
vinu-deriv authored Nov 5, 2024
1 parent c7cd1df commit 6fcf709
Show file tree
Hide file tree
Showing 23 changed files with 444 additions and 300 deletions.
566 changes: 300 additions & 266 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
},
"dependencies": {
"@babel/preset-typescript": "^7.24.7",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@sendbird/chat": "^4.9.7",
"@types/react-transition-group": "^4.4.4",
"babel-jest": "^29.7.0",
Expand All @@ -109,4 +109,4 @@
"optionalDependencies": {
"fsevents": "^2.3.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"dependencies": {
"@binary-com/binary-document-uploader": "^2.4.8",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/translations": "1.3.9",
"@deriv-com/utils": "^0.0.36",
"@deriv-com/ui": "1.36.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv/api-types": "1.0.172",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/appstore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"author": "Deriv",
"license": "Apache-2.0",
"dependencies": {
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/translations": "1.3.9",
"@deriv-com/ui": "1.36.4",
"@deriv/account": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cashier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"url": "https://github.com/deriv-com/deriv-app/issues"
},
"dependencies": {
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/ui": "1.36.4",
"@deriv/api": "^1.0.0",
"@deriv/api-types": "1.0.172",
Expand Down
2 changes: 1 addition & 1 deletion packages/cfd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
},
"dependencies": {
"@deriv-com/ui": "1.36.4",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/translations": "1.3.9",
"@deriv-com/utils": "^0.0.36",
"@deriv/account": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
},
"dependencies": {
"@datadog/browser-rum": "^5.11.0",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/quill-tokens": "2.0.4",
"@deriv-com/quill-ui": "1.18.1",
"@deriv-com/translations": "1.3.9",
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"sideEffects": false,
"dependencies": {
"@binary-com/binary-document-uploader": "^2.4.8",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv/api": "^1.0.0",
"@deriv-com/auth-client": "1.0.29",
"@deriv/stores": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"license": "ISC",
"dependencies": {
"@deriv-com/ui": "1.36.4",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/utils": "^0.0.36",
"@deriv/api": "^1.0.0",
"@deriv/api-types": "1.0.172",
Expand Down
2 changes: 1 addition & 1 deletion packages/reports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
},
"dependencies": {
"@deriv-com/ui": "1.36.4",
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv/components": "^1.0.0",
"@deriv/deriv-api": "^1.0.15",
"@deriv/api-types": "1.0.172",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv/quill-icons": "1.23.3",
"@deriv/api-types": "1.0.172",
"@deriv/translations": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/trader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"webpack-node-externals": "^2.5.2"
},
"dependencies": {
"@deriv-com/analytics": "1.25.1",
"@deriv-com/analytics": "1.26.1",
"@deriv-com/quill-tokens": "2.0.4",
"@deriv-com/quill-ui": "1.18.1",
"@deriv-com/utils": "^0.0.36",
Expand Down
56 changes: 56 additions & 0 deletions packages/trader/src/Analytics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Analytics } from '@deriv-com/analytics';

export const sendDtraderV2OpenToAnalytics = () => {
Analytics.trackEvent('ce_dtrader_trade_form', {
action: 'open',
});
};

export const sendSelectedTradeTypeToAnalytics = (
trade_name: string,
subform_name: string,
market_name: string,
trade_type_count: number
) => {
Analytics.trackEvent('ce_dtrader_trade_form', {
action: 'select_trade_type',
trade_name,
subform_name,
market_name,
trade_type_count,
});
};

export const sendOpenGuideToAnalytics = (trade_name: string, subform_name: string) => {
Analytics.trackEvent('ce_dtrader_trade_form', {
action: 'open_guide',
trade_name,
subform_name,
});
};

export const sendMarketTypeToAnalytics = (market_name: string, trade_name: string) => {
Analytics.trackEvent('ce_dtrader_trade_form', {
action: 'select_market_type',
market_name,
trade_name,
});
};

export const sendDtraderV2PurchaseToAnalytics = (trade_name: string, market_name: string, contract_id: number) => {
Analytics.trackEvent('ce_dtrader_trade_form', {
action: 'run_contract',
trade_name,
market_name,
contract_id,
});
};

export const sendDtraderPurchaseToAnalytics = (trade_name: string, market_name: string, contract_id: number) => {
Analytics.trackEvent('ce_contracts_set_up_form', {
action: 'run_contract',
trade_name,
market_name,
contract_id,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import SymbolsSearchField from '../SymbolsSearchField';
import MarketCategories from '../MarketCategories';
import SymbolSearchResults from '../SymbolSearchResults';
import { useTraderStore } from 'Stores/useTraderStores';
import { sendMarketTypeToAnalytics } from '../../../Analytics';

type TActiveSymbolsList = {
isOpen: boolean;
setIsOpen: (input: boolean) => void;
};

const ActiveSymbolsList = observer(({ isOpen, setIsOpen }: TActiveSymbolsList) => {
const { setTickData, symbol } = useTraderStore();
const { setTickData, symbol, contract_type } = useTraderStore();
const [isSearching, setIsSearching] = useState(false);
const [selectedSymbol, setSelectedSymbol] = useState(symbol);
const [searchValue, setSearchValue] = useState('');
Expand Down Expand Up @@ -50,12 +51,18 @@ const ActiveSymbolsList = observer(({ isOpen, setIsOpen }: TActiveSymbolsList) =
searchValue={searchValue}
setSearchValue={setSearchValue}
setIsOpen={setIsOpen}
setSelectedSymbol={setSelectedSymbol}
setSelectedSymbol={(symbol: string) => {
sendMarketTypeToAnalytics(symbol, contract_type);
setSelectedSymbol(symbol);
}}
/>
) : (
<MarketCategories
selectedSymbol={selectedSymbol}
setSelectedSymbol={setSelectedSymbol}
setSelectedSymbol={(symbol: string) => {
sendMarketTypeToAnalytics(symbol, contract_type);
setSelectedSymbol(symbol);
}}
setIsOpen={setIsOpen}
isOpen={isOpen}
marketCategoriesRef={marketCategoriesRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { CONTRACT_LIST, AVAILABLE_CONTRACTS } from 'AppV2/Utils/trade-types-util
import { getTerm } from 'AppV2/Utils/contract-description-utils';
import TraderProviders from '../../../../trader-providers';
import Guide from '../guide';
import { sendOpenGuideToAnalytics } from '../../../../Analytics';

jest.mock('../../../../Analytics', () => ({
sendOpenGuideToAnalytics: jest.fn(),
}));

const trade_types = 'Trade types';

Expand Down Expand Up @@ -124,6 +129,7 @@ describe('Guide', () => {
userEvent.click(screen.getByRole('button'));

expect(screen.getByText(trade_types)).toBeInTheDocument();
expect(sendOpenGuideToAnalytics).toHaveBeenCalledWith(TRADE_TYPES.RISE_FALL, 'trade_type_page');
AVAILABLE_CONTRACTS.forEach(({ id }) => expect(screen.getByText(id)).toBeInTheDocument());
});

Expand All @@ -134,6 +140,7 @@ describe('Guide', () => {

expect(screen.queryByText(trade_types)).not.toBeInTheDocument();
expect(screen.getByText(CONTRACT_LIST.RISE_FALL)).toBeInTheDocument();
expect(sendOpenGuideToAnalytics).toHaveBeenCalledWith(TRADE_TYPES.RISE_FALL, 'main_trade_page');

AVAILABLE_CONTRACTS.forEach(({ id }) =>
id === CONTRACT_LIST.RISE_FALL
Expand Down
9 changes: 8 additions & 1 deletion packages/trader/src/AppV2/Components/Guide/guide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AVAILABLE_CONTRACTS, CONTRACT_LIST } from 'AppV2/Utils/trade-types-util
import GuideDefinitionModal from './guide-definition-modal';
import GuideDescriptionModal from './guide-description-modal';
import useContractsForCompany from 'AppV2/Hooks/useContractsForCompany';
import { sendOpenGuideToAnalytics } from '../../../Analytics';

type TGuide = {
has_label?: boolean;
Expand Down Expand Up @@ -60,7 +61,13 @@ const Guide = observer(({ has_label, show_guide_for_selected_contract }: TGuide)
<Button
color={is_dark_mode_on ? 'white' : 'black'}
icon={<LabelPairedPresentationScreenSmRegularIcon key='guide-button-icon' />}
onClick={() => setIsDescriptionOpened(true)}
onClick={() => {
sendOpenGuideToAnalytics(
contract_type,
show_guide_for_selected_contract ? 'main_trade_page' : 'trade_type_page'
);
setIsDescriptionOpened(true);
}}
variant={has_label ? 'secondary' : 'tertiary'}
key={current_language}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { CSSTransition } from 'react-transition-group';
import { getDisplayedContractTypes } from 'AppV2/Utils/trade-types-utils';
import { usePrevious } from '@deriv/components';
import { checkIsServiceModalError } from 'AppV2/Utils/layout-utils';
import { sendDtraderV2PurchaseToAnalytics } from '../../../Analytics';

const PurchaseButton = observer(() => {
const [loading_button_index, setLoadingButtonIndex] = React.useState<number | null>(null);
Expand Down Expand Up @@ -108,8 +109,9 @@ const PurchaseButton = observer(() => {
return button_index ? 'sell' : 'purchase';
};

const addNotificationBannerCallback = (params: Parameters<typeof addBanner>[0]) =>
addBanner({
const addNotificationBannerCallback = (params: Parameters<typeof addBanner>[0], contract_id: number) => {
sendDtraderV2PurchaseToAnalytics(contract_type, symbol, contract_id);
return addBanner({
icon: (
<StandaloneStopwatchRegularIcon
iconSize='sm'
Expand All @@ -119,6 +121,7 @@ const PurchaseButton = observer(() => {
),
...params,
});
};

React.useEffect(() => {
if (is_purchase_enabled) setLoadingButtonIndex(null);
Expand Down
14 changes: 11 additions & 3 deletions packages/trader/src/AppV2/Containers/Trade/trade-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import TradeTypesSelectionGuide from 'AppV2/Components/OnboardingGuide/TradeType
import Guide from '../../Components/Guide';

type TTradeTypesProps = {
onTradeTypeSelect: (e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => void;
onTradeTypeSelect: (
e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
subform_name: string,
trade_type_count: number
) => void;
trade_types: ReturnType<typeof getTradeTypesList>;
contract_type: string;
is_dark_mode_on: boolean;
Expand Down Expand Up @@ -180,7 +184,7 @@ const TradeTypes = ({ contract_type, onTradeTypeSelect, trade_types, is_dark_mod
};

const handleOnTradeTypeSelect = (e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => {
onTradeTypeSelect(e);
onTradeTypeSelect(e, 'trade_type_page', getPinnedItems().length);
scrollToSelectedTradeType();
setIsOpen(false);
};
Expand Down Expand Up @@ -219,7 +223,11 @@ const TradeTypes = ({ contract_type, onTradeTypeSelect, trade_types, is_dark_mod
return (
<div className='trade__trade-types' ref={trade_types_ref}>
{trade_type_chips.map(({ title, id }: TItem) => (
<Chip.Selectable key={id} onChipSelect={onTradeTypeSelect} selected={isTradeTypeSelected(id)}>
<Chip.Selectable
key={id}
onChipSelect={e => onTradeTypeSelect(e, 'main_trade_page', getPinnedItems().length)}
selected={isTradeTypeSelected(id)}
>
<Text size='sm'>{title}</Text>
</Chip.Selectable>
))}
Expand Down
10 changes: 8 additions & 2 deletions packages/trader/src/AppV2/Containers/Trade/trade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import useContractsForCompany from 'AppV2/Hooks/useContractsForCompany';
import AccumulatorStats from 'AppV2/Components/AccumulatorStats';
import OnboardingGuide from 'AppV2/Components/OnboardingGuide/GuideForPages';
import ServiceErrorSheet from 'AppV2/Components/ServiceErrorSheet';
import { sendSelectedTradeTypeToAnalytics } from '../../../Analytics';

const Trade = observer(() => {
const [is_minimized_params_visible, setIsMinimizedParamsVisible] = React.useState(false);
Expand Down Expand Up @@ -55,16 +56,21 @@ const Trade = observer(() => {
);

const onTradeTypeSelect = React.useCallback(
(e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>) => {
(
e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
subform_name: string,
trade_type_count: number
) => {
const value = trade_types.find(({ text }) => text === (e.target as HTMLButtonElement).textContent)?.value;
onChange({
target: {
name: 'contract_type',
value,
},
});
sendSelectedTradeTypeToAnalytics(value || '', subform_name, symbol, trade_type_count);
},
[trade_types, onChange]
[trade_types, onChange, symbol]
);

const onScroll = React.useCallback(() => {
Expand Down
8 changes: 8 additions & 0 deletions packages/trader/src/AppV2/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'Sass/app.scss';
import Notifications from './Containers/Notifications';
import Router from './Routes/router';
import ServicesErrorSnackbar from './Components/ServicesErrorSnackbar';
import { sendDtraderV2OpenToAnalytics } from '../Analytics';
import { routes } from '@deriv/shared';

type Apptypes = {
passthrough: {
Expand All @@ -25,6 +27,12 @@ const App = ({ passthrough }: Apptypes) => {
return () => root_store.ui.setPromptHandler(false);
}, [root_store]);

React.useEffect(() => {
if (!window.location.pathname.startsWith('/contract')) {
sendDtraderV2OpenToAnalytics();
}
}, []);

return (
<TraderProviders store={root_store}>
<ReportsStoreProvider>
Expand Down
Loading

0 comments on commit 6fcf709

Please sign in to comment.