Skip to content

Commit

Permalink
custom useAnalytics hook, move plausibleprovider to proviers
Browse files Browse the repository at this point in the history
  • Loading branch information
buckhalt committed Nov 6, 2023
1 parent fc797be commit 255e821
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
13 changes: 4 additions & 9 deletions app/(dashboard)/dashboard/_components/ProtocolUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { ChevronDown, ChevronUp } from 'lucide-react';
import type { FileWithPath } from 'react-dropzone';
import { generateReactHelpers } from '@uploadthing/react/hooks';
import { useState, useCallback } from 'react';
import { usePlausible } from 'next-plausible';

import { importProtocol } from '../_actions/importProtocol';
import { Button } from '~/components/ui/Button';

Expand All @@ -22,6 +20,7 @@ import type { UploadFileResponse } from 'uploadthing/client';
import { Collapsible, CollapsibleContent } from '~/components/ui/collapsible';
import ActiveProtocolSwitch from '~/app/(dashboard)/dashboard/_components/ActiveProtocolSwitch';
import { api } from '~/trpc/client';
import { useAnalytics } from '~/hooks/useAnalytics';

export default function ProtocolUploader({
onUploaded,
Expand All @@ -36,11 +35,10 @@ export default function ProtocolUploader({
progress: true,
error: 'dsfsdf',
});
const plausible = usePlausible();

const utils = api.useUtils();

const appSettings = api.appSettings.get.useQuery();
const trackEvent = useAnalytics();

const handleUploadComplete = async (
res: UploadFileResponse[] | undefined,
Expand Down Expand Up @@ -70,11 +68,8 @@ export default function ProtocolUploader({
}

await utils.protocol.get.lastUploaded.refetch();
plausible('ProtocolImported', {
props: {
installationID: appSettings?.data?.installationId,
},
});

trackEvent('ProtocolImported', appSettings?.data?.installationId);

setDialogContent({
title: 'Protocol import',
Expand Down
8 changes: 3 additions & 5 deletions app/(onboard)/_components/OnboardSteps/Documentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ import { Card, CardContent, CardHeader, CardTitle } from '~/components/ui/card';
import { FileText, MonitorPlay } from 'lucide-react';
import { setAppConfigured } from '~/app/_actions';
import SubmitButton from '~/components/ui/SubmitButton';
import { usePlausible } from 'next-plausible';
import { api } from '~/trpc/client';
import { useAnalytics } from '~/hooks/useAnalytics';

function Documentation() {
const appSettings = api.appSettings.get.useQuery();
const trackEvent = useAnalytics();

const plausible = usePlausible();
const handleAppConfigured = async () => {
await setAppConfigured();

plausible('AppSetup', {
props: { installationID: appSettings?.data?.installationId },
});
trackEvent('AppSetup', appSettings?.data?.installationId);
};

return (
Expand Down
15 changes: 0 additions & 15 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { getServerSession } from '~/utils/auth';
import { api } from '~/trpc/server';
import { Toaster } from '~/components/ui/toaster';
import { revalidatePath, revalidateTag } from 'next/cache';
import PlausibleProvider from 'next-plausible';
import { env } from '~/env.mjs';

export const metadata = {
title: 'Network Canvas Fresco',
Expand All @@ -29,19 +27,6 @@ async function RootLayout({ children }: { children: React.ReactNode }) {

return (
<html lang="en">
<head>
<PlausibleProvider
domain="fresco.networkcanvas.com"
taggedEvents={true}
manualPageviews={true}
trackLocalhost={env.NODE_ENV === 'development' ? true : false}
// for testing, remove env.NODE_ENV === 'production' enable tracking in dev
enabled={appSettings?.allowAnalytics && env.NODE_ENV === 'production'}
// Uncomment the following lines to use self hosted
// selfHosted={true}
// customDomain="https://analytics.networkcanvas.com"
/>
</head>
<body>
<RedirectWrapper
configured={!!appSettings?.configured}
Expand Down
13 changes: 13 additions & 0 deletions hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { usePlausible } from 'next-plausible';

export function useAnalytics() {
const plausible = usePlausible();

const trackEvent = (eventName: string, installationId?: string) => {
installationId
? plausible(eventName, { props: { installationId } })
: plausible(eventName);
};

return trackEvent;
}
21 changes: 19 additions & 2 deletions providers/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@ import { TRPCReactProvider } from '~/trpc/client';
import { SessionProvider } from '~/providers/SessionProvider';
import type { Session } from 'lucia';
import { headers } from 'next/headers';
import PlausibleProvider from 'next-plausible';
import { env } from 'process';
import { api } from '~/trpc/server';

export default function Providers({
export default async function Providers({
children,
initialSession,
}: {
children: React.ReactNode;
initialSession: Session | null;
}): ReactElement {
}): Promise<ReactElement> {
const appSettings = await api.appSettings.get.query();

return (
<TRPCReactProvider headers={headers()}>
<ReactQueryDevtools initialIsOpen={true} />
<SessionProvider session={initialSession}>{children}</SessionProvider>
<PlausibleProvider
domain="fresco.networkcanvas.com"
taggedEvents={true}
manualPageviews={true}
trackLocalhost={env.NODE_ENV === 'development' ? true : false}
enabled={appSettings?.allowAnalytics}
// for production, add env.NODE_ENV === 'production' to disable tracking in dev
//enabled={appSettings?.allowAnalytics && env.NODE_ENV === 'production'}
// Uncomment the following lines to use self hosted
// selfHosted={true}
// customDomain="https://analytics.networkcanvas.com"
/>
</TRPCReactProvider>
);
}

0 comments on commit 255e821

Please sign in to comment.