Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

saleType → saleOccurrence

Steven Tey6994182b2a16
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/page-client.tsx
index f021bb7bf7..a7df83f705 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/page-client.tsx
@@ -10,7 +10,7 @@ import { ConversionBlock } from "@/ui/partners/overview/blocks/conversion-block"
import { CountriesBlock } from "@/ui/partners/overview/blocks/countries-block";
import { LinksBlock } from "@/ui/partners/overview/blocks/links-block";
import { PartnersBlock } from "@/ui/partners/overview/blocks/partners-block";
-import { SaleTypeBlock } from "@/ui/partners/overview/blocks/sale-type-block";
+import { SaleOccurrenceBlock } from "@/ui/partners/overview/blocks/sale-occurrence-block";
import { TrafficSourcesBlock } from "@/ui/partners/overview/blocks/traffic-sources-block";
import { ProgramOverviewCard } from "@/ui/partners/overview/program-overview-card";
import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
@@ -29,7 +29,7 @@ const BLOCKS = [
ConversionBlock,
CountriesBlock,
LinksBlock,
- SaleTypeBlock,
+ SaleOccurrenceBlock,
];
export default function ProgramOverviewPageClient() {
diff --git a/apps/web/lib/analytics/constants.ts b/apps/web/lib/analytics/constants.ts
index 8beae72190..5d59a0b09f 100644
--- a/apps/web/lib/analytics/constants.ts
+++ b/apps/web/lib/analytics/constants.ts
@@ -172,13 +172,13 @@ export const VALID_ANALYTICS_FILTERS = [
"refererUrl",
"url",
"partnerId",
- "saleType",
"folderId",
"tagId",
"tagIds",
"customerId",
"qr", // deprecated, but keeping for now for backwards compatibility
"root",
+ "saleOccurrence",
"utm_source",
"utm_medium",
"utm_campaign",
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index 6b2cae352b..72ed5f5f64 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -137,10 +137,6 @@ export const analyticsQuerySchema = z
"The IANA time zone code for aligning timeseries granularity (e.g. America/New_York). Defaults to UTC.",
)
.openapi({ example: "America/New_York", default: "UTC" }),
- saleType: z
- .enum(["new", "recurring"])
- .optional()
- .describe("The sale type to retrieve analytics for."),
country: z
.enum(COUNTRY_CODES)
.optional()
@@ -230,6 +226,12 @@ export const analyticsQuerySchema = z
.describe(
"Filter for root domains. If true, filter for domains only. If false, filter for links only. If undefined, return both.",
),
+ saleOccurrence: z
+ .enum(["new", "recurring"])
+ .optional()
+ .describe(
+ "The sale occurrence to retrieve analytics for (to differentiate between first and recurring sales). If undefined, return both.",
+ ),
})
.merge(utmTagsSchema);
@@ -242,8 +244,8 @@ export const analyticsFilterTB = z
.optional()
.transform((v) => (v ? prefixWorkspaceId(v) : undefined)),
customerId: z.string().optional(),
- saleType: z.string().optional(),
root: z.boolean().optional(),
+ saleOccurrence: z.string().optional(),
qr: z.boolean().optional(),
start: z.string(),
end: z.string(),
diff --git a/apps/web/ui/analytics/use-analytics-filters.tsx b/apps/web/ui/analytics/use-analytics-filters.tsx
index b493ea0b11..b844dfb676 100644
--- a/apps/web/ui/analytics/use-analytics-filters.tsx
+++ b/apps/web/ui/analytics/use-analytics-filters.tsx
@@ -464,6 +464,24 @@ export function useAnalyticsFilters({
icon,
})) ?? null,
},
+ {
+ key: "saleOccurrence",
+ icon: Receipt2,
+ label: "Sale occurrence",
+ separatorAfter: true,
+ options: [
+ {
+ value: "new",
+ label: "New",
+ icon: UserPlus,
+ },
+ {
+ value: "recurring",
+ label: "Recurring",
+ icon: Calendar6,
+ },
+ ],
+ },
...(dashboardProps
? []
: programPage
@@ -490,24 +508,6 @@ export function useAnalyticsFilters({
};
}) ?? null,
},
- {
- key: "saleType",
- icon: Receipt2,
- label: "Sale type",
- separatorAfter: true,
- options: [
- {
- value: "new",
- label: "First",
- icon: UserPlus,
- },
- {
- value: "recurring",
- label: "Recurring",
- icon: Calendar6,
- },
- ],
- },
]
: partnerPage
? [LinkFilterItem, CustomerFilterItem]
diff --git a/apps/web/ui/partners/overview/blocks/sale-type-block.tsx b/apps/web/ui/partners/overview/blocks/sale-occurrence-block.tsx
similarity index 97%
rename from apps/web/ui/partners/overview/blocks/sale-type-block.tsx
rename to apps/web/ui/partners/overview/blocks/sale-occurrence-block.tsx
index 39186b4b6f..91862edf21 100644
--- a/apps/web/ui/partners/overview/blocks/sale-type-block.tsx
+++ b/apps/web/ui/partners/overview/blocks/sale-occurrence-block.tsx
@@ -9,7 +9,7 @@ import { useContext, useMemo, useState } from "react";
import useSWR from "swr";
import { ProgramOverviewBlock } from "../program-overview-block";
-export function SaleTypeBlock() {
+export function SaleOccurrenceBlock() {
const { slug: workspaceSlug } = useWorkspace();
const { getQueryString } = useRouterStuff();
@@ -27,7 +27,7 @@ export function SaleTypeBlock() {
`/api/analytics?${editQueryString(queryString, {
event: "sales",
groupBy: "count",
- saleType: "recurring",
+ saleOccurrence: "recurring",
})}`,
fetcher,
{
@@ -112,7 +112,7 @@ export function SaleTypeBlock() {
<Link
key={item.key}
href={`/${workspaceSlug}/program/analytics${getQueryString(
- { tab: "sales", saleType: item.key },
+ { tab: "sales", saleOccurrence: item.key },
{
include: ["interval", "start", "end"],
},
diff --git a/packages/tinybird/pipes/v2_count.pipe b/packages/tinybird/pipes/v2_count.pipe
index 0fdae48d16..42341c89f5 100644
--- a/packages/tinybird/pipes/v2_count.pipe
+++ b/packages/tinybird/pipes/v2_count.pipe
@@ -122,16 +122,69 @@ SQL >
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
+
+
+
+NODE count_sales_with_occurrence
SQL >
%
WITH
- sale_rankings AS (
- SELECT
- *,
- ROW_NUMBER() OVER (
- PARTITION BY customer_id, link_id ORDER BY timestamp ASC
- ) as sale_rank
+ sales AS (
+ SELECT customer_id, link_id, timestamp, amount
FROM
dub_sale_events_mv
{% if not defined(linkId) and (
@@ -177,17 +230,31 @@ SQL >
{% if defined(url) %} AND url = {{ url }} {% end %}
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ ),
+ distinct_sales AS (
+ SELECT DISTINCT customer_id, link_id
+ FROM sales
+ ),
+ min_timestamps AS (
+ SELECT
+ customer_id,
+ link_id,
+ min(timestamp) AS first_sale_ts
+ FROM dub_sale_events_mv
+ WHERE (customer_id, link_id) IN distinct_sales
+ GROUP BY customer_id, link_id
)
SELECT sales, amount, amount AS saleAmount
FROM
(
SELECT COUNT(*) as sales, sum(amount) as amount
- FROM sale_rankings
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
WHERE
1 = 1
- {% if defined(saleType) %}
- {% if saleType == 'new' %} AND sale_rank = 1
- {% elif saleType == 'recurring' %} AND sale_rank > 1
+ {% if defined(saleOccurrence) %}
+ {% if saleOccurrence == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleOccurrence == 'recurring' %} AND timestamp > first_sale_ts
{% end %}
{% end %}
) AS subquery
@@ -209,7 +276,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} count_clicks
{% elif eventType == 'leads' %} count_leads
- {% elif eventType == 'sales' %} count_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} count_sales_with_occurrence {% else %} count_sales {% end %}
{% else %} count_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_events.pipe b/packages/tinybird/pipes/v2_events.pipe
index 6f7eebf092..ea7bece15e 100644
--- a/packages/tinybird/pipes/v2_events.pipe
+++ b/packages/tinybird/pipes/v2_events.pipe
@@ -180,6 +180,85 @@ SQL >
+NODE sale_events_with_occurrence
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT
+ *,
+ amount as saleAmount,
+ CONCAT(country, '-', region) as region_processed,
+ splitByString('?', referer_url)[1] as referer_url_processed,
+ 'sale' as event
+ 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') }}
+ {% if 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 %}
+ {% 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 %}
+ ),
+ distinct_sales AS (SELECT DISTINCT customer_id, link_id FROM sales),
+ min_timestamps AS (
+ SELECT customer_id, link_id, min(timestamp) AS first_sale_ts
+ FROM dub_sale_events_mv
+ WHERE (customer_id, link_id) IN distinct_sales
+ GROUP BY customer_id, link_id
+ )
+ SELECT *, CASE WHEN timestamp = first_sale_ts THEN 'new' ELSE 'recurring' END as sale_occurrence
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleOccurrence) %}
+ {% if saleOccurrence == 'new' %} AND timestamp = first_sale_ts
+ {% elif saleOccurrence == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
+ LIMIT {{ Int32(limit, 100) }}
+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}
+
+
+
NODE endpoint
SQL >
@@ -187,6 +266,9 @@ SQL >
SELECT *
FROM
{% if eventType == 'leads' %} lead_events
- {% elif eventType == 'sales' %} sale_events
+ {% elif eventType == 'sales' %}
+ {% if defined(saleOccurrence) %} sale_events_with_occurrence {% else %} sale_events {% end %}
{% else %} click_events
{% end %}
+
+