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.

fix saleType="new" bug

Steven Tey5bcf02a809cd
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 bfb7940abf..bb96479e3b 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,6 +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 { 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";
@@ -28,7 +29,7 @@ const BLOCKS = [
ConversionBlock,
CountriesBlock,
LinksBlock,
- // SaleTypeBlock, // TODO: Add this back once we fix sales by type
+ SaleTypeBlock,
];
export default function ProgramOverviewPageClient() {
@@ -58,7 +59,7 @@ export default function ProgramOverviewPageClient() {
!exceededEvents &&
`/api/analytics?${editQueryString(queryString, {
event: "composite",
- // saleType: "new", // TODO: Add this back once we fix sales by type
+ saleType: "new",
})}`,
fetcher,
{
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx
index 63b75bd7c3..5e1ce3945f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx
@@ -475,11 +475,7 @@ export function ProgramPartnersApplicationsPageClient() {
) : (
<AnimatedEmptyState
title="No applications found"
- description={
- isFiltered || search
- ? "No applications found for the selected filters."
- : "No applications have been submitted for this program."
- }
+ description={`No applications found${isFiltered || search ? " for the selected filters" : " for this program"}.`}
cardContent={() => (
<>
<Users className="size-4 text-neutral-700" />
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx
index 7a01506ac4..f54e25084c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx
@@ -417,12 +417,8 @@ export function ProgramPartnersRejectedApplicationsPageClient() {
<Table {...tableProps} table={table} />
) : (
<AnimatedEmptyState
- title="No applications found"
- description={
- isFiltered || search
- ? "No applications found for the selected filters."
- : "No applications have been submitted for this program."
- }
+ title="No rejected applications found"
+ description={`No rejected applications found${isFiltered || search ? " for the selected filters" : " for this program"}.`}
cardContent={() => (
<>
<Users className="size-4 text-neutral-700" />
diff --git a/apps/web/ui/partners/overview/blocks/conversion-block.tsx b/apps/web/ui/partners/overview/blocks/conversion-block.tsx
index ffc4d00945..40e26aa338 100644
--- a/apps/web/ui/partners/overview/blocks/conversion-block.tsx
+++ b/apps/web/ui/partners/overview/blocks/conversion-block.tsx
@@ -51,7 +51,7 @@ export function ConversionBlock() {
title="Conversion rate"
viewAllHref={`/${workspaceSlug}/program/analytics${getQueryString(
{
- // saleType: "new", // TODO: Add this back once we fix sales by type
+ saleType: "new",
view: "funnel",
},
{
diff --git a/packages/tinybird/datasources/dub_first_sale_mv.datasource b/packages/tinybird/datasources/dub_first_sale_mv.datasource
index 05b91b11df..3b64416089 100644
--- a/packages/tinybird/datasources/dub_first_sale_mv.datasource
+++ b/packages/tinybird/datasources/dub_first_sale_mv.datasource
@@ -1,9 +1,10 @@
# Data Source created from Pipe 'dub_first_sale_pipe'
SCHEMA >
- `customer_id` String,
+ `workspace_id` LowCardinality(String),
`link_id` String,
+ `customer_id` String,
`first_sale_state` AggregateFunction(min, DateTime64(3))
ENGINE "AggregatingMergeTree"
-ENGINE_SORTING_KEY "customer_id, link_id"
+ENGINE_SORTING_KEY "workspace_id, link_id, customer_id"
diff --git a/packages/tinybird/datasources/dub_sale_events_mv.datasource b/packages/tinybird/datasources/dub_sale_events_mv.datasource
index cbb7bf2a46..6628e09a71 100644
--- a/packages/tinybird/datasources/dub_sale_events_mv.datasource
+++ b/packages/tinybird/datasources/dub_sale_events_mv.datasource
@@ -14,7 +14,6 @@ SCHEMA >
`payment_processor` LowCardinality(String),
`invoice_id` String,
`amount` UInt32,
- `sale_type` String,
`metadata` String,
`continent` LowCardinality(String),
`country` LowCardinality(String),
diff --git a/packages/tinybird/pipes/dub_first_sale_pipe.pipe b/packages/tinybird/pipes/dub_first_sale_pipe.pipe
index 7d783e98c6..ec3ac2e640 100644
--- a/packages/tinybird/pipes/dub_first_sale_pipe.pipe
+++ b/packages/tinybird/pipes/dub_first_sale_pipe.pipe
@@ -2,13 +2,15 @@ NODE mv
SQL >
SELECT
- customer_id,
+ workspace_id,
link_id,
+ customer_id,
minState(timestamp) AS first_sale_state
- FROM dub_sale_events
+ FROM dub_sale_events_mv
GROUP BY
- customer_id,
- link_id
+ workspace_id,
+ link_id,
+ customer_id
TYPE materialized
DATASOURCE dub_first_sale_mv
diff --git a/packages/tinybird/pipes/dub_sale_events_pipe.pipe b/packages/tinybird/pipes/dub_sale_events_pipe.pipe
index 441ecca0be..acca4df6df 100644
--- a/packages/tinybird/pipes/dub_sale_events_pipe.pipe
+++ b/packages/tinybird/pipes/dub_sale_events_pipe.pipe
@@ -18,10 +18,6 @@ SQL >
se.payment_processor,
se.invoice_id,
se.amount,
- -- correct sale_type classification using summary table
- if(
- dateDiff('millisecond', fs.first_sale_ts, se.timestamp) < 1, 'new', 'recurring'
- ) AS sale_type,
se.metadata,
-- geolocation fields
toLowCardinality(se.continent) AS continent,
@@ -52,13 +48,6 @@ SQL >
se.qr,
se.bot
FROM dub_sale_events AS se
- -- join pre-aggregated first-sale timestamps
- LEFT JOIN
- (
- SELECT customer_id, link_id, minMerge(first_sale_state) AS first_sale_ts
- FROM dub_first_sale_mv
- GROUP BY customer_id, link_id
- ) AS fs USING (customer_id, link_id)
-- join link metadata for workspace/domain/key enrichment
LEFT JOIN
(
diff --git a/packages/tinybird/pipes/v3_count.pipe b/packages/tinybird/pipes/v3_count.pipe
index bb4ccf01ce..442ba99d72 100644
--- a/packages/tinybird/pipes/v3_count.pipe
+++ b/packages/tinybird/pipes/v3_count.pipe
@@ -172,65 +172,101 @@ SQL >
+NODE first_sale_by_link_customer
+SQL >
+
+ %
+ SELECT workspace_id, link_id, customer_id, minMerge(first_sale_state) AS first_sale_ts
+ FROM dub_first_sale_mv
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7pj5kq4006835rbjlt2ofka',
+ description="The unique ID for the workspace",
+ required=True,
+ )
+ }}
+ GROUP BY workspace_id, link_id, customer_id
+
+
+
NODE count_sales
SQL >
%
SELECT 'count' as groupByField, sales, amount, amount AS saleAmount
FROM
+ (
+ SELECT
+ count() AS sales,
+ sum(amount) AS amount
+ FROM
(
- SELECT COUNT(*) as sales, sum(amount) as amount
- FROM dub_sale_events_mv
+ SELECT
+ se.amount,
+ if(
+ isNull(fs.first_sale_ts),
+ 'new',
+ if(
+ toUnixTimestamp64Milli(se.timestamp) = toUnixTimestamp64Milli(fs.first_sale_ts),
+ 'new',
+ 'recurring'
+ )
+ ) AS sale_type
+ FROM dub_sale_events_mv AS se
+ LEFT JOIN first_sale_by_link_customer AS fs
+ USING (workspace_id, link_id, customer_id)
WHERE
true
- {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}
- {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}
- {% elif defined(linkId) %} AND link_id = {{ linkId }}
+ {% if defined(workspaceId) %} AND se.workspace_id = {{ workspaceId }} {% end %}
+ {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
+ {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
{% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
tenantId
) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
tagIds
) or defined(
root
- ) %} AND link_id in (SELECT link_id from workspace_links)
+ ) %} AND se.link_id in (SELECT link_id from workspace_links)
{% end %}
- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}
- {% if defined(saleType) %} AND sale_type = {{ String(saleType) }} {% 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(customerId) %} AND se.customer_id = {{ String(customerId) }} {% end %}
+ {% 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('?', referer_url)[1] = {{ refererUrl }}
+ AND splitByString('?', se.referer_url)[1] = {{ refererUrl }}
{% end %}
{% if defined(utm_source) %}
- AND url
+ AND se.url
LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
{% end %}
{% if defined(utm_medium) %}
- AND url
+ AND se.url
LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
{% end %}
{% if defined(utm_campaign) %}
- AND url LIKE concat(
+ AND se.url LIKE concat(
'%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%'
)
{% end %}
{% if defined(utm_term) %}
- AND url
+ AND se.url
LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
{% end %}
{% if defined(utm_content) %}
- AND url
+ AND se.url
LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
{% end %}
- {% if defined(url) %} AND splitByString('?', url)[1] = {{ url }} {% end %}
- {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
- {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
+ {% if defined(url) %} AND splitByString('?', se.url)[1] = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime(end) }} {% end %}
{% if defined(filters) %}
{% for item in JSON(filters, '[]') %}
{% if item.get('operand', '').startswith('metadata.') %}
@@ -238,22 +274,26 @@ SQL >
{% set operator = item.get('operator', 'equals') %}
{% set value = item.get('value', '') %}
{% if operator == 'equals' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) = {{ value }}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) = {{ value }}
{% elif operator == 'notEquals' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) != {{ value }}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) != {{ value }}
{% elif operator == 'greaterThan' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) > {{ value }}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) > {{ value }}
{% elif operator == 'lessThan' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) < {{ value }}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) < {{ value }}
{% elif operator == 'greaterThanOrEqual' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) >= {{ value }}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) >= {{ value }}
{% elif operator == 'lessThanOrEqual' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) <= {{ value }}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) <= {{ value }}
{% end %}
{% end %}
{% end %}
{% end %}
- ) AS subquery
+ ) AS typed
+ WHERE
+ true
+ {% if defined(saleType) %} AND typed.sale_type = {{ String(saleType) }} {% end %}
+ ) AS subquery
diff --git a/packages/tinybird/pipes/v3_events.pipe b/packages/tinybird/pipes/v3_events.pipe
index 00c2f0956c..caf12c54e1 100644
--- a/packages/tinybird/pipes/v3_events.pipe
+++ b/packages/tinybird/pipes/v3_events.pipe
@@ -186,83 +186,127 @@ SQL >
+NODE first_sale_by_link_customer
+SQL >
+
+ %
+ SELECT workspace_id, link_id, customer_id, minMerge(first_sale_state) AS first_sale_ts
+ FROM dub_first_sale_mv
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7pj5kq4006835rbjlt2ofka',
+ description="The unique ID for the workspace",
+ required=True,
+ )
+ }}
+ GROUP BY workspace_id, link_id, customer_id
+
+
+
NODE sale_events
SQL >
%
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
- true
- {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}
- {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}
- {% elif defined(linkId) %} AND link_id = {{ linkId }}
- {% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
- tenantId
- ) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
- tagIds
- ) or defined(
- root
- ) %} AND link_id in (SELECT link_id from workspace_links)
- {% end %}
- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}
- {% if defined(saleType) %} AND sale_type = {{ String(saleType) }} {% 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 splitByString('?', url)[1] = {{ url }} {% end %}
- {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
- {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
- {% if defined(filters) %}
- {% for item in JSON(filters, '[]') %}
- {% if item.get('operand', '').startswith('metadata.') %}
- {% set metadataKey = item.get('operand', '').split('.')[1] %}
- {% set operator = item.get('operator', 'equals') %}
- {% set value = item.get('value', '') %}
- {% if operator == 'equals' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) = {{ value }}
- {% elif operator == 'notEquals' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) != {{ value }}
- {% elif operator == 'greaterThan' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) > {{ value }}
- {% elif operator == 'lessThan' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) < {{ value }}
- {% elif operator == 'greaterThanOrEqual' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) >= {{ value }}
- {% elif operator == 'lessThanOrEqual' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) <= {{ value }}
+ t.*,
+ t.amount AS saleAmount,
+ CONCAT(t.country, '-', t.region) AS region_processed,
+ splitByString('?', t.referer_url)[1] AS referer_url_processed,
+ 'sale' AS event
+ FROM
+ (
+ SELECT
+ se.*,
+ if(
+ isNull (fs.first_sale_ts),
+ 'new',
+ if(
+ toUnixTimestamp64Milli(se.timestamp) = toUnixTimestamp64Milli(fs.first_sale_ts),
+ 'new',
+ 'recurring'
+ )
+ ) AS sale_type
+ FROM dub_sale_events_mv AS se
+ LEFT JOIN first_sale_by_link_customer AS fs USING (workspace_id, link_id, customer_id)
+ WHERE
+ true
+ {% if defined(workspaceId) %} AND se.workspace_id = {{ workspaceId }} {% end %}
+ {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
+ {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
+ {% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
+ tenantId
+ ) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
+ tagIds
+ ) or defined(
+ root
+ ) %} AND se.link_id in (SELECT link_id from workspace_links)
+ {% end %}
+ {% if defined(customerId) %} AND se.customer_id = {{ String(customerId) }} {% end %}
+ {% 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 splitByString('?', se.url)[1] = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime(end) }} {% end %}
+
+ {% if defined(filters) %}
+ {% for item in JSON(filters, '[]') %}
+ {% if item.get('operand', '').startswith('metadata.') %}
+ {% set metadataKey = item.get('operand', '').split('.')[1] %}
+ {% set operator = item.get('operator', 'equals') %}
+ {% set value = item.get('value', '') %}
+ {% if operator == 'equals' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) = {{ value }}
+ {% elif operator == 'notEquals' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) != {{ value }}
+ {% elif operator == 'greaterThan' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) > {{ value }}
+ {% elif operator == 'lessThan' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) < {{ value }}
+ {% elif operator == 'greaterThanOrEqual' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) >= {{ value }}
+ {% elif operator == 'lessThanOrEqual' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) <= {{ value }}
+ {% end %}
+ {% end %}
{% end %}
{% end %}
- {% end %}
- {% end %}
- ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
+
+ ) AS t
+ WHERE true {% if defined(saleType) %} AND t.sale_type = {{ String(saleType) }} {% end %}
+ ORDER BY t.timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
LIMIT {{ Int32(limit, 100) }}
{% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}
diff --git a/packages/tinybird/pipes/v3_group_by.pipe b/packages/tinybird/pipes/v3_group_by.pipe
index 3cf9e755d9..3e8926441f 100644
--- a/packages/tinybird/pipes/v3_group_by.pipe
+++ b/packages/tinybird/pipes/v3_group_by.pipe
@@ -276,6 +276,26 @@ SQL >
+NODE first_sale_by_link_customer
+SQL >
+
+ %
+ SELECT workspace_id, link_id, customer_id, minMerge(first_sale_state) AS first_sale_ts
+ FROM dub_first_sale_mv
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7pj5kq4006835rbjlt2ofka',
+ description="The unique ID for the workspace",
+ required=True,
+ )
+ }}
+ GROUP BY workspace_id, link_id, customer_id
+
+
+
NODE group_by_sales
SQL >
@@ -327,72 +347,90 @@ SQL >
{% end %}
COUNT(*) AS sales,
SUM(amount) AS saleAmount
- FROM dub_sale_events_mv
- WHERE
- groupByField != '' AND groupByField != 'Unknown'
- {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}
- {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}
- {% elif defined(linkId) %} AND link_id = {{ linkId }}
- {% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
- tenantId
- ) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
- tagIds
- ) or defined(
- root
- ) %} AND link_id in (SELECT link_id from workspace_links)
- {% end %}
- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}
- {% if defined(saleType) %} AND sale_type = {{ String(saleType) }} {% 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 splitByString('?', url)[1] = {{ url }} {% end %}
- {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}
- {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}
- {% if defined(filters) %}
- {% for item in JSON(filters, '[]') %}
- {% if item.get('operand', '').startswith('metadata.') %}
- {% set metadataKey = item.get('operand', '').split('.')[1] %}
- {% set operator = item.get('operator', 'equals') %}
- {% set value = item.get('value', '') %}
- {% if operator == 'equals' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) = {{ value }}
- {% elif operator == 'notEquals' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) != {{ value }}
- {% elif operator == 'greaterThan' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) > {{ value }}
- {% elif operator == 'lessThan' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) < {{ value }}
- {% elif operator == 'greaterThanOrEqual' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) >= {{ value }}
- {% elif operator == 'lessThanOrEqual' %}
- AND JSONExtractString(metadata, {{ metadataKey }}) <= {{ value }}
+ FROM
+ (
+ SELECT
+ se.*,
+ if(
+ isNull(fs.first_sale_ts),
+ 'new',
+ if(
+ toUnixTimestamp64Milli(se.timestamp) = toUnixTimestamp64Milli(fs.first_sale_ts),
+ 'new',
+ 'recurring'
+ )
+ ) AS sale_type
+ FROM dub_sale_events_mv AS se
+ LEFT JOIN first_sale_by_link_customer AS fs
+ USING (workspace_id, link_id, customer_id)
+ WHERE
+ true
+ {% if defined(workspaceId) %} AND se.workspace_id = {{ workspaceId }} {% end %}
+ {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
+ {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
+ {% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
+ tenantId
+ ) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
+ tagIds
+ ) or defined(
+ root
+ ) %} AND se.link_id in (SELECT link_id from workspace_links)
+ {% end %}
+ {% if defined(customerId) %} AND se.customer_id = {{ String(customerId) }} {% end %}
+ {% 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 splitByString('?', se.url)[1] = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime(end) }} {% end %}
+ {% if defined(filters) %}
+ {% for item in JSON(filters, '[]') %}
+ {% if item.get('operand', '').startswith('metadata.') %}
+ {% set metadataKey = item.get('operand', '').split('.')[1] %}
+ {% set operator = item.get('operator', 'equals') %}
+ {% set value = item.get('value', '') %}
+ {% if operator == 'equals' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) = {{ value }}
+ {% elif operator == 'notEquals' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) != {{ value }}
+ {% elif operator == 'greaterThan' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) > {{ value }}
+ {% elif operator == 'lessThan' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) < {{ value }}
+ {% elif operator == 'greaterThanOrEqual' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) >= {{ value }}
+ {% elif operator == 'lessThanOrEqual' %}
+ AND JSONExtractString(se.metadata, {{ metadataKey }}) <= {{ value }}
+ {% end %}
{% end %}
{% end %}
{% end %}
- {% end %}
+ ) AS typed
+ WHERE
+ groupByField != '' AND groupByField != 'Unknown'
+ {% if defined(saleType) %} AND typed.sale_type = {{ String(saleType) }} {% end %}
GROUP BY
groupByField
{% if String(groupBy) == 'cities' %}, country, region
diff --git a/packages/tinybird/pipes/v3_group_by_link_country.pipe b/packages/tinybird/pipes/v3_group_by_link_country.pipe
index 90024965fa..77db5a9549 100644
--- a/packages/tinybird/pipes/v3_group_by_link_country.pipe
+++ b/packages/tinybird/pipes/v3_group_by_link_country.pipe
@@ -1,3 +1,7 @@
+DESCRIPTION >
+ Only used for `/cron/aggregate-clicks` for now
+
+
NODE endpoint
SQL >
diff --git a/packages/tinybird/pipes/v3_group_by_link_metadata.pipe b/packages/tinybird/pipes/v3_group_by_link_metadata.pipe
index 292e07fb72..b0f277b3d1 100644
--- a/packages/tinybird/pipes/v3_group_by_link_metadata.pipe
+++ b/packages/tinybird/pipes/v3_group_by_link_metadata.pipe
@@ -173,13 +173,12 @@ SQL >
-NODE group_by_link_metadata_sales
+NODE first_sale_by_link_customer
SQL >
%
- SELECT wl.groupByField as groupByField, COUNT(*) AS sales, SUM(amount) as saleAmount
- FROM dub_sale_events_mv AS se
- JOIN workspace_links AS wl ON se.link_id = wl.link_id
+ SELECT workspace_id, link_id, customer_id, minMerge(first_sale_state) AS first_sale_ts
+ FROM dub_first_sale_mv
WHERE
workspace_id
= {{
@@ -190,40 +189,83 @@ SQL >
required=True,
)
}}
- {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
- {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
- {% end %}
- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}
- {% if defined(saleType) %} AND sale_type = {{ String(saleType) }} {% 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(trigger) %} AND trigger = {{ trigger }} {% 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 splitByString('?', url)[1] = {{ url }} {% end %}
- {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}
- {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
+ GROUP BY workspace_id, link_id, customer_id
+
+
+
+NODE group_by_link_metadata_sales
+SQL >
+
+ %
+ SELECT
+ wl.groupByField AS groupByField,
+ COUNT(*) AS sales,
+ SUM(t.amount) AS saleAmount
+ FROM
+ (
+ SELECT
+ se.*,
+ if(
+ isNull(fs.first_sale_ts),
+ 'new',
+ if(
+ toUnixTimestamp64Milli(se.timestamp) = toUnixTimestamp64Milli(fs.first_sale_ts),
+ 'new',
+ 'recurring'
+ )
+ ) AS sale_type
+ FROM dub_sale_events_mv AS se
+ LEFT JOIN first_sale_by_link_customer AS fs
+ USING (workspace_id, link_id, customer_id)
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7pj5kq4006835rbjlt2ofka',
+ description="The unique ID for the workspace",
+ required=True,
+ )
+ }}
+ {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
+ {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
+ {% end %}
+ {% if defined(customerId) %} AND se.customer_id = {{ String(customerId) }} {% end %}
+ {% 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(trigger) %} AND se.trigger = {{ trigger }} {% 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 splitByString('?', se.url)[1] = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime64(end) }} {% end %}
+ ) AS t
+ JOIN workspace_links AS wl
+ ON t.link_id = wl.link_id
+ WHERE
+ true
+ {% if defined(saleType) %} AND t.sale_type = {{ String(saleType) }} {% end %}
GROUP BY wl.groupByField
ORDER BY saleAmount DESC
LIMIT 5000
diff --git a/packages/tinybird/pipes/v3_timeseries.pipe b/packages/tinybird/pipes/v3_timeseries.pipe
index 511a3eb0f4..fb290381d1 100644
--- a/packages/tinybird/pipes/v3_timeseries.pipe
+++ b/packages/tinybird/pipes/v3_timeseries.pipe
@@ -277,69 +277,121 @@ SQL >
-NODE timeseries_sales_data
-DESCRIPTION >
- undefined
+NODE first_sale_by_link_customer
+SQL >
+
+ %
+ SELECT workspace_id, link_id, customer_id, minMerge(first_sale_state) AS first_sale_ts
+ FROM dub_first_sale_mv
+ WHERE
+ workspace_id
+ = {{
+ String(
+ workspaceId,
+ 'cl7pj5kq4006835rbjlt2ofka',
+ description="The unique ID for the workspace",
+ required=True,
+ )
+ }}
+ GROUP BY workspace_id, link_id, customer_id
+
+
+NODE timeseries_sales_data
SQL >
%
SELECT
- {% if granularity == "hour" %} toStartOfHour(timestamp, {{ String(timezone, 'UTC') }})
+ {% 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)
+ {% else %}
+ toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3)
{% end %} AS interval,
- uniq(*) as sales,
- sum(amount) as amount
- FROM dub_sale_events_mv
+ uniq(*) AS sales,
+ sum(amount) AS amount
+ FROM
+ (
+ SELECT
+ se.timestamp,
+ se.amount,
+ se.workspace_id,
+ se.link_id,
+ se.customer_id,
+ se.continent,
+ se.country,
+ se.region,
+ se.city,
+ se.device,
+ se.browser,
+ se.os,
+ se.referer,
+ se.referer_url,
+ se.url,
+ se.metadata,
+ if(
+ isNull(fs.first_sale_ts),
+ 'new',
+ if(
+ toUnixTimestamp64Milli(se.timestamp) = toUnixTimestamp64Milli(fs.first_sale_ts),
+ 'new',
+ 'recurring'
+ )
+ ) AS sale_type
+ FROM dub_sale_events_mv AS se
+ LEFT JOIN first_sale_by_link_customer AS fs
+ USING (workspace_id, link_id, customer_id)
+ WHERE
+ true
+ {% if defined(workspaceId) %} AND se.workspace_id = {{ workspaceId }} {% end %}
+ {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
+ {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
+ {% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
+ tenantId
+ ) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
+ tagIds
+ ) or defined(
+ root
+ ) %} AND se.link_id in (SELECT link_id from workspace_links)
+ {% end %}
+ {% if defined(customerId) %} AND se.customer_id = {{ String(customerId) }} {% end %}
+ {% 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 splitByString('?', se.url)[1] = {{ url }} {% end %}
+ {% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
+ {% if defined(end) %} AND se.timestamp <= {{ DateTime64(end) }} {% end %}
+ ) AS typed
WHERE
true
- {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}
- {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}
- {% elif defined(linkId) %} AND link_id = {{ linkId }}
- {% elif defined(programId) or defined(partnerId) or defined(groupId) or defined(
- tenantId
- ) or defined(folderIds) or defined(folderId) or defined(domain) or defined(
- tagIds
- ) or defined(
- root
- ) %} AND link_id in (SELECT link_id from workspace_links)
- {% end %}
- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}
- {% if defined(saleType) %} AND sale_type = {{ String(saleType) }} {% 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 splitByString('?', url)[1] = {{ url }} {% end %}
- {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}
- {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
+ {% if defined(saleType) %} AND typed.sale_type = {{ String(saleType) }} {% end %}
GROUP BY interval
ORDER BY interval