From 3c7edf96817ef94186879ee9e746196dafd6d0d8 Mon Sep 17 00:00:00 2001 From: Michael Wallace Date: Wed, 18 Sep 2024 15:40:38 +1000 Subject: [PATCH] feat: add initial analytics events --- .../src/components/app/app.tsx | 12 ++++++++++ .../components/wallet/Web3WalletProvider.ts | 22 +++++++++++++++++++ .../src/integration/IframeEthereumProvider.ts | 13 +++++++++++ 3 files changed, 47 insertions(+) diff --git a/javascript/tokenscript-viewer/src/components/app/app.tsx b/javascript/tokenscript-viewer/src/components/app/app.tsx index 0dea4bfd..de115977 100644 --- a/javascript/tokenscript-viewer/src/components/app/app.tsx +++ b/javascript/tokenscript-viewer/src/components/app/app.tsx @@ -63,9 +63,21 @@ const initViewerType = (params: URLSearchParams): ViewerTypes => { viewerType = "new"; } + if (window.gtag) { + window.gtag('set', { + 'viewer-type': viewerType + }); + } + return viewerType } +declare global { + interface Window { + gtag: any + } +} + @Component({ tag: 'app-root', styleUrl: 'app.css', diff --git a/javascript/tokenscript-viewer/src/components/wallet/Web3WalletProvider.ts b/javascript/tokenscript-viewer/src/components/wallet/Web3WalletProvider.ts index e0dc8791..8ca3ff3f 100644 --- a/javascript/tokenscript-viewer/src/components/wallet/Web3WalletProvider.ts +++ b/javascript/tokenscript-viewer/src/components/wallet/Web3WalletProvider.ts @@ -164,6 +164,8 @@ class Web3WalletProviderObj { for (const listener of this.walletChangeListeners){ listener(connection); } + + this.setGAWalletDetails(); } isWalletConnected(){ @@ -194,6 +196,26 @@ class Web3WalletProviderObj { return this.getConnectedWalletData('evm')[0]; } + private setGAWalletDetails(){ + if (window.gtag) { + const data = this.getConnectedWalletData('evm')?.[0]; + + window.gtag('set', { + 'wallet_address': data?.address, + 'wallet_name': data?.providerType + }); + + if (data){ + window.gtag('event', 'wallet_connected', { + 'wallet_address': data.address, + 'wallet_name': data.providerType + }) + } else { + window.gtag('event', 'wallet_disconnected'); + } + } + } + async disconnectWallet(){ await this.deleteConnections(); this.emitWalletChangeEvent(); diff --git a/javascript/tokenscript-viewer/src/integration/IframeEthereumProvider.ts b/javascript/tokenscript-viewer/src/integration/IframeEthereumProvider.ts index 0802e2c1..bc70c712 100644 --- a/javascript/tokenscript-viewer/src/integration/IframeEthereumProvider.ts +++ b/javascript/tokenscript-viewer/src/integration/IframeEthereumProvider.ts @@ -297,6 +297,19 @@ export class IFrameEthereumProvider implements ethers.Eip1193Provider { if (completer) { // Handle pending promise if ('error' in message || 'result' in message) { + + if ( + window.gtag && + 'result' in message && + 'method' in message && + message.method === "eth_accounts" + ) { + window.gtag('set', { + 'wallet_address': message.result?.[0], + 'wallet_name': "iframe-provider" + }); + } + completer.resolve(message); } else { completer.reject(