Skip to content

Commit

Permalink
feat(widget-configurator): google analytics events (#3335)
Browse files Browse the repository at this point in the history
* feat(widget-configurator): google analytics events

* chore: log

* chore: remove logs

* chore: update events
  • Loading branch information
shoom3301 authored Nov 7, 2023
1 parent 60a16ec commit 9b1074e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
2 changes: 2 additions & 0 deletions apps/widget-configurator/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Analytics
#REACT_APP_GOOGLE_ANALYTICS_ID=
6 changes: 6 additions & 0 deletions apps/widget-configurator/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#######################################
# Development
#######################################

# Enables mock mode (default = false)
REACT_APP_MOCK=false
22 changes: 22 additions & 0 deletions apps/widget-configurator/src/app/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { sendEvent, Category } from '@cowprotocol/analytics'

export function connectWalletToConfigurator() {
sendEvent({
category: Category.WIDGET_CONFIGURATOR,
action: 'Connect wallet',
})
}

export function viewEmbedCode() {
sendEvent({
category: Category.WIDGET_CONFIGURATOR,
action: 'View code',
})
}

export function copyEmbedCode() {
sendEvent({
category: Category.WIDGET_CONFIGURATOR,
action: 'Copy code',
})
}
10 changes: 9 additions & 1 deletion apps/widget-configurator/src/app/configurator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ConfiguratorState } from './types'

import { ColorModeContext } from '../../theme/ColorModeContext'
import { web3Modal } from '../../wagmiConfig'
import { connectWalletToConfigurator } from '../analytics'
import { EmbedDialog } from '../embedDialog'

const DEFAULT_STATE = {
Expand Down Expand Up @@ -57,7 +58,7 @@ export function Configurator({ title }: { title: string }) {
const [buyToken] = buyTokenState
const [buyTokenAmount] = buyTokenAmountState

const { isDisconnected } = useAccount()
const { isDisconnected, isConnected } = useAccount()
const network = useNetwork()

const walletChainId = network.chain?.id
Expand Down Expand Up @@ -85,6 +86,13 @@ export function Configurator({ title }: { title: string }) {
web3Modal.setThemeMode(mode)
}, [mode])

// Fire an event to GA when user connect a wallet
useEffect(() => {
if (isConnected) {
connectWalletToConfigurator()
}
}, [isConnected])

return (
<Box sx={WrapperStyled}>
{!isDrawerOpen && (
Expand Down
4 changes: 4 additions & 0 deletions apps/widget-configurator/src/app/embedDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { reactExample } from './utils/reactExample'
import { vanilaNpmExample } from './utils/vanilaNpmExample'
import { vanillaNoDepsExample } from './utils/vanillaNoDepsExample'

import { copyEmbedCode, viewEmbedCode } from '../analytics'

function a11yProps(index: number) {
return {
id: `simple-tab-${index}`,
Expand All @@ -38,6 +40,7 @@ export function EmbedDialog({ params }: EmbedDialogProps) {
const handleClickOpen = (scrollType: DialogProps['scroll']) => () => {
setOpen(true)
setScroll(scrollType)
viewEmbedCode()
}

const handleClose = () => {
Expand Down Expand Up @@ -65,6 +68,7 @@ export function EmbedDialog({ params }: EmbedDialogProps) {

const handleCopy = () => {
navigator.clipboard.writeText(code)
copyEmbedCode()
}

return (
Expand Down
32 changes: 21 additions & 11 deletions apps/widget-configurator/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/// <reference types="vitest" />
import react from '@vitejs/plugin-react-swc'
import { defineConfig } from 'vite'
import macrosPlugin from 'vite-plugin-babel-macros'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
import viteTsConfigPaths from 'vite-tsconfig-paths'

import { getReactProcessEnv } from '../../tools/getReactProcessEnv'

const plugins = [
nodePolyfills({
include: ['stream'],
Expand All @@ -14,24 +17,31 @@ const plugins = [
},
protocolImports: true,
}),
macrosPlugin(),
react(),
viteTsConfigPaths({
root: '../../',
}),
]

export default defineConfig({
cacheDir: '../../node_modules/.vite/widget-configurator',
export default defineConfig(({ mode }) => {
return {
define: {
...getReactProcessEnv(mode),
},

server: {
port: 4200,
host: 'localhost',
},
cacheDir: '../../node_modules/.vite/widget-configurator',

preview: {
port: 4300,
host: 'localhost',
},
server: {
port: 4200,
host: 'localhost',
},

preview: {
port: 4300,
host: 'localhost',
},

plugins,
plugins,
}
})
1 change: 1 addition & 0 deletions libs/analytics/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum Category {
SERVICE_WORKER = 'Service worker',
TWAP = 'TWAP',
COW_FORTUNE = 'CoWFortune',
WIDGET_CONFIGURATOR = 'Widget configurator',
}

export interface EventParams {
Expand Down

0 comments on commit 9b1074e

Please sign in to comment.