Skip to content

Commit

Permalink
chore(rwa): add analytics for different events to chain (#2813)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans authored Jan 21, 2025
1 parent c51d35c commit e32d12e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { IWalletAccount } from '@/components/AccountProvider/AccountType';
import { useAccount } from '@/hooks/account';
import { useNetwork } from '@/hooks/networks';
import { transactionsQuery } from '@/services/graph/transactionSubscription.graph';
import { analyticsEvent } from '@/utils/analytics';
import { store } from '@/utils/store';
import { useApolloClient } from '@apollo/client';
import type { ICommandResult } from '@kadena/client';
Expand Down Expand Up @@ -56,6 +57,8 @@ export interface ITransaction {
uuid: string;
requestKey: string;
type: ITxType;
chainId?: string;
networkId?: string;
listener?: any;
accounts: string[];
result?: ICommandResult['result'];
Expand Down Expand Up @@ -137,6 +140,15 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
nextData?.errors?.length !== undefined ||
nextData?.data?.transaction?.result.badResult
) {
analyticsEvent(`error:${data.type.name}`, {
name: data.type.name,
chainId: data?.chainId ?? '',
networkId: data?.networkId ?? '',
requestKey: data?.requestKey ?? '',
message: JSON.stringify(
nextData?.data.transaction?.result.badResult,
),
});
addNotification({
intent: 'negative',
label: 'there was an error',
Expand All @@ -153,6 +165,14 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
}
},
(errorData) => {
analyticsEvent(`error:${data.type.name}`, {
name: data.type.name,
chainId: data?.chainId ?? '',
networkId: data?.networkId ?? '',
requestKey: data?.requestKey ?? '',
message: JSON.stringify(errorData),
});

addNotification({
intent: 'negative',
label: 'there was an error',
Expand All @@ -161,6 +181,12 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
});
},
() => {
analyticsEvent(data.type.name, {
chainId: data?.chainId ?? '',
networkId: data?.networkId ?? '',
requestKey: data?.requestKey ?? '',
message: data?.result?.status,
});
// eslint-disable-next-line @typescript-eslint/no-floating-promises
store.removeTransaction(data);
},
Expand Down Expand Up @@ -252,8 +278,6 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
setTxsAnimationRefData(ref);
};

console.log({ transactions });

return (
<TransactionsContext.Provider
value={{
Expand Down
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/hooks/getAccountKDABalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const useGetAccountKDABalance = ({
};

const formatData = (data: any) => {
console.log({ data });
data?.events?.map(({ parameters }: any) => {
const params = JSON.parse(parameters);
const fromAccount = params.length > 1 && params[0];
Expand Down
32 changes: 21 additions & 11 deletions packages/apps/rwa-demo/src/utils/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,20 @@ describe('analytics', () => {

describe('analyticsEvent', () => {
it('should call analyticsEvent with correct props', () => {
analyticsEvent(EVENT_NAMES['click:search'], {
analyticsEvent(EVENT_NAMES['error:submitChain'], {
label: 'Master of the universe',
hash: '/he-man',
});

expect(window.gtag).toHaveBeenNthCalledWith(1, 'event', 'click:search', {
hash: '/he-man',
label: 'Master of the universe',
});
expect(window.gtag).toHaveBeenNthCalledWith(
1,
'event',
'error:submitChain',
{
hash: '/he-man',
label: 'Master of the universe',
},
);
});

it('should also console.warn when the nodeenv is development', () => {
Expand All @@ -111,19 +116,24 @@ describe('analytics', () => {
.mockImplementation(() => undefined);

vi.stubEnv('NODE_ENV', 'development');
analyticsEvent(EVENT_NAMES['click:search'], {
analyticsEvent(EVENT_NAMES['error:submitChain'], {
label: 'Master of the universe',
hash: '/he-man',
});

expect(consoleMock).toBeCalledWith('GTAG EVENT', {
name: 'click:search',
name: 'error:submitChain',
options: { hash: '/he-man', label: 'Master of the universe' },
});
expect(window.gtag).toHaveBeenNthCalledWith(1, 'event', 'click:search', {
hash: '/he-man',
label: 'Master of the universe',
});
expect(window.gtag).toHaveBeenNthCalledWith(
1,
'event',
'error:submitChain',
{
hash: '/he-man',
label: 'Master of the universe',
},
);
});
});
});
9 changes: 7 additions & 2 deletions packages/apps/rwa-demo/src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { TXTYPES } from '@/components/TransactionsProvider/TransactionsProvider';

// eslint-disable-next-line @kadena-dev/typedef-var
export const EVENT_NAMES = {
'click:search': 'click:search',
'error:submitChain': 'error:submitChain',
} as const;

const COOKIE_CONSENTNAME = 'cookie_consent';
Expand All @@ -13,7 +15,10 @@ interface IOptionsPageViewType {
}

export const analyticsEvent = (
name: keyof typeof EVENT_NAMES,
name:
| keyof typeof EVENT_NAMES
| keyof typeof TXTYPES
| `error:${keyof typeof TXTYPES}`,
options: IOptionsType = {},
): void => {
if (process.env.NODE_ENV === 'development') {
Expand Down

0 comments on commit e32d12e

Please sign in to comment.