Skip to content

Commit

Permalink
refactor: make property required
Browse files Browse the repository at this point in the history
  • Loading branch information
camerow committed Nov 8, 2024
1 parent f6bbd49 commit 09d6d63
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
15 changes: 9 additions & 6 deletions packages/analytics/src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ export const mockExternalAnalyticsClient = {
screen: vi.fn().mockResolvedValue(undefined),
group: vi.fn().mockResolvedValue(undefined),
identify: vi.fn().mockResolvedValue(undefined),
page: vi.fn().mockResolvedValue(undefined),
register: vi.fn().mockResolvedValue(undefined),
deregister: vi.fn().mockResolvedValue(undefined),
};

describe('AnalyticsClient', () => {
it('should be able to track all events with default properties', async () => {
const client = AnalyticsClient(mockExternalAnalyticsClient, {
const analytics = AnalyticsClient(mockExternalAnalyticsClient, {
defaultProperties: { platform: 'web' },
});

await client.track('background_analytics_schema_fail');
await client.screen('/home/screen', undefined);
await analytics.track('background_analytics_schema_fail');
await analytics.client.screen('/home/screen', undefined);

expect(mockExternalAnalyticsClient.track).toHaveBeenCalledWith(
'background_analytics_schema_fail',
Expand All @@ -30,12 +33,12 @@ describe('AnalyticsClient', () => {
});

it('should be able to track group and identify with default traits', async () => {
const client = AnalyticsClient(mockExternalAnalyticsClient, {
const analytics = AnalyticsClient(mockExternalAnalyticsClient, {
defaultTraits: { user: 'test' },
});

await client.identify('1df3_34j3');
await client.group('1df3_34j3');
await analytics.client.identify('1df3_34j3');
await analytics.client.group('1df3_34j3');

expect(mockExternalAnalyticsClient.identify).toHaveBeenCalledWith('1df3_34j3', {
user: 'test',
Expand Down
17 changes: 5 additions & 12 deletions packages/analytics/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ export function AnalyticsClient<T extends AnalyticsClientInterface>(
}
return analyticsClient.track(event as any, { ...properties, ...options.defaultProperties });
},

async screen(name: string, properties?: JsonMap) {
return analyticsClient.screen(name, { ...properties, ...options.defaultProperties });
},

async group(groupId: string, traits?: JsonMap) {
return analyticsClient.group(groupId, { ...traits, ...options.defaultTraits });
},

async identify(userId: string, traits?: JsonMap) {
return analyticsClient.identify(userId, { ...traits, ...options.defaultTraits });
},
screen: analyticsClient.screen,
group: analyticsClient.group,
identify: analyticsClient.identify,
page: analyticsClient.page ? analyticsClient.page : () => Promise.resolve(),
client: analyticsClient,
};
}
23 changes: 16 additions & 7 deletions packages/analytics/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ interface HistoricalEvents {
broadcast_ordinal_error: { error: any };
broadcast_transaction: {
symbol: string;
amount: number;
fee: number;
inputs: number;
outputs: number;
amount?: number;
fee?: number;
inputs?: number;
outputs?: number;
};
broadcast_btc_error: { error: any };
copy_btc_address_to_clipboard: { type: string };
Expand All @@ -34,7 +34,7 @@ interface HistoricalEvents {
request_update_profile_submit: undefined;
request_update_profile_cancel: undefined;
request_signature_cancel: undefined;
requesting_origin_tab_closed_with_pending_action: { status: 'action_pending' };
requesting_origin_tab_closed_with_pending_action: undefined;
select_add_new_collectible: undefined;
select_buy_option: { provider: string };
select_theme: { theme: string };
Expand All @@ -46,6 +46,8 @@ interface HistoricalEvents {
user_approved_send_transfer: { origin: string };
user_approved_sign_and_broadcast_psbt: { origin: string };
user_clicked_feedback_button: undefined;
unable_to_read_fee_in_stx_validator: undefined;
unable_to_read_available_balance_in_stx_validator: undefined;
view_bitcoin_transaction: undefined;
view_transaction: undefined;
view_collectibles: { ordinals_count?: number; stamps_count?: number; stacks_nfts_count?: number };
Expand All @@ -70,14 +72,18 @@ interface HistoricalEvents {
hash: string;
error: string;
};
request_signature_cannot_sign_message_no_account: undefined;
request_signature_sign: { type: 'software' | 'ledger' };
switch_account: { index: number; hasStxBalance: boolean };
non_compliant_entity_detected: { address: string };
non_compliant_entity_detected: { address: string | string[] };
ledger_transaction_publish_error: { error: { message: string; error: any } };
native_segwit_tx_hex_to_ledger_tx: { success: boolean };
psbt_sign_request: { status: 'missing_taproot_internal_key' };
psbt_sign_request_p2tr_missing_taproot_internal_key: undefined;
ledger_nativesegwit_add_nonwitnessutxo: {
action: 'add_nonwitness_utxo' | 'skip_add_nonwitness_utxo';
};
submit_invalid_secret_key: undefined;
submit_valid_secret_key: undefined;
increase_fee_transaction: { symbol: string; txid: string };
finalize_tx_signature_error_thrown: { data: unknown };
auth_response_with_illegal_redirect_uri: undefined;
Expand All @@ -89,6 +95,9 @@ interface HistoricalEvents {
ledger_message_signed_approved: undefined;
ledger_message_signed_rejected: undefined;
ledger_public_keys_pulled_from_device: undefined;
user_clicked_requested_by_link: { endpoint: string };
user_approved_get_addresses: { origin: string };
user_approved_message_signing: { origin: string };
}

export type EventName = keyof Events;
7 changes: 2 additions & 5 deletions packages/analytics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ export interface AnalyticsClientInterface {
screen: (name: string, ...args: any[]) => Promise<any>;
track: (event: string, ...args: any[]) => Promise<any>;
group: (groupId: string, traits?: any, ...args: any[]) => Promise<any>;
identify: (
userId?: string | undefined,
traits?: any,
...args: any[]
) => Promise<any> | ((traits?: object) => Promise<any>);
identify: (...args: any[]) => Promise<any>;
page?: (name: string, ...args: any[]) => Promise<any>;
}

export interface AnalyticsClientConfig<T extends AnalyticsClientInterface> {
Expand Down

0 comments on commit 09d6d63

Please sign in to comment.