Skip to content

Commit 2359059

Browse files
author
Overzunov
committed
feat(google analytics): add custom ga events
1 parent c2d892b commit 2359059

File tree

12 files changed

+117
-39
lines changed

12 files changed

+117
-39
lines changed

.github/env.develop

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ API_URL=https://api.dev.app.astrodao.com
55
NEAR_ENV=development
66
STATS_API_URL=https://dev.api.astrostats.magicpowered.io
77
APP_DOMAIN=dev.app.astrodao.com
8-
GOOGLE_ANALYTICS_KEY=G-2SWGMFRYJ8
8+
GOOGLE_ANALYTICS_KEY=G-0Q54E4Q8XL
99
NEAR_CONTRACT_NAME=sputnikv2.testnet

astro_2.0/components/ProposalCardRenderer/components/ProposalCard/ProposalCard.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { AmountBalanceCard } from 'astro_2.0/features/pages/nestedDaoPagesConten
3131

3232
import { Button } from 'components/button/Button';
3333

34+
import { GA_EVENTS, sendGAEvent } from 'utils/ga';
3435
import { DAOFormValues } from 'astro_2.0/features/CreateDao/components/types';
3536
import { DEFAULT_VOTE_GAS } from 'services/sputnik/constants';
3637
import { gasValidation } from 'astro_2.0/features/CreateProposal/helpers';
@@ -186,6 +187,17 @@ export const ProposalCard: React.FC<ProposalCardProps> = ({
186187
async (vote: VoteAction, gas?: string | number) => {
187188
try {
188189
await nearService?.vote(daoId, proposalId, vote, gas);
190+
191+
sendGAEvent({
192+
name: GA_EVENTS.ACT_PROPOSAL,
193+
daoId,
194+
accountId,
195+
params: {
196+
voteAction: vote,
197+
proposalId,
198+
},
199+
});
200+
189201
await router.reload();
190202
} catch (e) {
191203
// todo - handle errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { FC } from 'react';
2+
import Script from 'next/script';
3+
import { gtag, gtagScript } from 'constants/googleTagManager';
4+
import { configService } from 'services/ConfigService';
5+
import { CookieService } from 'services/CookieService';
6+
import { ACCOUNT_COOKIE } from 'constants/cookies';
7+
8+
export const AppMonitoring: FC = () => {
9+
const { appConfig } = configService.get();
10+
const accountId = CookieService.get(ACCOUNT_COOKIE);
11+
12+
if (appConfig?.GOOGLE_ANALYTICS_KEY) {
13+
return (
14+
<>
15+
<Script
16+
strategy="lazyOnload"
17+
id="gtag"
18+
src={gtag(appConfig.GOOGLE_ANALYTICS_KEY)}
19+
/>
20+
21+
<Script strategy="lazyOnload" id="gtagScript">
22+
{gtagScript(appConfig.GOOGLE_ANALYTICS_KEY, accountId)}
23+
</Script>
24+
</>
25+
);
26+
}
27+
28+
return null;
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './AppMonitoring';

astro_2.0/features/CreateDao/components/CustomEdit/CustomEdit.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const CustomEdit: FC<CustomEditProps> = ({ className }) => {
4141
const mappedArgs = jsonToBase64Str(result);
4242

4343
if (result?.config?.name) {
44-
await createDao(result.config.name, null, {
44+
await createDao(result.config.name, {
4545
args: mappedArgs,
4646
amountToTransfer: newDaoParams.amountToTransfer,
4747
gas: newDaoParams.gas,

astro_2.0/features/CreateDao/components/hooks.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from 'astro_2.0/features/CreateDao/components/helpers';
3131
import { useStateMachine } from 'little-state-machine';
3232
import { useWalletContext } from 'context/WalletContext';
33+
import { GA_EVENTS, sendGAEvent } from 'utils/ga';
3334

3435
type DaoFormStateReturn = {
3536
options: DaoSettingOption<
@@ -89,8 +90,7 @@ export function useCreateDao(): {
8990
uploadAssets: (defaultFlag: string) => Promise<string>;
9091
createDao: (
9192
daoName: string,
92-
data: CreateDaoInput | null,
93-
args?: CreateDaoCustomInput
93+
data: CreateDaoInput | CreateDaoCustomInput
9494
) => Promise<void>;
9595
} {
9696
const router = useRouter();
@@ -131,17 +131,18 @@ export function useCreateDao(): {
131131
);
132132

133133
const createDao = useCallback(
134-
async (
135-
daoName: string,
136-
data: CreateDaoInput | null,
137-
args?: CreateDaoCustomInput
138-
) => {
134+
async (daoName: string, data: CreateDaoInput | CreateDaoCustomInput) => {
139135
try {
140-
if (data) {
141-
await nearService?.createDao(data);
142-
} else if (args) {
143-
await nearService?.createDao(args);
144-
}
136+
await nearService?.createDao(data);
137+
138+
const { nearConfig } = configService.get();
139+
const daoId = `${daoName}.${nearConfig?.contractName ?? ''}`;
140+
141+
sendGAEvent({
142+
name: GA_EVENTS.CREATE_DAO,
143+
daoId,
144+
accountId,
145+
});
145146

146147
showNotification({
147148
type: NOTIFICATION_TYPES.INFO,
@@ -154,9 +155,7 @@ export function useCreateDao(): {
154155
getInitialValues(accountId, state.assets.defaultFlag)
155156
);
156157

157-
const { nearConfig } = configService.get();
158-
159-
await router.push(`/dao/${daoName}.${nearConfig?.contractName ?? ''}`);
158+
await router.push(`/dao/${daoId}`);
160159
} catch (error) {
161160
console.warn(error);
162161

astro_2.0/features/CreateProposal/CreateProposal.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { CustomTokensContext } from 'astro_2.0/features/CustomTokens/CustomToken
2828
import { getInitialProposalVariant } from 'astro_2.0/features/CreateProposal/createProposalHelpers';
2929
import { UserPermissions } from 'types/context';
3030

31+
import { GA_EVENTS, sendGAEvent } from 'utils/ga';
32+
3133
import { getFormInitialValues } from 'astro_2.0/features/CreateProposal/helpers/initialValues';
3234
import { getNewProposalObject } from 'astro_2.0/features/CreateProposal/helpers/newProposalObject';
3335
import {
@@ -218,6 +220,16 @@ export const CreateProposal: FC<CreateProposalProps> = ({
218220
return;
219221
}
220222

223+
sendGAEvent({
224+
name: GA_EVENTS.CREATE_PROPOSAL,
225+
daoId: dao.id,
226+
accountId,
227+
params: {
228+
variant: selectedProposalVariant,
229+
proposalId: newProposalId,
230+
},
231+
});
232+
221233
if (onCreate) {
222234
onCreate(newProposalId);
223235
}

astro_2.0/features/DaoDashboardHeader/components/hooks.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function useJoinDao(
2323
(async () => {
2424
if (
2525
!userPermissions.isCanCreateProposals ||
26-
daoMembers.includes(accountId)
26+
daoMembers.includes(accountId) ||
27+
!accountId
2728
) {
2829
return;
2930
}

constants/googleTagManager.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ export const gtag = (key: string): string => {
22
return `https://www.googletagmanager.com/gtag/js?id=${key}`;
33
};
44

5-
export const gtagScript = (key: string): string => {
5+
export const gtagScript = (key: string, accountId: string): string => {
66
return `
77
window.dataLayer = window.dataLayer || [];
88
function gtag(){dataLayer.push(arguments);}
99
gtag('js', new Date());
10+
1011
gtag('config', '${key}', {
11-
page_path: window.location.pathname,
12-
});
12+
page_path: window.location.pathname,
13+
user_id: '${accountId}',
14+
});
15+
16+
gtag('set', 'user_properties', {
17+
account_id: '${accountId}',
18+
});
1319
`;
1420
};

global.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ declare global {
2626
};
2727
near: SenderWalletInstance;
2828
APP_CONFIG: Config;
29+
gtag: (
30+
type: 'event',
31+
name: string,
32+
params: Record<string, string | number | string[]>
33+
) => void;
2934
}
3035
}
3136

pages/_app.tsx

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import React, { FunctionComponent, useEffect } from 'react';
22
import Head from 'next/head';
3-
import Script from 'next/script';
43
import { appWithTranslation } from 'next-i18next';
54
import type { AppContext, AppProps } from 'next/app';
65
import { withLDProvider } from 'launchdarkly-react-client-sdk';
76

87
import nextI18NextConfig from 'next-i18next.config';
98
import { appConfig as applicationConfig } from 'config';
109

11-
import { configService } from 'services/ConfigService';
12-
1310
import { ModalProvider } from 'components/modal';
1411
import { PageLayout } from 'astro_2.0/components/PageLayout';
1512
import { MobileNav } from 'astro_2.0/components/navigation/MobileNav';
@@ -23,12 +20,11 @@ import 'styles/globals.scss';
2320
import { useRouter } from 'next/router';
2421
import { WrappedWalletContext } from 'context/WalletContext';
2522
import { CookieService } from 'services/CookieService';
26-
import { gtag, gtagScript } from 'constants/googleTagManager';
2723
import { DAO_COOKIE, DEFAULT_OPTIONS } from 'constants/cookies';
24+
import { AppMonitoring } from 'astro_2.0/features/AppMonitoring/AppMonitoring';
2825

2926
function App({ Component, pageProps }: AppProps): JSX.Element | null {
3027
const router = useRouter();
31-
const { appConfig } = configService.get();
3228

3329
useEffect(() => {
3430
CookieService.set(DAO_COOKIE, router.query.dao, DEFAULT_OPTIONS);
@@ -38,19 +34,7 @@ function App({ Component, pageProps }: AppProps): JSX.Element | null {
3834

3935
return (
4036
<>
41-
{appConfig?.GOOGLE_ANALYTICS_KEY && (
42-
<>
43-
<Script
44-
strategy="lazyOnload"
45-
id="gtag"
46-
src={gtag(appConfig.GOOGLE_ANALYTICS_KEY)}
47-
/>
48-
49-
<Script strategy="lazyOnload" id="gtagScript">
50-
{gtagScript(appConfig.GOOGLE_ANALYTICS_KEY)}
51-
</Script>
52-
</>
53-
)}
37+
<AppMonitoring />
5438
<ModalProvider>
5539
<WrappedWalletContext>
5640
<SocketProvider>

utils/ga.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export enum GA_EVENTS {
2+
CREATE_PROPOSAL = 'create_proposal',
3+
ACT_PROPOSAL = 'act_proposal',
4+
CREATE_DAO = 'create_dao',
5+
}
6+
7+
type CustomGaEvent = {
8+
name: GA_EVENTS;
9+
accountId?: string;
10+
daoId?: string;
11+
params?: Record<string, string | number | string[]>;
12+
};
13+
14+
export function sendGAEvent({
15+
name,
16+
accountId,
17+
daoId,
18+
params = {},
19+
}: CustomGaEvent): void {
20+
if (!window || !window.gtag) {
21+
return;
22+
}
23+
24+
window.gtag('event', name, {
25+
...params,
26+
accountId: accountId ?? 'Not authorized',
27+
daoId: daoId ?? 'n/a',
28+
});
29+
}

0 commit comments

Comments
 (0)