Skip to content

Commit 62eb63b

Browse files
add analytics with posthog (#2205)
1 parent 82ed96d commit 62eb63b

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ neardev
1414
.docz
1515
serve*
1616
.vscode
17+
.env.local

website/docusaurus.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const config = {
2626
fonts: {
2727
myFont: ['Inter', 'sans-serif'],
2828
},
29+
REACT_APP_PUBLIC_POSTHOG_KEY: process.env.REACT_APP_PUBLIC_POSTHOG_KEY,
30+
REACT_APP_PUBLIC_POSTHOG_HOST: process.env.REACT_APP_PUBLIC_POSTHOG_HOST
2931
},
3032
themes: ['@saucelabs/theme-github-codeblock', '@docusaurus/theme-mermaid'],
3133
onBrokenLinks: 'log',
@@ -141,7 +143,7 @@ const config = {
141143
label: 'Tools',
142144
position: 'left',
143145
items: [
144-
{ label: '🧰 All Tools', to: '/tools/welcome'},
146+
{ label: '🧰 All Tools', to: '/tools/welcome' },
145147
{
146148
type: 'html',
147149
value: '<hr/> <small class="subtitle"> Essentials </small>',

website/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"monaco-editor": "^0.44.0",
5656
"near-api-js": "^2.1.4",
5757
"near-social-vm": "github:NearSocial/VM#2.5.5",
58+
"posthog-js": "^1.155.0",
5859
"react": "^18.2.0",
5960
"react-bootstrap": "^2.9.1",
6061
"react-bootstrap-typeahead": "^6.3.2",

website/src/theme/Root.js

+35-15
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,55 @@ import Gleap from "gleap"; // See https://gleap.io/docs/javascript/ and https://
66
import { withRouter } from 'react-router-dom';
77
import { useHistory } from '@docusaurus/router';
88
import useIsBrowser from '@docusaurus/useIsBrowser'; // https://docusaurus.io/docs/advanced/ssg#useisbrowser
9-
9+
import { PostHogProvider } from 'posthog-js/react';
10+
import posthog from 'posthog-js';
11+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
1012

1113
function Root({ children, location }) {
1214
const isBrowser = useIsBrowser();
13-
1415
const history = useHistory();
16+
const { siteConfig: {customFields} } = useDocusaurusContext();
1517

1618
useEffect(() => {
17-
// pass message to dev.near.org (docs is embed there)
19+
// Pass message to dev.near.org (docs is embedded there)
1820
const sendMessage = url => parent.postMessage({ type: 'urlChange', url }, 'https://dev.near.org/');
1921
sendMessage(location.pathname);
2022

2123
const unlisten = history.listen(loc => sendMessage(loc.pathname));
2224
return () => { unlisten() };
2325
}, [history]);
2426

25-
if (isBrowser) {
26-
const { initRudderAnalytics, recordPageView } = require('./scripts/rudderstack');
27-
28-
Gleap.initialize('K2v3kvAJ5XtPzNYSgk4Ulpe5ptgBkIMv');
29-
30-
const rudderAnalytics = initRudderAnalytics();
31-
recordPageView(rudderAnalytics, location.pathname);
32-
}
33-
34-
return <>{children}</>;
27+
useEffect(() => {
28+
if (isBrowser) {
29+
const { initRudderAnalytics, recordPageView } = require('./scripts/rudderstack');
30+
31+
Gleap.initialize('K2v3kvAJ5XtPzNYSgk4Ulpe5ptgBkIMv');
32+
33+
const rudderAnalytics = initRudderAnalytics();
34+
recordPageView(rudderAnalytics, location.pathname);
35+
36+
// Initialize PostHog
37+
posthog.init(customFields.REACT_APP_PUBLIC_POSTHOG_KEY, {
38+
api_host: customFields.REACT_APP_PUBLIC_POSTHOG_HOST,
39+
});
40+
41+
// Track initial page view
42+
posthog.capture('$pageview');
43+
44+
// Track page views on route changes
45+
history.listen((location) => {
46+
posthog.capture('$pageview', { path: location.pathname });
47+
});
48+
}
49+
}, [isBrowser, history]);
50+
51+
return (
52+
<PostHogProvider client={posthog}>
53+
{children}
54+
</PostHogProvider>
55+
);
3556
}
3657

3758
const router = withRouter(Root);
3859

39-
40-
export default router;
60+
export default router;

website/yarn.lock

+24
Original file line numberDiff line numberDiff line change
@@ -7428,6 +7428,11 @@ feed@^4.2.2:
74287428
dependencies:
74297429
xml-js "^1.6.11"
74307430

7431+
fflate@^0.4.8:
7432+
version "0.4.8"
7433+
resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae"
7434+
integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA==
7435+
74317436
file-loader@^6.2.0:
74327437
version "6.2.0"
74337438
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-6.2.0.tgz#baef7cf8e1840df325e4390b4484879480eebe4d"
@@ -11386,6 +11391,20 @@ postcss@^8.4.24, postcss@^8.4.38:
1138611391
picocolors "^1.0.0"
1138711392
source-map-js "^1.2.0"
1138811393

11394+
posthog-js@^1.155.0:
11395+
version "1.155.0"
11396+
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.155.0.tgz#e3c50f5152ccfd41945be4f2ea39668f4fd18cae"
11397+
integrity sha512-gxi72Qcp7Vnq6efe5gNxsq84zyEFd33NUmoLSgcbMPhxU30qgc89Aw/N2mRB4mGrD3Mq0rCnDJUzGFdN59nR0g==
11398+
dependencies:
11399+
fflate "^0.4.8"
11400+
preact "^10.19.3"
11401+
web-vitals "^4.0.1"
11402+
11403+
preact@^10.19.3:
11404+
version "10.23.1"
11405+
resolved "https://registry.yarnpkg.com/preact/-/preact-10.23.1.tgz#d400107289bc979881c5212cb5f5cd22cd1dc38c"
11406+
integrity sha512-O5UdRsNh4vdZaTieWe3XOgSpdMAmkIYBCT3VhQDlKrzyCm8lUYsk0fmVEvoQQifoOjFRTaHZO69ylrzTW2BH+A==
11407+
1138911408
prebuild-install@^7.1.1:
1139011409
version "7.1.1"
1139111410
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.1.1.tgz#de97d5b34a70a0c81334fd24641f2a1702352e45"
@@ -13873,6 +13892,11 @@ web-namespaces@^2.0.0:
1387313892
resolved "https://registry.yarnpkg.com/web-namespaces/-/web-namespaces-2.0.1.tgz#1010ff7c650eccb2592cebeeaf9a1b253fd40692"
1387413893
integrity sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==
1387513894

13895+
web-vitals@^4.0.1:
13896+
version "4.2.3"
13897+
resolved "https://registry.yarnpkg.com/web-vitals/-/web-vitals-4.2.3.tgz#270c4baecfbc6ec6fc15da1989e465e5f9b94fb7"
13898+
integrity sha512-/CFAm1mNxSmOj6i0Co+iGFJ58OS4NRGVP+AWS/l509uIK5a1bSoIVaHz/ZumpHTfHSZBpgrJ+wjfpAOrTHok5Q==
13899+
1387613900
web-worker@^1.2.0:
1387713901
version "1.3.0"
1387813902
resolved "https://registry.yarnpkg.com/web-worker/-/web-worker-1.3.0.tgz#e5f2df5c7fe356755a5fb8f8410d4312627e6776"

0 commit comments

Comments
 (0)