Skip to content

Commit

Permalink
react-app: Add analytics for wallet connect and disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
hochiw committed Aug 2, 2022
1 parent c3296d0 commit d6350da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion react-app/src/clients/baseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ export interface ArbitrarySigner {

export class BaseWallet {
protected chainInfo: ChainInfo;

public type: string;
public offlineSigner: OfflineSigner;
public provider: WalletProvider;

constructor(
type: string,
chainInfo: ChainInfo,
offlineSigner: OfflineSigner,
provider: WalletProvider
) {
this.type = type;
this.offlineSigner = offlineSigner;
this.chainInfo = chainInfo;
this.provider = provider;
Expand Down
10 changes: 9 additions & 1 deletion react-app/src/clients/keplrClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import {
newSignDataMessage,
SignDataMessageResponse,
} from "../models/cosmos/tx";
import { ArbitrarySigner, BaseWallet } from "./baseWallet";
import { ArbitrarySigner, BaseWallet, WalletProvider } from "./baseWallet";

export class KeplrWallet extends BaseWallet {
private constructor(
chainInfo: ChainInfo,
offlineSigner: OfflineSigner,
provider: WalletProvider
) {
super("keplr", chainInfo, offlineSigner, provider);
}

static async connect(chainInfo: ChainInfo): Promise<KeplrWallet> {
const keplrClient = window.keplr;

Expand Down
2 changes: 1 addition & 1 deletion react-app/src/clients/walletConnectClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class WalletConnectWallet extends BaseWallet {
provider: WalletProvider,
connector: WalletConnect
) {
super(chainInfo, offlineSigner, provider);
super("walletConnect", chainInfo, offlineSigner, provider);
this.connector = connector;
}

Expand Down
18 changes: 18 additions & 0 deletions react-app/src/providers/WalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { AccountData } from "@cosmjs/proto-signing";
import { toast } from "react-toastify";
import * as ReactGA from "react-ga";
import { KeplrWallet } from "../clients/keplrClient";
import {
BaseWallet,
Expand Down Expand Up @@ -118,6 +119,11 @@ const WalletProvider: React.FC<WalletProviderProps> = (props) => {
);
})
.finally(() => {
ReactGA.event({
category: "Wallet",
action: "Disconnect",
label: activeWallet.type,
});
setActiveWallet(null);
setAutoConnectWalletType(null);
});
Expand Down Expand Up @@ -148,6 +154,12 @@ const WalletProvider: React.FC<WalletProviderProps> = (props) => {
setAccountBalance(accountBalance);
setAutoConnectWalletType(AutoConnectWalletType.Keplr);
setWalletStatus(ConnectionStatus.Connected);

ReactGA.event({
category: "Wallet",
action: "Connect",
label: wallet.type,
});
// Validate the token in case the logged in wallet has a different account than the authenticated address
await authAPI.validate(account.address);
} catch (err: unknown) {
Expand Down Expand Up @@ -190,6 +202,12 @@ const WalletProvider: React.FC<WalletProviderProps> = (props) => {
setAccountBalance(accountBalance);
setAutoConnectWalletType(AutoConnectWalletType.WalletConnect);
setWalletStatus(ConnectionStatus.Connected);

ReactGA.event({
category: "Wallet",
action: "Connect",
label: wallet.type,
});
// Validate the token in case the logged in wallet has a different account than the authenticated address
await authAPI.validate(account.address);
} catch (err: unknown) {
Expand Down

0 comments on commit d6350da

Please sign in to comment.