Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat: Add pixel
Richie McIlroy51620ba886cf
diff --git a/apps/web/app/Layout/MetaPixel.tsx b/apps/web/app/Layout/MetaPixel.tsxnew file mode 100644index 000000000..c0b64d55e--- /dev/null+++ b/apps/web/app/Layout/MetaPixel.tsx@@ -0,0 +1,66 @@+"use client";++import { buildEnv } from "@cap/env";+import Script from "next/script";+import { useId, useRef } from "react";++declare global {+ interface Window {+ fbq: (command: string, ...args: unknown[]) => void;+ _fbq: unknown;+ }+}++export function MetaPixel() {+ const pixelInitialized = useRef(false);+ const pixelId = buildEnv.NEXT_PUBLIC_META_PIXEL_ID;+ const scriptId = useId();++ if (!pixelId) {+ return null;+ }++ return (+ <>+ <Script+ id={scriptId}+ strategy="afterInteractive"+ src="https://connect.facebook.net/en_US/fbevents.js"+ onLoad={() => {+ if (+ !pixelInitialized.current &&+ typeof window !== "undefined" &&+ window.fbq &&+ pixelId+ ) {+ window.fbq("init", pixelId);+ window.fbq("track", "PageView");+ pixelInitialized.current = true;+ }+ }}+ />+ <noscript>+ <img+ height="1"+ width="1"+ style={{ display: "none" }}+ src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`}+ alt=""+ />+ </noscript>+ </>+ );+}++export function trackMetaEvent(+ eventName: string,+ parameters?: Record<string, unknown>+) {+ if (typeof window !== "undefined" && window.fbq) {+ try {+ window.fbq("track", eventName, parameters);+ } catch (error) {+ console.error(`Error tracking Meta event ${eventName}:`, error);+ }+ }+}diff --git a/apps/web/app/api/webhooks/stripe/route.ts b/apps/web/app/api/webhooks/stripe/route.tsindex 675cc9db9..b99fe63b2 100644--- a/apps/web/app/api/webhooks/stripe/route.ts+++ b/apps/web/app/api/webhooks/stripe/route.ts@@ -124,9 +124,10 @@ export const POST = async (req: Request) => {}event = stripe().webhooks.constructEvent(buf, sig, webhookSecret);console.log(`✅ Event received: ${event.type}`);- } catch (err: any) {- console.log(`❌ Error message: ${err.message}`);- return new Response(`Webhook Error: ${err.message}`, { status: 400 });+ } catch (err: unknown) {+ const message = err instanceof Error ? err.message : String(err);+ console.log(`❌ Error message: ${message}`);+ return new Response(`Webhook Error: ${message}`, { status: 400 });}if (relevantEvents.has(event.type)) {@@ -149,8 +150,8 @@ export const POST = async (req: Request) => {metadata: "metadata" in customer ? customer.metadata : undefined,});- let foundUserId;- let customerEmail;+ let foundUserId: string | undefined;+ let customerEmail: string | null | undefined;if ("metadata" in customer) {foundUserId = customer.metadata.userId;@@ -263,8 +264,14 @@ export const POST = async (req: Request) => {price_id: subscription.items.data[0]?.price.id,quantity: inviteQuota,platform:- (session.metadata && (session.metadata as any).platform) ||- "web",+ typeof session.metadata === "object" &&+ session.metadata !== null &&+ "platform" in session.metadata &&+ typeof (session.metadata as Record<string, unknown>).platform ===+ "string"+ ? ((session.metadata as Record<string, unknown>)+ .platform as string)+ : "web",is_first_purchase: isFirstPurchase,is_guest_checkout: isGuestCheckout,},@@ -295,8 +302,8 @@ export const POST = async (req: Request) => {metadata: "metadata" in customer ? customer.metadata : undefined,});- let foundUserId;- let customerEmail;+ let foundUserId: string | undefined;+ let customerEmail: string | null | undefined;if ("metadata" in customer) {foundUserId = customer.metadata.userId;@@ -377,7 +384,7 @@ export const POST = async (req: Request) => {const customer = await stripe().customers.retrieve(subscription.customer as string,);- let foundUserId;+ let foundUserId: string | undefined;if ("metadata" in customer) {foundUserId = customer.metadata.userId;}diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsxindex 260600d46..47b3f10ff 100644--- a/apps/web/app/layout.tsx+++ b/apps/web/app/layout.tsx@@ -11,6 +11,7 @@ import { SonnerToaster } from "@/components/SonnerToastProvider";import { getBootstrapData } from "@/utils/getBootstrapData";import { PublicEnvContext } from "@/utils/public-env";import { AuthContextProvider } from "./Layout/AuthContext";+import { MetaPixel } from "./Layout/MetaPixel";import { PosthogIdentify } from "./Layout/PosthogIdentify";import {PostHogProvider,@@ -120,6 +121,7 @@ export default async function RootLayout({ children }: PropsWithChildren) {<SonnerToaster /><main className="w-full">{children}</main><PosthogIdentify />+ <MetaPixel /></ReactQueryProvider></PublicEnvContext></SessionProvider>diff --git a/apps/web/app/utils/analytics.ts b/apps/web/app/utils/analytics.tsindex 12ba177ed..a5b01d795 100644--- a/apps/web/app/utils/analytics.ts+++ b/apps/web/app/utils/analytics.ts@@ -1,5 +1,6 @@import posthog from "posthog-js";import * as uuid from "uuid";+import { trackMetaEvent } from "../Layout/MetaPixel";export function initAnonymousUser() {try {@@ -42,6 +43,16 @@ export function trackEvent(}posthog.capture(eventName, { ...properties, platform: "web" });++ const metaEventMap: Record<string, string> = {+ "purchase_completed": "Purchase",+ "subscription_purchased": "Purchase",+ };++ const metaEventName = metaEventMap[eventName];+ if (metaEventName) {+ trackMetaEvent(metaEventName, properties);+ }} catch (error) {console.error(`Error tracking event ${eventName}:`, error);}diff --git a/packages/env/build.ts b/packages/env/build.tsindex af510a9d8..56dc5d653 100644--- a/packages/env/build.ts+++ b/packages/env/build.ts@@ -10,6 +10,7 @@ export const buildEnv = createEnv({NEXT_PUBLIC_IS_CAP: z.string().optional(),NEXT_PUBLIC_POSTHOG_KEY: z.string().optional(),NEXT_PUBLIC_POSTHOG_HOST: z.string().optional(),+ NEXT_PUBLIC_META_PIXEL_ID: z.string().optional(),NEXT_PUBLIC_WEB_URL: z.string(),NEXT_PUBLIC_CAP_AWS_BUCKET: z.string(),NEXT_PUBLIC_CAP_AWS_REGION: z.string(),@@ -21,6 +22,7 @@ export const buildEnv = createEnv({NEXT_PUBLIC_IS_CAP: process.env.NEXT_PUBLIC_IS_CAP,NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,NEXT_PUBLIC_POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST,+ NEXT_PUBLIC_META_PIXEL_ID: process.env.NEXT_PUBLIC_META_PIXEL_ID,NEXT_PUBLIC_WEB_URL: process.env.WEB_URL ?? process.env.NEXT_PUBLIC_WEB_URL,NEXT_PUBLIC_CAP_AWS_BUCKET:process.env.CAP_AWS_BUCKET ?? process.env.NEXT_PUBLIC_CAP_AWS_BUCKET,