add short url event track
This commit is contained in:
@@ -50,4 +50,6 @@ WR.DO provide a **free DNS record** management service that can help you quickly
|
||||
## Examples
|
||||
|
||||
- [Vercel custom domain](/docs/examples/vercel)
|
||||
- [Zeabur custom domain](/docs/examples/zeabur)
|
||||
- [Zeabur custom domain](/docs/examples/zeabur)
|
||||
|
||||
WR.DO provides limited free domain name resolution. If you need more subdomain resolution, please contact the administrator email `support@wr.do` .
|
||||
39
lib/umami.ts
Normal file
39
lib/umami.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
const UMAMI_CONFIG = {
|
||||
websiteId: "56549e9d-61df-470d-a1b1-cbf12cfafe9d",
|
||||
endpoint: "https://umami.oiov.dev/api/send",
|
||||
};
|
||||
|
||||
export async function trackUmamiEvent(
|
||||
req: Request,
|
||||
eventName: string,
|
||||
eventData: any,
|
||||
) {
|
||||
try {
|
||||
const ua = req.headers.get("user-agent") || "";
|
||||
// console.log("Umami tracking event:", eventName, eventData);
|
||||
|
||||
await fetch(UMAMI_CONFIG.endpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": ua,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: "event",
|
||||
payload: {
|
||||
name: eventName,
|
||||
website: UMAMI_CONFIG.websiteId,
|
||||
url: new URL(req.url).pathname,
|
||||
data: eventData,
|
||||
referrer: `${eventData.referer}?tracker=umami`,
|
||||
hostname: new URL(req.url).hostname,
|
||||
language: eventData.language,
|
||||
screen: "1000x1000",
|
||||
},
|
||||
}),
|
||||
}).catch((err) => console.error("Umami tracking error:", err));
|
||||
} catch (error) {
|
||||
// 只记录错误,不影响主流程
|
||||
console.error("Failed to track Umami event:", error);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { auth } from "auth";
|
||||
import UAParser from "ua-parser-js";
|
||||
|
||||
import { siteConfig } from "./config/site";
|
||||
import { trackUmamiEvent } from "./lib/umami";
|
||||
|
||||
// export { auth as middleware } from "auth";
|
||||
|
||||
@@ -25,6 +26,7 @@ export default auth(async (req) => {
|
||||
const device = parser.getDevice();
|
||||
|
||||
const referer = req.headers.get("referer") || "(None)";
|
||||
const slug = match[1];
|
||||
|
||||
const res = await fetch(`${siteConfig.url}/api/s`, {
|
||||
method: "POST",
|
||||
@@ -32,7 +34,7 @@ export default auth(async (req) => {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
slug: match[1],
|
||||
slug,
|
||||
referer,
|
||||
ip,
|
||||
city: geo?.city,
|
||||
@@ -54,6 +56,15 @@ export default auth(async (req) => {
|
||||
);
|
||||
}
|
||||
|
||||
await trackUmamiEvent(req, "short_link_redirect", {
|
||||
slug,
|
||||
referer,
|
||||
country: geo?.country,
|
||||
browser: browser.name || "Unknown",
|
||||
device: device.model || "Unknown",
|
||||
language: userLanguage,
|
||||
});
|
||||
|
||||
const target = await res.json();
|
||||
if (!target) {
|
||||
return NextResponse.redirect(
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user