Skip to content

Commit 59772db

Browse files
committed
add top_domains
1 parent 7de1f51 commit 59772db

File tree

5 files changed

+69
-101
lines changed

5 files changed

+69
-101
lines changed

apps/web/lib/analytics/constants.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ export const VALID_ANALYTICS_ENDPOINTS = [
126126
"triggers",
127127
"referers",
128128
"referer_urls",
129-
"top_partners",
129+
"top_folders",
130+
"top_link_tags",
131+
"top_domains",
130132
"top_links",
131133
"top_urls",
132-
"top_link_tags",
133-
"top_link_folders",
134+
"top_partners",
134135
"utm_sources",
135136
"utm_mediums",
136137
"utm_campaigns",
@@ -155,6 +156,9 @@ export const SINGULAR_ANALYTICS_ENDPOINTS = {
155156
utm_terms: "utm_term",
156157
utm_contents: "utm_content",
157158
// extra fields
159+
top_folders: "folderId",
160+
top_link_tags: "tagIds",
161+
top_domains: "domain",
158162
top_links: "link",
159163
top_urls: "url",
160164
timeseries: "start",

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
115115
const pipe = tb.buildPipe({
116116
pipe: ["count", "timeseries"].includes(groupBy)
117117
? `v3_${groupBy}`
118-
: ["top_partners", "top_link_tags", "top_link_folders"].includes(groupBy)
118+
: [
119+
"top_folders",
120+
"top_link_tags",
121+
"top_domains",
122+
"top_partners",
123+
].includes(groupBy)
119124
? "v3_group_by_link_metadata"
120125
: "v3_group_by",
121126
parameters: analyticsFilterTB,
@@ -255,7 +260,7 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
255260
});
256261
})
257262
.filter((d) => d !== null);
258-
} else if (groupBy === "top_link_folders") {
263+
} else if (groupBy === "top_folders") {
259264
const folders = await prisma.folder.findMany({
260265
where: {
261266
id: {

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -467,30 +467,25 @@ export const analyticsResponse = {
467467
.default(0),
468468
})
469469
.openapi({ ref: "AnalyticsUTMContents" }),
470-
top_partners: z
470+
top_folders: z
471471
.object({
472-
partnerId: z.string().describe("The ID of the partner"),
473-
partner: z.object({
474-
id: z.string().describe("The ID of the partner"),
475-
name: z.string().describe("The name of the partner"),
476-
image: z.string().nullable().describe("The image of the partner"),
477-
payoutsEnabledAt: z
478-
.string()
479-
.nullable()
480-
.describe("The date the partner enabled payouts"),
481-
country: z.string().nullable().describe("The country of the partner"),
472+
folderId: z.string().describe("The ID of the folder"),
473+
folder: z.object({
474+
id: z.string().describe("The ID of the folder"),
475+
name: z.string().describe("The name of the folder"),
476+
accessLevel: z
477+
.nativeEnum(FolderAccessLevel)
478+
.describe("The access level of the folder"),
482479
}),
483480
clicks: z.number().describe("The total number of clicks").default(0),
484481
leads: z.number().describe("The total number of leads").default(0),
485482
sales: z.number().describe("The total number of sales").default(0),
486483
saleAmount: z
487484
.number()
488-
.describe(
489-
"The total amount of sales from this partner for this program, in cents",
490-
)
485+
.describe("The total amount of sales from this link folder, in cents")
491486
.default(0),
492487
})
493-
.openapi({ ref: "AnalyticsTopPartners" }),
488+
.openapi({ ref: "AnalyticsTopFolders" }),
494489
top_link_tags: z
495490
.object({
496491
tagId: z.string().describe("The ID of the tag"),
@@ -504,23 +499,40 @@ export const analyticsResponse = {
504499
.default(0),
505500
})
506501
.openapi({ ref: "AnalyticsTopLinkTags" }),
507-
top_link_folders: z
502+
top_domains: z
508503
.object({
509-
folderId: z.string().describe("The ID of the folder"),
510-
folder: z.object({
511-
id: z.string().describe("The ID of the folder"),
512-
name: z.string().describe("The name of the folder"),
513-
accessLevel: z
514-
.nativeEnum(FolderAccessLevel)
515-
.describe("The access level of the folder"),
504+
domain: z.string().describe("The unique domain name"),
505+
clicks: z.number().describe("The total number of clicks").default(0),
506+
leads: z.number().describe("The total number of leads").default(0),
507+
sales: z.number().describe("The total number of sales").default(0),
508+
saleAmount: z
509+
.number()
510+
.describe("The total amount of sales from this domain, in cents")
511+
.default(0),
512+
})
513+
.openapi({ ref: "AnalyticsTopDomains" }),
514+
top_partners: z
515+
.object({
516+
partnerId: z.string().describe("The ID of the partner"),
517+
partner: z.object({
518+
id: z.string().describe("The ID of the partner"),
519+
name: z.string().describe("The name of the partner"),
520+
image: z.string().nullable().describe("The image of the partner"),
521+
payoutsEnabledAt: z
522+
.string()
523+
.nullable()
524+
.describe("The date the partner enabled payouts"),
525+
country: z.string().nullable().describe("The country of the partner"),
516526
}),
517527
clicks: z.number().describe("The total number of clicks").default(0),
518528
leads: z.number().describe("The total number of leads").default(0),
519529
sales: z.number().describe("The total number of sales").default(0),
520530
saleAmount: z
521531
.number()
522-
.describe("The total amount of sales from this link folder, in cents")
532+
.describe(
533+
"The total amount of sales from this partner for this program, in cents",
534+
)
523535
.default(0),
524536
})
525-
.openapi({ ref: "AnalyticsTopLinkFolders" }),
537+
.openapi({ ref: "AnalyticsTopPartners" }),
526538
} as const;

apps/web/ui/analytics/use-analytics-filters.tsx

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import { generateFilters } from "@/lib/ai/generate-filters";
22
import { VALID_ANALYTICS_FILTERS } from "@/lib/analytics/constants";
33
import useCustomer from "@/lib/swr/use-customer";
4-
import useDomains from "@/lib/swr/use-domains";
5-
import useDomainsCount from "@/lib/swr/use-domains-count";
64
import usePartner from "@/lib/swr/use-partner";
75
import usePartnerCustomer from "@/lib/swr/use-partner-customer";
8-
import useTagsCount from "@/lib/swr/use-tags-count";
96
import { LinkProps } from "@/lib/types";
10-
import { DOMAINS_MAX_PAGE_SIZE } from "@/lib/zod/schemas/domains";
11-
import { TAGS_MAX_PAGE_SIZE } from "@/lib/zod/schemas/tags";
127
import {
138
BlurImage,
149
Filter,
@@ -64,7 +59,6 @@ import {
6459
useMemo,
6560
useState,
6661
} from "react";
67-
import { useDebounce } from "use-debounce";
6862
import { FolderIcon } from "../folders/folder-icon";
6963
import { LinkIcon } from "../links/link-icon";
7064
import TagBadge from "../links/tag-badge";
@@ -102,28 +96,6 @@ export function useAnalyticsFilters({
10296

10397
const { queryParams, searchParamsObj } = useRouterStuff();
10498

105-
// Determine whether filters should be fetched async
106-
const { data: tagsCount } = useTagsCount();
107-
const { data: domainsCount } = useDomainsCount({ ignoreParams: true });
108-
const tagsAsync = Boolean(tagsCount && tagsCount > TAGS_MAX_PAGE_SIZE);
109-
const domainsAsync = domainsCount && domainsCount > DOMAINS_MAX_PAGE_SIZE;
110-
111-
const [selectedFilter, setSelectedFilter] = useState<string | null>(null);
112-
const [search, setSearch] = useState("");
113-
const [debouncedSearch] = useDebounce(search, 500);
114-
115-
const {
116-
allDomains: domains,
117-
primaryDomain,
118-
loading: loadingDomains,
119-
} = useDomains({
120-
ignoreParams: true,
121-
opts: {
122-
search:
123-
domainsAsync && selectedFilter === "domain" ? debouncedSearch : "",
124-
},
125-
});
126-
12799
const selectedTagIds = useMemo(
128100
() => searchParamsObj.tagIds?.split(",")?.filter(Boolean) ?? [],
129101
[searchParamsObj.tagIds],
@@ -223,8 +195,8 @@ export function useAnalyticsFilters({
223195
omitGroupByFilterKey: true,
224196
context,
225197
});
226-
const { data: partners } = useAnalyticsFilterOption("top_partners", {
227-
disabled: !isRequested("partnerId"),
198+
const { data: folders } = useAnalyticsFilterOption("top_folders", {
199+
disabled: !isRequested("folderId"),
228200
omitGroupByFilterKey: true,
229201
context,
230202
});
@@ -233,8 +205,13 @@ export function useAnalyticsFilters({
233205
omitGroupByFilterKey: true,
234206
context,
235207
});
236-
const { data: linkFolders } = useAnalyticsFilterOption("top_link_folders", {
237-
disabled: !isRequested("folderId"),
208+
const { data: domains } = useAnalyticsFilterOption("top_domains", {
209+
disabled: !isRequested("domain"),
210+
omitGroupByFilterKey: true,
211+
context,
212+
});
213+
const { data: partners } = useAnalyticsFilterOption("top_partners", {
214+
disabled: !isRequested("partnerId"),
238215
omitGroupByFilterKey: true,
239216
context,
240217
});
@@ -339,14 +316,6 @@ export function useAnalyticsFilters({
339316
// Some suggestions will only appear if previously requested (see isRequested above)
340317
const aiFilterSuggestions = useMemo(
341318
() => [
342-
...(dashboardProps || partnerPage
343-
? []
344-
: [
345-
{
346-
value: `Clicks on ${primaryDomain} domain this year`,
347-
icon: Globe2,
348-
},
349-
]),
350319
{
351320
value: "Mobile users, US only",
352321
icon: MobilePhone,
@@ -364,7 +333,7 @@ export function useAnalyticsFilters({
364333
icon: QRCode,
365334
},
366335
],
367-
[primaryDomain, dashboardProps, partnerPage],
336+
[dashboardProps, partnerPage],
368337
);
369338

370339
const [streaming, setStreaming] = useState<boolean>(false);
@@ -473,7 +442,7 @@ export function useAnalyticsFilters({
473442
) : null;
474443
},
475444
options:
476-
linkFolders?.map(({ folder, ...rest }) => ({
445+
folders?.map(({ folder, ...rest }) => ({
477446
value: folder.id,
478447
icon: (
479448
<FolderIcon
@@ -492,7 +461,6 @@ export function useAnalyticsFilters({
492461
icon: Tag,
493462
label: "Tag",
494463
multiple: true,
495-
shouldFilter: !tagsAsync,
496464
getOptionIcon: (_value, props) => {
497465
const tagColor = props.option?.data?.color;
498466
return tagColor ? (
@@ -514,7 +482,6 @@ export function useAnalyticsFilters({
514482
key: "domain",
515483
icon: Globe2,
516484
label: "Domain",
517-
shouldFilter: !domainsAsync,
518485
getOptionIcon: (value) => (
519486
<BlurImage
520487
src={`${GOOGLE_FAVICON_URL}${value}`}
@@ -524,25 +491,12 @@ export function useAnalyticsFilters({
524491
height={16}
525492
/>
526493
),
527-
options: loadingDomains
528-
? null
529-
: [
530-
...domains.map((domain) => ({
531-
value: domain.slug,
532-
label: domain.slug,
533-
})),
534-
// Add currently filtered domain if not already in the list
535-
...(!searchParamsObj.domain ||
536-
domains.some((d) => d.slug === searchParamsObj.domain)
537-
? []
538-
: [
539-
{
540-
value: searchParamsObj.domain,
541-
label: searchParamsObj.domain,
542-
hideDuringSearch: true,
543-
},
544-
]),
545-
],
494+
options:
495+
domains?.map(({ domain, ...rest }) => ({
496+
value: domain,
497+
label: domain,
498+
right: getFilterOptionTotal(rest),
499+
})) ?? null,
546500
},
547501
LinkFilterItem,
548502
{
@@ -823,7 +777,7 @@ export function useAnalyticsFilters({
823777
domains,
824778
links,
825779
linkTags,
826-
linkFolders,
780+
folders,
827781
selectedTagIds,
828782
selectedCustomerId,
829783
countries,
@@ -835,9 +789,6 @@ export function useAnalyticsFilters({
835789
refererUrls,
836790
urls,
837791
utmData,
838-
tagsAsync,
839-
domainsAsync,
840-
loadingDomains,
841792
searchParamsObj.tagIds,
842793
searchParamsObj.domain,
843794
],
@@ -943,8 +894,6 @@ export function useAnalyticsFilters({
943894
return {
944895
filters,
945896
activeFilters,
946-
setSearch,
947-
setSelectedFilter,
948897
onSelect,
949898
onRemove,
950899
onRemoveAll,

apps/web/ui/analytics/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export function useAnalyticsFilterOption(
5252
(() => {
5353
if (!groupBy || !options?.omitGroupByFilterKey) return undefined;
5454
if (groupBy === "top_links") return ["domain", "key"];
55-
if (groupBy === "top_link_tags") return "tagIds";
56-
if (groupBy === "top_link_folders") return "folderId";
5755
return SINGULAR_ANALYTICS_ENDPOINTS[groupBy]
5856
? SINGULAR_ANALYTICS_ENDPOINTS[groupBy]
5957
: undefined;

0 commit comments

Comments
 (0)