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.

support tagIds in analytics tab

Steven Tey62a4667ace2f
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index 3ea256f9e1..2410a4bbad 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -143,6 +143,7 @@ export const VALID_ANALYTICS_FILTERS = [
"refererUrl",
"url",
"tagId",
+ "tagIds",
"qr",
"root",
];
diff --git a/apps/web/lib/analytics/get-analytics.ts b/apps/web/lib/analytics/get-analytics.ts
index 1ad41fe49c..96d446bcdb 100644
--- a/apps/web/lib/analytics/get-analytics.ts
+++ b/apps/web/lib/analytics/get-analytics.ts
@@ -98,8 +98,8 @@ export const getAnalytics = async (params: AnalyticsFilters) => {
...params,
eventType: event,
workspaceId,
- qr,
tagIds,
+ qr,
start: start.toISOString().replace("T", " ").replace("Z", ""),
end: end.toISOString().replace("T", " ").replace("Z", ""),
granularity,
diff --git a/apps/web/ui/analytics/analytics-provider.tsx b/apps/web/ui/analytics/analytics-provider.tsx
index 2412866eae..7942fb5a20 100644
--- a/apps/web/ui/analytics/analytics-provider.tsx
+++ b/apps/web/ui/analytics/analytics-provider.tsx
@@ -11,6 +11,7 @@ import {
EventType,
} from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
+import { combineTagIds } from "@/lib/api/links/utils/combine-tag-ids";
import useWorkspace from "@/lib/swr/use-workspace";
import { PlanProps } from "@/lib/types";
import { fetcher } from "@dub/utils";
@@ -49,7 +50,7 @@ export const AnalyticsContext = createContext<{
start?: Date;
end?: Date;
interval?: string;
- tagId?: string;
+ tagIds?: string;
totalEvents?: {
[key in AnalyticsResponseOptions]: number;
};
@@ -108,7 +109,10 @@ export default function AnalyticsProvider({
// Whether to show conversions in shared analytics dashboards
const showConversions = dashboardProps?.showConversions;
- const tagId = searchParams?.get("tagId") ?? undefined;
+ const tagIds = combineTagIds({
+ tagId: searchParams?.get("tagId"),
+ tagIds: searchParams?.get("tagIds")?.split(","),
+ })?.join(",");
// Default to last 24 hours
const { start, end } = useMemo(() => {
@@ -219,11 +223,11 @@ export default function AnalyticsProvider({
...(start &&
end && { start: start.toISOString(), end: end.toISOString() }),
...(interval && { interval }),
- ...(tagId && { tagId }),
+ ...(tagIds && { tagIds }),
...(root && { root: root.toString() }),
event: selectedTab,
}).toString();
- }, [workspaceId, domain, key, searchParams, start, end, tagId, selectedTab]);
+ }, [workspaceId, domain, key, searchParams, start, end, tagIds, selectedTab]);
// Reset requiresUpgrade when query changes
useEffect(() => setRequiresUpgrade(false), [queryString]);
@@ -279,7 +283,7 @@ export default function AnalyticsProvider({
start, // start of time period
end, // end of time period
interval, /// time period interval
- tagId, // id of a single tag
+ tagIds, // ids of the tags to filter by
totalEvents, // totalEvents (clicks, leads, sales)
adminPage, // whether the user is an admin
demoPage, // whether the user is viewing demo analytics
diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsx
index 10a942cc33..0aaef232cc 100644
--- a/apps/web/ui/analytics/toggle.tsx
+++ b/apps/web/ui/analytics/toggle.tsx
@@ -11,7 +11,7 @@ import useDomainsCount from "@/lib/swr/use-domains-count";
import useTags from "@/lib/swr/use-tags";
import useTagsCount from "@/lib/swr/use-tags-count";
import useWorkspace from "@/lib/swr/use-workspace";
-import { LinkProps, TagProps } from "@/lib/types";
+import { LinkProps } from "@/lib/types";
import { DOMAINS_MAX_PAGE_SIZE } from "@/lib/zod/schemas/domains";
import { TAGS_MAX_PAGE_SIZE } from "@/lib/zod/schemas/tags";
import {
@@ -71,7 +71,7 @@ import {
} from "react";
import useSWR from "swr";
import { useDebounce } from "use-debounce";
-import { COLORS_LIST } from "../links/tag-badge";
+import TagBadge from "../links/tag-badge";
import AnalyticsOptions from "./analytics-options";
import { AnalyticsContext } from "./analytics-provider";
import ContinentIcon from "./continent-icon";
@@ -115,11 +115,8 @@ export default function Toggle({
const [search, setSearch] = useState("");
const [debouncedSearch] = useDebounce(search, 500);
- const { tags, loading: loadingTags } = useTags({
- query: {
- search: tagsAsync && selectedFilter === "tagId" ? debouncedSearch : "",
- },
- });
+ const { tags, loading: loadingTags } = useTags();
+
const {
allDomains: domains,
primaryDomain,
@@ -132,8 +129,13 @@ export default function Toggle({
},
});
+ const selectedTagIds = useMemo(
+ () => searchParamsObj.tagIds?.split(",")?.filter(Boolean) ?? [],
+ [searchParamsObj.tagIds],
+ );
+
const { tags: selectedTags } = useTags({
- query: { ids: searchParamsObj.tagId ? [searchParamsObj.tagId] : [] },
+ query: { ids: selectedTagIds },
enabled: tagsAsync,
});
@@ -143,7 +145,6 @@ export default function Toggle({
const {
domain,
key,
- tagId,
continent,
country,
city,
@@ -166,7 +167,7 @@ export default function Toggle({
},
]
: []),
- ...(tagId ? [{ key: "tagId", value: tagId }] : []),
+ ...(selectedTagIds ? [{ key: "tagIds", value: selectedTagIds }] : []),
...(continent ? [{ key: "continent", value: continent }] : []),
...(country ? [{ key: "country", value: country }] : []),
...(city ? [{ key: "city", value: city }] : []),
@@ -349,59 +350,26 @@ export default function Toggle({
],
},
{
- key: "tagId",
+ key: "tagIds",
icon: Tag,
label: "Tag",
+ multiple: true,
shouldFilter: !tagsAsync,
getOptionIcon: (value, props) => {
const tagColor =
props.option?.data?.color ??
tags?.find(({ id }) => id === value)?.color;
return tagColor ? (
- <div
- className={cn(
- "rounded-md p-1.5",
- COLORS_LIST.find(({ color }) => color === tagColor)?.css,
- )}
- >
- <Tag className="h-2.5 w-2.5" />
- </div>
+ <TagBadge color={tagColor} withIcon className="sm:p-1" />
) : null;
},
options:
- loadingTags ||
- // Consider tags loading if we can't find the currently filtered tag
- (searchParamsObj.tagId &&
- ![...(selectedTags ?? []), ...(tags ?? [])].some(
- (t) => t.id === searchParamsObj.tagId,
- ))
- ? null
- : [
- // Add selected tag to list if not already in tags
- ...(tags ?? []),
- ...(selectedTags
- ?.filter((st) => !tags?.some((t) => t.id === st.id))
- ?.map((st) => ({ ...st, hideDuringSearch: true })) ??
- []),
- ].map((tag) => ({
- value: tag.id,
- icon: (
- <div
- className={cn(
- "rounded-md p-1.5",
- COLORS_LIST.find(({ color }) => color === tag.color)
- ?.css,
- )}
- >
- <Tag className="h-2.5 w-2.5" />
- </div>
- ),
- label: tag.name,
- data: { color: tag.color },
- hideDuringSearch: (
- tag as TagProps & { hideDuringSearch?: boolean }
- ).hideDuringSearch,
- })) ?? null,
+ tags?.map(({ id, name, color }) => ({
+ value: id,
+ icon: <TagBadge color={color} withIcon className="sm:p-1" />,
+ label: name,
+ data: { color },
+ })) ?? null,
},
]),
{
@@ -579,7 +547,7 @@ export default function Toggle({
domainsAsync,
loadingTags,
loadingDomains,
- searchParamsObj.tagId,
+ searchParamsObj.tagIds,
searchParamsObj.domain,
],
);
diff --git a/packages/ui/src/filter/filter-list.tsx b/packages/ui/src/filter/filter-list.tsx
index 135391bcc2..d8b8cf79ba 100644
--- a/packages/ui/src/filter/filter-list.tsx
+++ b/packages/ui/src/filter/filter-list.tsx
@@ -39,7 +39,7 @@ export function FilterList({
>
<div className="flex grow flex-wrap gap-x-4 gap-y-2">
<AnimatePresence>
- {activeFilters?.map(({ key, value: filterValue }) => {
+ {activeFilters?.map(({ key, value: filterValue }, idx) => {
if (key === "loader") {
return (
<motion.div
@@ -85,7 +85,7 @@ export function FilterList({
return (
<motion.div
- key={`${key}-${value}`}
+ key={`${key}-${value}-${idx}`}
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
className="flex items-center divide-x rounded-md border border-gray-200 bg-white text-sm text-black"