Skip to content

Commit

Permalink
update usePartnerAnalytics
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Nov 24, 2024
1 parent e58e4a1 commit 019528f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ export const GET = withPartner(async ({ partner, params, searchParams }) => {
}

const parsedParams = analyticsQuerySchema
.pick({
event: true,
start: true,
end: true,
interval: true,
groupBy: true,
timezone: true,
.omit({
domain: true,
key: true,
linkId: true,
externalId: true,
})
.parse(searchParams);

Expand Down
15 changes: 13 additions & 2 deletions apps/web/lib/swr/use-partner-analytics.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import { fetcher } from "@dub/utils";
import { useParams } from "next/navigation";
import { useParams, useSearchParams } from "next/navigation";
import useSWR from "swr";
import { VALID_ANALYTICS_FILTERS } from "../analytics/constants";
import { PartnerAnalyticsFilters } from "../analytics/types";

export default function usePartnerAnalytics(
params?: PartnerAnalyticsFilters & { programId?: string },
) {
const { partnerId, programId } = useParams();
const searchParams = useSearchParams();

const { data, error } = useSWR<any>(
`/api/partners/${partnerId}/programs/${params?.programId ?? programId}/analytics?${new URLSearchParams(
{
event: params?.event ?? "composite",
groupBy: params?.groupBy ?? "count",
...VALID_ANALYTICS_FILTERS.reduce(
(acc, filter) => ({
...acc,
...(searchParams?.get(filter) && {
[filter]: searchParams.get(filter),
}),
}),
{},
),
...(params?.start && params?.end
? {
start: params.start.toISOString(),
end: params.end.toISOString(),
}
: { interval: params?.interval ?? "all_unfiltered" }),
groupBy: params?.groupBy ?? "count",
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
},
).toString()}`,
Expand Down

0 comments on commit 019528f

Please sign in to comment.