From e5e378513a0da6f0064e6f15e558b646d61d94ba Mon Sep 17 00:00:00 2001 From: nullptr Date: Fri, 10 Jan 2025 16:29:56 +0300 Subject: [PATCH 1/2] feat: fix bugs and new events --- package.json | 2 +- src/enum/event-type.enum.ts | 3 +- src/observers/ton-connect.observer.ts | 47 ++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 4faeb4c..044d827 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tonsolutions/telemetree-react", - "version": "1.2.5", + "version": "1.2.6", "main": "./lib/index.сjs", "module": "./lib/index.mjs", "types": "./lib/index.d.ts", diff --git a/src/enum/event-type.enum.ts b/src/enum/event-type.enum.ts index df53cb1..f71135c 100755 --- a/src/enum/event-type.enum.ts +++ b/src/enum/event-type.enum.ts @@ -1,7 +1,6 @@ export enum EventType { Click = 'Click', PageView = 'Pageview', - Wallet = 'Wallet', Closed = 'Closed', MainButtonPressed = 'MainButtonPressed', SettingsButtonPressed = 'SettingsButtonPressed', @@ -40,8 +39,8 @@ export enum EventType { SessionEnd = 'Session end', // Wallet events + Wallet = 'Wallet', WalletConnectStarted = 'Wallet connect started', - WalletConnected = 'Wallet connected', WalletConnectError = 'Wallet connect error', WalletConnectionRestoringStarted = 'Wallet connection restoring started', WalletConnectionRestored = 'Wallet connection restored', diff --git a/src/observers/ton-connect.observer.ts b/src/observers/ton-connect.observer.ts index e50234a..b14671c 100644 --- a/src/observers/ton-connect.observer.ts +++ b/src/observers/ton-connect.observer.ts @@ -19,6 +19,7 @@ interface CustomData { } interface BaseEventDetails { + wallet_address?: string; wallet_type?: string; wallet_version?: string; custom_data?: CustomData; @@ -143,15 +144,14 @@ export class TonConnectObserver { ): Promise { const config = getConfig(); Logger.debug('Raw event details:', { eventName, eventDetails }); // Add this debug log - try { switch (eventName) { case TonConnectEvent.WalletConnectSuccess: { const details = eventDetails as WalletConnectionEvent; await this.eventBuilder.track( - `${config.defaultSystemEventPrefix} ${EventType.WalletConnected}`, + `${EventType.Wallet}`, { - wallet: details?.account?.address, + wallet: details?.account?.address || details?.wallet_address, provider: details?.wallet?.name || 'unknown', chain: details?.account?.chain, wallet_type: eventDetails.wallet_type, @@ -239,7 +239,46 @@ export class TonConnectObserver { await this.eventBuilder.track( `${config.defaultSystemEventPrefix} ${EventType.WalletDisconnected}`, { - lastWallet: eventDetails?.wallet, + lastWallet: eventDetails?.wallet_address, + timestamp: Date.now(), + }, + ); + break; + } + + case TonConnectEvent.ConnectionRestoringError: { + await this.eventBuilder.track( + `${config.defaultSystemEventPrefix} ${EventType.WalletConnectionRestoreError}`, + { + wallet_type: eventDetails.wallet_type, + wallet_version: eventDetails.wallet_version, + wallet: eventDetails?.wallet_address, + timestamp: Date.now(), + }, + ); + break; + } + + case TonConnectEvent.ConnectionRestoringSuccess: { + await this.eventBuilder.track( + `${config.defaultSystemEventPrefix} ${EventType.WalletConnectionRestored}`, + { + wallet_type: eventDetails.wallet_type, + wallet_version: eventDetails.wallet_version, + wallet: eventDetails?.wallet_address, + timestamp: Date.now(), + }, + ); + break; + } + + case TonConnectEvent.ConnectionRestoringStarted: { + await this.eventBuilder.track( + `${config.defaultSystemEventPrefix} ${EventType.WalletConnectionRestoringStarted}`, + { + wallet_type: eventDetails.wallet_type, + wallet_version: eventDetails.wallet_version, + wallet: eventDetails?.wallet_address, timestamp: Date.now(), }, ); From 592ae9a03c050c0e4ab3f26493e618e9ccfeaf0d Mon Sep 17 00:00:00 2001 From: nullptr Date: Fri, 10 Jan 2025 17:07:57 +0300 Subject: [PATCH 2/2] feat: add new flag ton observer started --- src/components/TwaAnalyticsProvider.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/components/TwaAnalyticsProvider.tsx b/src/components/TwaAnalyticsProvider.tsx index 16f9709..1b2d1ba 100755 --- a/src/components/TwaAnalyticsProvider.tsx +++ b/src/components/TwaAnalyticsProvider.tsx @@ -21,6 +21,7 @@ declare global { WebView?: Telegram.WebView; }; __telemetreeSessionStarted?: boolean; + __telemetreeTonObserverStarted?: boolean; } } @@ -83,13 +84,16 @@ const TwaAnalyticsProvider: FunctionComponent = ({ useEffect(() => { let observer: TonConnectObserver | null = null; - try { - observer = new TonConnectObserver(eventBuilder); - Logger.info('TON Connect observer initialized successfully'); - } catch (error) { - Logger.error('Failed to initialize TON Connect observer', { - error: error instanceof Error ? error.message : 'Unknown error', - }); + if (!window.__telemetreeTonObserverStarted){ + try { + observer = new TonConnectObserver(eventBuilder); + window.__telemetreeTonObserverStarted = true; + Logger.info('TON Connect observer initialized successfully'); + } catch (error) { + Logger.error('Failed to initialize TON Connect observer', { + error: error instanceof Error ? error.message : 'Unknown error', + }); + } } return () => {