diff --git a/apps/web/app/admin.dub.co/(dashboard)/commissions/client.tsx b/apps/web/app/admin.dub.co/(dashboard)/commissions/client.tsx new file mode 100644 index 00000000000..75a73034257 --- /dev/null +++ b/apps/web/app/admin.dub.co/(dashboard)/commissions/client.tsx @@ -0,0 +1,280 @@ +"use client"; + +import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip"; +import { AnalyticsLoadingSpinner } from "@/ui/analytics/analytics-loading-spinner"; +import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker"; +import { + CrownSmall, + Table, + usePagination, + useRouterStuff, + useTable, +} from "@dub/ui"; +import { Areas, TimeSeriesChart, XAxis, YAxis } from "@dub/ui/charts"; +import { + cn, + currencyFormatter, + fetcher, + THE_BEGINNING_OF_TIME, +} from "@dub/utils"; +import NumberFlow from "@number-flow/react"; +import { Fragment, useMemo, useState } from "react"; +import useSWR from "swr"; + +type Tab = { + id: string; + label: string; + colorClassName: string; +}; + +export default function PayoutsPageClient() { + const { queryParams, getQueryString, searchParamsObj } = useRouterStuff(); + const { interval, start, end } = searchParamsObj; + + const { data: { programs, timeseries } = {}, isLoading } = useSWR<{ + programs: { + id: string; + name: string; + logo: string; + earnings: number; + }[]; + timeseries: { + start: Date; + earnings: number; + }[]; + }>( + `/api/admin/commissions${getQueryString({ + timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, + })}`, + fetcher, + { + keepPreviousData: true, + }, + ); + + const tabs: Tab[] = [ + { + id: "commissions", + label: "Commissions", + colorClassName: "text-teal-500 bg-teal-500/50 border-teal-500", + }, + ]; + + const [selectedTab, setSelectedTab] = useState("commissions"); + const tab = tabs.find(({ id }) => id === selectedTab) ?? tabs[0]; + + const chartData = + timeseries?.map(({ start, earnings }) => ({ + date: start ? new Date(start) : new Date(), + values: { + value: earnings || 0, + }, + })) ?? null; + + const totals = useMemo(() => { + return { + commissions: timeseries?.reduce( + (acc, { earnings }) => acc + (earnings || 0), + 0, + ), + }; + }, [timeseries]); + + const { pagination, setPagination } = usePagination(); + + const { table, ...tableProps } = useTable({ + data: programs ?? [], + columns: [ + { + id: "position", + header: "Position", + size: 12, + minSize: 12, + maxSize: 12, + cell: ({ row }) => { + return ( +
+ {row.index + 1} + {row.index <= 2 && ( + + )} +
+ ); + }, + }, + { + id: "program", + header: "Program", + cell: ({ row }) => ( +
+ {row.original.name} + {row.original.name} +
+ ), + }, + { + id: "commissions", + header: "Commissions", + accessorKey: "earnings", + cell: ({ row }) => + currencyFormatter(row.original.earnings / 100, { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + }), + }, + ], + pagination, + onPaginationChange: setPagination, + resourceName: (plural) => `program${plural ? "s" : ""}`, + rowCount: programs?.length ?? 0, + loading: isLoading, + }); + + return ( +
+ +
+
+ {tabs.map(({ id, label, colorClassName }) => { + return ( + + ); + })} +
+
+
+ {chartData ? ( + chartData.length > 0 ? ( + d.values.value, + isActive: true, + colorClassName: tab.colorClassName, + }, + ]} + tooltipClassName="p-0" + tooltipContent={(d) => ( + <> +

+ {formatDateTooltip(d.date, { + interval, + start, + end, + })} +

+
+ +
+
+

+ {tab.label} +

+
+

+ {currencyFormatter(d.values.value / 100)} +

+ +
+ + )} + > + + + formatDateTooltip(d, { + interval, + start, + end, + dataAvailableFrom: THE_BEGINNING_OF_TIME, + }) + } + /> + currencyFormatter(value / 100)} + /> + + ) : ( +
+ No data available. +
+ ) + ) : ( + + )} +
+
+
+
+ + + + ); +} diff --git a/apps/web/app/admin.dub.co/(dashboard)/commissions/page.tsx b/apps/web/app/admin.dub.co/(dashboard)/commissions/page.tsx new file mode 100644 index 00000000000..b6d5bcf7f30 --- /dev/null +++ b/apps/web/app/admin.dub.co/(dashboard)/commissions/page.tsx @@ -0,0 +1,10 @@ +import { Suspense } from "react"; +import PayoutsPageClient from "./client"; + +export default async function PayoutsPage() { + return ( + + + + ); +} diff --git a/apps/web/app/admin.dub.co/(dashboard)/layout.tsx b/apps/web/app/admin.dub.co/(dashboard)/layout.tsx index 31c4bdbea2c..901d50f0a64 100644 --- a/apps/web/app/admin.dub.co/(dashboard)/layout.tsx +++ b/apps/web/app/admin.dub.co/(dashboard)/layout.tsx @@ -15,12 +15,12 @@ const tabs = [ label: "Analytics", }, { - href: "/payouts", - label: "Payouts", + href: "/commissions", + label: "Commissions", }, { - href: "/demo", - label: "Demo", + href: "/payouts", + label: "Payouts", }, ]; diff --git a/apps/web/app/api/admin/commissions/route.ts b/apps/web/app/api/admin/commissions/route.ts new file mode 100644 index 00000000000..94175313ee3 --- /dev/null +++ b/apps/web/app/api/admin/commissions/route.ts @@ -0,0 +1,26 @@ +import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates"; +import { withAdmin } from "@/lib/auth"; +import { THE_BEGINNING_OF_TIME } from "@dub/utils"; +import { NextResponse } from "next/server"; +import { getCommissionsTimeseries, getTopProgramsByCommissions } from "./utils"; + +export const GET = withAdmin(async ({ searchParams }) => { + const { interval = "mtd", start, end, timezone = "UTC" } = searchParams; + + const { startDate, endDate, granularity } = getStartEndDates({ + interval, + start, + end, + dataAvailableFrom: THE_BEGINNING_OF_TIME, + }); + + const [programs, timeseries] = await Promise.all([ + getTopProgramsByCommissions({ startDate, endDate }), + getCommissionsTimeseries({ startDate, endDate, granularity, timezone }), + ]); + + return NextResponse.json({ + programs, + timeseries, + }); +}); diff --git a/apps/web/app/api/admin/commissions/utils.ts b/apps/web/app/api/admin/commissions/utils.ts new file mode 100644 index 00000000000..716b05f60a9 --- /dev/null +++ b/apps/web/app/api/admin/commissions/utils.ts @@ -0,0 +1,116 @@ +import { sqlGranularityMap } from "@/lib/planetscale/granularity"; +import { prisma } from "@dub/prisma"; +import { ACME_PROGRAM_ID } from "@dub/utils"; +import { DateTime } from "luxon"; + +export async function getTopProgramsByCommissions({ + startDate, + endDate, +}: { + startDate: Date; + endDate: Date; +}) { + const programCommissions = await prisma.commission.groupBy({ + by: ["programId"], + _sum: { + earnings: true, + }, + where: { + earnings: { + gt: 0, + }, + programId: { + not: ACME_PROGRAM_ID, + }, + createdAt: { + gte: startDate, + lte: endDate, + }, + }, + orderBy: { + _sum: { + earnings: "desc", + }, + }, + }); + + const topPrograms = await prisma.program.findMany({ + where: { + id: { + in: programCommissions.map(({ programId }) => programId), + }, + }, + }); + + const topProgramsWithCommissions = programCommissions.map( + ({ programId, _sum }) => ({ + ...topPrograms.find((program) => program.id === programId), + earnings: _sum.earnings || 0, + }), + ); + + return topProgramsWithCommissions; +} + +interface Commission { + start: string; + earnings: number; +} + +export async function getCommissionsTimeseries({ + startDate, + endDate, + granularity, + timezone, +}: { + startDate: Date; + endDate: Date; + granularity: string; + timezone: string; +}) { + const { dateFormat, dateIncrement, startFunction, formatString } = + sqlGranularityMap[granularity]; + + const commissions = await prisma.$queryRaw` + SELECT + DATE_FORMAT(CONVERT_TZ(createdAt, "UTC", ${timezone || "UTC"}), ${dateFormat}) AS start, + SUM(earnings) AS earnings + FROM Commission + WHERE + earnings > 0 + AND programId != ${ACME_PROGRAM_ID} + AND createdAt >= ${startDate} + AND createdAt < ${endDate} + GROUP BY start + ORDER BY start ASC;`; + + let currentDate = startFunction( + DateTime.fromJSDate(startDate).setZone(timezone || "UTC"), + ); + + const earningsLookup = Object.fromEntries( + commissions.map((item) => [ + item.start, + { + earnings: Number(item.earnings), + }, + ]), + ); + + const timeseries: Commission[] = []; + + while (currentDate < endDate) { + const periodKey = currentDate.toFormat(formatString); + + timeseries.push({ + start: currentDate.toISO(), + ...(earningsLookup[periodKey] || { + earnings: 0, + }), + }); + + currentDate = dateIncrement(currentDate); + } + + return timeseries; +} diff --git a/apps/web/app/api/admin/payouts/route.ts b/apps/web/app/api/admin/payouts/route.ts index 4416bb59622..b2122722964 100644 --- a/apps/web/app/api/admin/payouts/route.ts +++ b/apps/web/app/api/admin/payouts/route.ts @@ -2,7 +2,7 @@ import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates"; import { withAdmin } from "@/lib/auth"; import { sqlGranularityMap } from "@/lib/planetscale/granularity"; import { prisma } from "@dub/prisma"; -import { ACME_WORKSPACE_ID } from "@dub/utils"; +import { ACME_PROGRAM_ID } from "@dub/utils"; import { DateTime } from "luxon"; import { NextResponse } from "next/server"; @@ -28,8 +28,8 @@ export const GET = withAdmin(async ({ searchParams }) => { // Fetch invoices const invoices = await prisma.invoice.findMany({ where: { - workspaceId: { - not: ACME_WORKSPACE_ID, + programId: { + not: ACME_PROGRAM_ID, }, status: "completed", paidAt: { @@ -65,7 +65,7 @@ export const GET = withAdmin(async ({ searchParams }) => { SUM(total) as total FROM Invoice WHERE - workspaceId != ${ACME_WORKSPACE_ID} + programId != ${ACME_PROGRAM_ID} AND status = 'completed' AND paidAt IS NOT NULL AND paidAt >= ${startDate} diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx index d91f83b9ac2..ac36b3ec25a 100644 --- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx +++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/page-client.tsx @@ -73,11 +73,11 @@ export default function ProgramOverviewPageClient() {

- Recent sales + Recent commissions

{program.wordmark || program.logo ? ( ) : ( diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx index b1a10983c6f..a94712b2881 100644 --- a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx +++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/(enrolled)/links/page-client.tsx @@ -1,8 +1,8 @@ "use client"; import { + DATE_RANGE_INTERVAL_PRESETS, DUB_PARTNERS_ANALYTICS_INTERVAL, - intervals, } from "@/lib/analytics/constants"; import { IntervalOptions } from "@/lib/analytics/types"; import usePartnerLinks from "@/lib/swr/use-partner-links"; @@ -18,7 +18,7 @@ import { PartnerLinkCard } from "./partner-link-card"; const PartnerLinksContext = createContext<{ start?: Date; end?: Date; - interval: (typeof intervals)[number]; + interval: (typeof DATE_RANGE_INTERVAL_PRESETS)[number]; } | null>(null); export function usePartnerLinksContext() { diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts index 9e6a3367490..36b036a251a 100644 --- a/apps/web/lib/analytics/constants.ts +++ b/apps/web/lib/analytics/constants.ts @@ -1,18 +1,6 @@ import { THE_BEGINNING_OF_TIME } from "@dub/utils"; -export const intervals = [ - "24h", - "7d", - "30d", - "90d", - "1y", - "mtd", - "qtd", - "ytd", - "all", -] as const; - -export const eventIntervals = [ +export const DATE_RANGE_INTERVAL_PRESETS = [ "24h", "7d", "30d", diff --git a/apps/web/lib/analytics/types.ts b/apps/web/lib/analytics/types.ts index 53fcff011de..54b462380f6 100644 --- a/apps/web/lib/analytics/types.ts +++ b/apps/web/lib/analytics/types.ts @@ -7,12 +7,12 @@ import { getPartnerEarningsTimeseriesSchema } from "../zod/schemas/partner-profi import { ANALYTICS_SALE_UNIT, ANALYTICS_VIEWS, + DATE_RANGE_INTERVAL_PRESETS, EVENT_TYPES, VALID_ANALYTICS_ENDPOINTS, - intervals, } from "./constants"; -export type IntervalOptions = (typeof intervals)[number]; +export type IntervalOptions = (typeof DATE_RANGE_INTERVAL_PRESETS)[number]; export type AnalyticsGroupByOptions = (typeof VALID_ANALYTICS_ENDPOINTS)[number]; diff --git a/apps/web/lib/swr/use-customer.ts b/apps/web/lib/swr/use-customer.ts index 494de68a077..3b9ce736cc6 100644 --- a/apps/web/lib/swr/use-customer.ts +++ b/apps/web/lib/swr/use-customer.ts @@ -14,16 +14,14 @@ export default function useCustomer< >({ customerId, query, - enabled = true, }: { customerId: T["id"]; query?: z.infer; - enabled?: boolean; }) { const { id: workspaceId } = useWorkspace(); const { data, error, isLoading } = useSWR( - enabled && workspaceId + customerId && workspaceId ? `/api/customers/${customerId}?${new URLSearchParams({ workspaceId: workspaceId, ...query, diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts index 978ebc9a989..68d069dd7a2 100644 --- a/apps/web/lib/zod/schemas/analytics.ts +++ b/apps/web/lib/zod/schemas/analytics.ts @@ -1,11 +1,10 @@ import { + DATE_RANGE_INTERVAL_PRESETS, EVENT_TYPES, OLD_ANALYTICS_ENDPOINTS, OLD_TO_NEW_ANALYTICS_ENDPOINTS, TRIGGER_TYPES, VALID_ANALYTICS_ENDPOINTS, - eventIntervals, - intervals, } from "@/lib/analytics/constants"; import { prefixWorkspaceId } from "@/lib/api/workspace-id"; import z from "@/lib/zod"; @@ -106,7 +105,7 @@ export const analyticsQuerySchema = z .optional() .describe("The ID of the customer to retrieve analytics for."), interval: z - .enum(intervals) + .enum(DATE_RANGE_INTERVAL_PRESETS) .optional() .describe( "The interval to retrieve analytics for. If undefined, defaults to 24h.", @@ -117,12 +116,12 @@ export const analyticsQuerySchema = z }) .optional() .describe( - "The start date and time when to retrieve analytics from. Takes precedence over `interval`.", + "The start date and time when to retrieve analytics from. If set, takes precedence over `interval`.", ), end: parseDateSchema .optional() .describe( - "The end date and time when to retrieve analytics from. If not provided, defaults to the current date. Takes precedence over `interval`.", + "The end date and time when to retrieve analytics from. If not provided, defaults to the current date. If set along with `start`, takes precedence over `interval`.", ), timezone: z .string() @@ -300,12 +299,6 @@ export const eventsQuerySchema = analyticsQuerySchema .describe( "The type of event to retrieve analytics for. Defaults to 'clicks'.", ), - interval: z - .enum(eventIntervals) - .default("24h") - .describe( - "The interval to retrieve events for. Takes precedence over start and end. If undefined, defaults to 24h.", - ), page: z.coerce.number().default(1), limit: z.coerce.number().default(PAGINATION_LIMIT), sortOrder, diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts index 68855b90f5a..450711f5ccd 100644 --- a/apps/web/lib/zod/schemas/commissions.ts +++ b/apps/web/lib/zod/schemas/commissions.ts @@ -1,4 +1,4 @@ -import { intervals } from "@/lib/analytics/constants"; +import { DATE_RANGE_INTERVAL_PRESETS } from "@/lib/analytics/constants"; import { CommissionStatus } from "@dub/prisma/client"; import { z } from "zod"; import { CustomerSchema } from "./customers"; @@ -35,7 +35,7 @@ export const getCommissionsQuerySchema = z status: z.nativeEnum(CommissionStatus).optional(), sortBy: z.enum(["createdAt", "amount"]).default("createdAt"), sortOrder: z.enum(["asc", "desc"]).default("desc"), - interval: z.enum(intervals).default("all"), + interval: z.enum(DATE_RANGE_INTERVAL_PRESETS).default("all"), start: parseDateSchema.optional(), end: parseDateSchema.optional(), }) diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.ts index f073bc17661..77432024227 100644 --- a/apps/web/lib/zod/schemas/partner-profile.ts +++ b/apps/web/lib/zod/schemas/partner-profile.ts @@ -1,6 +1,6 @@ import { + DATE_RANGE_INTERVAL_PRESETS, DUB_PARTNERS_ANALYTICS_INTERVAL, - intervals, } from "@/lib/analytics/constants"; import { z } from "zod"; import { @@ -39,7 +39,9 @@ export const getPartnerEarningsQuerySchema = getCommissionsQuerySchema }) .merge( z.object({ - interval: z.enum(intervals).default(DUB_PARTNERS_ANALYTICS_INTERVAL), + interval: z + .enum(DATE_RANGE_INTERVAL_PRESETS) + .default(DUB_PARTNERS_ANALYTICS_INTERVAL), type: z.enum(["click", "lead", "sale"]).optional(), linkId: z.string().optional(), sortBy: z.enum(["createdAt", "amount", "earnings"]).default("createdAt"), @@ -52,7 +54,9 @@ export const getPartnerEarningsCountQuerySchema = getCommissionsCountQuerySchema }) .merge( z.object({ - interval: z.enum(intervals).default(DUB_PARTNERS_ANALYTICS_INTERVAL), + interval: z + .enum(DATE_RANGE_INTERVAL_PRESETS) + .default(DUB_PARTNERS_ANALYTICS_INTERVAL), type: z.enum(["click", "lead", "sale"]).optional(), linkId: z.string().optional(), groupBy: z.enum(["linkId", "customerId", "status", "type"]).optional(), diff --git a/apps/web/lib/zod/schemas/programs.ts b/apps/web/lib/zod/schemas/programs.ts index 23ffadb783f..ccd4de4f9c0 100644 --- a/apps/web/lib/zod/schemas/programs.ts +++ b/apps/web/lib/zod/schemas/programs.ts @@ -1,6 +1,6 @@ import { + DATE_RANGE_INTERVAL_PRESETS, DUB_PARTNERS_ANALYTICS_INTERVAL, - intervals, } from "@/lib/analytics/constants"; import { DUB_MIN_PAYOUT_AMOUNT_CENTS } from "@/lib/partners/constants"; import { ProgramEnrollmentStatus, ProgramType } from "@dub/prisma/client"; @@ -97,7 +97,9 @@ export const ProgramInviteSchema = z.object({ }); export const getProgramMetricsQuerySchema = z.object({ - interval: z.enum(intervals).default(DUB_PARTNERS_ANALYTICS_INTERVAL), + interval: z + .enum(DATE_RANGE_INTERVAL_PRESETS) + .default(DUB_PARTNERS_ANALYTICS_INTERVAL), start: parseDateSchema.optional(), end: parseDateSchema.optional(), }); diff --git a/apps/web/ui/analytics/events/customer-row-item.tsx b/apps/web/ui/analytics/events/customer-row-item.tsx index f5ed182be14..87206710a7d 100644 --- a/apps/web/ui/analytics/events/customer-row-item.tsx +++ b/apps/web/ui/analytics/events/customer-row-item.tsx @@ -9,22 +9,20 @@ export function CustomerRowItem({ customer }: { customer: Customer }) { const display = customer.email || customer.name || generateRandomName(); return ( - <> - -
- {display} - {display} -
- - - + +
+ {display} + {display} +
+ + ); } diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx index 6602ba67160..efafa15cc1f 100644 --- a/apps/web/ui/analytics/events/events-table.tsx +++ b/apps/web/ui/analytics/events/events-table.tsx @@ -156,6 +156,11 @@ export default function EventsTable({ size: 250, maxSize: 400, cell: ({ getValue }) => , + meta: { + filterParams: ({ getValue }) => ({ + customerId: getValue().id, + }), + }, }, { id: "country", diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsx index 7fe5cecc7df..21a872851b1 100644 --- a/apps/web/ui/analytics/toggle.tsx +++ b/apps/web/ui/analytics/toggle.tsx @@ -214,12 +214,29 @@ export default function Toggle({ ...(root ? [{ key: "root", value: root === "true" }] : []), // Handle folderId special case ...(folderId ? [{ key: "folderId", value: folderId }] : []), + // Handle customerId special case + ...(selectedCustomerId && selectedCustomer + ? [ + { + key: "customerId", + value: + selectedCustomer.email || + selectedCustomer.name || + selectedCustomer.externalId, + }, + ] + : []), ]; // Handle all other filters dynamically VALID_ANALYTICS_FILTERS.forEach((filter) => { // Skip special cases we handled above - if (["domain", "key", "tagId", "tagIds", "root"].includes(filter)) return; + if ( + ["domain", "key", "tagId", "tagIds", "root", "customerId"].includes( + filter, + ) + ) + return; // also skip date range filters and qr if (["interval", "start", "end", "qr"].includes(filter)) return; @@ -230,7 +247,7 @@ export default function Toggle({ }); return filters; - }, [searchParamsObj, selectedTagIds]); + }, [searchParamsObj, selectedTagIds, selectedCustomerId, selectedCustomer]); const isRequested = useCallback( (key: string) => @@ -371,30 +388,6 @@ export default function Toggle({ : partnerPage ? [LinkFilterItem] : [ - ...(["leads", "sales"].includes(selectedTab) - ? [ - { - key: "customerId", - icon: User, - label: "Customer", - shouldFilter: !customersAsync, - options: - customers?.map(({ id, email, name, avatar }) => { - return { - value: id, - label: email ?? name, - icon: ( - {`${email} - ), - }; - }) ?? null, - }, - ] - : []), ...(flags?.linkFolders ? [ { @@ -504,6 +497,40 @@ export default function Toggle({ ]), ], }, + + { + key: "customerId", + icon: User, + label: "Customer", + shouldFilter: !customersAsync, + getOptionIcon: (value) => { + return selectedCustomer ? ( + {`${selectedCustomer.email} + ) : null; + }, + options: + customers?.map(({ id, email, name, avatar }) => { + return { + value: id, + label: email ?? name, + icon: ( + {`${email} + ), + }; + }) ?? null, + }, + LinkFilterItem, { key: "root", @@ -531,7 +558,7 @@ export default function Toggle({ getOptionIcon: (value) => ( {value} ), diff --git a/packages/tinybird/pipes/v2_browsers.pipe b/packages/tinybird/pipes/v2_browsers.pipe index 12ad08294fd..300485afa34 100644 --- a/packages/tinybird/pipes/v2_browsers.pipe +++ b/packages/tinybird/pipes/v2_browsers.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_browsers_endpoint_read_1118" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT browser, COUNT(browser) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE browser != 'Unknown' @@ -50,11 +64,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,6 +101,7 @@ SQL > WHERE browser != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -86,11 +112,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -118,6 +144,7 @@ SQL > WHERE browser != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -128,11 +155,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -165,3 +192,4 @@ SQL > {% else %} browsers_composite {% end %} + diff --git a/packages/tinybird/pipes/v2_cities.pipe b/packages/tinybird/pipes/v2_cities.pipe index 1f338958d98..70e6d927bdc 100644 --- a/packages/tinybird/pipes/v2_cities.pipe +++ b/packages/tinybird/pipes/v2_cities.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_cities_endpoint_read_6984" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,8 +38,16 @@ SQL > % SELECT city, country, COUNT(city) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE @@ -50,11 +63,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,6 +89,7 @@ SQL > WHERE city != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -86,11 +100,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -119,6 +133,7 @@ SQL > WHERE city != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -129,11 +144,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_continents.pipe b/packages/tinybird/pipes/v2_continents.pipe index 020a434bbcb..926ad0baf37 100644 --- a/packages/tinybird/pipes/v2_continents.pipe +++ b/packages/tinybird/pipes/v2_continents.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top continents -TOKEN "v2_continents_endpoint_read_0662" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT continent, COUNT(continent) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE continent != '' @@ -59,11 +73,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -94,6 +119,7 @@ SQL > ) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -104,11 +130,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -145,6 +171,7 @@ SQL > ) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -155,11 +182,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_count.pipe b/packages/tinybird/pipes/v2_count.pipe index 7df39f6c8a4..9b6fb8d0889 100644 --- a/packages/tinybird/pipes/v2_count.pipe +++ b/packages/tinybird/pipes/v2_count.pipe @@ -38,14 +38,22 @@ SQL > % SELECT COUNT(*) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} - {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -56,11 +64,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -106,40 +125,56 @@ NODE count_sales SQL > % - SELECT - sales, - amount, - amount AS saleAmount - FROM ( - SELECT COUNT(*) as sales, sum(amount) as amount - FROM - dub_sale_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) - {% end %} - WHERE - true - {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} - {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} - {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} - {% if defined(continent) %} AND continent = {{ continent }} {% end %} - {% if defined(country) %} AND country = {{ country }} {% end %} - {% if defined(region) %} AND region = {{ region }} {% end %} - {% if defined(city) %} AND city = {{ city }} {% end %} - {% if defined(device) %} AND device = {{ device }} {% end %} - {% if defined(browser) %} AND browser = {{ browser }} {% end %} - {% if defined(os) %} AND os = {{ os }} {% end %} - {% if defined(referer) %} AND referer = {{ referer }} {% end %} - {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} - {% if defined(url) %} AND url = {{ url }} {% end %} - {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} - {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} - ) AS subquery + SELECT sales, amount, amount AS saleAmount + FROM + ( + SELECT COUNT(*) as sales, sum(amount) as amount + FROM + dub_sale_events_mv + {% if not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} + WHERE + true + {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} + {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} + {% if defined(continent) %} AND continent = {{ continent }} {% end %} + {% if defined(country) %} AND country = {{ country }} {% end %} + {% if defined(region) %} AND region = {{ region }} {% end %} + {% if defined(city) %} AND city = {{ city }} {% end %} + {% if defined(device) %} AND device = {{ device }} {% end %} + {% if defined(browser) %} AND browser = {{ browser }} {% end %} + {% if defined(os) %} AND os = {{ os }} {% end %} + {% if defined(referer) %} AND referer = {{ referer }} {% end %} + {% if defined(refererUrl) %} + AND splitByString('?', referer_url)[1] = {{ refererUrl }} + {% end %} + {% if defined(utm_source) %} + AND url + LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url + LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url LIKE concat( + '%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%' + ) + {% end %} + {% if defined(utm_term) %} + AND url + LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url + LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} + {% if defined(url) %} AND url = {{ url }} {% end %} + {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} + {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} + ) AS subquery diff --git a/packages/tinybird/pipes/v2_countries.pipe b/packages/tinybird/pipes/v2_countries.pipe index fcdd2f2e606..1fd9b6d25d4 100644 --- a/packages/tinybird/pipes/v2_countries.pipe +++ b/packages/tinybird/pipes/v2_countries.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_countries_endpoint_read_6734" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,8 +38,16 @@ SQL > % SELECT country, COUNT(country) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE @@ -59,11 +72,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -94,6 +107,7 @@ SQL > ) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -104,11 +118,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -145,6 +159,7 @@ SQL > ) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -155,11 +170,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -192,3 +207,4 @@ SQL > {% else %} countries_composite {% end %} + diff --git a/packages/tinybird/pipes/v2_devices.pipe b/packages/tinybird/pipes/v2_devices.pipe index 0c98d07c241..8555c81c5e0 100644 --- a/packages/tinybird/pipes/v2_devices.pipe +++ b/packages/tinybird/pipes/v2_devices.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_devices_endpoint_read_4705" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT device, COUNT(device) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE device != 'Unknown' @@ -50,11 +64,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,6 +101,7 @@ SQL > WHERE device != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -86,11 +112,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -118,6 +144,7 @@ SQL > WHERE device != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -128,11 +155,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_events.pipe b/packages/tinybird/pipes/v2_events.pipe index f5cbd568e57..593dff0d6ca 100644 --- a/packages/tinybird/pipes/v2_events.pipe +++ b/packages/tinybird/pipes/v2_events.pipe @@ -41,11 +41,17 @@ SQL > splitByString('?', referer_url)[1] as referer_url_processed, CONCAT(country, '-', region) as region_processed, 'click' as event - FROM dub_click_events_mv + FROM {% if defined(customerId) %} dub_click_events_id {% else %} dub_click_events_mv {% end %} WHERE timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }} AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }} - {% if defined(linkId) %} AND link_id = {{ String(linkId) }} + {% if defined(customerId) %} + AND click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif defined(linkId) %} AND link_id = {{ String(linkId) }} {% elif defined(workspaceId) or defined(partnerId) or defined(programId) %} AND link_id IN (SELECT link_id FROM workspace_links) {% end %} @@ -59,11 +65,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %} LIMIT {{ Int32(limit, 100) }} @@ -80,7 +97,7 @@ SQL > splitByString('?', referer_url)[1] as referer_url_processed, CONCAT(country, '-', region) as region_processed, 'lead' as event - FROM dub_lead_events_mv_new + FROM dub_lead_events_mv WHERE timestamp >= {{ DateTime(start, '2024-01-01 00:00:00') }} AND timestamp < {{ DateTime(end, '2025-12-31 00:00:00') }} @@ -132,7 +149,7 @@ SQL > CONCAT(country, '-', region) as region_processed, splitByString('?', referer_url)[1] as referer_url_processed, 'sale' as event - FROM dub_sale_events_mv_new + FROM dub_sale_events_mv WHERE timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }} AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }} diff --git a/packages/tinybird/pipes/v2_os.pipe b/packages/tinybird/pipes/v2_os.pipe index 577cae12a5e..8c0874617e9 100644 --- a/packages/tinybird/pipes/v2_os.pipe +++ b/packages/tinybird/pipes/v2_os.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_os_endpoint_read_0301" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT os, COUNT(os) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE os != 'Unknown' @@ -50,11 +64,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,6 +101,7 @@ SQL > WHERE os != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -86,11 +112,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -118,6 +144,7 @@ SQL > WHERE os != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -128,11 +155,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_referer_urls.pipe b/packages/tinybird/pipes/v2_referer_urls.pipe index 901bddfe801..23b91c2bc7e 100644 --- a/packages/tinybird/pipes/v2_referer_urls.pipe +++ b/packages/tinybird/pipes/v2_referer_urls.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_referer_urls_endpoint_read_1826" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -52,11 +57,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -79,6 +84,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -89,11 +95,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -122,6 +128,7 @@ SQL > WHERE referer != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -132,11 +139,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_referers.pipe b/packages/tinybird/pipes/v2_referers.pipe index 7ec6508dbdb..0b56aa70f3a 100644 --- a/packages/tinybird/pipes/v2_referers.pipe +++ b/packages/tinybird/pipes/v2_referers.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_referers_endpoint_read_1177" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT referer, COUNT(referer) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true @@ -50,11 +64,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,6 +101,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -86,11 +112,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -118,6 +144,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -128,11 +155,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_regions.pipe b/packages/tinybird/pipes/v2_regions.pipe index da0324a63cf..0c42ff5c642 100644 --- a/packages/tinybird/pipes/v2_regions.pipe +++ b/packages/tinybird/pipes/v2_regions.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_regions_endpoint_read_9112" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -56,11 +61,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -89,6 +94,7 @@ SQL > region NOT IN ('Unknown', 'arn1', 'bom1', 'cdg1', 'cle1', 'cpt1', 'dub1', 'fra1', 'gru1', 'hkg1', 'hnd1', 'iad1', 'icn1', 'kix1', 'lhr1', 'pdx1', 'sfo1', 'sin1', 'syd1') AND country != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -99,11 +105,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -134,6 +140,7 @@ SQL > region NOT IN ('Unknown', 'arn1', 'bom1', 'cdg1', 'cle1', 'cpt1', 'dub1', 'fra1', 'gru1', 'hkg1', 'hnd1', 'iad1', 'icn1', 'kix1', 'lhr1', 'pdx1', 'sfo1', 'sin1', 'syd1') AND country != 'Unknown' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -144,11 +151,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_timeseries.pipe b/packages/tinybird/pipes/v2_timeseries.pipe index cb4f69a50a6..9d32ee9d515 100644 --- a/packages/tinybird/pipes/v2_timeseries.pipe +++ b/packages/tinybird/pipes/v2_timeseries.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Timeseries data -TOKEN "v2_timeseries_endpoint_read_8675" READ - TAGS "Dub Endpoints" NODE month_intervals @@ -49,7 +47,7 @@ SQL > arrayJoin( arrayMap( x -> toDateTime64(toStartOfDay(toDateTime64(x, 3), {{ String(timezone, 'UTC') }}), 3), - range(toUInt32(start + 86400), toUInt32(end + 86400), + range(toUInt32(start), toUInt32(end + 86400), 86400 ) ) @@ -73,7 +71,7 @@ SQL > end SELECT arrayJoin( - arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start + 3600), toUInt32(end + 3600), 3600) + arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start), toUInt32(end + 3600), 3600) ) ) as interval @@ -84,16 +82,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -114,11 +119,20 @@ SQL > ) {% else %} toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) {% end %} AS interval, - uniq(*) as clicks + uniq(click_id) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true @@ -133,11 +147,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} AND timestamp >= {{ DateTime(start, '2024-04-25 00:00:00') }} AND timestamp < {{ DateTime(end, '2024-05-23 00:00:00') }} @@ -184,6 +209,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -194,11 +220,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} AND timestamp >= {{ DateTime(start, '2024-04-25 00:00:00') }} AND timestamp < {{ DateTime(end, '2024-05-23 00:00:00') }} @@ -249,6 +275,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -259,11 +286,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} AND timestamp >= {{ DateTime(start, '2024-04-25 00:00:00') }} AND timestamp < {{ DateTime(end, '2024-05-23 00:00:00') }} diff --git a/packages/tinybird/pipes/v2_top_links.pipe b/packages/tinybird/pipes/v2_top_links.pipe index 021435ad7b1..6854c7a55bc 100644 --- a/packages/tinybird/pipes/v2_top_links.pipe +++ b/packages/tinybird/pipes/v2_top_links.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_top_links_endpoint_read_5142" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT link_id as link, COUNT(*) AS clicks FROM - dub_click_events_mv - {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true @@ -50,17 +64,28 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} GROUP BY link_id ORDER BY clicks DESC - LIMIT 5000 + LIMIT 1000 @@ -71,12 +96,13 @@ SQL > SELECT link_id as link, COUNT(*) AS leads FROM dub_lead_events_mv - {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -87,11 +113,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -114,12 +140,13 @@ SQL > SELECT link_id as link, COUNT(*) as sales, sum(amount) as amount FROM dub_sale_events_mv - {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -130,17 +157,17 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} GROUP BY link_id ORDER BY sales DESC - LIMIT 5000 + LIMIT 1000 ) as subquery diff --git a/packages/tinybird/pipes/v2_top_tags.pipe b/packages/tinybird/pipes/v2_top_tags.pipe new file mode 100644 index 00000000000..9132b119b70 --- /dev/null +++ b/packages/tinybird/pipes/v2_top_tags.pipe @@ -0,0 +1,283 @@ +DESCRIPTION > + Top countries + + +TAGS "Dub Endpoints" + +NODE workspace_links_with_tags +SQL > + + % + SELECT link_id, arrayJoin(tag_ids) as tag_id + FROM dub_links_metadata_latest FINAL + WHERE + deleted == 0 + {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} + {% if defined(programId) %} AND program_id = {{ programId }} {% end %} + {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} + {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} + {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} + {% if defined(root) %} + {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} + {% end %} + + + +NODE top_tags_clicks +SQL > + + % + SELECT tag_id, COUNT(*) AS clicks + FROM (SELECT link_id, tag_id FROM workspace_links_with_tags) AS tags + JOIN + ( + SELECT link_id + FROM + dub_click_events_mv + {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + PREWHERE link_id in (SELECT link_id from workspace_links_with_tags) + {% end %} + WHERE + true + {% if defined(continent) %} AND continent = {{ continent }} {% end %} + {% if defined(country) %} AND country = {{ country }} {% end %} + {% if defined(region) %} AND region = {{ region }} {% end %} + {% if defined(city) %} AND city = {{ city }} {% end %} + {% if defined(device) %} AND device = {{ device }} {% end %} + {% if defined(browser) %} AND browser = {{ browser }} {% end %} + {% if defined(os) %} AND os = {{ os }} {% end %} + {% if defined(referer) %} AND referer = {{ referer }} {% end %} + {% if defined(refererUrl) %} + AND splitByString('?', referer_url)[1] = {{ refererUrl }} + {% end %} + {% if defined(utm_source) %} + AND url + LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url + LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url LIKE concat( + '%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%' + ) + {% end %} + {% if defined(utm_term) %} + AND url + LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url + LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} + {% if defined(url) %} AND url = {{ url }} {% end %} + {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} + {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} + ) AS clicks + ON clicks.link_id = tags.link_id + GROUP BY tag_id + ORDER BY clicks DESC + LIMIT 5000 + + + +NODE top_tags_leads +SQL > + + % + SELECT tag_id, COUNT(*) AS leads + FROM (SELECT link_id, tag_id FROM workspace_links_with_tags) AS tags + JOIN + ( + SELECT link_id + FROM + dub_lead_events_mv + {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + PREWHERE link_id in (SELECT link_id from workspace_links_with_tags) + {% end %} + WHERE + true + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} + {% if defined(continent) %} AND continent = {{ continent }} {% end %} + {% if defined(country) %} AND country = {{ country }} {% end %} + {% if defined(region) %} AND region = {{ region }} {% end %} + {% if defined(city) %} AND city = {{ city }} {% end %} + {% if defined(device) %} AND device = {{ device }} {% end %} + {% if defined(browser) %} AND browser = {{ browser }} {% end %} + {% if defined(os) %} AND os = {{ os }} {% end %} + {% if defined(referer) %} AND referer = {{ referer }} {% end %} + {% if defined(refererUrl) %} + AND splitByString('?', referer_url)[1] = {{ refererUrl }} + {% end %} + {% if defined(utm_source) %} + AND url + LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url + LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url LIKE concat( + '%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%' + ) + {% end %} + {% if defined(utm_term) %} + AND url + LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url + LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} + {% if defined(url) %} AND url = {{ url }} {% end %} + {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} + {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} + ) AS leads + ON leads.link_id = tags.link_id + GROUP BY tag_id + ORDER BY leads DESC + LIMIT 5000 + + + +NODE top_tags_sales +SQL > + + % + SELECT tag_id, sales, amount, amount AS saleAmount + FROM + ( + SELECT tag_id, COUNT(*) as sales, sum(amount) as amount + FROM (SELECT link_id, tag_id FROM workspace_links_with_tags) AS tags + JOIN + ( + SELECT link_id, amount + FROM + dub_sale_events_mv + {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + PREWHERE link_id in (SELECT link_id from workspace_links_with_tags) + {% end %} + WHERE + true + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} + {% if defined(continent) %} AND continent = {{ continent }} {% end %} + {% if defined(country) %} AND country = {{ country }} {% end %} + {% if defined(region) %} AND region = {{ region }} {% end %} + {% if defined(city) %} AND city = {{ city }} {% end %} + {% if defined(device) %} AND device = {{ device }} {% end %} + {% if defined(browser) %} AND browser = {{ browser }} {% end %} + {% if defined(os) %} AND os = {{ os }} {% end %} + {% if defined(referer) %} AND referer = {{ referer }} {% end %} + {% if defined(refererUrl) %} + AND splitByString('?', referer_url)[1] = {{ refererUrl }} + {% end %} + {% if defined(utm_source) %} + AND url LIKE concat( + '%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%' + ) + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat( + '%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%' + ) + {% end %} + {% if defined(utm_campaign) %} + AND url LIKE concat( + '%utm_campaign=', + encodeURLFormComponent({{ String(utm_campaign) }}), + '%' + ) + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat( + '%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%' + ) + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat( + '%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%' + ) + {% end %} + {% if defined(url) %} AND url = {{ url }} {% end %} + {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} + {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} + ) AS sales + ON sales.link_id = tags.link_id + GROUP BY tag_id + ORDER BY sales DESC + LIMIT 5000 + ) as subquery + + + +NODE top_tags_composite +SQL > + + SELECT COALESCE(c.tag_id, l.tag_id, s.tag_id) as tag_id, clicks, leads, sales, amount, saleAmount + FROM (SELECT tag_id, clicks FROM top_tags_clicks) AS c + FULL OUTER JOIN (SELECT tag_id, leads FROM top_tags_leads) AS l ON c.tag_id = l.tag_id + FULL OUTER JOIN + (SELECT tag_id, sales, amount, saleAmount FROM top_tags_sales) AS s + ON COALESCE(c.tag_id, l.tag_id) = s.tag_id + ORDER BY clicks DESC NULLS LAST + + + +NODE endpoint +SQL > + + % + SELECT * + FROM + {% if eventType == 'clicks' %} top_tags_clicks + {% elif eventType == 'leads' %} top_tags_leads + {% elif eventType == 'sales' %} top_tags_sales + {% else %} top_tags_composite + {% end %} + + + +NODE filtered_clicks +SQL > + + % + SELECT link_id, COUNT(*) AS clicks + FROM dub_click_events_mv + {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} + PREWHERE link_id in (SELECT link_id from workspace_links) + {% end %} + WHERE + true + {% if defined(continent) %} AND continent = {{ continent }} {% end %} + {% if defined(country) %} AND country = {{ country }} {% end %} + {% if defined(region) %} AND region = {{ region }} {% end %} + {% if defined(city) %} AND city = {{ city }} {% end %} + {% if defined(device) %} AND device = {{ device }} {% end %} + {% if defined(browser) %} AND browser = {{ browser }} {% end %} + {% if defined(os) %} AND os = {{ os }} {% end %} + {% if defined(referer) %} AND referer = {{ referer }} {% end %} + {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') + {% end %} + {% if defined(utm_campaign) %} + AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') + {% end %} + {% if defined(url) %} AND url = {{ url }} {% end %} + {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} + {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} + GROUP BY link_id + + diff --git a/packages/tinybird/pipes/v2_top_urls.pipe b/packages/tinybird/pipes/v2_top_urls.pipe index fd8a368937b..a2c6332109b 100644 --- a/packages/tinybird/pipes/v2_top_urls.pipe +++ b/packages/tinybird/pipes/v2_top_urls.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_top_urls_endpoint_read_4482" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -34,7 +39,7 @@ SQL > SELECT url, COUNT(*) AS clicks FROM dub_click_events_mv - {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE @@ -50,11 +55,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -71,12 +76,13 @@ SQL > SELECT url, COUNT(*) AS leads FROM dub_lead_events_mv - {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE url != '' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -87,11 +93,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -114,12 +120,13 @@ SQL > SELECT url, COUNT(*) as sales, sum(amount) as amount FROM dub_sale_events_mv - {% if defined(workspaceId) or defined(partnerId) or defined(programId) %} + {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE url != '' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -130,11 +137,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_triggers.pipe b/packages/tinybird/pipes/v2_triggers.pipe index 7097375fcb0..5a86db8a2a0 100644 --- a/packages/tinybird/pipes/v2_triggers.pipe +++ b/packages/tinybird/pipes/v2_triggers.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Top countries -TOKEN "v2_triggers_endpoint_read_4261" READ - TAGS "Dub Endpoints" NODE workspace_links @@ -11,16 +9,23 @@ SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -33,9 +38,18 @@ SQL > % SELECT CASE WHEN qr = 0 THEN 'link' WHEN qr = 1 THEN 'qr' END as trigger, COUNT(qr) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE true @@ -50,11 +64,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,6 +101,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -86,11 +112,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} @@ -121,6 +147,7 @@ SQL > WHERE true {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -131,11 +158,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %} diff --git a/packages/tinybird/pipes/v2_usage.pipe b/packages/tinybird/pipes/v2_usage.pipe index 8784169fa9f..ffb5a238735 100644 --- a/packages/tinybird/pipes/v2_usage.pipe +++ b/packages/tinybird/pipes/v2_usage.pipe @@ -2,8 +2,6 @@ DESCRIPTION > Timeseries data -TOKEN "v1_usage_endpoint_read_1647" READ - NODE day_intervals SQL > diff --git a/packages/tinybird/pipes/v2_utms.pipe b/packages/tinybird/pipes/v2_utms.pipe index d713070bfff..707904fa2b0 100644 --- a/packages/tinybird/pipes/v2_utms.pipe +++ b/packages/tinybird/pipes/v2_utms.pipe @@ -1,20 +1,25 @@ -TOKEN "v2_utms_endpoint_read_8196" READ - NODE workspace_links SQL > % SELECT link_id - from dub_links_metadata_latest FINAL + FROM + {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest + {% else %} dub_regular_links_metadata_latest + {% end %} FINAL WHERE deleted == 0 {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %} {% if defined(programId) %} AND program_id = {{ programId }} {% end %} {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %} {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %} - {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %} + {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }} + {% elif defined(folderId) %} AND folder_id = {{ folderId }} + {% end %} {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %} - {% if defined(tagIds) %} AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] {% end %} + {% if defined(tagIds) %} + AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != [] + {% end %} {% if defined(root) %} {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %} {% end %} @@ -27,7 +32,7 @@ SQL > % WITH extractURLParameter( - url, + decodeURLFormComponent(url), {{ String( groupByUtmTag, @@ -39,9 +44,18 @@ SQL > ) as utm SELECT utm, count(*) as clicks FROM - dub_click_events_mv - {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} - PREWHERE link_id in (SELECT link_id from workspace_links) + {% if defined(customerId) %} dub_click_events_id + {% else %} dub_click_events_mv + {% end %} + {% if defined(customerId) %} + PREWHERE click_id IN ( + SELECT DISTINCT click_id + FROM dub_lead_events_mv + WHERE customer_id = {{ String(customerId) }} + ) + {% elif not defined(linkId) and ( + defined(workspaceId) or defined(partnerId) or defined(programId) + ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %} WHERE url != '' @@ -56,11 +70,22 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} + AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') + {% end %} + {% if defined(utm_medium) %} + AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') + {% end %} + {% if defined(utm_campaign) %} + AND url + LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') + {% end %} + {% if defined(utm_term) %} + AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') + {% end %} + {% if defined(utm_content) %} + AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') + {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -76,7 +101,7 @@ SQL > % WITH extractURLParameter( - url, + decodeURLFormComponent(url), {{ String( groupByUtmTag, @@ -95,6 +120,7 @@ SQL > WHERE url != '' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -105,11 +131,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} @@ -125,7 +151,7 @@ SQL > % WITH extractURLParameter( - url, + decodeURLFormComponent(url), {{ String( groupByUtmTag, @@ -144,6 +170,7 @@ SQL > WHERE url != '' {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %} + {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %} {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %} {% if defined(continent) %} AND continent = {{ continent }} {% end %} {% if defined(country) %} AND country = {{ country }} {% end %} @@ -154,11 +181,11 @@ SQL > {% if defined(os) %} AND os = {{ os }} {% end %} {% if defined(referer) %} AND referer = {{ referer }} {% end %} {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %} - {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', {{ String(utm_source) }}, '%') {% end %} - {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%') {% end %} - {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%') {% end %} - {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%') {% end %} - {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%') {% end %} + {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %} + {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %} + {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %} + {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %} + {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %} {% if defined(url) %} AND url = {{ url }} {% end %} {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %} {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %} diff --git a/packages/utils/src/constants/main.ts b/packages/utils/src/constants/main.ts index ce6e1928ec9..4bddf529fe6 100644 --- a/packages/utils/src/constants/main.ts +++ b/packages/utils/src/constants/main.ts @@ -70,6 +70,7 @@ export const DUB_THUMBNAIL = "https://assets.dub.co/thumbnail.jpg"; export const DUB_WORKSPACE_ID = "cl7pj5kq4006835rbjlt2ofka"; export const ACME_WORKSPACE_ID = "clrei1gld0002vs9mzn93p8ik"; +export const ACME_PROGRAM_ID = "prog_CYCu7IMAapjkRpTnr8F1azjN"; export const LEGAL_WORKSPACE_ID = "clrflia0j0000vs7sqfhz9c7q"; export const LEGAL_USER_ID = "clqei1lgc0000vsnzi01pbf47";