Skip to content

Commit 67634e4

Browse files
authored
Merge pull request #153 from vechain/jennifer/78-request---setup-analytics
Setup Analytics
2 parents 6fc987a + afdd9ca commit 67634e4

File tree

59 files changed

+2174
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2174
-197
lines changed

examples/homepage/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"build": "next build",
77
"clean": "rm -rf .next dist .turbo",
8-
"dev": "next dev"
8+
"dev": "next dev",
9+
"start": "next start"
910
},
1011
"dependencies": {
1112
"@chakra-ui/react": "2.8.2",

examples/homepage/src/app/components/features/Introduction/Introduction.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ import { CiLogin } from 'react-icons/ci';
2020
import { SiNpm, SiFarcaster } from 'react-icons/si';
2121
import { FcGoogle } from 'react-icons/fc';
2222
import { FaSquareXTwitter } from 'react-icons/fa6';
23+
import { trackEvent } from '@/app/lib/mixpanelClient';
2324

2425
export function Introduction() {
2526
const { connection } = useWallet();
27+
28+
const trackExternalLink = (destination: string) => {
29+
trackEvent('External Link Click', {
30+
destination,
31+
isConnected: connection.isConnected,
32+
source: 'Introduction',
33+
});
34+
};
35+
2636
const basePath = process.env.basePath ?? '';
2737
return (
2838
<Box
@@ -68,6 +78,9 @@ export function Introduction() {
6878
colorScheme="gray"
6979
size="lg"
7080
width="100%"
81+
onClick={() =>
82+
trackExternalLink('documentation')
83+
}
7184
>
7285
Get Started with our Docs
7386
</Button>
@@ -85,6 +98,7 @@ export function Introduction() {
8598
rel="noopener noreferrer"
8699
colorScheme="red"
87100
width="100%"
101+
onClick={() => trackExternalLink('npm')}
88102
>
89103
View Package on NPM
90104
</Button>
@@ -96,6 +110,7 @@ export function Introduction() {
96110
rel="noopener noreferrer"
97111
colorScheme="gray"
98112
width="100%"
113+
onClick={() => trackExternalLink('github')}
99114
>
100115
View GitHub Repository
101116
</Button>
@@ -114,6 +129,9 @@ export function Introduction() {
114129
rel="noopener noreferrer"
115130
variant="outline"
116131
width="100%"
132+
onClick={() =>
133+
trackExternalLink('smart-accounts')
134+
}
117135
>
118136
Learn about Smart Accounts
119137
</Button>
@@ -273,6 +291,11 @@ export function Introduction() {
273291
as={Link}
274292
href={app.href}
275293
isExternal
294+
onClick={() =>
295+
trackExternalLink(
296+
`example-app-${app.name.toLowerCase()}`,
297+
)
298+
}
276299
>
277300
<HStack
278301
align="start"

examples/homepage/src/app/constants.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,19 @@ export const b3trAbi = [
2424
type: 'function',
2525
},
2626
] as const;
27+
28+
export const ENV = {
29+
isDevelopment: process.env.NEXT_PUBLIC_NETWORK_TYPE === 'test',
30+
isProduction: process.env.NEXT_PUBLIC_NETWORK_TYPE === 'main',
31+
};
32+
33+
export const PROJECT_TOKENS = {
34+
development: process.env.NEXT_PUBLIC_MIXPANEL_TOKEN,
35+
production: process.env.NEXT_PUBLIC_MIXPANEL_TOKEN,
36+
};
37+
38+
export const HOMEPAGE_MIXPANEL_PROJECT_TOKEN = ENV.isProduction
39+
? PROJECT_TOKENS.production
40+
: PROJECT_TOKENS.development;
41+
42+
export const HOMEPAGE_MIXPANEL_PROJECT_NAME = 'homepage';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import mixpanel from 'mixpanel-browser';
2+
import {
3+
ENV,
4+
HOMEPAGE_MIXPANEL_PROJECT_NAME,
5+
HOMEPAGE_MIXPANEL_PROJECT_TOKEN,
6+
} from '../constants';
7+
8+
let homepageInstance: any;
9+
10+
if (typeof window !== 'undefined' && HOMEPAGE_MIXPANEL_PROJECT_TOKEN) {
11+
homepageInstance = mixpanel.init(
12+
HOMEPAGE_MIXPANEL_PROJECT_TOKEN,
13+
{
14+
debug: !ENV.isProduction,
15+
},
16+
HOMEPAGE_MIXPANEL_PROJECT_NAME,
17+
);
18+
}
19+
20+
const trackEvent = (event: string, properties: Record<string, any> = {}) => {
21+
homepageInstance?.track(event, properties);
22+
};
23+
24+
const resetUser = () => {
25+
homepageInstance?.reset();
26+
};
27+
28+
export { trackEvent, resetUser };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type ExternalLinkDestination =
2+
| 'documentation'
3+
| 'npm'
4+
| 'github'
5+
| 'smart-accounts'
6+
| `example-app-${string}`;
7+
8+
export interface ExternalLinkProperties {
9+
destination: ExternalLinkDestination;
10+
isConnected: boolean;
11+
source: string;
12+
}

examples/homepage/src/app/pages/Home.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { type ReactElement, useRef, useState } from 'react';
3+
import { type ReactElement, useEffect, useRef, useState } from 'react';
44
import {
55
Container,
66
VStack,
@@ -24,6 +24,7 @@ import { DataReadingExample } from '../components/features/DataReading';
2424
import { VechainLogo } from '@vechain/vechain-kit/assets';
2525
import { LoginUIControl } from '../components/features/LoginUIControl/LoginUIControl';
2626
import { LoginToContinueBox } from '../components/features/LoginToContinueBox';
27+
import { trackEvent } from '@/app/lib/mixpanelClient';
2728

2829
export default function Home(): ReactElement {
2930
const { account } = useWallet();
@@ -37,6 +38,10 @@ export default function Home(): ReactElement {
3738
setHasScrolled(true);
3839
};
3940

41+
useEffect(() => {
42+
trackEvent('Home Page Viewed');
43+
}, []);
44+
4045
if (!account) {
4146
return (
4247
<Container

examples/next-template/src/app/pages/Home.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { LanguageSelector } from '@/app/components/features/LanguageSelector';
1111
import { TransactionExamples } from '@/app/components/features/TransactionExamples';
1212
import { SigningExample } from '@/app/components/features/SigningExample/SigningExample';
1313
import { WelcomeSection } from '../components/features/WelcomeSection';
14+
import mixpanelClient from '@/lib/mixpanelClient';
1415

1516
export default function Home(): ReactElement {
1617
const { account, connection } = useWallet();
@@ -27,6 +28,8 @@ export default function Home(): ReactElement {
2728
);
2829
}
2930

31+
mixpanelClient.trackEvent('Home Page Viewed');
32+
3033
return (
3134
<Container
3235
height={'full'}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import mixpanel from 'mixpanel-browser';
2+
3+
const MIXPANEL_PROJECT_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL_TOKEN || '';
4+
5+
// Ensure Mixpanel is initialized only once
6+
if (!MIXPANEL_PROJECT_TOKEN) {
7+
mixpanel.init(MIXPANEL_PROJECT_TOKEN, { debug: true });
8+
}
9+
10+
// **Core Function to Track Events**
11+
const trackEvent = (event, properties = {}) => {
12+
mixpanel.track(event, properties);
13+
};
14+
15+
// **Reset User Data (For Logout)**
16+
const resetUser = () => {
17+
mixpanel.reset();
18+
};
19+
20+
// **Exporting All Tracking Methods**
21+
export default {
22+
trackEvent,
23+
resetUser,
24+
};

examples/next-template/tsconfig.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
],
2222
"paths": {
2323
"@/*": ["./src/*"],
24-
"@vechain-kit/*": ["../../packages/vechain-kit/src/*"],
24+
"@vechain-kit/*": ["../../packages/vechain-kit/src/*"]
2525
}
2626
},
2727
"include": [
2828
"next-env.d.ts",
2929
"**/*.ts",
3030
"**/*.tsx",
3131
".next/types/**/*.ts",
32-
"dist/types/**/*.ts"
32+
"dist/types/**/*.ts",
33+
"src/lib/mixpanelClient.js"
3334
],
34-
"exclude": ["node_modules"],
35-
35+
"exclude": ["node_modules"]
3636
}

packages/vechain-kit/eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default tseslint.config({
77
rules: {
88
'@typescript-eslint/no-explicit-any': 'off',
99
'@typescript-eslint/ban-ts-comment': 'off',
10-
'no-console': ['error', { allow: ['error'] }],
10+
'no-console': ['error', { allow: ['error', 'info', 'warn'] }],
1111
'eslint-comments/no-unused-disable': 'off',
1212
'import/no-anonymous-default-export': 'off',
1313
'@typescript-eslint/no-unused-vars': [

0 commit comments

Comments
 (0)