mirror of
https://github.com/jordienr/zenblog.git
synced 2026-09-27 01:30:30 -04:00
test tinybird
This commit is contained in:
@@ -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" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user