Skip to content

Commit 128da8a

Browse files
authored
Merge pull request #2710 from dubinc/trigger-refactor
Triggers refactor
2 parents ba06df6 + f02ef50 commit 128da8a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+300
-201
lines changed

apps/web/app/(ee)/api/events/export/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const columnNames: Record<string, string> = {
2222
};
2323

2424
const columnAccessors = {
25-
trigger: (r: Row) => (r.qr ? "QR scan" : "Link click"),
25+
trigger: (r: Row) => r.click.trigger,
2626
event: (r: LeadEvent | SaleEvent) => r.eventName,
2727
link: (r: Row) => r.domain + (r.key === "_root" ? "" : `/${r.key}`),
2828
country: (r: Row) =>

apps/web/lib/analytics/constants.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,6 @@ export const DIMENSIONAL_ANALYTICS_FILTERS = [
207207
"utm_content",
208208
];
209209

210-
export const TRIGGER_DISPLAY = {
211-
qr: "QR Code Scan",
212-
link: "Link Click",
213-
pageview: "Page View",
214-
deeplink: "Deep Link",
215-
};
216210
export const TRIGGER_TYPES = ["qr", "link", "pageview", "deeplink"] as const;
217211

218212
export const EVENT_TYPES = ["clicks", "leads", "sales"] as const;

apps/web/lib/analytics/get-analytics.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,8 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
7878
dataAvailableFrom,
7979
});
8080

81-
if (trigger) {
82-
if (trigger === "qr") {
83-
qr = true;
84-
} else if (trigger === "link") {
85-
qr = false;
86-
}
81+
if (qr) {
82+
trigger = "qr";
8783
}
8884

8985
if (region) {
@@ -112,7 +108,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
112108
eventType: event,
113109
workspaceId,
114110
tagIds,
115-
qr,
111+
trigger,
116112
start: startDate.toISOString().replace("T", " ").replace("Z", ""),
117113
end: endDate.toISOString().replace("T", " ").replace("Z", ""),
118114
granularity,

apps/web/lib/analytics/get-events.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ export const getEvents = async (params: EventsFilters) => {
4848
dataAvailableFrom,
4949
});
5050

51-
if (trigger) {
52-
if (trigger === "qr") {
53-
qr = true;
54-
} else if (trigger === "link") {
55-
qr = false;
56-
}
51+
if (qr) {
52+
trigger = "qr";
5753
}
5854

5955
if (region) {
@@ -82,7 +78,7 @@ export const getEvents = async (params: EventsFilters) => {
8278
...params,
8379
eventType,
8480
workspaceId,
85-
qr,
81+
trigger,
8682
country,
8783
region,
8884
order: sortOrder,

apps/web/lib/zod/schemas/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const analyticsQuerySchema = z
184184
.enum(TRIGGER_TYPES)
185185
.optional()
186186
.describe(
187-
"The trigger to retrieve analytics for. If undefined, return both QR and link clicks.",
187+
"The trigger to retrieve analytics for. If undefined, returns all trigger types.",
188188
),
189189
referer: z
190190
.string()
@@ -246,7 +246,7 @@ export const analyticsFilterTB = z
246246
customerId: z.string().optional(),
247247
root: z.boolean().optional(),
248248
saleType: z.string().optional(),
249-
qr: z.boolean().optional(),
249+
trigger: z.enum(TRIGGER_TYPES).optional(),
250250
start: z.string(),
251251
end: z.string(),
252252
granularity: z.enum(["minute", "hour", "day", "month"]).optional(),

apps/web/lib/zod/schemas/clicks.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const clickEventSchemaTB = z.object({
2020
browser_version: z.string().nullable(),
2121
os: z.string().nullable(),
2222
os_version: z.string().nullable(),
23+
trigger: z.string().nullish(), // backwards compatibility
2324
engine: z.string().nullable(),
2425
engine_version: z.string().nullable(),
2526
cpu_architecture: z.string().nullable(),
@@ -45,6 +46,7 @@ export const clickEventSchemaTBEndpoint = z.object({
4546
device: z.string().nullable(),
4647
browser: z.string().nullable(),
4748
os: z.string().nullable(),
49+
trigger: z.string().nullish(), // backwards compatibility
4850
referer: z.string().nullable(),
4951
referer_url: z.string().nullable(),
5052
referer_url_processed: z.string().nullable(),
@@ -63,6 +65,7 @@ export const clickEventSchema = z.object({
6365
device: z.string(),
6466
browser: z.string(),
6567
os: z.string(),
68+
trigger: z.string().nullish(), // backwards compatibility
6669
referer: z.string(),
6770
refererUrl: z.string(),
6871
qr: z.coerce.boolean(),

apps/web/lib/zod/schemas/leads.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const leadEventSchemaTBEndpoint = z.object({
113113
device: z.string().nullable(),
114114
browser: z.string().nullable(),
115115
os: z.string().nullable(),
116+
trigger: z.string().nullish(), // backwards compatibility
116117
referer: z.string().nullable(),
117118
referer_url: z.string().nullable(),
118119
referer_url_processed: z.string().nullable(),

apps/web/lib/zod/schemas/sales.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export const saleEventSchemaTBEndpoint = z.object({
127127
device: z.string().nullable(),
128128
browser: z.string().nullable(),
129129
os: z.string().nullable(),
130+
trigger: z.string().nullish(), // backwards compatibility
130131
referer: z.string().nullable(),
131132
referer_url: z.string().nullable(),
132133
referer_url_processed: z.string().nullable(),

apps/web/ui/analytics/device-icon.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import { BlurImage } from "@dub/ui";
44
import {
55
AppleLogo,
66
Cube,
7-
CursorRays,
87
Desktop,
98
GamingConsole,
109
MobilePhone,
11-
QRCode,
1210
TV,
1311
Tablet,
1412
Watch,
1513
Window,
1614
} from "@dub/ui/icons";
15+
import { TRIGGER_DISPLAY } from "./trigger-display";
1716

1817
export default function DeviceIcon({
1918
display,
@@ -89,11 +88,8 @@ export default function DeviceIcon({
8988
);
9089
}
9190
} else if (tab === "triggers") {
92-
if (display === "qr") {
93-
return <QRCode className={className} />;
94-
} else {
95-
return <CursorRays className={className} />;
96-
}
91+
const { icon: Icon } = TRIGGER_DISPLAY[display ?? "link"];
92+
return <Icon className={className} />;
9793
} else {
9894
return (
9995
<BlurImage

apps/web/ui/analytics/devices.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
SINGULAR_ANALYTICS_ENDPOINTS,
3-
TRIGGER_DISPLAY,
4-
} from "@/lib/analytics/constants";
1+
import { SINGULAR_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
52
import { DeviceTabs } from "@/lib/analytics/types";
63
import { useRouterStuff } from "@dub/ui";
74
import { Cube, CursorRays, MobilePhone, Window } from "@dub/ui/icons";
@@ -11,6 +8,7 @@ import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
118
import { AnalyticsContext } from "./analytics-provider";
129
import BarList from "./bar-list";
1310
import DeviceIcon from "./device-icon";
11+
import { TRIGGER_DISPLAY } from "./trigger-display";
1412
import { useAnalyticsFilterOption } from "./utils";
1513

1614
export default function Devices() {
@@ -20,7 +18,7 @@ export default function Devices() {
2018
const dataKey = selectedTab === "sales" ? saleUnit : "count";
2119

2220
const [tab, setTab] = useState<DeviceTabs>("devices");
23-
const { data, loading } = useAnalyticsFilterOption(tab);
21+
const { data } = useAnalyticsFilterOption(tab);
2422
const singularTabName = SINGULAR_ANALYTICS_ENDPOINTS[tab];
2523

2624
return (
@@ -53,7 +51,7 @@ export default function Devices() {
5351
),
5452
title:
5553
tab === "triggers"
56-
? TRIGGER_DISPLAY[d.trigger]
54+
? TRIGGER_DISPLAY[d.trigger].title
5755
: d[singularTabName],
5856
href: queryParams({
5957
...(searchParams.has(singularTabName)

0 commit comments

Comments
 (0)