From c135a7e9797792a889b23c63a62a27627b19c3e8 Mon Sep 17 00:00:00 2001 From: tomiir Date: Tue, 19 Nov 2024 12:18:40 +0100 Subject: [PATCH 1/2] feat: add INITIALIZE event to track configuration options --- packages/appkit/src/client.ts | 15 ++++++++++ .../core/src/controllers/EventsController.ts | 13 ++++++++- packages/core/src/utils/TypeUtil.ts | 29 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index f80556acec..b2c2b941a6 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -211,6 +211,21 @@ export class AppKit { await this.initOrContinue() await this.syncExistingConnection() this.version = options.sdkVersion + + const { ...optionsCopy } = options + delete optionsCopy.adapters + + EventsController.sendEvent({ + type: 'track', + event: 'INITIALIZE', + properties: { + ...optionsCopy, + networks: options.networks.map(n => n.id), + siweConfig: { + options: options.siweConfig?.options || {} + } + } + }) } // -- Public ------------------------------------------------------------------- diff --git a/packages/core/src/controllers/EventsController.ts b/packages/core/src/controllers/EventsController.ts index c81b5051e1..dd3291bb98 100644 --- a/packages/core/src/controllers/EventsController.ts +++ b/packages/core/src/controllers/EventsController.ts @@ -45,8 +45,17 @@ export const EventsController = { async _sendAnalyticsEvent(payload: EventsControllerState) { try { if (excluded.includes(payload.data.event) || typeof window === 'undefined') { + console.log( + '>> Analytics early return', + payload, + excluded.includes(payload.data.event), + typeof window === 'undefined' + ) + return } + + console.log('>> Final analytics event', payload) await api.post({ path: '/e', headers: EventsController._getApiHeaders(), @@ -58,14 +67,16 @@ export const EventsController = { props: payload.data } }) - } catch { + } catch (e) { // Catch silently + console.log('>> Analytics error', e) } }, sendEvent(data: EventsControllerState['data']) { state.timestamp = Date.now() state.data = data + console.log('>> Sending analytics event', OptionsController.state.features?.analytics, state) if (OptionsController.state.features?.analytics) { EventsController._sendAnalyticsEvent(state) } diff --git a/packages/core/src/utils/TypeUtil.ts b/packages/core/src/utils/TypeUtil.ts index 4fec7b8f7a..6f7d9a63b7 100644 --- a/packages/core/src/utils/TypeUtil.ts +++ b/packages/core/src/utils/TypeUtil.ts @@ -18,6 +18,30 @@ import type { ConstantsUtil } from './ConstantsUtil.js' import type { ReownName } from '../controllers/EnsController.js' import type UniversalProvider from '@walletconnect/universal-provider' +type AppKitConfigs = { + showWallets?: boolean + siweConfig?: { + options: { + enabled?: boolean + nonceRefetchIntervalMs?: number + sessionRefetchIntervalMs?: number + signOutOnDisconnect?: boolean + signOutOnAccountChange?: boolean + signOutOnNetworkChange?: boolean + } + } + themeMode?: 'dark' | 'light' + + themeVariables?: ThemeVariables + allowUnsupportedChain?: boolean + networks: (string | number)[] + defaultNetwork?: AppKitNetwork + chainImages?: Record + connectorImages?: Record + coinbasePreference?: 'all' | 'smartWalletOnly' | 'eoaOnly' + metadata?: Metadata +} + export type CaipNetworkCoinbaseNetwork = | 'Ethereum' | 'Arbitrum One' @@ -747,6 +771,11 @@ export type Event = search: string } } + | { + type: 'track' + event: 'INITIALIZE' + properties: AppKitConfigs + } // Onramp Types export type DestinationWallet = { address: string From 161868ba37255290b8a6ba2aef5d769f86db88eb Mon Sep 17 00:00:00 2001 From: tomiir Date: Tue, 19 Nov 2024 12:20:07 +0100 Subject: [PATCH 2/2] chore: add test --- packages/appkit/src/tests/appkit.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/appkit/src/tests/appkit.test.ts b/packages/appkit/src/tests/appkit.test.ts index 16c72584b3..eafa5f9fd0 100644 --- a/packages/appkit/src/tests/appkit.test.ts +++ b/packages/appkit/src/tests/appkit.test.ts @@ -54,6 +54,11 @@ describe('Base', () => { expect(OptionsController.setSdkVersion).toHaveBeenCalledWith(mockOptions.sdkVersion) expect(OptionsController.setProjectId).toHaveBeenCalledWith(mockOptions.projectId) expect(OptionsController.setMetadata).toHaveBeenCalledWith(mockOptions.metadata) + + const copyMockOptions = { ...mockOptions } + delete copyMockOptions.adapters + + expect(EventsController.sendEvent).toHaveBeenCalledWith(mockOptions) }) it('should initialize adapters in ChainController', () => {