Skip to content

Commit

Permalink
chore: add plausible event tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
ImLunaHey committed Jan 9, 2025
1 parent 2d7b179 commit f4a3cfe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/components/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from './ui/dropdown-menu';
import { memo, useState } from 'react';
import { toast } from 'sonner';
import { usePlausible } from '@/hooks/usePlausible';

const contextToText = (context: string) => {
if (context === 'following') return 'following';
Expand All @@ -61,6 +62,7 @@ const BetterContext = ({ context }: { context?: string }) => {
};

const PostDropdownMenu = ({ post, setTranslatedText }: { post: BSkyPost; setTranslatedText: (text: string) => void }) => {
const { trackEvent } = usePlausible();
const isProd = window.location.hostname === 'akari.blue';
const handleTranslate = async (event: React.MouseEvent<HTMLDivElement>) => {
event.stopPropagation();
Expand All @@ -87,6 +89,7 @@ const PostDropdownMenu = ({ post, setTranslatedText }: { post: BSkyPost; setTran
translatedText: string;
};
setTranslatedText(json.translatedText);
trackEvent('translate', { language: source });
};

return (
Expand All @@ -104,6 +107,7 @@ const PostDropdownMenu = ({ post, setTranslatedText }: { post: BSkyPost; setTran
event.stopPropagation();
navigator.clipboard.writeText(post.record.text);
toast.info('Copied post text to clipboard');
trackEvent('copyToClipboard', { type: 'post-text' });
}}
>
{'copy post text'} <ClipboardIcon />
Expand All @@ -125,6 +129,7 @@ const PostDropdownMenu = ({ post, setTranslatedText }: { post: BSkyPost; setTran
`https://akari.blue/profile/${post.author.handle}/post/${post.uri.split('/').pop()}`,
);
toast.info('Copied post link to clipboard');
trackEvent('copyToClipboard', { type: 'post-link' });
}}
>
{'copy link to post'} <ShareIcon />
Expand Down
35 changes: 35 additions & 0 deletions src/hooks/usePlausible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type Events = {
translate: { language: string };
copyToClipboard: { type: 'post-text' | 'post-link' };
};

declare global {
interface Window {
plausible: {
(...args: unknown[]): void;
q: unknown[];
};
}
}
const plausible = (window.plausible =
window.plausible ||
function (...args: unknown[]) {
(window.plausible.q = window.plausible.q || []).push(args);
});

function trackEvent<EventType extends keyof Events>(
eventType: EventType,
...args: Events[EventType] extends never ? [] : [Events[EventType]]
) {
if (args.length > 0) {
plausible(eventType, { props: args[0] });
} else {
plausible(eventType);
}
}

export const usePlausible = () => {
return {
trackEvent,
};
};

0 comments on commit f4a3cfe

Please sign in to comment.