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.

update pipes + filters

Steven Tey85651bba0343
diff --git a/apps/web/ui/analytics/use-analytics-filters.tsx b/apps/web/ui/analytics/use-analytics-filters.tsx
index f5dc25533e..e6d047cf84 100644
--- a/apps/web/ui/analytics/use-analytics-filters.tsx
+++ b/apps/web/ui/analytics/use-analytics-filters.tsx
@@ -450,6 +450,25 @@ export function useAnalyticsFilters({
}) ?? null,
};
+ const SaleTypeFilterItem = {
+ key: "saleType",
+ icon: Receipt2,
+ label: "Sale type",
+ separatorAfter: true,
+ options: [
+ {
+ value: "new",
+ label: "New",
+ icon: UserPlus,
+ },
+ {
+ value: "recurring",
+ label: "Recurring",
+ icon: Calendar6,
+ },
+ ],
+ };
+
const filters: ComponentProps<typeof Filter.Select>["filters"] = useMemo(
() => [
{
@@ -464,24 +483,6 @@ export function useAnalyticsFilters({
icon,
})) ?? null,
},
- {
- key: "saleType",
- icon: Receipt2,
- label: "Sale type",
- separatorAfter: true,
- options: [
- {
- value: "new",
- label: "New",
- icon: UserPlus,
- },
- {
- value: "recurring",
- label: "Recurring",
- icon: Calendar6,
- },
- ],
- },
...(dashboardProps
? []
: programPage
@@ -508,11 +509,13 @@ export function useAnalyticsFilters({
};
}) ?? null,
},
+ SaleTypeFilterItem,
]
: partnerPage
- ? [LinkFilterItem, CustomerFilterItem]
+ ? [LinkFilterItem, CustomerFilterItem, SaleTypeFilterItem]
: [
...(canManageCustomers ? [CustomerFilterItem] : []),
+ SaleTypeFilterItem,
{
key: "folderId",
icon: Folder,
diff --git a/packages/tinybird/pipes/v2_browsers.pipe b/packages/tinybird/pipes/v2_browsers.pipe
index 300485afa3..9c3c4e918d 100644
--- a/packages/tinybird/pipes/v2_browsers.pipe
+++ b/packages/tinybird/pipes/v2_browsers.pipe
@@ -164,18 +164,95 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY browser
- ORDER BY sales DESC
+ ORDER BY amount DESC
) as subquery
+NODE browsers_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT browser, customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 %}
+ ),
+ 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
+ browser,
+ sales,
+ amount,
+ amount AS saleAmount
+ FROM (
+ SELECT browser, COUNT(*) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY browser
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
+
NODE browsers_composite
SQL >
+ %
SELECT dce.browser AS browser, clicks, leads, sales, amount, saleAmount
FROM (SELECT * FROM browsers_clicks) AS dce
LEFT JOIN (SELECT * FROM browsers_leads) AS dle ON dce.browser = dle.browser
- LEFT JOIN (SELECT * FROM browsers_sales) AS dse ON dce.browser = dse.browser
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} browsers_sales_with_type {% else %} browsers_sales {% end %}
+ ) AS dse
+ ON dce.browser = dse.browser
ORDER BY clicks DESC
@@ -188,7 +265,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} browsers_clicks
{% elif eventType == 'leads' %} browsers_leads
- {% elif eventType == 'sales' %} browsers_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} browsers_sales_with_type {% else %} browsers_sales {% end %}
{% else %} browsers_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_cities.pipe b/packages/tinybird/pipes/v2_cities.pipe
index ce5e8600e4..46dc9735f7 100644
--- a/packages/tinybird/pipes/v2_cities.pipe
+++ b/packages/tinybird/pipes/v2_cities.pipe
@@ -78,6 +78,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY city, region, country
ORDER BY clicks DESC
+ LIMIT 1000
@@ -131,6 +132,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY city, region, country
ORDER BY leads DESC
+ LIMIT 1000
@@ -176,18 +178,134 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY city, region, country
- ORDER BY sales DESC
+ ORDER BY amount DESC
+ LIMIT 1000
) as subquery
+NODE cities_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT city, region, country, customer_id, link_id, timestamp, 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
+ city != 'Unknown'
+ AND region NOT IN (
+ 'Unknown',
+ 'arn1',
+ 'bom1',
+ 'cdg1',
+ 'cle1',
+ 'cpt1',
+ 'dub1',
+ 'fra1',
+ 'gru1',
+ 'hkg1',
+ 'hnd1',
+ 'iad1',
+ 'icn1',
+ 'kix1',
+ 'lhr1',
+ 'pdx1',
+ 'sfo1',
+ 'sin1',
+ 'syd1'
+ )
+ {% 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 %}
+ ),
+ 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 city, CONCAT(country, '-', region) as region, country, sales, amount, amount AS saleAmount
+ FROM
+ (
+ SELECT city, region, country, COUNT(city) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY city, region, country
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
NODE cities_composite
SQL >
- SELECT dce.city AS city, dce.region AS region, dce.country AS country, clicks, leads, sales, amount, saleAmount
+ %
+ SELECT
+ dce.city AS city,
+ dce.region AS region,
+ dce.country AS country,
+ clicks,
+ leads,
+ sales,
+ amount,
+ saleAmount
FROM (SELECT city, region, country, clicks FROM cities_clicks) AS dce
LEFT JOIN (SELECT * FROM cities_leads) AS dle ON dce.city = dle.city AND dce.region = dle.region
- LEFT JOIN (SELECT * FROM cities_sales) AS dse ON dce.city = dse.city AND dce.region = dse.region
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} cities_sales_with_type {% else %} cities_sales {% end %}
+ ) AS dse
+ ON dce.city = dse.city
+ AND dce.region = dse.region
ORDER BY clicks DESC
@@ -200,7 +318,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} cities_clicks
{% elif eventType == 'leads' %} cities_leads
- {% elif eventType == 'sales' %} cities_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} cities_sales_with_type {% else %} cities_sales {% end %}
{% else %} cities_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_continents.pipe b/packages/tinybird/pipes/v2_continents.pipe
index 926ad0baf3..a5f9aacded 100644
--- a/packages/tinybird/pipes/v2_continents.pipe
+++ b/packages/tinybird/pipes/v2_continents.pipe
@@ -191,18 +191,106 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY continent
- ORDER BY sales DESC
+ ORDER BY amount DESC
) as subquery
+NODE continents_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT continent, customer_id, link_id, timestamp, 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
+ continent != ''
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% 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 %}
+ ),
+ 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
+ continent,
+ sales,
+ amount,
+ amount AS saleAmount
+ FROM (
+ SELECT continent, COUNT(continent) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY continent
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
NODE continents_composite
SQL >
+ %
SELECT dce.continent AS continent, clicks, leads, sales, amount, saleAmount
FROM (SELECT continent, clicks FROM continents_clicks) AS dce
- LEFT JOIN (SELECT * FROM continents_leads) AS dle ON dce.continent = dle.continent
- LEFT JOIN (SELECT * FROM continents_sales) AS dse ON dce.continent = dse.continent
+ LEFT JOIN (SELECT * FROM continents_leads) AS dle ON dce.continent = dle.continent
+ LEFT JOIN
+ (
+ SELECT *
+ FROM
+ {% if defined(saleType) %} continents_sales_with_type
+ {% else %} continents_sales
+ {% end %}
+ ) AS dse
+ ON dce.continent = dse.continent
ORDER BY clicks DESC
@@ -215,7 +303,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} continents_clicks
{% elif eventType == 'leads' %} continents_leads
- {% elif eventType == 'sales' %} continents_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} continents_sales_with_type {% else %} continents_sales {% end %}
{% else %} continents_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_count.pipe b/packages/tinybird/pipes/v2_count.pipe
index dffc215272..074432d643 100644
--- a/packages/tinybird/pipes/v2_count.pipe
+++ b/packages/tinybird/pipes/v2_count.pipe
@@ -264,7 +264,14 @@ SQL >
NODE count_composite
SQL >
- SELECT clicks, leads, sales, amount, saleAmount FROM count_clicks, count_leads, count_sales
+ %
+ SELECT clicks, leads, sales, amount, saleAmount
+ FROM
+ count_clicks,
+ count_leads,
+ {% if defined(saleType) %} count_sales_with_type
+ {% else %} count_sales
+ {% end %}
@@ -277,7 +284,7 @@ SQL >
{% if eventType == 'clicks' %} count_clicks
{% elif eventType == 'leads' %} count_leads
{% elif eventType == 'sales' %}
- {% if defined(saleType) %} count_sales {% else %} count_sales {% end %}
+ {% if defined(saleType) %} count_sales_with_type {% else %} count_sales {% end %}
{% else %} count_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_countries.pipe b/packages/tinybird/pipes/v2_countries.pipe
index 1fd9b6d25d..44e2d43f2b 100644
--- a/packages/tinybird/pipes/v2_countries.pipe
+++ b/packages/tinybird/pipes/v2_countries.pipe
@@ -179,18 +179,106 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY country
- ORDER BY sales DESC
+ ORDER BY amount DESC
) as subquery
+NODE countries_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT country, customer_id, link_id, timestamp, 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
+ country != 'Unknown'
+ {% if defined(linkId) %}
+ AND link_id
+ = {{
+ String(
+ linkId,
+ 'clsdzhhlq0003vskwcaz6nm39',
+ description="The ID of the link",
+ )
+ }}
+ {% 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 %}
+ ),
+ 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
+ country,
+ sales,
+ amount,
+ amount AS saleAmount
+ FROM (
+ SELECT country, COUNT(country) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY country
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
NODE countries_composite
SQL >
+ %
SELECT dce.country AS country, clicks, leads, sales, amount, saleAmount
FROM (SELECT country, clicks FROM countries_clicks) AS dce
- LEFT JOIN (SELECT * FROM countries_leads) AS dle ON dce.country = dle.country
- LEFT JOIN (SELECT * FROM countries_sales) AS dse ON dce.country = dse.country
+ LEFT JOIN (SELECT * FROM countries_leads) AS dle ON dce.country = dle.country
+ LEFT JOIN
+ (
+ SELECT *
+ FROM
+ {% if defined(saleType) %} countries_sales_with_type
+ {% else %} countries_sales
+ {% end %}
+ ) AS dse
+ ON dce.country = dse.country
ORDER BY clicks DESC
@@ -203,7 +291,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} countries_clicks
{% elif eventType == 'leads' %} countries_leads
- {% elif eventType == 'sales' %} countries_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} countries_sales_with_type {% else %} countries_sales {% end %}
{% else %} countries_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_devices.pipe b/packages/tinybird/pipes/v2_devices.pipe
index 8555c81c5e..533d7507e0 100644
--- a/packages/tinybird/pipes/v2_devices.pipe
+++ b/packages/tinybird/pipes/v2_devices.pipe
@@ -164,18 +164,103 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY device
- ORDER BY sales DESC
+ ORDER BY amount DESC
) subquery
+NODE devices_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT device, customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 %}
+ ),
+ 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 device, sales, amount, amount AS saleAmount
+ FROM
+ (
+ SELECT device, COUNT(device) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY device
+ ORDER BY sales DESC
+ ) subquery
+
+
+
NODE devices_composite
SQL >
+ %
SELECT dce.device AS device, clicks, leads, sales, amount
FROM (SELECT device, clicks FROM devices_clicks) AS dce
- LEFT JOIN (SELECT * FROM devices_leads) AS dle ON dce.device = dle.device
- LEFT JOIN (SELECT * FROM devices_sales) AS dse ON dce.device = dse.device
+ LEFT JOIN (SELECT * FROM devices_leads) AS dle ON dce.device = dle.device
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} devices_sales_with_type {% else %} devices_sales {% end %}
+ ) AS dse
+ ON dce.device = dse.device
ORDER BY clicks DESC
@@ -188,7 +273,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} devices_clicks
{% elif eventType == 'leads' %} devices_leads
- {% elif eventType == 'sales' %} devices_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} devices_sales_with_type {% else %} devices_sales {% end %}
{% else %} devices_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_os.pipe b/packages/tinybird/pipes/v2_os.pipe
index 8c0874617e..2121f30942 100644
--- a/packages/tinybird/pipes/v2_os.pipe
+++ b/packages/tinybird/pipes/v2_os.pipe
@@ -164,18 +164,93 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY os
- ORDER BY sales DESC
+ ORDER BY amount DESC
) as subquery
+NODE os_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT os, customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 %}
+ ),
+ 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
+ os,
+ sales,
+ amount,
+ amount AS saleAmount
+ FROM (
+ SELECT os, COUNT(os) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY os
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
NODE os_composite
SQL >
+ %
SELECT dce.os AS os, clicks, leads, sales, amount, saleAmount
FROM (SELECT os, clicks FROM os_clicks) AS dce
- LEFT JOIN (SELECT * FROM os_leads) AS dle ON dce.os = dle.os
- LEFT JOIN (SELECT * FROM os_sales) AS dse ON dce.os = dse.os
+ LEFT JOIN (SELECT * FROM os_leads) AS dle ON dce.os = dle.os
+ LEFT JOIN
+ (
+ SELECT * FROM {% if defined(saleType) %} os_sales_with_type {% else %} os_sales {% end %}
+ ) AS dse
+ ON dce.os = dse.os
ORDER BY clicks DESC
@@ -188,7 +263,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} os_clicks
{% elif eventType == 'leads' %} os_leads
- {% elif eventType == 'sales' %} os_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} os_sales_with_type {% else %} os_sales {% end %}
{% else %} os_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_referer_urls.pipe b/packages/tinybird/pipes/v2_referer_urls.pipe
index 23b91c2bc7..d6d8b2d9c0 100644
--- a/packages/tinybird/pipes/v2_referer_urls.pipe
+++ b/packages/tinybird/pipes/v2_referer_urls.pipe
@@ -105,7 +105,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY splitByString('?', referer_url)[1]
ORDER BY leads DESC
- LIMIT 5000
+ LIMIT 1000
@@ -148,19 +148,101 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY splitByString('?', referer_url)[1]
- ORDER BY sales DESC
- LIMIT 5000
+ ORDER BY amount DESC
+ LIMIT 1000
) as subquery
+NODE referer_urls_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT
+ splitByString('?', referer_url)[1] as refererUrl,
+ customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 %}
+ ),
+ 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
+ refererUrl,
+ sales,
+ amount,
+ amount AS saleAmount
+ FROM (
+ SELECT refererUrl, COUNT(refererUrl) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY refererUrl
+ ORDER BY sales DESC
+ LIMIT 5000
+ ) as subquery
+
+
+
NODE referer_urls_composite
SQL >
+ %
SELECT dce.refererUrl as refererUrl, clicks, leads, sales, amount, saleAmount
FROM (SELECT refererUrl, clicks FROM referer_urls_clicks) AS dce
LEFT JOIN (SELECT * FROM referer_urls_leads) AS dle ON dce.refererUrl = dle.refererUrl
- LEFT JOIN (SELECT * FROM referer_urls_sales) AS dse ON dce.refererUrl = dse.refererUrl
+ LEFT JOIN
+ (
+ SELECT *
+ FROM
+ {% if defined(saleType) %} referer_urls_sales_with_type
+ {% else %} referer_urls_sales
+ {% end %}
+ ) AS dse
+ ON dce.refererUrl = dse.refererUrl
ORDER BY clicks DESC
@@ -173,7 +255,10 @@ SQL >
FROM
{% if eventType == 'clicks' %} referer_urls_clicks
{% elif eventType == 'leads' %} referer_urls_leads
- {% elif eventType == 'sales' %} referer_urls_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} referer_urls_sales_with_type
+ {% else %} referer_urls_sales
+ {% end %}
{% else %} referer_urls_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_referers.pipe b/packages/tinybird/pipes/v2_referers.pipe
index 0b56aa70f3..3056bdf358 100644
--- a/packages/tinybird/pipes/v2_referers.pipe
+++ b/packages/tinybird/pipes/v2_referers.pipe
@@ -85,6 +85,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY referer
ORDER BY clicks DESC
+ LIMIT 1000
@@ -122,6 +123,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY referer
ORDER BY leads DESC
+ LIMIT 1000
@@ -164,18 +166,102 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY referer
- ORDER BY sales DESC
+ ORDER BY amount DESC
+ LIMIT 1000
) as subquery
+NODE referers_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT referer, customer_id, link_id, timestamp, 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 %}
+ ),
+ 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 referer, sales, amount, amount AS saleAmount
+ FROM
+ (
+ SELECT referer, COUNT(referer) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY referer
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
NODE referers_composite
SQL >
+ %
SELECT dce.referer AS referer, clicks, leads, sales, amount, saleAmount
FROM (SELECT referer, clicks FROM referers_clicks) AS dce
LEFT JOIN (SELECT * FROM referers_leads) AS dle ON dce.referer = dle.referer
- LEFT JOIN (SELECT * FROM referers_sales) AS dse ON dce.referer = dse.referer
+ LEFT JOIN (SELECT * FROM
+ {% if defined(saleType) %} referers_sales_with_type
+ {% else %} referers_sales
+ {% end %}) AS dse ON dce.referer = dse.referer
ORDER BY clicks DESC
@@ -188,7 +274,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} referers_clicks
{% elif eventType == 'leads' %} referers_leads
- {% elif eventType == 'sales' %} referers_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} referers_sales_with_type {% else %} referers_sales {% end %}
{% else %} referers_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_regions.pipe b/packages/tinybird/pipes/v2_regions.pipe
index 0c42ff5c64..cb43702d8d 100644
--- a/packages/tinybird/pipes/v2_regions.pipe
+++ b/packages/tinybird/pipes/v2_regions.pipe
@@ -71,6 +71,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY region, country
ORDER BY clicks DESC
+ LIMIT 1000
) as subquery
@@ -115,6 +116,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY region, country
ORDER BY leads DESC
+ LIMIT 1000
) as subquery
@@ -160,18 +162,98 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY region, country
- ORDER BY sales DESC
+ ORDER BY amount DESC
+ LIMIT 1000
) as subquery
+NODE regions_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT region, country, customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 %}
+ ),
+ 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
+ CONCAT(country, '-', region) as region,
+ country,
+ sales,
+ amount,
+ amount AS saleAmount
+ FROM (
+ SELECT region, country, COUNT(city) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY region, country
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
+
NODE regions_composite
SQL >
+ %
SELECT dce.region AS region, dce.country AS country, clicks, leads, sales, amount, saleAmount
FROM (SELECT region, country, clicks FROM regions_clicks) AS dce
LEFT JOIN (SELECT * FROM regions_leads) AS dle ON dce.region = dle.region
- LEFT JOIN (SELECT * FROM regions_sales) AS dse ON dce.region = dse.region
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} regions_sales_with_type {% else %} regions_sales {% end %}
+ ) AS dse
+ ON dce.region = dse.region
ORDER BY clicks DESC
@@ -184,7 +266,9 @@ SQL >
FROM
{% if eventType == 'clicks' %} regions_clicks
{% elif eventType == 'leads' %} regions_leads
- {% elif eventType == 'sales' %} regions_sales
+ {% elif eventType == 'sales' %} {% if defined(saleType) %} regions_sales_with_type
+ {% else %} regions_sales
+ {% end %}
{% else %} regions_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_timeseries.pipe b/packages/tinybird/pipes/v2_timeseries.pipe
index 9d32ee9d51..d201c2a1cc 100644
--- a/packages/tinybird/pipes/v2_timeseries.pipe
+++ b/packages/tinybird/pipes/v2_timeseries.pipe
@@ -299,6 +299,94 @@ SQL >
+NODE timeseries_sales_data_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT
+ {% if granularity == "hour" %} toStartOfHour(timestamp, {{ String(timezone, 'UTC') }})
+ {% elif granularity == "month" %}
+ toDateTime64(
+ toStartOfMonth(timestamp, {{ String(timezone, 'UTC') }}),
+ 3,
+ {{ String(timezone, 'UTC') }}
+ )
+ {% else %} toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
+ {% end %} AS interval,
+ customer_id,
+ link_id,
+ timestamp,
+ 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 %}
+ AND timestamp >= {{ DateTime(start, '2024-04-25 00:00:00') }}
+ AND timestamp < {{ DateTime(end, '2024-05-23 00:00:00') }}
+ ),
+ 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 interval, uniq(*) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY interval
+ ORDER BY interval
+
+
+
NODE timeseries_sales
SQL >
@@ -310,7 +398,10 @@ SQL >
{% elif granularity == "month" %} month_intervals
{% else %} day_intervals
{% end %}
- LEFT JOIN timeseries_sales_data USING interval
+ LEFT JOIN
+ {% if defined(saleType) %} timeseries_sales_data_with_type
+ {% else %} timeseries_sales_data
+ {% end %} USING interval
diff --git a/packages/tinybird/pipes/v2_top_links.pipe b/packages/tinybird/pipes/v2_top_links.pipe
index 6854c7a55b..7895a4adc7 100644
--- a/packages/tinybird/pipes/v2_top_links.pipe
+++ b/packages/tinybird/pipes/v2_top_links.pipe
@@ -166,19 +166,108 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY link_id
- ORDER BY sales DESC
+ ORDER BY amount DESC
LIMIT 1000
) as subquery
+NODE top_links_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT link_id, customer_id, timestamp, 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 >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND timestamp <= {{ DateTime64(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 link, sales, amount, amount AS saleAmount
+ FROM
+ (
+ SELECT link_id as link, COUNT(*) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY link_id
+ ORDER BY sales DESC
+ LIMIT 1000
+ ) as subquery
+
+
+
NODE top_links_composite
SQL >
+ %
SELECT dce.link AS link, clicks, leads, sales, amount, saleAmount
FROM (SELECT link, clicks FROM top_links_clicks) AS dce
- LEFT JOIN (SELECT * FROM top_links_leads) AS dle ON dce.link = dle.link
- LEFT JOIN (SELECT * FROM top_links_sales) AS dse ON dce.link = dse.link
+ LEFT JOIN (SELECT * FROM top_links_leads) AS dle ON dce.link = dle.link
+ LEFT JOIN
+ (
+ SELECT *
+ FROM
+ {% if defined(saleType) %} top_links_sales_with_type
+ {% else %} top_links_sales
+ {% end %}
+ ) AS dse
+ ON dce.link = dse.link
ORDER BY clicks DESC
@@ -191,7 +280,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} top_links_clicks
{% elif eventType == 'leads' %} top_links_leads
- {% elif eventType == 'sales' %} top_links_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} top_links_sales_with_type {% else %} top_links_sales {% end %}
{% else %} top_links_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_top_partners.pipe b/packages/tinybird/pipes/v2_top_partners.pipe
index 9c8bc446ef..b5346ce3aa 100644
--- a/packages/tinybird/pipes/v2_top_partners.pipe
+++ b/packages/tinybird/pipes/v2_top_partners.pipe
@@ -1,4 +1,4 @@
-NODE workspace_links
+NODE partner_links
SQL >
%
@@ -17,52 +17,85 @@ NODE top_partners_clicks
SQL >
%
- SELECT wl.partner_id as partnerId, COUNT(*) AS clicks
- FROM dub_click_events_mv AS ce
- JOIN workspace_links AS wl ON ce.link_id = wl.link_id
- WHERE 1
- {% if defined(continent) %} AND ce.continent = {{ continent }} {% end %}
- {% if defined(country) %} AND ce.country = {{ country }} {% end %}
- {% if defined(region) %} AND ce.region = {{ region }} {% end %}
- {% if defined(city) %} AND ce.city = {{ city }} {% end %}
- {% if defined(device) %} AND ce.device = {{ device }} {% end %}
- {% if defined(browser) %} AND ce.browser = {{ browser }} {% end %}
- {% if defined(os) %} AND ce.os = {{ os }} {% end %}
- {% if defined(referer) %} AND ce.referer = {{ referer }} {% end %}
- {% if defined(refererUrl) %}
- AND splitByString('?', ce.referer_url)[1] = {{ refererUrl }}
- {% end %}
- {% if defined(utm_source) %}
- AND ce.url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
- {% end %}
- {% if defined(utm_medium) %}
- AND ce.url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
- {% end %}
- {% if defined(utm_campaign) %}
- AND ce.url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
- {% end %}
- {% if defined(utm_term) %}
- AND ce.url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
- {% end %}
- {% if defined(utm_content) %}
- AND ce.url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
+ WITH
+ sales AS (
+ SELECT se.customer_id, se.link_id, se.timestamp, se.amount, pl.partner_id
+ FROM dub_sale_events AS se
+ JOIN partner_links AS pl ON se.link_id = pl.link_id
+ WHERE
+ 1
+ {% if defined(continent) %} AND se.continent = {{ continent }} {% end %}
+ {% if defined(country) %} AND se.country = {{ country }} {% end %}
+ {% if defined(region) %} AND se.region = {{ region }} {% end %}
+ {% if defined(city) %} AND se.city = {{ city }} {% end %}
+ {% if defined(device) %} AND se.device = {{ device }} {% end %}
+ {% if defined(browser) %} AND se.browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND se.os = {{ os }} {% end %}
+ {% if defined(referer) %} AND se.referer = {{ referer }} {% end %}
+ {% if defined(refererUrl) %}
+ AND splitByString('?', se.referer_url)[1] = {{ refererUrl }}
+ {% end %}
+ {% if defined(utm_source) %}
+ AND se.url
+ LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
+ {% end %}
+ {% if defined(utm_medium) %}
+ AND se.url
+ LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
+ {% end %}
+ {% if defined(utm_campaign) %}
+ AND se.url
+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
+ {% end %}
+ {% if defined(utm_term) %}
+ AND se.url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
+ {% end %}
+ {% if defined(utm_content) %}
+ AND se.url
+ LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
+ {% end %}
+ {% if defined(url) %} AND se.url = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime64(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
+ WHERE (customer_id, link_id) IN distinct_sales
+ GROUP BY customer_id, link_id
+ )
+ SELECT pl.partner_id as partnerId, COUNT(*) AS sales, sum(amount) as saleAmount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ JOIN partner_links AS pl ON sales.link_id = pl.link_id
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
{% end %}
- {% if defined(url) %} AND ce.url = {{ url }} {% end %}
- {% if defined(start) %} AND ce.timestamp >= {{ DateTime64(start) }} {% end %}
- {% if defined(end) %} AND ce.timestamp <= {{ DateTime64(end) }} {% end %}
- GROUP BY wl.partner_id
- ORDER BY clicks DESC
+ GROUP BY pl.partner_id
+ ORDER BY saleAmount DESC
LIMIT 5000
+
NODE top_partners_leads
SQL >
%
- SELECT wl.partner_id as partnerId, COUNT(*) AS leads
+ SELECT pl.partner_id as partnerId, COUNT(*) AS leads
FROM dub_lead_events AS le
- JOIN workspace_links AS wl ON le.link_id = wl.link_id
+ JOIN partner_links AS pl ON le.link_id = pl.link_id
WHERE
1
{% if defined(continent) %} AND le.continent = {{ continent }} {% end %}
@@ -98,7 +131,7 @@ SQL >
{% if defined(url) %} AND le.url = {{ url }} {% end %}
{% if defined(start) %} AND le.timestamp >= {{ DateTime64(start) }} {% end %}
{% if defined(end) %} AND le.timestamp <= {{ DateTime64(end) }} {% end %}
- GROUP BY wl.partner_id
+ GROUP BY pl.partner_id
ORDER BY leads DESC
LIMIT 5000
@@ -108,9 +141,9 @@ NODE top_partners_sales
SQL >
%
- SELECT wl.partner_id as partnerId, COUNT(*) AS sales, sum(amount) as saleAmount
+ SELECT pl.partner_id as partnerId, COUNT(*) AS sales, sum(amount) as saleAmount
FROM dub_sale_events AS se
- JOIN workspace_links AS wl ON se.link_id = wl.link_id
+ JOIN partner_links AS pl ON se.link_id = pl.link_id
WHERE
1
{% if defined(continent) %} AND se.continent = {{ continent }} {% end %}
@@ -146,12 +179,83 @@ SQL >
{% if defined(url) %} AND se.url = {{ url }} {% end %}
{% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
{% if defined(end) %} AND se.timestamp <= {{ DateTime64(end) }} {% end %}
- GROUP BY wl.partner_id
- ORDER BY sales DESC
+ GROUP BY pl.partner_id
+ ORDER BY saleAmount DESC, sales DESC, partnerId ASC
LIMIT 5000
+NODE top_partners_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT se.customer_id, se.link_id, se.timestamp, se.amount, pl.partner_id
+ FROM dub_sale_events AS se
+ JOIN partner_links AS pl ON se.link_id = pl.link_id
+ WHERE
+ 1
+ {% if defined(continent) %} AND se.continent = {{ continent }} {% end %}
+ {% if defined(country) %} AND se.country = {{ country }} {% end %}
+ {% if defined(region) %} AND se.region = {{ region }} {% end %}
+ {% if defined(city) %} AND se.city = {{ city }} {% end %}
+ {% if defined(device) %} AND se.device = {{ device }} {% end %}
+ {% if defined(browser) %} AND se.browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND se.os = {{ os }} {% end %}
+ {% if defined(referer) %} AND se.referer = {{ referer }} {% end %}
+ {% if defined(refererUrl) %}
+ AND splitByString('?', se.referer_url)[1] = {{ refererUrl }}
+ {% end %}
+ {% if defined(utm_source) %}
+ AND se.url
+ LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
+ {% end %}
+ {% if defined(utm_medium) %}
+ AND se.url
+ LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
+ {% end %}
+ {% if defined(utm_campaign) %}
+ AND se.url LIKE concat(
+ '%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%'
+ )
+ {% end %}
+ {% if defined(utm_term) %}
+ AND se.url
+ LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
+ {% end %}
+ {% if defined(utm_content) %}
+ AND se.url
+ LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
+ {% end %}
+ {% if defined(url) %} AND se.url = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime64(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
+ WHERE (customer_id, link_id) IN distinct_sales
+ GROUP BY customer_id, link_id
+ )
+ SELECT pl.partner_id as partnerId, COUNT(*) AS sales, sum(amount) as saleAmount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ JOIN partner_links AS pl ON sales.link_id = pl.link_id
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY pl.partner_id
+ ORDER BY saleAmount DESC, sales DESC, partnerId ASC
+ LIMIT 5000
+
+
+
NODE top_partners_composite
SQL >
@@ -159,15 +263,11 @@ SQL >
SELECT c.partnerId as partnerId, clicks, leads, sales, saleAmount
FROM top_partners_clicks AS c
LEFT JOIN top_partners_leads AS l ON c.partnerId = l.partnerId
- LEFT JOIN top_partners_sales AS s ON c.partnerId = s.partnerId
- ORDER BY
- {% if defined(sortBy) %}
- {% if sortBy == 'sales' %} s.sales
- {% elif sortBy == 'leads' %} l.leads
- {% else %} c.clicks
- {% end %}
- {% else %} c.clicks
- {% end %} DESC
+ LEFT JOIN
+ {% if defined(saleType) %} top_partners_sales_with_type
+ {% else %} top_partners_sales
+ {% end %} AS s ON c.partnerId = s.partnerId
+ ORDER BY saleAmount DESC
@@ -179,8 +279,10 @@ SQL >
FROM
{% if eventType == 'clicks' %} top_partners_clicks
{% elif eventType == 'leads' %} top_partners_leads
- {% elif eventType == 'sales' %} top_partners_sales
- {% else %} top_partners_composite
+ {% elif eventType == 'composite' %} top_partners_composite
+ {% else %} {% if defined(saleType) %} top_partners_sales_with_type
+ {% else %} top_partners_sales
+ {% end %}
{% end %}
diff --git a/packages/tinybird/pipes/v2_top_programs.pipe b/packages/tinybird/pipes/v2_top_programs.pipe
new file mode 100644
index 0000000000..2d2be4230a
--- /dev/null
+++ b/packages/tinybird/pipes/v2_top_programs.pipe
@@ -0,0 +1,177 @@
+DESCRIPTION >
+ Internal pipe for getting top programs on Dub
+
+
+NODE program_links
+SQL >
+
+ %
+ SELECT link_id, program_id
+ FROM dub_regular_links_metadata_latest FINAL
+ WHERE deleted == 0 AND program_id != ''
+
+
+
+NODE top_programs_clicks
+SQL >
+
+ %
+ SELECT pl.program_id as programId, COUNT(*) AS clicks
+ FROM dub_click_events_mv AS ce
+ JOIN program_links AS pl ON ce.link_id = pl.link_id
+ WHERE 1
+ {% if defined(continent) %} AND ce.continent = {{ continent }} {% end %}
+ {% if defined(country) %} AND ce.country = {{ country }} {% end %}
+ {% if defined(region) %} AND ce.region = {{ region }} {% end %}
+ {% if defined(city) %} AND ce.city = {{ city }} {% end %}
+ {% if defined(device) %} AND ce.device = {{ device }} {% end %}
+ {% if defined(browser) %} AND ce.browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND ce.os = {{ os }} {% end %}
+ {% if defined(referer) %} AND ce.referer = {{ referer }} {% end %}
+ {% if defined(refererUrl) %}
+ AND splitByString('?', ce.referer_url)[1] = {{ refererUrl }}
+ {% end %}
+ {% if defined(utm_source) %}
+ AND ce.url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
+ {% end %}
+ {% if defined(utm_medium) %}
+ AND ce.url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
+ {% end %}
+ {% if defined(utm_campaign) %}
+ AND ce.url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
+ {% end %}
+ {% if defined(utm_term) %}
+ AND ce.url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
+ {% end %}
+ {% if defined(utm_content) %}
+ AND ce.url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
+ {% end %}
+ {% if defined(url) %} AND ce.url = {{ url }} {% end %}
+ {% if defined(start) %} AND ce.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND ce.timestamp <= {{ DateTime64(end) }} {% end %}
+ GROUP BY pl.program_id
+ ORDER BY clicks DESC
+ LIMIT 5000
+
+
+
+NODE top_programs_leads
+SQL >
+
+ %
+ SELECT pl.program_id as programId, COUNT(*) AS leads
+ FROM dub_lead_events AS le
+ JOIN program_links AS pl ON le.link_id = pl.link_id
+ WHERE
+ 1
+ {% if defined(continent) %} AND le.continent = {{ continent }} {% end %}
+ {% if defined(country) %} AND le.country = {{ country }} {% end %}
+ {% if defined(region) %} AND le.region = {{ region }} {% end %}
+ {% if defined(city) %} AND le.city = {{ city }} {% end %}
+ {% if defined(device) %} AND le.device = {{ device }} {% end %}
+ {% if defined(browser) %} AND le.browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND le.os = {{ os }} {% end %}
+ {% if defined(referer) %} AND le.referer = {{ referer }} {% end %}
+ {% if defined(refererUrl) %}
+ AND splitByString('?', le.referer_url)[1] = {{ refererUrl }}
+ {% end %}
+ {% if defined(utm_source) %}
+ AND le.url
+ LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
+ {% end %}
+ {% if defined(utm_medium) %}
+ AND le.url
+ LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
+ {% end %}
+ {% if defined(utm_campaign) %}
+ AND le.url
+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
+ {% end %}
+ {% if defined(utm_term) %}
+ AND le.url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
+ {% end %}
+ {% if defined(utm_content) %}
+ AND le.url
+ LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
+ {% end %}
+ {% if defined(url) %} AND le.url = {{ url }} {% end %}
+ {% if defined(start) %} AND le.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND le.timestamp <= {{ DateTime64(end) }} {% end %}
+ GROUP BY pl.program_id
+ ORDER BY leads DESC
+ LIMIT 5000
+
+
+
+NODE top_programs_sales
+SQL >
+
+ %
+ SELECT pl.program_id as programId, COUNT(*) AS sales, sum(amount) as saleAmount
+ FROM dub_sale_events AS se
+ JOIN program_links AS pl ON se.link_id = pl.link_id
+ WHERE
+ 1
+ {% if defined(continent) %} AND se.continent = {{ continent }} {% end %}
+ {% if defined(country) %} AND se.country = {{ country }} {% end %}
+ {% if defined(region) %} AND se.region = {{ region }} {% end %}
+ {% if defined(city) %} AND se.city = {{ city }} {% end %}
+ {% if defined(device) %} AND se.device = {{ device }} {% end %}
+ {% if defined(browser) %} AND se.browser = {{ browser }} {% end %}
+ {% if defined(os) %} AND se.os = {{ os }} {% end %}
+ {% if defined(referer) %} AND se.referer = {{ referer }} {% end %}
+ {% if defined(refererUrl) %}
+ AND splitByString('?', se.referer_url)[1] = {{ refererUrl }}
+ {% end %}
+ {% if defined(utm_source) %}
+ AND se.url
+ LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
+ {% end %}
+ {% if defined(utm_medium) %}
+ AND se.url
+ LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
+ {% end %}
+ {% if defined(utm_campaign) %}
+ AND se.url
+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
+ {% end %}
+ {% if defined(utm_term) %}
+ AND se.url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
+ {% end %}
+ {% if defined(utm_content) %}
+ AND se.url
+ LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
+ {% end %}
+ {% if defined(url) %} AND se.url = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime64(end) }} {% end %}
+ GROUP BY pl.program_id
+ ORDER BY saleAmount DESC
+ LIMIT 5000
+
+
+
+NODE top_programs_composite
+SQL >
+
+ SELECT c.programId as programId, clicks, leads, sales, saleAmount
+ FROM top_programs_clicks AS c
+ LEFT JOIN top_programs_leads AS l ON c.programId = l.programId
+ LEFT JOIN top_programs_sales AS s ON c.programId = s.programId
+ ORDER BY saleAmount DESC
+
+
+
+NODE endpoint
+SQL >
+
+ %
+ SELECT *
+ FROM
+ {% if eventType == 'clicks' %} top_programs_clicks
+ {% elif eventType == 'leads' %} top_programs_leads
+ {% elif eventType == 'composite' %} top_programs_composite
+ {% else %} top_programs_sales
+ {% end %}
+
+
diff --git a/packages/tinybird/pipes/v2_top_tags.pipe b/packages/tinybird/pipes/v2_top_tags.pipe
index 9132b119b7..9f1455815c 100644
--- a/packages/tinybird/pipes/v2_top_tags.pipe
+++ b/packages/tinybird/pipes/v2_top_tags.pipe
@@ -207,7 +207,7 @@ SQL >
) AS sales
ON sales.link_id = tags.link_id
GROUP BY tag_id
- ORDER BY sales DESC
+ ORDER BY amount DESC
LIMIT 5000
) as subquery
diff --git a/packages/tinybird/pipes/v2_top_urls.pipe b/packages/tinybird/pipes/v2_top_urls.pipe
index a2c6332109..6c64fe7ac1 100644
--- a/packages/tinybird/pipes/v2_top_urls.pipe
+++ b/packages/tinybird/pipes/v2_top_urls.pipe
@@ -103,7 +103,7 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY url
ORDER BY leads DESC
- LIMIT 5000
+ LIMIT 1000
@@ -146,19 +146,105 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY url
- ORDER BY sales DESC
- LIMIT 5000
+ ORDER BY amount DESC
+ LIMIT 1000
) as subquery
+NODE top_urls_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT url, customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 %}
+ ),
+ 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 url, sales, amount, amount AS saleAmount
+ FROM
+ (
+ SELECT url, COUNT(*) as sales, sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY url
+ ORDER BY sales DESC
+ LIMIT 5000
+ ) as subquery
+
+
+
NODE top_urls_composite
SQL >
+ %
SELECT dce.url AS url, clicks, leads, sales, amount, saleAmount
FROM (SELECT url, clicks FROM top_urls_clicks) AS dce
- LEFT JOIN (SELECT * FROM top_urls_leads) AS dle ON dce.url = dle.url
- LEFT JOIN (SELECT * FROM top_urls_sales) AS dse ON dce.url = dse.url
+ LEFT JOIN (SELECT * FROM top_urls_leads) AS dle ON dce.url = dle.url
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} top_urls_sales_with_type {% else %} top_urls_sales {% end %}
+ ) AS dse
+ ON dce.url = dse.url
ORDER BY clicks DESC
@@ -171,7 +257,9 @@ SQL >
FROM
{% if eventType == 'clicks' %} top_urls_clicks
{% elif eventType == 'leads' %} top_urls_leads
- {% elif eventType == 'sales' %} top_urls_sales
+ {% elif eventType == 'sales' %} {% if defined(saleType) %} top_urls_sales_with_type
+ {% else %} top_urls_sales
+ {% end %}
{% else %} top_urls_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_triggers.pipe b/packages/tinybird/pipes/v2_triggers.pipe
index 5a86db8a2a..a58de410fc 100644
--- a/packages/tinybird/pipes/v2_triggers.pipe
+++ b/packages/tinybird/pipes/v2_triggers.pipe
@@ -167,18 +167,106 @@ SQL >
{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
GROUP BY qr
- ORDER BY sales DESC
+ ORDER BY amount DESC
) as subquery
+NODE trigger_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT qr, customer_id, link_id, timestamp, 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 %}
+ ),
+ 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 trigger, sales, amount, amount AS saleAmount
+ FROM
+ (
+ SELECT
+ CASE WHEN qr = 0 THEN 'link' WHEN qr = 1 THEN 'qr' END as trigger,
+ COUNT(qr) as sales,
+ sum(amount) as amount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY qr
+ ORDER BY sales DESC
+ ) as subquery
+
+
+
NODE trigger_composite
SQL >
+ %
SELECT dce.trigger AS trigger, clicks, leads, sales, amount, saleAmount
FROM (SELECT * FROM trigger_clicks) AS dce
LEFT JOIN (SELECT * FROM trigger_leads) AS dle ON dce.trigger = dle.trigger
- LEFT JOIN (SELECT * FROM trigger_sales) AS dse ON dce.trigger = dse.trigger
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} trigger_sales_with_type {% else %} trigger_sales {% end %}
+ ) AS dse
+ ON dce.trigger = dse.trigger
ORDER BY clicks DESC
@@ -191,7 +279,9 @@ SQL >
FROM
{% if eventType == 'clicks' %} trigger_clicks
{% elif eventType == 'leads' %} trigger_leads
- {% elif eventType == 'sales' %} trigger_sales
+ {% elif eventType == 'sales' %} {% if defined(saleType) %} trigger_sales_with_type
+ {% else %} trigger_sales
+ {% end %}
{% else %} trigger_composite
{% end %}
diff --git a/packages/tinybird/pipes/v2_usage.pipe b/packages/tinybird/pipes/v2_usage.pipe
index ffb5a23873..7c9917349e 100644
--- a/packages/tinybird/pipes/v2_usage.pipe
+++ b/packages/tinybird/pipes/v2_usage.pipe
@@ -2,6 +2,25 @@ DESCRIPTION >
Timeseries data
+NODE workspace_links
+SQL >
+
+ %
+ SELECT link_id
+ from dub_regular_links_metadata_latest FINAL
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'ws_clrei1gld0002vs9mzn93p8ik',
+ description="The ID of the workspace",
+ required=True,
+ )
+ }}
+
+
+
NODE day_intervals
SQL >
@@ -28,25 +47,6 @@ SQL >
-NODE workspace_links
-SQL >
-
- %
- SELECT link_id
- from dub_links_metadata_latest FINAL
- WHERE
- workspace_id
- = {{
- String(
- workspaceId,
- 'ws_clrei1gld0002vs9mzn93p8ik',
- description="The ID of the workspace",
- required=True,
- )
- }}
-
-
-
NODE usage_clicks_data
SQL >
diff --git a/packages/tinybird/pipes/v2_utms.pipe b/packages/tinybird/pipes/v2_utms.pipe
index 58907e28c4..0740099a2d 100644
--- a/packages/tinybird/pipes/v2_utms.pipe
+++ b/packages/tinybird/pipes/v2_utms.pipe
@@ -228,17 +228,115 @@ SQL >
{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY utm
HAVING utm != ''
- ORDER BY sales DESC
+ ORDER BY saleAmount DESC
+
+
+
+NODE utms_sales_with_type
+SQL >
+
+ %
+ WITH
+ sales AS (
+ SELECT
+ decodeURLFormComponent(
+ extractURLParameter(
+ url,
+ {{
+ String(
+ groupByUtmTag,
+ 'utm_source',
+ description="The UTM tag to retrieve data for",
+ required=True,
+ )
+ }}
+ )
+ ) as utm,
+ customer_id, link_id, timestamp, 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
+ 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 %}
+ {% 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 decodeURLFormComponent(extractURLParameter(url, 'utm_source'))
+ = {{ String(utm_source) }}
+ {% end %}
+ {% if defined(utm_medium) %}
+ AND decodeURLFormComponent(extractURLParameter(url, 'utm_medium'))
+ = {{ String(utm_medium) }}
+ {% end %}
+ {% if defined(utm_campaign) %}
+ AND decodeURLFormComponent(extractURLParameter(url, 'utm_campaign'))
+ = {{ String(utm_campaign) }}
+ {% end %}
+ {% if defined(utm_term) %}
+ AND decodeURLFormComponent(extractURLParameter(url, 'utm_term')) = {{ String(utm_term) }}
+ {% end %}
+ {% if defined(utm_content) %}
+ AND decodeURLFormComponent(extractURLParameter(url, '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 %}
+ ),
+ 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 utm, count(*) as sales, sum(amount) as saleAmount
+ FROM sales
+ INNER JOIN min_timestamps USING (customer_id, link_id)
+ WHERE
+ 1 = 1
+ {% if defined(saleType) %}
+ {% if saleType == 'new' %} AND timestamp <= first_sale_ts
+ {% elif saleType == 'recurring' %} AND timestamp > first_sale_ts
+ {% end %}
+ {% end %}
+ GROUP BY utm
+ HAVING utm != ''
+ ORDER BY sales DESC
NODE utms_composite
SQL >
+ %
SELECT dce.utm AS utm, dce.clicks as clicks, leads, sales, saleAmount
FROM (SELECT utm, clicks FROM utms_clicks) AS dce
- LEFT JOIN (SELECT * FROM utms_leads) AS dle ON dce.utm = dle.utm
- LEFT JOIN (SELECT * FROM utms_sales) AS dse ON dce.utm = dse.utm
+ LEFT JOIN (SELECT * FROM utms_leads) AS dle ON dce.utm = dle.utm
+ LEFT JOIN
+ (
+ SELECT *
+ FROM {% if defined(saleType) %} utms_sales_with_type {% else %} utms_sales {% end %}
+ ) AS dse
+ ON dce.utm = dse.utm
ORDER BY clicks DESC
@@ -251,7 +349,8 @@ SQL >
FROM
{% if eventType == 'clicks' %} utms_clicks
{% elif eventType == 'leads' %} utms_leads
- {% elif eventType == 'sales' %} utms_sales
+ {% elif eventType == 'sales' %}
+ {% if defined(saleType) %} utms_sales_with_type {% else %} utms_sales {% end %}
{% else %} utms_composite
{% end %}