Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactor: remove UTM tracking

Snazzahff68407f5514
diff --git a/components/stats/index.tsx b/components/stats/index.tsx
index 569ad3794a..e3dcd76eb3 100644
--- a/components/stats/index.tsx
+++ b/components/stats/index.tsx
@@ -3,7 +3,6 @@ import Clicks from "@/components/stats/clicks";
import Toggle from "@/components/stats/toggle";
import Devices from "@/components/stats/devices";
import Locations from "@/components/stats/locations";
-import UTM from "@/components/stats/utm";
import Referer from "@/components/stats/referer";
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
@@ -47,7 +46,6 @@ export default function Stats({
<Locations data={data!} />
<Devices data={data!} />
<Referer data={data!} />
- <UTM data={data!} />
</div>
</div>
</div>
diff --git a/components/stats/utm.tsx b/components/stats/utm.tsx
deleted file mode 100644
index ab80a7b982..0000000000
--- a/components/stats/utm.tsx
+++ /dev/null
@@ -1,94 +0,0 @@
-import { AnimatePresence, motion } from 'framer-motion';
-import { UIEvent, useMemo, useState } from 'react';
-
-import BadgeSelect from '@/components/shared/badge-select';
-import { processUTMData, StatsProps, UTMTabs } from '@/lib/stats';
-import { nFormatter } from '@/lib/utils';
-
-import { LoadingDots } from "@/components/shared/icons";
-
-export default function UTM({ data: rawData }: { data: StatsProps }) {
- const [tab, setTab] = useState<UTMTabs>('source / medium');
- const data = {
- ...rawData,
- utmData: useMemo(() => {
- if (rawData?.utmData) {
- return processUTMData(rawData.utmData, tab);
- } else {
- return null;
- }
- }, [rawData, tab])
- };
-
- const [scrolled, setScrolled] = useState(false);
-
- const handleScroll = (event: UIEvent<HTMLElement>) => {
- if (event.currentTarget.scrollTop > 0) {
- setScrolled(true);
- } else {
- setScrolled(false);
- }
- };
-
- return (
- <div
- className="relative bg-white px-7 py-5 shadow-lg rounded-lg border border-gray-100 h-[420px] overflow-scroll scrollbar-hide"
- onScroll={handleScroll}
- >
- <div className="mb-5 flex justify-between">
- <h1 className="text-xl font-semibold">UTM</h1>
- <BadgeSelect
- options={['source / medium', 'source', 'medium']}
- selected={tab}
- // @ts-ignore
- selectAction={setTab}
- />
- </div>
- <div className={data.utmData && data.utmData.length > 0 ? 'grid gap-4' : 'h-[300px] flex justify-center items-center'}>
- {data.utmData ? (
- data.utmData.length > 0 ? (
- data.utmData.map(({ display, count }, idx) => (
- <div key={idx} className="flex justify-between items-center">
- <div className="relative flex items-center z-10 w-full max-w-[calc(100%-3rem)]">
- <span className="flex space-x-2 px-2 items-center z-10">
- <p className="text-gray-800 text-sm">{display}</p>
- </span>
- <motion.div
- style={{
- width: `${(count / data.totalClicks) * 100}%`
- }}
- className="bg-blue-100 absolute h-8 origin-left"
- transition={{ ease: 'easeOut', duration: 0.3 }}
- initial={{ transform: 'scaleX(0)' }}
- animate={{ transform: 'scaleX(1)' }}
- />
- </div>
- <p className="text-gray-600 text-sm z-10">{nFormatter(count)}</p>
- </div>
- ))
- ) : (
- <p className="text-gray-600 text-sm">No data available</p>
- )
- ) : (
- <LoadingDots color="#71717A" />
- )}
- </div>
- <AnimatePresence>
- {data.utmData && data.utmData.length > 9 && !scrolled && (
- <motion.div
- initial={{ opacity: 0, y: 50 }}
- animate={{
- opacity: 1,
- y: 0,
- transition: { type: 'linear', duration: 0.2 }
- }}
- exit={{ opacity: 0, y: 50, transition: { duration: 0 } }}
- className="absolute w-full h-8 flex justify-center items-center bottom-0 left-0 right-0 bg-gradient-to-b from-white to-gray-100 text-sm text-gray-500"
- >
- Show more
- </motion.div>
- )}
- </AnimatePresence>
- </div>
- );
-}
\ No newline at end of file
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index 8d87bc302e..e30a7a1213 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -30,7 +30,7 @@ export default async function LinkMiddleware(
if (target) {
const isBot = detectBot(req);
- ev.waitUntil(recordClick(hostname, req, query, key)); // increment click count
+ ev.waitUntil(recordClick(hostname, req, key)); // increment click count
if (image && description && isBot) {
// rewrite to proxy page (dub.sh/proxy/[domain]/[key])
diff --git a/lib/stats.ts b/lib/stats.ts
index 099e488792..2f2e1fa1e6 100644
--- a/lib/stats.ts
+++ b/lib/stats.ts
@@ -6,13 +6,6 @@ export interface RawStatsProps {
ua: any;
referer: string;
timestamp: number;
- utm?: {
- source?: string;
- medium?: string;
- campaign?: string;
- content?: string;
- tags?: string;
- };
}
export interface StatsProps {
@@ -27,7 +20,6 @@ export interface StatsProps {
region: string;
}[];
deviceData: { device: string; browser: string; os: string; bot: string }[];
- utmData: { source: string; medium: string }[];
refererData: { domain: string }[];
}
@@ -149,14 +141,6 @@ export function processData(
};
});
- const utmData = data.map(({ utm }) => {
- const { source, medium } = utm || {};
- return {
- source: source ?? '(none)',
- medium: medium ?? '(none)'
- };
- });
-
const refererData = data.map(({ referer }) => {
try {
const url = referer ? new URL(referer) : null;
@@ -173,7 +157,6 @@ export function processData(
clicksData,
locationData,
deviceData,
- utmData,
refererData
};
}
@@ -251,32 +234,6 @@ export const processDeviceData = (
.sort((a, b) => b.count - a.count);
};
-export interface UTMStatsProps {
- display: string;
- count: number;
-}
-
-export type UTMTabs = 'source / medium' | 'source' | 'medium';
-
-export const processUTMData = (data: StatsProps['utmData'], tab: UTMTabs): UTMStatsProps[] => {
- const results =
- data && data.length > 0
- ? data.reduce<Record<string, number>>((acc, d) => {
- const currentVal = tab === 'source / medium' ? `${d.source} / ${d.medium}` : d[tab];
- const count = acc[currentVal] || 0;
- acc[currentVal] = count + 1;
- return acc;
- }, {})
- : {};
-
- return Object.entries(results)
- .map(([display, count]) => ({
- display,
- count
- }))
- .sort((a, b) => b.count - a.count);
-};
-
export interface RefererStatsProps {
display: string;
count: number;
@@ -314,8 +271,6 @@ export const dummyData: StatsProps = {
// @ts-ignore
deviceData: null,
// @ts-ignore
- utmData: null,
- // @ts-ignore
refererData: null
};
diff --git a/lib/upstash.ts b/lib/upstash.ts
index 2372c80f32..5d267ffc4f 100644
--- a/lib/upstash.ts
+++ b/lib/upstash.ts
@@ -88,11 +88,8 @@ export async function checkIfKeyExists(hostname: string, key: string) {
export async function recordClick(
hostname: string,
req: NextRequest,
- query: URLSearchParams,
key?: string
) {
- const truncate = (v: null | string) => (!v ? undefined : v.slice(0, 256));
-
return await redis.zadd(
key ? `${hostname}:clicks:${key}` : `${hostname}:root:clicks`,
{
@@ -101,23 +98,7 @@ export async function recordClick(
geo: process.env.VERCEL === "1" ? req.geo : LOCALHOST_GEO_DATA,
ua: userAgent(req),
referer: req.headers.get("referer"),
- timestamp: Date.now(),
- ...(query &&
- [
- "utm_source",
- "utm_medium",
- "utm_campaign",
- "utm_content",
- "utm_term",
- ].some((p) => query.has(p)) && {
- utm: {
- source: truncate(query.get("utm_source")),
- medium: truncate(query.get("utm_medium")),
- compaign: truncate(query.get("utm_campaign")),
- content: truncate(query.get("utm_content")),
- term: truncate(query.get("utm_term")),
- },
- }),
+ timestamp: Date.now()
},
}
);