Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

✨ feat: Add basic event tracking

Dillion Verma5d310beea12b
diff --git a/components/copy-button.tsx b/components/copy-button.tsx
index c359b284..4f55db46 100644
--- a/components/copy-button.tsx
+++ b/components/copy-button.tsx
@@ -5,21 +5,16 @@ import * as React from "react";
// import { NpmCommands } from "types/unist";
import { Icons } from "@/components/icons";
+import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-// import { Event, trackEvent } from "@/lib/events";
-import { Button } from "@/components/ui/button";
+import { Event, trackEvent } from "@/lib/events";
import { cn } from "@/lib/utils";
-interface Event {
- name: string;
- properties?: Record<string, any>;
-}
-
interface CopyButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
value: string;
src?: string;
@@ -29,7 +24,7 @@ interface CopyButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
async function copyToClipboardWithMeta(value: string, event?: Event) {
navigator.clipboard.writeText(value);
if (event) {
- // trackEvent(event);
+ trackEvent(event);
}
}
@@ -63,6 +58,7 @@ export function CopyButton({
? {
name: event,
properties: {
+ name: src!,
code: value,
},
}
diff --git a/components/mdx-components.tsx b/components/mdx-components.tsx
index 1739c852..22bb5fc0 100644
--- a/components/mdx-components.tsx
+++ b/components/mdx-components.tsx
@@ -1,3 +1,4 @@
+import { Event } from "@/lib/events";
import { cn } from "@/lib/utils";
import { useMDXComponent } from "next-contentlayer/hooks";
import Image from "next/image";
@@ -78,15 +79,17 @@ const components = {
// __yarnCommand__,
__withMeta__,
__src__,
- // __event__,
+ __event__,
// __style__,
+ __name__,
...props
}: React.HTMLAttributes<HTMLPreElement> & {
// __style__?: Style["name"]
__rawString__?: string;
__withMeta__?: boolean;
__src__?: string;
- // __event__?: Event["name"]
+ __event__?: Event["name"];
+ __name__?: string;
}) => {
return (
<>
@@ -101,7 +104,7 @@ const components = {
<CopyButton
value={__rawString__}
src={__src__}
- // event={__event__}
+ event={__event__}
className={cn("absolute right-4 top-4", __withMeta__ && "top-16")}
/>
)}
diff --git a/components/posthog-provider.tsx b/components/posthog-provider.tsx
index cb3c3ba8..6fe7ee57 100644
--- a/components/posthog-provider.tsx
+++ b/components/posthog-provider.tsx
@@ -5,12 +5,7 @@ import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { useEffect } from "react";
-if (
- typeof window !== "undefined" &&
- (!window.location.href.includes("automatic.chat/chats/") ||
- window.location.href.includes("share")) &&
- process.env.NODE_ENV !== "development"
-) {
+if (typeof window !== "undefined") {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_API_KEY!, {
api_host: "https://app.posthog.com",
session_recording: {
diff --git a/lib/events.ts b/lib/events.ts
new file mode 100644
index 00000000..4bdc4ab2
--- /dev/null
+++ b/lib/events.ts
@@ -0,0 +1,25 @@
+import posthog from "posthog-js";
+import { z } from "zod";
+
+const eventSchema = z.object({
+ name: z.enum([
+ "copy_npm_command",
+ "copy_usage_code",
+ "copy_source_code",
+ // "copy_primitive_code",
+ // "copy_theme_code",
+ ]),
+ // declare type AllowedPropertyValues = string | number | boolean | null
+ properties: z
+ .record(z.union([z.string(), z.number(), z.boolean(), z.null()]))
+ .optional(),
+});
+
+export type Event = z.infer<typeof eventSchema>;
+
+export function trackEvent(input: Event): void {
+ const event = eventSchema.parse(input);
+ if (event) {
+ posthog.capture(event.name, event.properties);
+ }
+}
diff --git a/lib/rehype-component.ts b/lib/rehype-component.ts
index bc1d5e48..bf3a9227 100644
--- a/lib/rehype-component.ts
+++ b/lib/rehype-component.ts
@@ -53,6 +53,9 @@ export function rehypeComponent() {
properties: {
className: ["language-tsx"],
},
+ data: {
+ meta: `event="copy_source_code"`,
+ },
children: [
{
type: "text",
@@ -107,6 +110,9 @@ export function rehypeComponent() {
properties: {
className: ["language-tsx"],
},
+ data: {
+ meta: `event="copy_usage_code"`,
+ },
children: [
{
type: "text",