Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare landing for public #482

Merged
merged 11 commits into from
Jun 24, 2024
72 changes: 71 additions & 1 deletion site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"@mdx-js/react": "^3.0.0",
"clsx": "^2.0.0",
"framer-motion": "^11.2.11",
"i18next": "^23.11.5",
"plausible-tracker": "^0.3.9",
"prism-react-renderer": "^2.3.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react-dom": "^18.0.0",
"react-i18next": "^14.1.2"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.4.0",
Expand Down
57 changes: 57 additions & 0 deletions site/src/components/Analytics/AnalyticsProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { PropsWithChildren, useCallback, useEffect, useState } from 'react';
import Plausible from 'plausible-tracker';
import { PlausibleInitOptions } from 'plausible-tracker/build/main/lib/tracker';

import { analyticsContext, IAnalyticsContext } from './useAnalyticsContext';

export const AnalyticsProvider = ({
options,
children,
}: PropsWithChildren<{ options: PlausibleInitOptions }>) => {
const [plausible] = useState(() => Plausible(options));

const trackEvent: IAnalyticsContext['trackEvent'] = useCallback(
(eventName, props) => {
const timestamp = performance.now();
plausible.trackEvent(eventName, {
// Additional props for every event
props: {
// Current location
location: location.toString(),
// Time since visit page
timestamp: timestamp,
timestampSeconds: timestamp / 1000,
...props,
},
});
},
[plausible],
);

// Setup default analytic listeners
useEffect(() => {
plausible.enableAutoPageviews();
plausible.enableAutoOutboundTracking();

// Track clicks
document.body.addEventListener('click', (event: MouseEvent) => {
// Explore click targets to find a link element
const targets = event?.composedPath() || [event.target];
for (const target of targets) {
if (!(target instanceof HTMLAnchorElement)) continue;

trackEvent('Link click', {
url: target.href,
text: target.innerText,
});
break;
}
});
}, [plausible, trackEvent]);

return (
<analyticsContext.Provider value={{ trackEvent }}>
{children}
</analyticsContext.Provider>
);
};
17 changes: 17 additions & 0 deletions site/src/components/Analytics/useAnalyticsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createContext, useContext } from 'react';

export type IAnalyticsContext = {
trackEvent: (eventName: string, props: Record<string, string>) => void;
};

export const analyticsContext = createContext<IAnalyticsContext>(null as any);

export const useAnalyticsContext = () => {
const context = useContext(analyticsContext);
if (context === null)
throw new Error(
'Analytics context is not available. Make sure component is wrapped with AnalyticsProvider',
);

return context;
};
Loading
Loading