test tinybird

This commit is contained in:
Jordi Enric
2024-05-03 19:59:08 +02:00
parent a7669a327e
commit 40e6acfaf3
2 changed files with 42 additions and 1 deletions
+33
View File
@@ -0,0 +1,33 @@
export function sendViewEvent({
blog_id,
post_id,
post_slug,
post_title,
}: {
blog_id: string;
post_id: string;
post_slug: string;
post_title: string;
}) {
fetch(
"https://api.eu-central-1.aws.tinybird.co/v0/events?name=events_example",
{
method: "POST",
body: JSON.stringify({
timestamp: new Date().toISOString(),
blog_id,
post_id,
post_slug,
post_title,
event_type: "post_view",
}),
headers: {
Authorization: `Bearer ${process.env.TINYBIRD_TOKEN}`,
},
}
).then((res) => {
if (!res.ok) {
throw new Error("Failed to send event");
}
});
}
@@ -1,3 +1,4 @@
import { sendViewEvent } from "@/analytics";
import { createAdminClient } from "@/lib/server/supabase";
import { NextApiRequest, NextApiResponse } from "next";
@@ -19,7 +20,7 @@ export default async function handler(
const postPromise = db
.from("posts")
.select(
"slug, title, content, cover_image, published_at, created_at, updated_at, metadata"
"id, slug, title, content, cover_image, published_at, created_at, updated_at, metadata"
)
.eq("blog_id", blogId)
.eq("published", true)
@@ -46,6 +47,13 @@ export default async function handler(
.eq("user_id", ownerId)
.single();
sendViewEvent({
blog_id: blogId,
post_id: post?.id || "",
post_slug: post?.slug || "",
post_title: post?.title || "",
});
if (subscription?.status !== "active") {
return res.status(401).json({ error: "UNAUTHORIZED" });
}