Skip to content

Commit

Permalink
Don't track bot clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Oct 30, 2023
1 parent 6acc260 commit 9ee332a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 66 deletions.
2 changes: 1 addition & 1 deletion apps/web/lib/middleware/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default async function LinkMiddleware(

// only track the click when there is no `dub-no-track` header
if (!req.headers.get("dub-no-track")) {
ev.waitUntil(recordClick({ domain, req, key }));
ev.waitUntil(recordClick({ req, domain, key }));
}

const isBot = detectBot(req);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/lib/middleware/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function RootMiddleware(
}

// record clicks on root page
ev.waitUntil(recordClick({ domain, req }));
ev.waitUntil(recordClick({ req, domain }));

const response = await redis.get<{
target: string;
Expand Down
36 changes: 1 addition & 35 deletions apps/web/lib/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,7 @@ export const intervalData = {

export type LocationTabs = "country" | "city" | "region";

export type DeviceTabs = "device" | "browser" | "os" | "bot" | "ua";

export const uaToBot = (ua: string): string => {
if (!ua) return "Unknown Bot";
if (ua.includes("curl")) {
return "Curl Request";
} else if (ua.includes("Slackbot")) {
return "Slack Bot";
} else if (ua.includes("Twitterbot")) {
return "Twitter Bot";
} else if (ua.includes("facebookexternalhit")) {
return "Facebook Bot";
} else if (ua.includes("LinkedInBot")) {
return "LinkedIn Bot";
} else if (ua.includes("WhatsApp")) {
return "WhatsApp Bot";
} else if (ua.includes("TelegramBot")) {
return "Telegram Bot";
} else if (ua.includes("Discordbot")) {
return "Discord Bot";
} else if (ua.includes("Googlebot")) {
return "Google Bot";
} else if (ua.includes("Baiduspider")) {
return "Baidu Bot";
} else if (ua.includes("bingbot")) {
return "Bing Bot";
} else if (ua.includes("YandexBot")) {
return "Yandex Bot";
} else if (ua.includes("DuckDuckBot")) {
return "DuckDuckGo Bot";
} else {
return "Unknown Bot";
}
};
export type DeviceTabs = "device" | "browser" | "os" | "ua";

const VALID_TINYBIRD_ENDPOINTS = new Set([
"timeseries",
Expand All @@ -102,7 +69,6 @@ const VALID_TINYBIRD_ENDPOINTS = new Set([
"device",
"browser",
"os",
"bot",
"referer",
]);

Expand Down
9 changes: 7 additions & 2 deletions apps/web/lib/tinybird.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import { ipAddress } from "@vercel/edge";
import { NextRequest, userAgent } from "next/server";
import { conn } from "./planetscale";
import { ratelimit } from "./upstash";
import { detectBot } from "./middleware/utils";

/**
* Recording clicks with geo, ua, referer and timestamp data
* If key is not specified, record click as the root click ("_root", e.g. dub.sh, vercel.fyi)
**/
export async function recordClick({
domain,
req,
domain,
key,
}: {
domain: string;
req: NextRequest;
domain: string;
key?: string;
}) {
const isBot = detectBot(req);
if (isBot) {
return null;
}
const geo = process.env.VERCEL === "1" ? req.geo : LOCALHOST_GEO_DATA;
const ua = userAgent(req);
const referer = req.headers.get("referer");
Expand Down
17 changes: 0 additions & 17 deletions apps/web/ui/stats/device-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ export default function DeviceIcon({
tab: DeviceTabs;
className: string;
}) {
if (display === "Bot") {
return (
<img
alt={display}
src="https://api.dicebear.com/7.x/bottts/svg?seed=dub"
className={className}
/>
);
}
if (tab === "device") {
return (
<BlurImage
Expand Down Expand Up @@ -75,14 +66,6 @@ export default function DeviceIcon({
/>
);
}
} else if (tab === "bot") {
return (
<img
alt={display}
src={`https://api.dicebear.com/7.x/bottts/svg?seed=${display}`}
className={className}
/>
);
} else {
return (
<BlurImage
Expand Down
14 changes: 4 additions & 10 deletions apps/web/ui/stats/devices.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceTabs, uaToBot } from "@/lib/stats";
import { DeviceTabs } from "@/lib/stats";
import { LoadingSpinner, Modal, TabSelect } from "@dub/ui";
import { fetcher } from "@dub/utils";
import { Maximize } from "lucide-react";
Expand Down Expand Up @@ -27,14 +27,8 @@ export default function Devices() {
tab={tab}
data={
data?.map((d) => ({
icon: (
<DeviceIcon
display={tab === "bot" ? uaToBot(d.ua) : d[tab]}
tab={tab}
className="h-4 w-4"
/>
),
title: tab === "bot" ? uaToBot(d.ua) : d[tab],
icon: <DeviceIcon display={d[tab]} tab={tab} className="h-4 w-4" />,
title: d[tab],
clicks: d.clicks,
})) || []
}
Expand All @@ -60,7 +54,7 @@ export default function Devices() {
<div className="mb-5 flex justify-between">
<h1 className="text-xl font-semibold">Devices</h1>
<TabSelect
options={["device", "browser", "os", "bot"]}
options={["device", "browser", "os"]}
selected={tab}
// @ts-ignore
selectAction={setTab}
Expand Down

0 comments on commit 9ee332a

Please sign in to comment.