The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Tinybird migration
Steven Tey0844d2b520dd
diff --git a/apps/web/app/(ee)/api/cron/framer/backfill-leads-batch/route.ts b/apps/web/app/(ee)/api/cron/framer/backfill-leads-batch/route.tsindex 7725806233..d009317fd8 100644--- a/apps/web/app/(ee)/api/cron/framer/backfill-leads-batch/route.ts+++ b/apps/web/app/(ee)/api/cron/framer/backfill-leads-batch/route.ts@@ -300,6 +300,18 @@ export const POST = withWorkspace(async ({ req, workspace }) => {body: dataArray.map((d) => JSON.stringify(d.clickData)).join("\n"),},),+ // TODO: Remove after Tinybird migration+ fetch(+ `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_click_events&wait=true`,+ {+ method: "POST",+ headers: {+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY_NEW}`,+ "Content-Type": "application/x-ndjson",+ },+ body: dataArray.map((d) => JSON.stringify(d.clickData)).join("\n"),+ },+ ),// Record leadsrecordLeadWithTimestamp(dataArray.map((d) => d.leadEventData)),diff --git a/apps/web/lib/api/audit-logs/record-audit-log.ts b/apps/web/lib/api/audit-logs/record-audit-log.tsindex 9844f1d2d9..f4fe79a3d7 100644--- a/apps/web/lib/api/audit-logs/record-audit-log.ts+++ b/apps/web/lib/api/audit-logs/record-audit-log.ts@@ -1,7 +1,7 @@import { getIP } from "@/lib/api/utils/get-ip";-import { tb } from "@/lib/tinybird";+import { tb, tbNew } from "@/lib/tinybird";import { log } from "@dub/utils";-import { ipAddress as getIPAddress } from "@vercel/functions";+import { ipAddress as getIPAddress, waitUntil } from "@vercel/functions";import { headers } from "next/headers";import { z } from "zod";import { createId } from "../create-id";@@ -56,6 +56,7 @@ export const recordAuditLog = async (data: AuditLogInput | AuditLogInput[]) => {: [transformAuditLogTB(data, { headersList, ipAddress })];try {+ waitUntil(recordAuditLogTBNew(auditLogs));await recordAuditLogTB(auditLogs);} catch (error) {console.error(@@ -77,3 +78,10 @@ const recordAuditLogTB = tb.buildIngestEndpoint({event: auditLogSchemaTB,wait: true,});++// TODO: Remove after Tinybird migration+const recordAuditLogTBNew = tbNew.buildIngestEndpoint({+ datasource: "dub_audit_logs",+ event: auditLogSchemaTB,+ wait: true,+});diff --git a/apps/web/lib/api/conversions/track-lead.ts b/apps/web/lib/api/conversions/track-lead.tsindex 942f81a163..bb069ee755 100644--- a/apps/web/lib/api/conversions/track-lead.ts+++ b/apps/web/lib/api/conversions/track-lead.ts@@ -4,7 +4,7 @@ import { includeTags } from "@/lib/api/links/include-tags";import { generateRandomName } from "@/lib/names";import { createPartnerCommission } from "@/lib/partners/create-partner-commission";import { isStored, storage } from "@/lib/storage";-import { getClickEvent, recordLead, recordLeadSync } from "@/lib/tinybird";+import { getClickEvent, recordLead } from "@/lib/tinybird";import { logConversionEvent } from "@/lib/tinybird/log-conversion-events";import { ClickEventTB, WebhookPartner, WorkspaceProps } from "@/lib/types";import { redis } from "@/lib/upstash";@@ -214,9 +214,6 @@ export const trackLead = async ({: leadEventPayload;await Promise.all([- // Use recordLeadSync which waits for the operation to complete- recordLeadSync(leadEventPayload),-// Cache the latest lead event for 5 minutes because the ingested event is not available immediately on Tinybird// we're setting two keys because we want to support the use case where the customer has multiple lead eventsredis.set(`leadCache:${customer.id}`, cacheLeadEventPayload, {@@ -235,9 +232,8 @@ export const trackLead = async ({waitUntil((async () => {- // for async mode, record the lead event in the background- // for deferred mode, defer the lead event creation to a subsequent request- if (mode === "async") {+ // for deferred mode, we defer the lead event creation to a subsequent request+ if (mode !== "deferred") {await recordLead(createLeadEventPayload(customer.id));}diff --git a/apps/web/lib/tinybird/client.ts b/apps/web/lib/tinybird/client.tsindex 17d00be96c..f4cb9eda12 100644--- a/apps/web/lib/tinybird/client.ts+++ b/apps/web/lib/tinybird/client.ts@@ -4,3 +4,9 @@ export const tb = new Tinybird({token: process.env.TINYBIRD_API_KEY as string,baseUrl: process.env.TINYBIRD_API_URL as string,});++// TODO: Remove after Tinybird migration+export const tbNew = new Tinybird({+ token: process.env.TINYBIRD_API_KEY_NEW as string,+ baseUrl: process.env.TINYBIRD_API_URL as string,+});diff --git a/apps/web/lib/tinybird/log-conversion-events.ts b/apps/web/lib/tinybird/log-conversion-events.tsindex 2ec8638006..e0292062e8 100644--- a/apps/web/lib/tinybird/log-conversion-events.ts+++ b/apps/web/lib/tinybird/log-conversion-events.ts@@ -1,5 +1,6 @@+import { waitUntil } from "@vercel/functions";import { z } from "zod";-import { tb } from "./client";+import { tb, tbNew } from "./client";const schema = z.object({workspace_id: z.string(),@@ -10,7 +11,18 @@ const schema = z.object({});// Log the conversion events for debugging purposes-export const logConversionEvent = tb.buildIngestEndpoint({+export const logConversionEventTB = tb.buildIngestEndpoint({datasource: "dub_conversion_events_log",event: schema,});++// TODO: Remove after Tinybird migration+export const logConversionEventTBNew = tbNew.buildIngestEndpoint({+ datasource: "dub_conversion_events_log",+ event: schema,+});++export const logConversionEvent = async (payload: any) => {+ waitUntil(logConversionEventTBNew(payload));+ return await logConversionEventTB(payload);+};diff --git a/apps/web/lib/tinybird/log-import-error.ts b/apps/web/lib/tinybird/log-import-error.tsindex 210b1f1ed8..59756fb564 100644--- a/apps/web/lib/tinybird/log-import-error.ts+++ b/apps/web/lib/tinybird/log-import-error.ts@@ -1,7 +1,19 @@+import { waitUntil } from "@vercel/functions";import { importErrorLogSchema } from "../zod/schemas/import-error-log";-import { tb } from "./client";+import { tb, tbNew } from "./client";-export const logImportError = tb.buildIngestEndpoint({+export const logImportErrorTB = tb.buildIngestEndpoint({datasource: "dub_import_error_logs",event: importErrorLogSchema,});++// TODO: Remove after Tinybird migration+export const logImportErrorTBNew = tbNew.buildIngestEndpoint({+ datasource: "dub_import_error_logs",+ event: importErrorLogSchema,+});++export const logImportError = async (payload: any) => {+ waitUntil(logImportErrorTBNew(payload));+ return await logImportErrorTB(payload);+};diff --git a/apps/web/lib/tinybird/record-click-zod.ts b/apps/web/lib/tinybird/record-click-zod.tsindex c1ce7f1f7d..84293cf741 100644--- a/apps/web/lib/tinybird/record-click-zod.ts+++ b/apps/web/lib/tinybird/record-click-zod.ts@@ -1,5 +1,6 @@+import { waitUntil } from "@vercel/functions";import { z } from "zod";-import { tb } from "./client";+import { tb, tbNew } from "./client";export const recordClickZodSchema = z.object({timestamp: z.string().default(""),@@ -33,8 +34,20 @@ export const recordClickZodSchema = z.object({trigger: z.string().default("link"),});-export const recordClickZod = tb.buildIngestEndpoint({+export const recordClickZodTB = tb.buildIngestEndpoint({datasource: "dub_click_events",event: recordClickZodSchema,wait: true,});++// TODO: Remove after Tinybird migration+export const recordClickZodTBNew = tbNew.buildIngestEndpoint({+ datasource: "dub_click_events",+ event: recordClickZodSchema,+ wait: true,+});++export const recordClickZod = async (payload: any) => {+ waitUntil(recordClickZodTBNew(payload));+ return await recordClickZodTB(payload);+};diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.tsindex db1c34c990..8e7d6d41ce 100644--- a/apps/web/lib/tinybird/record-click.ts+++ b/apps/web/lib/tinybird/record-click.ts@@ -177,6 +177,17 @@ export async function recordClick({body: JSON.stringify(clickData),},).then((res) => res.json()),+ // TODO: Remove after Tinybird migration+ fetchWithRetry(+ `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_click_events&wait=true`,+ {+ method: "POST",+ headers: {+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY_NEW}`,+ },+ body: JSON.stringify(clickData),+ },+ ).then((res) => res.json()),// cache the recorded click for the corresponding IP address in Redis for 1 hourrecordClickCache.set({ domain, key, ip, clickId }),diff --git a/apps/web/lib/tinybird/record-lead.ts b/apps/web/lib/tinybird/record-lead.tsindex 57f7da76cd..841bafe52c 100644--- a/apps/web/lib/tinybird/record-lead.ts+++ b/apps/web/lib/tinybird/record-lead.ts@@ -1,21 +1,39 @@+import { waitUntil } from "@vercel/functions";import { z } from "zod";import { leadEventSchemaTB } from "../zod/schemas/leads";-import { tb } from "./client";+import { tb, tbNew } from "./client";-export const recordLead = tb.buildIngestEndpoint({+export const recordLeadTB = tb.buildIngestEndpoint({datasource: "dub_lead_events",event: leadEventSchemaTB,});-export const recordLeadSync = tb.buildIngestEndpoint({+// TODO: Remove after Tinybird migration+export const recordLeadTBNew = tbNew.buildIngestEndpoint({datasource: "dub_lead_events",event: leadEventSchemaTB,- wait: true,});-export const recordLeadWithTimestamp = tb.buildIngestEndpoint({+export const recordLead = async (payload: any) => {+ waitUntil(recordLeadTBNew(payload));+ return await recordLeadTB(payload);+};++export const recordLeadWithTimestampTB = tb.buildIngestEndpoint({+ datasource: "dub_lead_events",+ event: leadEventSchemaTB.extend({+ timestamp: z.string(),+ }),+});++export const recordLeadWithTimestampTBNew = tbNew.buildIngestEndpoint({datasource: "dub_lead_events",event: leadEventSchemaTB.extend({timestamp: z.string(),}),});++export const recordLeadWithTimestamp = async (payload: any) => {+ waitUntil(recordLeadWithTimestampTBNew(payload));+ return await recordLeadWithTimestampTB(payload);+};diff --git a/apps/web/lib/tinybird/record-link.ts b/apps/web/lib/tinybird/record-link.tsindex ef5e01fb7e..b4433b1278 100644--- a/apps/web/lib/tinybird/record-link.ts+++ b/apps/web/lib/tinybird/record-link.ts@@ -1,8 +1,9 @@import z from "@/lib/zod";+import { waitUntil } from "@vercel/functions";import { ExpandedLink } from "../api/links";import { decodeKeyIfCaseSensitive } from "../api/links/case-sensitivity";import { prefixWorkspaceId } from "../api/workspaces/workspace-id";-import { tb } from "./client";+import { tb, tbNew } from "./client";export const dubLinksMetadataSchema = z.object({link_id: z.string(),@@ -49,6 +50,13 @@ export const recordLinkTB = tb.buildIngestEndpoint({wait: true,});+// TODO: Remove after Tinybird migration+export const recordLinkTBNew = tbNew.buildIngestEndpoint({+ datasource: "dub_links_metadata",+ event: dubLinksMetadataSchema,+ wait: true,+});+export const transformLinkTB = (link: ExpandedLink) => {const key = decodeKeyIfCaseSensitive({domain: link.domain,@@ -72,8 +80,10 @@ export const transformLinkTB = (link: ExpandedLink) => {export const recordLink = async (payload: ExpandedLink | ExpandedLink[]) => {if (Array.isArray(payload)) {+ waitUntil(recordLinkTBNew(payload.map(transformLinkTB)));return await recordLinkTB(payload.map(transformLinkTB));} else {+ waitUntil(recordLinkTBNew(transformLinkTB(payload)));return await recordLinkTB(transformLinkTB(payload));}};diff --git a/apps/web/lib/tinybird/record-sale.ts b/apps/web/lib/tinybird/record-sale.tsindex 382dda67f8..59d3a190b1 100644--- a/apps/web/lib/tinybird/record-sale.ts+++ b/apps/web/lib/tinybird/record-sale.ts@@ -1,15 +1,39 @@+import { waitUntil } from "@vercel/functions";import z from "../zod";import { saleEventSchemaTB } from "../zod/schemas/sales";-import { tb } from "./client";+import { tb, tbNew } from "./client";-export const recordSale = tb.buildIngestEndpoint({+export const recordSaleTB = tb.buildIngestEndpoint({datasource: "dub_sale_events",event: saleEventSchemaTB,});-export const recordSaleWithTimestamp = tb.buildIngestEndpoint({+// TODO: Remove after Tinybird migration+export const recordSaleNewTB = tbNew.buildIngestEndpoint({+ datasource: "dub_sale_events",+ event: saleEventSchemaTB,+});++export const recordSale = async (payload: any) => {+ waitUntil(recordSaleNewTB(payload));+ return await recordSaleTB(payload);+};++export const recordSaleWithTimestampTB = tb.buildIngestEndpoint({+ datasource: "dub_sale_events",+ event: saleEventSchemaTB.extend({+ timestamp: z.string(),+ }),+});++export const recordSaleWithTimestampNewTB = tbNew.buildIngestEndpoint({datasource: "dub_sale_events",event: saleEventSchemaTB.extend({timestamp: z.string(),}),});++export const recordSaleWithTimestamp = async (payload: any) => {+ waitUntil(recordSaleWithTimestampNewTB(payload));+ return await recordSaleWithTimestampTB(payload);+};diff --git a/apps/web/lib/tinybird/record-webhook-event.ts b/apps/web/lib/tinybird/record-webhook-event.tsindex 65a708610e..65cdc00d0f 100644--- a/apps/web/lib/tinybird/record-webhook-event.ts+++ b/apps/web/lib/tinybird/record-webhook-event.ts@@ -1,7 +1,19 @@+import { waitUntil } from "@vercel/functions";import { webhookEventSchemaTB } from "../zod/schemas/webhooks";-import { tb } from "./client";+import { tb, tbNew } from "./client";-export const recordWebhookEvent = tb.buildIngestEndpoint({+export const recordWebhookEventTB = tb.buildIngestEndpoint({datasource: "dub_webhook_events",event: webhookEventSchemaTB.omit({ timestamp: true }),});++// TODO: Remove after Tinybird migration+export const recordWebhookEventTBNew = tbNew.buildIngestEndpoint({+ datasource: "dub_webhook_events",+ event: webhookEventSchemaTB.omit({ timestamp: true }),+});++export const recordWebhookEvent = async (payload: any) => {+ waitUntil(recordWebhookEventTBNew(payload));+ return await recordWebhookEventTB(payload);+};diff --git a/packages/tinybird/datasources/dub_audit_logs.datasource b/packages/tinybird/datasources/dub_audit_logs.datasourceindex 7dff35a1d4..8d4e8135db 100644--- a/packages/tinybird/datasources/dub_audit_logs.datasource+++ b/packages/tinybird/datasources/dub_audit_logs.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`id` String `json:$.id`,diff --git a/packages/tinybird/datasources/dub_click_events.datasource b/packages/tinybird/datasources/dub_click_events.datasourceindex 822e22097f..766db57e82 100644--- a/packages/tinybird/datasources/dub_click_events.datasource+++ b/packages/tinybird/datasources/dub_click_events.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime64(3) `json:$.timestamp`,diff --git a/packages/tinybird/datasources/dub_click_events_id.datasource b/packages/tinybird/datasources/dub_click_events_id.datasourceindex 82839c38e0..00d2d9e67d 100644--- a/packages/tinybird/datasources/dub_click_events_id.datasource+++ b/packages/tinybird/datasources/dub_click_events_id.datasource@@ -1,4 +1,4 @@-# Data Source created from Pipe 'dub_click_events_id_pipe_new'+# Data Source created from Pipe 'dub_click_events_id_pipe'SCHEMA >`timestamp` DateTime64(3),diff --git a/packages/tinybird/datasources/dub_conversion_events_log.datasource b/packages/tinybird/datasources/dub_conversion_events_log.datasourceindex 06fcd9802f..c7137e111b 100644--- a/packages/tinybird/datasources/dub_conversion_events_log.datasource+++ b/packages/tinybird/datasources/dub_conversion_events_log.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime64(3) `json:$.timestamp` DEFAULT now(),diff --git a/packages/tinybird/datasources/dub_import_error_logs.datasource b/packages/tinybird/datasources/dub_import_error_logs.datasourceindex 3a43bad1ae..f2d06efc4c 100644--- a/packages/tinybird/datasources/dub_import_error_logs.datasource+++ b/packages/tinybird/datasources/dub_import_error_logs.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime64(3) `json:$.timestamp` DEFAULT now(),diff --git a/packages/tinybird/datasources/dub_lead_events.datasource b/packages/tinybird/datasources/dub_lead_events.datasourceindex fcabefa222..67068fcf84 100644--- a/packages/tinybird/datasources/dub_lead_events.datasource+++ b/packages/tinybird/datasources/dub_lead_events.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime64(3) `json:$.timestamp` DEFAULT now(),diff --git a/packages/tinybird/datasources/dub_links_metadata.datasource b/packages/tinybird/datasources/dub_links_metadata.datasourceindex d8dd74d455..3a46ee0b1b 100644--- a/packages/tinybird/datasources/dub_links_metadata.datasource+++ b/packages/tinybird/datasources/dub_links_metadata.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime `json:$.timestamp` DEFAULT now(),diff --git a/packages/tinybird/datasources/dub_sale_events.datasource b/packages/tinybird/datasources/dub_sale_events.datasourceindex 69daa38d88..52e0c6102b 100644--- a/packages/tinybird/datasources/dub_sale_events.datasource+++ b/packages/tinybird/datasources/dub_sale_events.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime64(3) `json:$.timestamp` DEFAULT now(),diff --git a/packages/tinybird/datasources/dub_sale_events_id.datasource b/packages/tinybird/datasources/dub_sale_events_id.datasourcenew file mode 100644index 0000000000..d9d5a90feb--- /dev/null+++ b/packages/tinybird/datasources/dub_sale_events_id.datasource@@ -0,0 +1,41 @@+# Data Source created from Pipe 'dub_sale_events_id_pipe'++SCHEMA >+ `timestamp` DateTime64(3),+ `event_id` String,+ `event_name` String,+ `customer_id` String,+ `payment_processor` LowCardinality(String),+ `invoice_id` String,+ `amount` UInt32,+ `currency` LowCardinality(String),+ `click_id` String,+ `link_id` String,+ `url` String,+ `continent` LowCardinality(String),+ `country` LowCardinality(String),+ `city` String,+ `region` String,+ `latitude` String,+ `longitude` String,+ `device` LowCardinality(String),+ `device_model` LowCardinality(String),+ `device_vendor` LowCardinality(String),+ `browser` LowCardinality(String),+ `browser_version` String,+ `os` LowCardinality(String),+ `os_version` String,+ `engine` LowCardinality(String),+ `engine_version` String,+ `cpu_architecture` LowCardinality(String),+ `ua` String,+ `bot` UInt8,+ `referer` String,+ `referer_url` String,+ `ip` String,+ `qr` UInt8,+ `metadata` String++ENGINE "MergeTree"+ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"+ENGINE_SORTING_KEY "event_id"diff --git a/packages/tinybird/datasources/dub_webhook_events.datasource b/packages/tinybird/datasources/dub_webhook_events.datasourceindex 0b84db7aff..bd92d3c5e1 100644--- a/packages/tinybird/datasources/dub_webhook_events.datasource+++ b/packages/tinybird/datasources/dub_webhook_events.datasource@@ -1,3 +1,4 @@+TOKEN dub_tinybird_token APPENDSCHEMA >`timestamp` DateTime64(3) `json:$.timestamp` DEFAULT now(),diff --git a/packages/tinybird/endpoints/all_stats.pipe b/packages/tinybird/endpoints/all_stats.pipenew file mode 100644index 0000000000..51c9a00869--- /dev/null+++ b/packages/tinybird/endpoints/all_stats.pipe@@ -0,0 +1,13 @@+TOKEN dub_tinybird_token READ++TAGS "Dub Misc Endpoints"++NODE endpoint+SQL >++ SELECT+ (SELECT COUNT(timestamp) FROM dub_click_events_mv) AS clicks,+ (SELECT COUNT(timestamp) + 42036155 FROM dub_links_metadata_latest FINAL) AS links,+ (SELECT SUM(amount) FROM dub_sale_events_mv) AS sales++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/coordinates_all.pipe b/packages/tinybird/endpoints/coordinates_all.pipenew file mode 100644index 0000000000..d21d554b10--- /dev/null+++ b/packages/tinybird/endpoints/coordinates_all.pipe@@ -0,0 +1,98 @@+TOKEN dub_tinybird_token READ++TAGS "Dub Misc Endpoints"++NODE coordinates_clicks_data+SQL >++ %+ SELECT+ 'click' AS event,+ timestamp,+ country,+ city,+ latitude,+ longitude,+ device+ FROM dub_click_events_mv+ WHERE+ timestamp > now() - INTERVAL 5 MINUTE+ AND country != 'Unknown' AND city != 'Unknown' AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 100++++NODE coordinates_leads_data+SQL >++ %+ SELECT+ 'lead' AS event,+ timestamp,+ country,+ city,+ latitude,+ longitude,+ device+ FROM dub_lead_events_mv+ WHERE+ timestamp > now() - INTERVAL 12 HOUR+ AND country != 'Unknown'+ AND city != 'Unknown'+ AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 100++++NODE coordinates_sales_data+SQL >++ %+ SELECT+ 'sale' AS event,+ timestamp,+ country,+ city,+ latitude,+ longitude,+ device,+ amount,+ round(+ amount * arrayElement([0.3, 0.4, 0.5], 1 + (toUnixTimestamp(timestamp) % 3))+ ) as commission+ FROM dub_sale_events_mv+ WHERE+ timestamp > now() - INTERVAL 12 HOUR+ AND country != 'Unknown'+ AND city != 'Unknown'+ AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 100++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ (+ SELECT *, NULL AS amount, NULL AS commission+ FROM coordinates_leads_data+ UNION ALL+ SELECT+ *,+ NULL AS amount,+ NULL AS commission+ FROM coordinates_clicks_data+ UNION ALL+ SELECT *+ FROM coordinates_sales_data+ )+ ORDER BY+ timestamp DESC++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/coordinates_sales.pipe b/packages/tinybird/endpoints/coordinates_sales.pipenew file mode 100644index 0000000000..93bfca795f--- /dev/null+++ b/packages/tinybird/endpoints/coordinates_sales.pipe@@ -0,0 +1,28 @@+TOKEN dub_tinybird_token READ++TAGS "Dub Misc Endpoints"++NODE endpoint+SQL >++ %+ SELECT+ timestamp,+ amount,+ round(+ amount * arrayElement([0.3, 0.4, 0.5], 1 + (toUnixTimestamp(timestamp) % 3))+ ) as commission,+ country,+ city,+ latitude,+ longitude+ FROM dub_sale_events_mv+ WHERE+ timestamp > now() - INTERVAL 12 HOUR+ AND country != 'Unknown'+ AND city != 'Unknown'+ AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 500++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_audit_logs.pipe b/packages/tinybird/endpoints/get_audit_logs.pipenew file mode 100644index 0000000000..ed6f4a0512--- /dev/null+++ b/packages/tinybird/endpoints/get_audit_logs.pipe@@ -0,0 +1,30 @@+TOKEN dub_tinybird_token READ++NODE endpoint+SQL >++ %+ SELECT+ id,+ timestamp,+ action,+ actor_id,+ actor_type,+ actor_name,+ targets,+ description,+ ip_address,+ user_agent,+ metadata+ FROM dub_audit_logs+ WHERE+ true+ {% if defined(start) and defined(end) %}+ AND timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }}+ {% end %}+ {% if defined(workspaceId) %} AND workspace_id = {{ String(workspaceId) }} {% end %}+ {% if defined(programId) %} AND program_id = {{ String(programId) }} {% end %}+ ORDER BY timestamp DESC++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_click_event.pipe b/packages/tinybird/endpoints/get_click_event.pipenew file mode 100644index 0000000000..3d5c7fd189--- /dev/null+++ b/packages/tinybird/endpoints/get_click_event.pipe@@ -0,0 +1,22 @@+TOKEN dub_tinybird_token READ++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_click_events_id+ WHERE+ click_id+ = {{+ String(+ clickId,+ 'Z4f1TFvkejrp9o0L',+ description="The unique ID for a given click event",+ required=True,+ )+ }}+ ORDER BY timestamp DESC+ LIMIT 1++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_framer_lead_events.pipe b/packages/tinybird/endpoints/get_framer_lead_events.pipenew file mode 100644index 0000000000..c76dba6989--- /dev/null+++ b/packages/tinybird/endpoints/get_framer_lead_events.pipe@@ -0,0 +1,13 @@+TOKEN dub_tinybird_token READ++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events_mv+ WHERE+ link_id IN {{ Array(linkIds, 'String', ['link_1JWRSXGRTN95H1YCKTC5BM41B','link_1JWQHXN0Y1QBR7X07YQ6MHWTZ']) }}+ AND customer_id IN {{ Array(customerIds, 'String', ['cus_1JWTHSGTT67WY9NK98QSFJ3M4','cus_1JWTQS8S008Q3VTB8ZRG7AE3W']) }}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_import_error_logs.pipe b/packages/tinybird/endpoints/get_import_error_logs.pipenew file mode 100644index 0000000000..0eb2c32362--- /dev/null+++ b/packages/tinybird/endpoints/get_import_error_logs.pipe@@ -0,0 +1,33 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Get import logs by ID+++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_import_error_logs+ WHERE+ workspace_id+ = {{+ String(+ workspaceId,+ 'wh_fjs8YfXkgsFL7eF7LOBvbluDb',+ required=True,+ )+ }}+ AND import_id+ = {{+ String(+ importId,+ 'uC2DOqy',+ required=True,+ )+ }}+ ORDER BY timestamp DESC+ limit 5000++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_lead_event.pipe b/packages/tinybird/endpoints/get_lead_event.pipenew file mode 100644index 0000000000..82fda4a50f--- /dev/null+++ b/packages/tinybird/endpoints/get_lead_event.pipe@@ -0,0 +1,22 @@+TOKEN dub_tinybird_token READ++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events+ WHERE+ customer_id+ = {{+ String(+ customerId,+ "cus_JzMqCLdaiVM1o1grw0yk84uC",+ description="The unique ID for a given customer.",+ required=True,+ )+ }}+ {% if defined(eventName) %} AND event_name = {{ eventName }} {% end %}+ ORDER BY timestamp DESC++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_lead_event_by_id.pipe b/packages/tinybird/endpoints/get_lead_event_by_id.pipenew file mode 100644index 0000000000..8bcfb710ee--- /dev/null+++ b/packages/tinybird/endpoints/get_lead_event_by_id.pipe@@ -0,0 +1,25 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ internal pipe for updating lead event+++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events+ WHERE+ event_id+ = {{+ String(+ eventId,+ "nLvob3FdmsYuI1BI",+ description="The unique event ID.",+ required=True,+ )+ }}+ ORDER BY timestamp DESC++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_lead_events.pipe b/packages/tinybird/endpoints/get_lead_events.pipenew file mode 100644index 0000000000..2eece42e4a--- /dev/null+++ b/packages/tinybird/endpoints/get_lead_events.pipe@@ -0,0 +1,14 @@+TOKEN dub_tinybird_token READ++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events+ WHERE+ true+ {% if defined(customerIds) %} AND customer_id IN {{ Array(customerIds, 'String') }} {% end %}+ ORDER BY timestamp DESC++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_sale_event.pipe b/packages/tinybird/endpoints/get_sale_event.pipenew file mode 100644index 0000000000..ff3cbbf337--- /dev/null+++ b/packages/tinybird/endpoints/get_sale_event.pipe@@ -0,0 +1,20 @@+TOKEN dub_tinybird_token READ++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_sale_events_id+ WHERE+ event_id+ = {{+ String(+ eventId,+ "O9Q8fKbfY809HXBp",+ description="The unique event ID.",+ required=True,+ )+ }}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/get_webhook_events.pipe b/packages/tinybird/endpoints/get_webhook_events.pipenew file mode 100644index 0000000000..776605bee8--- /dev/null+++ b/packages/tinybird/endpoints/get_webhook_events.pipe@@ -0,0 +1,26 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Get webhook events+++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_webhook_events+ WHERE+ webhook_id+ = {{+ String(+ webhookId,+ 'wh_fjs8YfXkgsFL7eF7LOBvbluDb',+ description="The ID of the webhook",+ required=True,+ )+ }}+ ORDER BY timestamp DESC+ limit 100++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_browsers.pipe b/packages/tinybird/endpoints/v2_browsers.pipenew file mode 100644index 0000000000..73f6b975df--- /dev/null+++ b/packages/tinybird/endpoints/v2_browsers.pipe@@ -0,0 +1,318 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE browsers_clicks+SQL >++ %+ SELECT browser, COUNT(browser) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY browser+ ORDER BY clicks DESC++++NODE browsers_leads+SQL >++ %+ SELECT browser, COUNT(browser) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'+ {% 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 %}+ GROUP BY browser+ ORDER BY leads DESC++++NODE browsers_sales+SQL >++ %+ SELECT browser, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT browser, COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'+ {% 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 %}+ GROUP BY browser+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'+ {% 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 amount 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 {% if defined(saleType) %} browsers_sales_with_type {% else %} browsers_sales {% end %}+ ) AS dse+ ON dce.browser = dse.browser+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} browsers_clicks+ {% elif eventType == 'leads' %} browsers_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} browsers_sales_with_type {% else %} browsers_sales {% end %}+ {% else %} browsers_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_cities.pipe b/packages/tinybird/endpoints/v2_cities.pipenew file mode 100644index 0000000000..d9edfb22e9--- /dev/null+++ b/packages/tinybird/endpoints/v2_cities.pipe@@ -0,0 +1,418 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE cities_clicks+SQL >++ %+ SELECT city, CONCAT(country, '-', region) as region, country, COUNT(city) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY city, region, country+ ORDER BY clicks DESC+ LIMIT 1000++++NODE cities_leads+SQL >++ %+ SELECT city, CONCAT(country, '-', region) as region, country, COUNT(city) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY city, region, country+ ORDER BY leads DESC+ LIMIT 1000++++NODE cities_sales+SQL >++ %+ 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+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY city, region, country+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 amount 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+ 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 {% 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++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} cities_clicks+ {% elif eventType == 'leads' %} cities_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} cities_sales_with_type {% else %} cities_sales {% end %}+ {% else %} cities_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_continents.pipe b/packages/tinybird/endpoints/v2_continents.pipenew file mode 100644index 0000000000..d64662a645--- /dev/null+++ b/packages/tinybird/endpoints/v2_continents.pipe@@ -0,0 +1,321 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top continents+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE continents_clicks+SQL >++ %+ SELECT continent, COUNT(continent) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY continent+ ORDER BY clicks DESC++++NODE continents_leads+SQL >++ %+ SELECT continent, COUNT(continent) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''+ {% 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 %}+ GROUP BY continent+ ORDER BY leads DESC++++NODE continents_sales+SQL >++ %+ SELECT continent, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT continent, COUNT(continent) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''+ {% 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 %}+ GROUP BY continent+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''+ {% 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 amount 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+ {% if defined(saleType) %} continents_sales_with_type+ {% else %} continents_sales+ {% end %}+ ) AS dse+ ON dce.continent = dse.continent+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} continents_clicks+ {% elif eventType == 'leads' %} continents_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} continents_sales_with_type {% else %} continents_sales {% end %}+ {% else %} continents_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_count.pipe b/packages/tinybird/endpoints/v2_count.pipenew file mode 100644index 0000000000..5a04867747--- /dev/null+++ b/packages/tinybird/endpoints/v2_count.pipe@@ -0,0 +1,366 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE count_clicks+SQL >++ %+ SELECT COUNT(*) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}++++NODE count_leads+SQL >++ %+ SELECT COUNT(*) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ {% 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}++++NODE count_sales+SQL >++ %+ SELECT sales, amount, amount AS saleAmount+ FROM+ (+ SELECT COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ {% 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}+ ) AS subquery++++NODE count_sales_with_type+SQL >++ %+ WITH+ sales AS (+ SELECT customer_id, link_id, timestamp, amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ {% 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}+ ),+ distinct_sales AS (SELECT DISTINCT customer_id, link_id FROM sales),+ min_timestamps AS (+ SELECT customer_id, link_id, min(timestamp) AS first_sale_ts+ FROM dub_sale_events_mv+ WHERE (customer_id, link_id) IN distinct_sales+ GROUP BY customer_id, link_id+ )+ SELECT sales, amount, amount AS saleAmount+ FROM+ (+ SELECT COUNT(*) as sales, sum(amount) as amount+ FROM 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 %}+ ) AS subquery++++NODE count_composite+SQL >++ %+ SELECT clicks, leads, sales, amount, saleAmount+ FROM+ count_clicks,+ count_leads,+ {% if defined(saleType) %} count_sales_with_type+ {% else %} count_sales+ {% end %}++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} count_clicks+ {% elif eventType == 'leads' %} count_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} count_sales_with_type {% else %} count_sales {% end %}+ {% else %} count_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_countries.pipe b/packages/tinybird/endpoints/v2_countries.pipenew file mode 100644index 0000000000..85dd144aae--- /dev/null+++ b/packages/tinybird/endpoints/v2_countries.pipe@@ -0,0 +1,310 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE countries_clicks+SQL >++ %+ SELECT country, COUNT(country) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND country != 'Unknown'+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY country+ ORDER BY clicks DESC++++NODE countries_leads+SQL >++ %+ SELECT country, COUNT(country) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND country != 'Unknown'+ {% 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 %}+ GROUP BY country+ ORDER BY leads DESC++++NODE countries_sales+SQL >++ %+ SELECT country, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT country, COUNT(country) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND country != 'Unknown'+ {% 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 %}+ GROUP BY country+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND country != 'Unknown'+ {% 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 amount 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+ {% if defined(saleType) %} countries_sales_with_type+ {% else %} countries_sales+ {% end %}+ ) AS dse+ ON dce.country = dse.country+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} countries_clicks+ {% elif eventType == 'leads' %} countries_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} countries_sales_with_type {% else %} countries_sales {% end %}+ {% else %} countries_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_customer_events.pipe b/packages/tinybird/endpoints/v2_customer_events.pipenew file mode 100644index 0000000000..2c46cfdbbc--- /dev/null+++ b/packages/tinybird/endpoints/v2_customer_events.pipe@@ -0,0 +1,166 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Customer events+++TAGS "Dub Endpoints"++NODE lead_events+SQL >++ %+ SELECT+ timestamp,+ click_id,+ link_id,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ browser,+ os,+ engine,+ ua,+ referer,+ referer_url,+ qr,+ ip,+ CONCAT(country, '-', region) as region_processed,+ splitByString('?', referer_url)[1] as referer_url_processed,+ 'lead' as event,+ event_id,+ event_name,+ metadata+ FROM dub_lead_events_mv+ WHERE+ customer_id+ = {{+ String(+ customerId,+ 'cus_1JRJNSVARH220RCNJ2K5SAX9Q',+ description="The unique ID for a given customer",+ required=True,+ )+ }}+ {% if defined(linkIds) %} AND link_id IN ({{ Array(linkIds, 'link_id') }}) {% end %}+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}+ LIMIT {{ Int32(limit, 100) }}++++NODE click_events+SQL >++ %+ SELECT+ timestamp,+ click_id,+ link_id,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ browser,+ os,+ engine,+ ua,+ referer,+ referer_url,+ qr,+ ip,+ CONCAT(country, '-', region) as region_processed,+ splitByString('?', referer_url)[1] as referer_url_processed,+ 'click' as event+ FROM dub_click_events_id+ WHERE+ click_id IN (SELECT DISTINCT click_id FROM lead_events)+ {% if defined(linkIds) %} AND link_id IN ({{ Array(linkIds, 'link_id') }}) {% end %}+ LIMIT {{ Int32(limit, 100) }}++++NODE sale_events+SQL >++ %+ SELECT+ timestamp,+ click_id,+ link_id,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ browser,+ os,+ engine,+ ua,+ referer,+ referer_url,+ qr,+ ip,+ CONCAT(country, '-', region) as region_processed,+ splitByString('?', referer_url)[1] as referer_url_processed,+ 'sale' as event,+ event_id,+ event_name,+ metadata,+ amount as saleAmount,+ invoice_id,+ payment_processor+ FROM dub_sale_events_mv+ WHERE+ customer_id+ = {{+ String(+ customerId,+ 'cus_1JRJNSVARH220RCNJ2K5SAX9Q',+ description="The unique ID for a given customer",+ required=True,+ )+ }}+ {% if defined(linkIds) %} AND link_id IN ({{ Array(linkIds, 'link_id') }}) {% end %}+ LIMIT {{ Int32(limit, 100) }}++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ (+ SELECT *, NULL AS saleAmount, NULL AS invoice_id, NULL AS payment_processor+ FROM lead_events+ UNION ALL+ SELECT+ *,+ NULL AS event_id,+ NULL AS event_name,+ NULL AS metadata,+ NULL AS saleAmount,+ NULL AS invoice_id,+ NULL AS payment_processor+ FROM click_events+ UNION ALL+ SELECT *+ FROM sale_events+ )+ ORDER BY+ timestamp DESC, CASE event WHEN 'click' THEN 1 WHEN 'lead' THEN 2 WHEN 'sale' THEN 3 END DESC++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_devices.pipe b/packages/tinybird/endpoints/v2_devices.pipenew file mode 100644index 0000000000..057700526d--- /dev/null+++ b/packages/tinybird/endpoints/v2_devices.pipe@@ -0,0 +1,318 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE devices_clicks+SQL >++ %+ SELECT device, COUNT(device) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY device+ ORDER BY clicks DESC++++NODE devices_leads+SQL >++ %+ SELECT device, COUNT(device) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'+ {% 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 %}+ GROUP BY device+ ORDER BY leads DESC++++NODE devices_sales+SQL >++ %+ SELECT device, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT device, COUNT(device) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'+ {% 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 %}+ GROUP BY device+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'+ {% 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 amount 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 {% if defined(saleType) %} devices_sales_with_type {% else %} devices_sales {% end %}+ ) AS dse+ ON dce.device = dse.device+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} devices_clicks+ {% elif eventType == 'leads' %} devices_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} devices_sales_with_type {% else %} devices_sales {% end %}+ {% else %} devices_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_events.pipe b/packages/tinybird/endpoints/v2_events.pipenew file mode 100644index 0000000000..d1067a1751--- /dev/null+++ b/packages/tinybird/endpoints/v2_events.pipe@@ -0,0 +1,367 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id, domain, key+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE click_events+SQL >++ %+ SELECT+ *,+ splitByString('?', referer_url)[1] as referer_url_processed,+ CONCAT(country, '-', region) as region_processed,+ 'click' as event+ FROM {% if defined(customerId) %} dub_click_events_id {% else %} dub_click_events_mv {% end %}+ WHERE+ timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }}+ {% if defined(customerId) %}+ AND click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}+ {% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}+ AND link_id IN (SELECT link_id FROM workspace_links)+ {% end %}+ {% 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(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 url = {{ url }} {% end %}+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}+ LIMIT {{ Int32(limit, 100) }}+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}++++NODE lead_events+SQL >++ %+ SELECT+ *,+ splitByString('?', referer_url)[1] as referer_url_processed,+ CONCAT(country, '-', region) as region_processed,+ 'lead' as event+ FROM dub_lead_events_mv+ WHERE+ timestamp >= {{ DateTime(start, '2024-01-01 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2025-12-31 00:00:00') }}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}+ {% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}+ AND link_id IN (SELECT link_id FROM workspace_links)+ {% end %}+ {% 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(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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}++ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}+ LIMIT {{ Int32(limit, 100) }}+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}++++NODE 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+ timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2025-12-31 00:00:00') }}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}+ {% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}+ AND link_id IN (SELECT link_id FROM workspace_links)+ {% end %}+ {% 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(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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}++ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}+ LIMIT {{ Int32(limit, 100) }}+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}++++NODE sale_events_with_type+SQL >++ %+ WITH+ sales AS (+ SELECT+ *,+ amount as saleAmount,+ CONCAT(country, '-', region) as region_processed,+ splitByString('?', referer_url)[1] as referer_url_processed,+ 'sale' as event+ FROM dub_sale_events_mv+ WHERE+ timestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}+ {% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}+ AND link_id IN (SELECT link_id FROM workspace_links)+ {% end %}+ {% 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(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 }}+ {% end %}+ {% end %}+ {% 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 *, CASE WHEN timestamp = first_sale_ts THEN 'new' ELSE 'recurring' END as sale_type+ 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 %}+ ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}+ LIMIT {{ Int32(limit, 100) }}+ {% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'leads' %} lead_events+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} sale_events_with_type {% else %} sale_events {% end %}+ {% else %} click_events+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_os.pipe b/packages/tinybird/endpoints/v2_os.pipenew file mode 100644index 0000000000..925fd59d11--- /dev/null+++ b/packages/tinybird/endpoints/v2_os.pipe@@ -0,0 +1,317 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE os_clicks+SQL >++ %+ SELECT os, COUNT(os) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY os+ ORDER BY clicks DESC++++NODE os_leads+SQL >++ %+ SELECT os, COUNT(os) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'+ {% 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 %}+ GROUP BY os+ ORDER BY leads DESC++++NODE os_sales+SQL >++ %+ SELECT os, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT os, COUNT(os) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'+ {% 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 %}+ GROUP BY os+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'+ {% 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 amount 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 {% if defined(saleType) %} os_sales_with_type {% else %} os_sales {% end %}+ ) AS dse+ ON dce.os = dse.os+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} os_clicks+ {% elif eventType == 'leads' %} os_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} os_sales_with_type {% else %} os_sales {% end %}+ {% else %} os_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_referer_urls.pipe b/packages/tinybird/endpoints/v2_referer_urls.pipenew file mode 100644index 0000000000..d71613063c--- /dev/null+++ b/packages/tinybird/endpoints/v2_referer_urls.pipe@@ -0,0 +1,326 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE referer_urls_clicks+SQL >++ %+ SELECT splitByString('?', referer_url)[1] as refererUrl, COUNT(*) as clicks+ FROM+ dub_click_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY splitByString('?', referer_url)[1]+ ORDER BY clicks DESC+ LIMIT 5000++++NODE referer_urls_leads+SQL >++ %+ SELECT splitByString('?', referer_url)[1] as refererUrl, COUNT(referer_url) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'+ {% 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 %}+ GROUP BY splitByString('?', referer_url)[1]+ ORDER BY leads DESC+ LIMIT 1000++++NODE referer_urls_sales+SQL >++ %+ SELECT refererUrl, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT+ splitByString('?', referer_url)[1] as refererUrl,+ COUNT(referer_url) as sales,+ sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'+ {% 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 %}+ GROUP BY splitByString('?', referer_url)[1]+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'+ {% 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 amount 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+ {% if defined(saleType) %} referer_urls_sales_with_type+ {% else %} referer_urls_sales+ {% end %}+ ) AS dse+ ON dce.refererUrl = dse.refererUrl+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} referer_urls_clicks+ {% elif eventType == 'leads' %} referer_urls_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} referer_urls_sales_with_type+ {% else %} referer_urls_sales+ {% end %}+ {% else %} referer_urls_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_referers.pipe b/packages/tinybird/endpoints/v2_referers.pipenew file mode 100644index 0000000000..ae0e059756--- /dev/null+++ b/packages/tinybird/endpoints/v2_referers.pipe@@ -0,0 +1,307 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE referers_clicks+SQL >++ %+ SELECT referer, COUNT(referer) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY referer+ ORDER BY clicks DESC+ LIMIT 1000++++NODE referers_leads+SQL >++ %+ SELECT referer, COUNT(referer) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY referer+ ORDER BY leads DESC+ LIMIT 1000++++NODE referers_sales+SQL >++ %+ SELECT referer, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT referer, COUNT(referer) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY referer+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 amount 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+ {% if defined(saleType) %} referers_sales_with_type+ {% else %} referers_sales+ {% end %}) AS dse ON dce.referer = dse.referer+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} referers_clicks+ {% elif eventType == 'leads' %} referers_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} referers_sales_with_type {% else %} referers_sales {% end %}+ {% else %} referers_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_regions.pipe b/packages/tinybird/endpoints/v2_regions.pipenew file mode 100644index 0000000000..1ceaf9cfb3--- /dev/null+++ b/packages/tinybird/endpoints/v2_regions.pipe@@ -0,0 +1,423 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE regions_clicks+SQL >++ %+ SELECT CONCAT(country, '-', region) as region, country, clicks,+ FROM+ (+ SELECT region, country, COUNT(region) as clicks+ FROM+ dub_click_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY region, country+ ORDER BY clicks DESC+ LIMIT 1000+ ) as subquery++++NODE regions_leads+SQL >++ %+ SELECT CONCAT(country, '-', region) as region, country, leads,+ FROM+ (+ SELECT region, country, COUNT(region) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY region, country+ ORDER BY leads DESC+ LIMIT 1000+ ) as subquery++++NODE regions_sales+SQL >++ %+ SELECT CONCAT(country, '-', region) as region, country, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT region, country, COUNT(region) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY region, country+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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(region) 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 amount 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 {% if defined(saleType) %} regions_sales_with_type {% else %} regions_sales {% end %}+ ) AS dse+ ON dce.region = dse.region+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} regions_clicks+ {% elif eventType == 'leads' %} regions_leads+ {% elif eventType == 'sales' %} {% if defined(saleType) %} regions_sales_with_type+ {% else %} regions_sales+ {% end %}+ {% else %} regions_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_timeseries.pipe b/packages/tinybird/endpoints/v2_timeseries.pipenew file mode 100644index 0000000000..2856a2b750--- /dev/null+++ b/packages/tinybird/endpoints/v2_timeseries.pipe@@ -0,0 +1,461 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Timeseries data+++TAGS "Dub Endpoints"++NODE month_intervals+SQL >++ %+ WITH+ toStartOfMonth(+ toDateTime64({{ DateTime64(start, '2024-02-24 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS start,+ toStartOfMonth(+ toDateTime64({{ DateTime64(end, '2024-05-23 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS+ end,+ dateDiff('month', start, end) + 1 AS months_diff+ SELECT+ arrayJoin(+ arrayMap(+ x -> toDateTime64(start + toIntervalMonth(x), 3, {{ String(timezone, 'UTC') }}),+ range(0, months_diff)+ )+ ) as interval++++NODE day_intervals+SQL >++ %+ WITH+ toStartOfDay(+ toDateTime64({{ DateTime64(start, '2024-02-24 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS start,+ toStartOfDay(+ toDateTime64({{ DateTime64(end, '2024-05-23 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS+ end+ SELECT+ arrayJoin(+ arrayMap(+ x -> toDateTime64(toStartOfDay(toDateTime64(x, 3), {{ String(timezone, 'UTC') }}), 3),+ range(toUInt32(start), toUInt32(end + 86400),+ 86400+ )+ )+ ) as interval++++NODE hour_intervals+SQL >++ %+ WITH+ toStartOfHour(+ toDateTime64({{ DateTime64(start, '2024-05-22 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS start,+ toStartOfHour(+ toDateTime64({{ DateTime64(end, '2024-05-23 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS+ end+ SELECT+ arrayJoin(+ arrayMap(x -> toDateTime64(x, 3), range(toUInt32(start), toUInt32(end + 3600), 3600)+ )+ ) as interval++++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE timeseries_clicks_data+SQL >++ %+ 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,+ uniq(click_id) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% 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(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 url = {{ url }} {% end %}+ AND timestamp >= {{ DateTime(start, '2024-04-25 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-05-23 00:00:00') }}+ GROUP BY interval+ ORDER BY interval++++NODE timeseries_clicks+SQL >++ %+ SELECT formatDateTime(interval, '%FT%T.000%z') as start, clicks+ FROM+ {% if granularity == "minute" %} minute_intervals+ {% elif granularity == "hour" %} hour_intervals+ {% elif granularity == "month" %} month_intervals+ {% else %} day_intervals+ {% end %}+ LEFT JOIN timeseries_clicks_data USING interval++++NODE timeseries_leads_data+SQL >++ %+ 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,+ uniq(*) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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') }}+ GROUP BY interval+ ORDER BY interval++++NODE timeseries_leads+SQL >++ %+ SELECT formatDateTime(interval, '%FT%T.000%z') as start, leads+ FROM+ {% if granularity == "minute" %} minute_intervals+ {% elif granularity == "hour" %} hour_intervals+ {% elif granularity == "month" %} month_intervals+ {% else %} day_intervals+ {% end %}+ LEFT JOIN timeseries_leads_data USING interval++++NODE timeseries_sales_data+DESCRIPTION >+ undefined++SQL >++ %+ 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,+ uniq(*) as sales,+ sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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') }}+ GROUP BY interval+ ORDER BY interval++++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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 >++ %+ SELECT formatDateTime(interval, '%FT%T.000%z') as start, sales, amount, amount as saleAmount+ FROM+ {% if granularity == "minute" %} minute_intervals+ {% elif granularity == "hour" %} hour_intervals+ {% elif granularity == "month" %} month_intervals+ {% else %} day_intervals+ {% end %}+ LEFT JOIN+ {% if defined(saleType) %} timeseries_sales_data_with_type+ {% else %} timeseries_sales_data+ {% end %} USING interval++++NODE timeseries_composite+SQL >++ SELECT dce.start AS start, clicks, leads, sales, amount, saleAmount+ FROM (SELECT start, clicks FROM timeseries_clicks) AS dce+ LEFT JOIN (SELECT * FROM timeseries_leads) AS dle ON dce.start = dle.start+ LEFT JOIN (SELECT * FROM timeseries_sales) AS dse ON dce.start = dse.start++++NODE timeseries_billing+SQL >++ SELECT+ dce.start AS start,+ clicks,+ leads,+ sales,+ amount,+ saleAmount,+ (clicks + leads + sales) AS totalEvents+ FROM (SELECT start, clicks FROM timeseries_clicks) AS dce+ LEFT JOIN (SELECT * FROM timeseries_leads) AS dle ON dce.start = dle.start+ LEFT JOIN (SELECT * FROM timeseries_sales) AS dse ON dce.start = dse.start++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} timeseries_clicks+ {% elif eventType == 'leads' %} timeseries_leads+ {% elif eventType == 'sales' %} timeseries_sales+ {% elif eventType == 'billing' %} timeseries_billing+ {% else %} timeseries_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_top_links.pipe b/packages/tinybird/endpoints/v2_top_links.pipenew file mode 100644index 0000000000..0676a35696--- /dev/null+++ b/packages/tinybird/endpoints/v2_top_links.pipe@@ -0,0 +1,315 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top links endpoint (most updated version with linkIds filter support)+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE top_links_clicks+SQL >++ %+ SELECT link_id as link, COUNT(*) AS clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY link_id+ ORDER BY clicks DESC+ LIMIT 1000++++NODE top_links_leads+SQL >++ %+ SELECT link_id as link, COUNT(*) AS leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY link_id+ ORDER BY leads DESC+ LIMIT 5000++++NODE top_links_sales+SQL >++ %+ SELECT link, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT link_id as link, COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY link_id+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 amount 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+ {% if defined(saleType) %} top_links_sales_with_type+ {% else %} top_links_sales+ {% end %}+ ) AS dse+ ON dce.link = dse.link+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} top_links_clicks+ {% elif eventType == 'leads' %} top_links_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} top_links_sales_with_type {% else %} top_links_sales {% end %}+ {% else %} top_links_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_top_partners.pipe b/packages/tinybird/endpoints/v2_top_partners.pipenew file mode 100644index 0000000000..89a13811b7--- /dev/null+++ b/packages/tinybird/endpoints/v2_top_partners.pipe@@ -0,0 +1,263 @@+TOKEN dub_tinybird_token READ++NODE partner_links+SQL >++ %+ SELECT link_id, partner_id+ FROM dub_regular_links_metadata_latest FINAL+ WHERE+ deleted == 0 AND partner_id != ''+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}++++NODE top_partners_clicks+SQL >++ %+ SELECT pl.partner_id as partnerId, COUNT(*) AS clicks+ FROM dub_click_events_mv AS ce+ JOIN partner_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(trigger) %} AND trigger = {{ trigger }} {% 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.partner_id+ ORDER BY clicks DESC+ LIMIT 5000++++NODE top_partners_leads+SQL >++ %+ SELECT pl.partner_id as partnerId, COUNT(*) AS leads+ FROM dub_lead_events AS le+ JOIN partner_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.partner_id+ ORDER BY leads DESC+ LIMIT 5000++++NODE top_partners_sales+SQL >++ %+ SELECT pl.partner_id as partnerId, COUNT(*) AS sales, sum(amount) as saleAmount+ 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 %}+ 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 >++ %+ SELECT c.partnerId as partnerId, c.clicks as clicks, l.leads as leads, s.sales as sales, s.saleAmount as saleAmount+ FROM top_partners_clicks AS c+ LEFT JOIN top_partners_leads AS l ON c.partnerId = l.partnerId+ 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++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} top_partners_clicks+ {% elif eventType == 'leads' %} top_partners_leads+ {% elif eventType == 'composite' %} top_partners_composite+ {% else %} {% if defined(saleType) %} top_partners_sales_with_type+ {% else %} top_partners_sales+ {% end %}+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_top_programs.pipe b/packages/tinybird/endpoints/v2_top_programs.pipenew file mode 100644index 0000000000..19b9d644ca--- /dev/null+++ b/packages/tinybird/endpoints/v2_top_programs.pipe@@ -0,0 +1,180 @@+TOKEN dub_tinybird_token READ++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(trigger) %} AND trigger = {{ trigger }} {% 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 %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_top_tags.pipe b/packages/tinybird/endpoints/v2_top_tags.pipenew file mode 100644index 0000000000..594a580d03--- /dev/null+++ b/packages/tinybird/endpoints/v2_top_tags.pipe@@ -0,0 +1,289 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links_with_tags+SQL >++ %+ SELECT link_id, arrayJoin(tag_ids) as tag_id+ FROM dub_links_metadata_latest FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderId) %} AND folder_id = {{ folderId }} {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE top_tags_clicks+SQL >++ %+ SELECT tag_id, COUNT(*) AS clicks+ FROM (SELECT link_id, tag_id FROM workspace_links_with_tags) AS tags+ JOIN+ (+ SELECT link_id+ FROM+ dub_click_events_mv+ {% if defined(workspaceId) or defined(partnerId) or defined(programId) %}+ PREWHERE link_id in (SELECT link_id from workspace_links_with_tags)+ {% end %}+ WHERE+ true+ {% if defined(continent) %} AND continent = {{ continent }} {% end %}+ {% if defined(country) %} AND country = {{ country }} {% end %}+ {% if defined(region) %} AND region = {{ region }} {% end %}+ {% if defined(city) %} AND city = {{ city }} {% end %}+ {% if defined(device) %} AND device = {{ device }} {% end %}+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}+ {% if defined(os) %} AND os = {{ os }} {% end %}+ {% if defined(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ ) AS clicks+ ON clicks.link_id = tags.link_id+ GROUP BY tag_id+ ORDER BY clicks DESC+ LIMIT 5000++++NODE top_tags_leads+SQL >++ %+ SELECT tag_id, COUNT(*) AS leads+ FROM (SELECT link_id, tag_id FROM workspace_links_with_tags) AS tags+ JOIN+ (+ SELECT link_id+ FROM+ dub_lead_events_mv+ {% if defined(workspaceId) or defined(partnerId) or defined(programId) %}+ PREWHERE link_id in (SELECT link_id from workspace_links_with_tags)+ {% end %}+ WHERE+ true+ {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}+ {% if defined(continent) %} AND continent = {{ continent }} {% end %}+ {% if defined(country) %} AND country = {{ country }} {% end %}+ {% if defined(region) %} AND region = {{ region }} {% end %}+ {% if defined(city) %} AND city = {{ city }} {% end %}+ {% if defined(device) %} AND device = {{ device }} {% end %}+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}+ {% if defined(os) %} AND os = {{ os }} {% end %}+ {% if defined(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ ) AS leads+ ON leads.link_id = tags.link_id+ GROUP BY tag_id+ ORDER BY leads DESC+ LIMIT 5000++++NODE top_tags_sales+SQL >++ %+ SELECT tag_id, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT tag_id, COUNT(*) as sales, sum(amount) as amount+ FROM (SELECT link_id, tag_id FROM workspace_links_with_tags) AS tags+ JOIN+ (+ SELECT link_id, amount+ FROM+ dub_sale_events_mv+ {% if defined(workspaceId) or defined(partnerId) or defined(programId) %}+ PREWHERE link_id in (SELECT link_id from workspace_links_with_tags)+ {% end %}+ WHERE+ true+ {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}+ {% if defined(continent) %} AND continent = {{ continent }} {% end %}+ {% if defined(country) %} AND country = {{ country }} {% end %}+ {% if defined(region) %} AND region = {{ region }} {% end %}+ {% if defined(city) %} AND city = {{ city }} {% end %}+ {% if defined(device) %} AND device = {{ device }} {% end %}+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}+ {% if defined(os) %} AND os = {{ os }} {% end %}+ {% if defined(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ ) AS sales+ ON sales.link_id = tags.link_id+ GROUP BY tag_id+ ORDER BY amount DESC+ LIMIT 5000+ ) as subquery++++NODE top_tags_composite+SQL >++ SELECT COALESCE(c.tag_id, l.tag_id, s.tag_id) as tag_id, clicks, leads, sales, amount, saleAmount+ FROM (SELECT tag_id, clicks FROM top_tags_clicks) AS c+ FULL OUTER JOIN (SELECT tag_id, leads FROM top_tags_leads) AS l ON c.tag_id = l.tag_id+ FULL OUTER JOIN+ (SELECT tag_id, sales, amount, saleAmount FROM top_tags_sales) AS s+ ON COALESCE(c.tag_id, l.tag_id) = s.tag_id+ ORDER BY clicks DESC NULLS LAST++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} top_tags_clicks+ {% elif eventType == 'leads' %} top_tags_leads+ {% elif eventType == 'sales' %} top_tags_sales+ {% else %} top_tags_composite+ {% end %}++++NODE filtered_clicks+SQL >++ %+ SELECT link_id, COUNT(*) AS clicks+ FROM dub_click_events_mv+ {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}+ PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(continent) %} AND continent = {{ continent }} {% end %}+ {% if defined(country) %} AND country = {{ country }} {% end %}+ {% if defined(region) %} AND region = {{ region }} {% end %}+ {% if defined(city) %} AND city = {{ city }} {% end %}+ {% if defined(device) %} AND device = {{ device }} {% end %}+ {% if defined(browser) %} AND browser = {{ browser }} {% end %}+ {% if defined(os) %} AND os = {{ os }} {% end %}+ {% if defined(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=', {{ String(utm_source) }}, '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', {{ String(utm_medium) }}, '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url LIKE concat('%utm_campaign=', {{ String(utm_campaign) }}, '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', {{ String(utm_term) }}, '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', {{ String(utm_content) }}, '%')+ {% end %}+ {% if defined(url) %} AND url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY link_id++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_top_urls.pipe b/packages/tinybird/endpoints/v2_top_urls.pipenew file mode 100644index 0000000000..7407b5e13e--- /dev/null+++ b/packages/tinybird/endpoints/v2_top_urls.pipe@@ -0,0 +1,301 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE top_urls_clicks+SQL >++ %+ SELECT url, COUNT(*) AS clicks+ FROM+ dub_click_events_mv+ {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}+ PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ url != ''+ {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY url+ ORDER BY clicks DESC+ LIMIT 5000++++NODE top_urls_leads+SQL >++ %+ SELECT url, COUNT(*) AS leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 %}+ GROUP BY url+ ORDER BY leads DESC+ LIMIT 1000++++NODE top_urls_sales+SQL >++ %+ SELECT url, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT url, COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 %}+ GROUP BY url+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 amount 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 {% if defined(saleType) %} top_urls_sales_with_type {% else %} top_urls_sales {% end %}+ ) AS dse+ ON dce.url = dse.url+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} top_urls_clicks+ {% elif eventType == 'leads' %} top_urls_leads+ {% elif eventType == 'sales' %} {% if defined(saleType) %} top_urls_sales_with_type+ {% else %} top_urls_sales+ {% end %}+ {% else %} top_urls_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_triggers.pipe b/packages/tinybird/endpoints/v2_triggers.pipenew file mode 100644index 0000000000..342db1f510--- /dev/null+++ b/packages/tinybird/endpoints/v2_triggers.pipe@@ -0,0 +1,325 @@+TOKEN dub_tinybird_token READ++DESCRIPTION >+ Top countries+++TAGS "Dub Endpoints"++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE trigger_clicks+SQL >++ %+ SELECT trigger, COUNT(trigger) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% 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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY trigger+ ORDER BY clicks DESC++++NODE trigger_leads+SQL >++ %+ SELECT CASE WHEN qr = 0 THEN 'link' WHEN qr = 1 THEN 'qr' END as trigger, COUNT(qr) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY qr+ ORDER BY leads DESC++++NODE trigger_sales+SQL >++ %+ 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+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY qr+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 amount 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 {% if defined(saleType) %} trigger_sales_with_type {% else %} trigger_sales {% end %}+ ) AS dse+ ON dce.trigger = dse.trigger+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} trigger_clicks+ {% elif eventType == 'leads' %} trigger_leads+ {% elif eventType == 'sales' %} {% if defined(saleType) %} trigger_sales_with_type+ {% else %} trigger_sales+ {% end %}+ {% else %} trigger_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_usage.pipe b/packages/tinybird/endpoints/v2_usage.pipenew file mode 100644index 0000000000..8dfb2b2182--- /dev/null+++ b/packages/tinybird/endpoints/v2_usage.pipe@@ -0,0 +1,176 @@+TOKEN dub_tinybird_token READ++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 >++ %+ WITH+ toStartOfDay(+ toDateTime64({{ DateTime64(start, '2024-09-03 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS start,+ toStartOfDay(+ toDateTime64({{ DateTime64(end, '2024-10-03 00:00:00.000') }}, 3),+ {{ String(timezone, 'UTC') }}+ ) AS+ end+ SELECT+ arrayJoin(+ arrayMap(+ x -> toDateTime64(toStartOfDay(toDateTime64(x, 3), {{ String(timezone, 'UTC') }}), 3),+ range(toUInt32(start + 86400), toUInt32(end + 86400),+ 86400+ )+ )+ ) as interval++++NODE usage_clicks_data+SQL >++ %+ SELECT+ toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,+ uniq(*) as clicks+ FROM+ dub_click_events_mv+ PREWHERE link_id in (SELECT link_id from workspace_links)+ WHERE+ timestamp >= {{ DateTime(start, '2024-09-03 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-10-03 00:00:00') }}+ GROUP BY interval+ ORDER BY interval++++NODE usage_leads_data+SQL >++ %+ SELECT+ toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,+ uniq(*) as leads+ FROM+ dub_lead_events_mv+ PREWHERE link_id in (SELECT link_id from workspace_links)+ WHERE+ timestamp >= {{ DateTime(start, '2024-09-03 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-10-03 00:00:00') }}+ GROUP BY interval+ ORDER BY interval++++NODE usage_events+SQL >++ SELECT+ formatDateTime(di.interval, '%FT%T.000%z') as date, clicks, leads, (clicks + leads) as value+ FROM day_intervals as di+ LEFT JOIN (SELECT * FROM usage_clicks_data) AS uc ON di.interval = uc.interval+ LEFT JOIN (SELECT * FROM usage_leads_data) AS ul ON di.interval = ul.interval++++NODE usage_links_data+DESCRIPTION >+ undefined++SQL >++ %+ SELECT+ toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,+ uniq(*) as links+ FROM dub_links_metadata_latest FINAL+ WHERE+ workspace_id+ = {{+ String(+ workspaceId,+ 'ws_clrei1gld0002vs9mzn93p8ik',+ description="The ID of the workspace",+ required=True,+ )+ }}+ AND created_at >= {{ DateTime(start, '2024-09-03 00:00:00') }}+ AND created_at < {{ DateTime(end, '2024-10-03 00:00:00') }}+ GROUP BY interval+ ORDER BY interval++++NODE usage_links+SQL >++ %+ SELECT formatDateTime(interval, '%FT%T.000%z') as date, links as value+ FROM day_intervals+ LEFT JOIN usage_links_data USING interval++++NODE usage_revenue_data+SQL >++ %+ SELECT+ toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,+ sum(amount) as revenue+ FROM+ dub_sale_events_mv+ PREWHERE link_id in (SELECT link_id from workspace_links)+ WHERE+ timestamp >= {{ DateTime(start, '2024-09-03 00:00:00') }}+ AND timestamp < {{ DateTime(end, '2024-10-03 00:00:00') }}+ GROUP BY interval+ ORDER BY interval++++NODE usage_revenue+SQL >++ %+ SELECT formatDateTime(interval, '%FT%T.000%z') as date, revenue as value+ FROM day_intervals+ LEFT JOIN usage_revenue_data USING interval++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if resource == 'events' %} usage_events+ {% elif resource == 'revenue' %} usage_revenue+ {% else %} usage_links+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/endpoints/v2_utms.pipe b/packages/tinybird/endpoints/v2_utms.pipenew file mode 100644index 0000000000..73708c6ad2--- /dev/null+++ b/packages/tinybird/endpoints/v2_utms.pipe@@ -0,0 +1,368 @@+TOKEN dub_tinybird_token READ++NODE workspace_links+SQL >++ %+ SELECT link_id+ FROM+ {% if defined(isMegaFolder) and Boolean(isMegaFolder) == 1 %} dub_links_metadata_latest+ {% else %} dub_regular_links_metadata_latest+ {% end %} FINAL+ WHERE+ deleted == 0+ {% if defined(workspaceId) %} AND workspace_id = {{ workspaceId }} {% end %}+ {% if defined(programId) %} AND program_id = {{ programId }} {% end %}+ {% if defined(partnerId) %} AND partner_id = {{ partnerId }} {% end %}+ {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}+ {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}+ {% elif defined(folderId) %} AND folder_id = {{ folderId }}+ {% end %}+ {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}+ {% if defined(tagIds) %}+ AND arrayIntersect(tag_ids, {{ Array(tagIds, 'String') }}) != []+ {% end %}+ {% if defined(root) %}+ {% if Boolean(root) == 1 %} AND key = '_root' {% else %} AND key != '_root' {% end %}+ {% end %}++++NODE utms_clicks+SQL >++ %+ WITH+ decodeURLFormComponent(+ extractURLParameter(+ url,+ {{+ String(+ groupByUtmTag,+ 'utm_source',+ description="The UTM tag to retrieve data for",+ required=True,+ )+ }}+ )+ ) as utm+ SELECT utm, count(*) as clicks+ FROM+ {% if defined(customerId) %} dub_click_events_id+ {% else %} dub_click_events_mv+ {% end %}+ {% if defined(customerId) %}+ PREWHERE click_id IN (+ SELECT DISTINCT click_id+ FROM dub_lead_events_mv+ WHERE customer_id = {{ String(customerId) }}+ )+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links)+ {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND url != ''+ {% 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 %}+ GROUP BY utm+ HAVING utm != ''+ ORDER BY clicks DESC++++NODE utms_leads+SQL >++ %+ WITH+ decodeURLFormComponent(+ extractURLParameter(+ url,+ {{+ String(+ groupByUtmTag,+ 'utm_source',+ description="The UTM tag to retrieve data for",+ required=True,+ )+ }}+ )+ ) as utm+ SELECT utm, count(*) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 %}+ GROUP BY utm+ HAVING utm != ''+ ORDER BY leads DESC++++NODE utms_sales+SQL >++ %+ WITH+ decodeURLFormComponent(+ extractURLParameter(+ url,+ {{+ String(+ groupByUtmTag,+ 'utm_source',+ description="The UTM tag to retrieve data for",+ required=True,+ )+ }}+ )+ ) as utm+ SELECT utm, count(*) as sales, sum(amount) as saleAmount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 %}+ GROUP BY utm+ HAVING utm != ''+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 saleAmount 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 {% if defined(saleType) %} utms_sales_with_type {% else %} utms_sales {% end %}+ ) AS dse+ ON dce.utm = dse.utm+ ORDER BY clicks DESC++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ {% if eventType == 'clicks' %} utms_clicks+ {% elif eventType == 'leads' %} utms_leads+ {% elif eventType == 'sales' %}+ {% if defined(saleType) %} utms_sales_with_type {% else %} utms_sales {% end %}+ {% else %} utms_composite+ {% end %}++TYPE endpoint\ No newline at end of filediff --git a/packages/tinybird/materializations/dub_click_events_id_pipe.pipe b/packages/tinybird/materializations/dub_click_events_id_pipe.pipenew file mode 100644index 0000000000..e59e06ab0d--- /dev/null+++ b/packages/tinybird/materializations/dub_click_events_id_pipe.pipe@@ -0,0 +1,42 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT+ timestamp,+ click_id,+ link_id,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ device_model,+ device_vendor,+ browser,+ browser_version,+ os,+ os_version,+ engine,+ engine_version,+ cpu_architecture,+ ua,+ bot,+ referer,+ referer_url,+ identity_hash,+ ip,+ qr,+ CASE+ WHEN trigger = '' THEN CASE WHEN qr = true THEN 'qr' ELSE 'link' END ELSE trigger+ END as trigger+ FROM dub_click_events++TYPE materialized+DATASOURCE dub_click_events_id++diff --git a/packages/tinybird/materializations/dub_click_events_pipe.pipe b/packages/tinybird/materializations/dub_click_events_pipe.pipenew file mode 100644index 0000000000..ccd4e4e972--- /dev/null+++ b/packages/tinybird/materializations/dub_click_events_pipe.pipe@@ -0,0 +1,40 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT+ timestamp,+ click_id,+ link_id,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ browser,+ os,+ CASE+ WHEN trigger = '' THEN+ CASE+ WHEN qr = true THEN 'qr'+ ELSE 'link'+ END+ ELSE trigger+ END as trigger,+ engine,+ ua,+ coalesce(identity_hash, '') as identity_hash,+ referer,+ referer_url,+ ip,+ qr+ FROM dub_click_events++TYPE materialized+DATASOURCE dub_click_events_mv++diff --git a/packages/tinybird/materializations/dub_lead_events_pipe.pipe b/packages/tinybird/materializations/dub_lead_events_pipe.pipenew file mode 100644index 0000000000..b40cfdbc6f--- /dev/null+++ b/packages/tinybird/materializations/dub_lead_events_pipe.pipe@@ -0,0 +1,35 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT+ timestamp,+ event_id,+ event_name,+ click_id,+ link_id,+ customer_id,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ browser,+ os,+ engine,+ ua,+ referer,+ referer_url,+ qr,+ ip,+ metadata+ FROM dub_lead_events++TYPE materialized+DATASOURCE dub_lead_events_mv++diff --git a/packages/tinybird/materializations/dub_links_metadata_pipe.pipe b/packages/tinybird/materializations/dub_links_metadata_pipe.pipenew file mode 100644index 0000000000..96cea42280--- /dev/null+++ b/packages/tinybird/materializations/dub_links_metadata_pipe.pipe@@ -0,0 +1,11 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT * FROM dub_links_metadata++TYPE materialized+DATASOURCE dub_links_metadata_latest++diff --git a/packages/tinybird/materializations/dub_regular_links_metadata_pipe.pipe b/packages/tinybird/materializations/dub_regular_links_metadata_pipe.pipenew file mode 100644index 0000000000..c034ca2046--- /dev/null+++ b/packages/tinybird/materializations/dub_regular_links_metadata_pipe.pipe@@ -0,0 +1,19 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT *+ FROM dub_links_metadata+ WHERE+ workspace_id not in [+ 'cm05wnnpo000711ztj05wwdbu', -- buffer+ 'ws_1JYEZKSFX7RMB5M9XP4G0CCAT', -- jobber+ 'clawqph22020408jng5s9b2tp', -- cal+ 'ws_1JQ7175XGRN2ZYE2QAPVA4A26' -- homeserve+ ]++TYPE materialized+DATASOURCE dub_regular_links_metadata_latest++diff --git a/packages/tinybird/materializations/dub_sale_events_id_pipe.pipe b/packages/tinybird/materializations/dub_sale_events_id_pipe.pipenew file mode 100644index 0000000000..f143560596--- /dev/null+++ b/packages/tinybird/materializations/dub_sale_events_id_pipe.pipe@@ -0,0 +1,11 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT * FROM dub_sale_events++TYPE materialized+DATASOURCE dub_sale_events_id++diff --git a/packages/tinybird/materializations/dub_sale_events_pipe.pipe b/packages/tinybird/materializations/dub_sale_events_pipe.pipenew file mode 100644index 0000000000..0526f5a090--- /dev/null+++ b/packages/tinybird/materializations/dub_sale_events_pipe.pipe@@ -0,0 +1,38 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT+ timestamp,+ event_id,+ event_name,+ click_id,+ link_id,+ customer_id,+ payment_processor,+ invoice_id,+ amount,+ url,+ continent,+ country,+ city,+ region,+ latitude,+ longitude,+ device,+ browser,+ os,+ engine,+ ua,+ referer,+ referer_url,+ qr,+ ip,+ metadata+ FROM dub_sale_events++TYPE materialized+DATASOURCE dub_sale_events_mv++diff --git a/packages/tinybird/pipes/all_stats.pipe b/packages/tinybird/pipes/all_stats.pipenew file mode 100644index 0000000000..c545951ce1--- /dev/null+++ b/packages/tinybird/pipes/all_stats.pipe@@ -0,0 +1,11 @@+TAGS "Dub Misc Endpoints"++NODE endpoint+SQL >++ SELECT+ (SELECT COUNT(timestamp) FROM dub_click_events_mv) AS clicks,+ (SELECT COUNT(timestamp) + 42036155 FROM dub_links_metadata_latest FINAL) AS links,+ (SELECT SUM(amount) FROM dub_sale_events_mv) AS sales++diff --git a/packages/tinybird/pipes/coordinates_all.pipe b/packages/tinybird/pipes/coordinates_all.pipenew file mode 100644index 0000000000..a0c752c734--- /dev/null+++ b/packages/tinybird/pipes/coordinates_all.pipe@@ -0,0 +1,96 @@+TAGS "Dub Misc Endpoints"++NODE coordinates_clicks_data+SQL >++ %+ SELECT+ 'click' AS event,+ timestamp,+ country,+ city,+ latitude,+ longitude,+ device+ FROM dub_click_events_mv+ WHERE+ timestamp > now() - INTERVAL 5 MINUTE+ AND country != 'Unknown' AND city != 'Unknown' AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 100++++NODE coordinates_leads_data+SQL >++ %+ SELECT+ 'lead' AS event,+ timestamp,+ country,+ city,+ latitude,+ longitude,+ device+ FROM dub_lead_events_mv+ WHERE+ timestamp > now() - INTERVAL 12 HOUR+ AND country != 'Unknown'+ AND city != 'Unknown'+ AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 100++++NODE coordinates_sales_data+SQL >++ %+ SELECT+ 'sale' AS event,+ timestamp,+ country,+ city,+ latitude,+ longitude,+ device,+ amount,+ round(+ amount * arrayElement([0.3, 0.4, 0.5], 1 + (toUnixTimestamp(timestamp) % 3))+ ) as commission+ FROM dub_sale_events_mv+ WHERE+ timestamp > now() - INTERVAL 12 HOUR+ AND country != 'Unknown'+ AND city != 'Unknown'+ AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 100++++NODE endpoint+SQL >++ %+ SELECT *+ FROM+ (+ SELECT *, NULL AS amount, NULL AS commission+ FROM coordinates_leads_data+ UNION ALL+ SELECT+ *,+ NULL AS amount,+ NULL AS commission+ FROM coordinates_clicks_data+ UNION ALL+ SELECT *+ FROM coordinates_sales_data+ )+ ORDER BY+ timestamp DESC++diff --git a/packages/tinybird/pipes/coordinates_sales.pipe b/packages/tinybird/pipes/coordinates_sales.pipenew file mode 100644index 0000000000..d79c2d70a4--- /dev/null+++ b/packages/tinybird/pipes/coordinates_sales.pipe@@ -0,0 +1,26 @@+TAGS "Dub Misc Endpoints"++NODE endpoint+SQL >++ %+ SELECT+ timestamp,+ amount,+ round(+ amount * arrayElement([0.3, 0.4, 0.5], 1 + (toUnixTimestamp(timestamp) % 3))+ ) as commission,+ country,+ city,+ latitude,+ longitude+ FROM dub_sale_events_mv+ WHERE+ timestamp > now() - INTERVAL 12 HOUR+ AND country != 'Unknown'+ AND city != 'Unknown'+ AND city != 'Ashburn'+ ORDER BY timestamp DESC+ LIMIT 500++diff --git a/packages/tinybird/pipes/dub_click_events_pipe_with_domain_key.pipe b/packages/tinybird/pipes/dub_click_events_pipe_with_domain_key.pipenew file mode 100644index 0000000000..0f46b94427--- /dev/null+++ b/packages/tinybird/pipes/dub_click_events_pipe_with_domain_key.pipe@@ -0,0 +1,37 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT+ dce.timestamp,+ dce.click_id,+ dce.link_id,+ coalesce(dlm.domain, '') as domain,+ coalesce(dlm.key, '') as key,+ dce.url,+ dce.continent,+ dce.country,+ dce.city,+ dce.region,+ dce.latitude,+ dce.longitude,+ dce.device,+ dce.browser,+ dce.os,+ CASE+ WHEN dce.trigger = ''+ THEN CASE WHEN dce.qr = true THEN 'qr' ELSE 'link' END+ ELSE dce.trigger+ END as trigger,+ dce.engine,+ dce.ua,+ coalesce(dce.identity_hash, '') as identity_hash,+ dce.referer,+ dce.referer_url,+ dce.ip,+ dce.qr+ FROM dub_click_events dce+ LEFT JOIN (SELECT * FROM dub_links_metadata_latest FINAL) dlm ON dce.link_id = dlm.link_id++diff --git a/packages/tinybird/pipes/dub_regular_links_metadata_pipe.pipe b/packages/tinybird/pipes/dub_regular_links_metadata_pipe.pipeindex 06d69fadf7..c034ca2046 100644--- a/packages/tinybird/pipes/dub_regular_links_metadata_pipe.pipe+++ b/packages/tinybird/pipes/dub_regular_links_metadata_pipe.pipe@@ -3,7 +3,15 @@ TAGS "Dub MV Pipes"NODE mvSQL >- SELECT * FROM dub_links_metadata WHERE domain not in ['buff.ly', 'sms.cal.com']+ SELECT *+ FROM dub_links_metadata+ WHERE+ workspace_id not in [+ 'cm05wnnpo000711ztj05wwdbu', -- buffer+ 'ws_1JYEZKSFX7RMB5M9XP4G0CCAT', -- jobber+ 'clawqph22020408jng5s9b2tp', -- cal+ 'ws_1JQ7175XGRN2ZYE2QAPVA4A26' -- homeserve+ ]TYPE materializedDATASOURCE dub_regular_links_metadata_latestdiff --git a/packages/tinybird/pipes/dub_sale_events_id_pipe.pipe b/packages/tinybird/pipes/dub_sale_events_id_pipe.pipenew file mode 100644index 0000000000..f143560596--- /dev/null+++ b/packages/tinybird/pipes/dub_sale_events_id_pipe.pipe@@ -0,0 +1,11 @@+TAGS "Dub MV Pipes"++NODE mv+SQL >++ SELECT * FROM dub_sale_events++TYPE materialized+DATASOURCE dub_sale_events_id++diff --git a/packages/tinybird/pipes/get_framer_lead_events.pipe b/packages/tinybird/pipes/get_framer_lead_events.pipenew file mode 100644index 0000000000..e7e1ed8427--- /dev/null+++ b/packages/tinybird/pipes/get_framer_lead_events.pipe@@ -0,0 +1,11 @@+NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events_mv+ WHERE+ link_id IN {{ Array(linkIds, 'String', ['link_1JWRSXGRTN95H1YCKTC5BM41B','link_1JWQHXN0Y1QBR7X07YQ6MHWTZ']) }}+ AND customer_id IN {{ Array(customerIds, 'String', ['cus_1JWTHSGTT67WY9NK98QSFJ3M4','cus_1JWTQS8S008Q3VTB8ZRG7AE3W']) }}++diff --git a/packages/tinybird/pipes/get_lead_event_by_id.pipe b/packages/tinybird/pipes/get_lead_event_by_id.pipenew file mode 100644index 0000000000..f1bf522d9e--- /dev/null+++ b/packages/tinybird/pipes/get_lead_event_by_id.pipe@@ -0,0 +1,23 @@+DESCRIPTION >+ internal pipe for updating lead event+++NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events+ WHERE+ event_id+ = {{+ String(+ eventId,+ "nLvob3FdmsYuI1BI",+ description="The unique event ID.",+ required=True,+ )+ }}+ ORDER BY timestamp DESC++diff --git a/packages/tinybird/pipes/get_lead_events.pipe b/packages/tinybird/pipes/get_lead_events.pipenew file mode 100644index 0000000000..0502e950b0--- /dev/null+++ b/packages/tinybird/pipes/get_lead_events.pipe@@ -0,0 +1,12 @@+NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_lead_events+ WHERE+ true+ {% if defined(customerIds) %} AND customer_id IN {{ Array(customerIds, 'String') }} {% end %}+ ORDER BY timestamp DESC++diff --git a/packages/tinybird/pipes/get_sale_event.pipe b/packages/tinybird/pipes/get_sale_event.pipenew file mode 100644index 0000000000..8e53e249b9--- /dev/null+++ b/packages/tinybird/pipes/get_sale_event.pipe@@ -0,0 +1,18 @@+NODE endpoint+SQL >++ %+ SELECT *+ FROM dub_sale_events_id+ WHERE+ event_id+ = {{+ String(+ eventId,+ "O9Q8fKbfY809HXBp",+ description="The unique event ID.",+ required=True,+ )+ }}++diff --git a/packages/tinybird/pipes/v2_browsers.pipe b/packages/tinybird/pipes/v2_browsers.pipeindex 2b01bd9b66..362f18af4e 100644--- a/packages/tinybird/pipes/v2_browsers.pipe+++ b/packages/tinybird/pipes/v2_browsers.pipe@@ -47,13 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -96,12 +98,14 @@ SQL >SELECT browser, COUNT(browser) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -113,11 +117,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -130,43 +145,60 @@ NODE browsers_salesSQL >%- SELECT- browser,- sales,- amount,- amount AS saleAmount- FROM (- SELECT browser, COUNT(*) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY browser- ORDER BY amount DESC- ) as subquery+ SELECT browser, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT browser, COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'+ {% 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 %}+ GROUP BY browser+ ORDER BY amount DESC+ ) as subquery@@ -174,56 +206,67 @@ NODE browsers_sales_with_typeSQL >%- 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 (+ WITH+ sales AS (+ SELECT browser, customer_id, link_id, timestamp, amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND browser != 'Unknown'+ {% 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 amountFROM salesINNER JOIN min_timestamps USING (customer_id, link_id)@@ -240,7 +283,6 @@ SQL >-NODE browsers_compositeSQL >diff --git a/packages/tinybird/pipes/v2_cities.pipe b/packages/tinybird/pipes/v2_cities.pipeindex 39db55dace..d74d974605 100644--- a/packages/tinybird/pipes/v2_cities.pipe+++ b/packages/tinybird/pipes/v2_cities.pipe@@ -36,11 +36,7 @@ NODE cities_clicksSQL >%- SELECT- city,- CONCAT(country, '-', region) as region,- country,- COUNT(city) as clicks+ SELECT city, CONCAT(country, '-', region) as region, country, COUNT(city) as clicksFROM{% if defined(customerId) %} dub_click_events_id{% else %} dub_click_events_mv@@ -51,13 +47,37 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)+ {% elif not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -69,11 +89,22 @@ SQL >{% 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(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 %}@@ -87,20 +118,39 @@ NODE cities_leadsSQL >%- SELECT- city,- CONCAT(country, '-', region) as region,- country,- COUNT(city) as leads+ SELECT city, CONCAT(country, '-', region) as region, country, COUNT(city) as leadsFROMdub_lead_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -141,47 +191,83 @@ NODE cities_salesSQL >%- 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- 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 %}- GROUP BY city, region, country- ORDER BY amount DESC- LIMIT 1000- ) as subquery+ 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+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY city, region, country+ ORDER BY amount DESC+ LIMIT 1000+ ) as subquery@@ -194,11 +280,15 @@ SQL >SELECT city, region, country, customer_id, link_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHERE- city != 'Unknown'+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND city != 'Unknown'AND region NOT IN ('Unknown','arn1',@@ -220,7 +310,6 @@ SQL >'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 %}diff --git a/packages/tinybird/pipes/v2_continents.pipe b/packages/tinybird/pipes/v2_continents.pipeindex a922bd6075..a9cb129485 100644--- a/packages/tinybird/pipes/v2_continents.pipe+++ b/packages/tinybird/pipes/v2_continents.pipe@@ -47,22 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -105,21 +98,14 @@ SQL >SELECT continent, COUNT(continent) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -131,11 +117,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -148,52 +145,60 @@ NODE continents_salesSQL >%- SELECT- continent,- sales,- amount,- amount AS saleAmount- FROM (- SELECT continent, COUNT(continent) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY continent- ORDER BY amount DESC- ) as subquery+ SELECT continent, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT continent, COUNT(continent) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''+ {% 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 %}+ GROUP BY continent+ ORDER BY amount DESC+ ) as subquery@@ -201,65 +206,67 @@ NODE continents_sales_with_typeSQL >%- 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 (+ WITH+ sales AS (+ SELECT continent, customer_id, link_id, timestamp, amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND continent != ''+ {% 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 amountFROM salesINNER JOIN min_timestamps USING (customer_id, link_id)diff --git a/packages/tinybird/pipes/v2_count.pipe b/packages/tinybird/pipes/v2_count.pipeindex b96d2830ef..86db1a0a45 100644--- a/packages/tinybird/pipes/v2_count.pipe+++ b/packages/tinybird/pipes/v2_count.pipe@@ -47,13 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -94,12 +96,15 @@ SQL >SELECT COUNT(*) as leadsFROMdub_lead_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -119,6 +124,29 @@ SQL >{% if defined(url) %} AND url = {{ 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}@@ -132,12 +160,14 @@ SQL >SELECT COUNT(*) as sales, sum(amount) as amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -175,6 +205,29 @@ SQL >{% if defined(url) %} AND url = {{ 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}) AS subquery@@ -188,12 +241,14 @@ SQL >SELECT customer_id, link_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -231,19 +286,36 @@ SQL >{% if defined(url) %} AND url = {{ 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}),- distinct_sales AS (- SELECT DISTINCT customer_id, link_id- FROM sales- ),+ 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 customer_id, link_id, min(timestamp) AS first_sale_ts+ FROM dub_sale_events_mv+ WHERE (customer_id, link_id) IN distinct_sales+ GROUP BY customer_id, link_id)SELECT sales, amount, amount AS saleAmountFROMdiff --git a/packages/tinybird/pipes/v2_countries.pipe b/packages/tinybird/pipes/v2_countries.pipeindex b9fcceff45..aca78d3cf0 100644--- a/packages/tinybird/pipes/v2_countries.pipe+++ b/packages/tinybird/pipes/v2_countries.pipe@@ -47,21 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- country != 'Unknown'- {% if defined(linkId) %}- AND link_id- = {{- String(- linkId,- 'clsdzhhlq0003vskwcaz6nm39',- description="The ID of the link",- )- }}- {% end %}+ {% elif not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND country != 'Unknown'{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -93,21 +87,14 @@ SQL >SELECT country, COUNT(country) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND country != 'Unknown'{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -119,11 +106,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -136,52 +134,60 @@ NODE countries_salesSQL >%- SELECT- country,- sales,- amount,- amount AS saleAmount- FROM (- SELECT country, COUNT(country) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY country- ORDER BY amount DESC- ) as subquery+ SELECT country, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT country, COUNT(country) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND country != 'Unknown'+ {% 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 %}+ GROUP BY country+ ORDER BY amount DESC+ ) as subquery@@ -189,65 +195,67 @@ NODE countries_sales_with_typeSQL >%- 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 (+ WITH+ sales AS (+ SELECT country, customer_id, link_id, timestamp, amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND country != 'Unknown'+ {% 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 amountFROM salesINNER JOIN min_timestamps USING (customer_id, link_id)diff --git a/packages/tinybird/pipes/v2_devices.pipe b/packages/tinybird/pipes/v2_devices.pipeindex 8aa7a640a7..078650166d 100644--- a/packages/tinybird/pipes/v2_devices.pipe+++ b/packages/tinybird/pipes/v2_devices.pipe@@ -47,13 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -96,12 +98,14 @@ SQL >SELECT device, COUNT(device) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -113,11 +117,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -130,43 +145,60 @@ NODE devices_salesSQL >%- SELECT- device,- sales,- amount,- amount AS saleAmount- FROM (- SELECT device, COUNT(device) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY device- ORDER BY amount DESC- ) subquery+ SELECT device, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT device, COUNT(device) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'+ {% 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 %}+ GROUP BY device+ ORDER BY amount DESC+ ) subquery@@ -179,12 +211,14 @@ SQL >SELECT device, customer_id, link_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND device != 'Unknown'{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}diff --git a/packages/tinybird/pipes/v2_events.pipe b/packages/tinybird/pipes/v2_events.pipeindex 0ff64bcb57..25fc13fbdf 100644--- a/packages/tinybird/pipes/v2_events.pipe+++ b/packages/tinybird/pipes/v2_events.pipe@@ -51,6 +51,7 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})+ {% elif defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}{% elif defined(linkId) %} AND link_id = {{ String(linkId) }}{% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}AND link_id IN (SELECT link_id FROM workspace_links)@@ -102,7 +103,8 @@ SQL >WHEREtimestamp >= {{ DateTime(start, '2024-01-01 00:00:00') }}AND timestamp < {{ DateTime(end, '2025-12-31 00:00:00') }}- {% if defined(linkId) %} AND link_id = {{ String(linkId) }}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}{% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}AND link_id IN (SELECT link_id FROM workspace_links){% end %}@@ -158,7 +160,7 @@ SQL >{% end %}{% end %}{% end %}-+ORDER BY timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}LIMIT {{ Int32(limit, 100) }}{% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}@@ -178,8 +180,9 @@ SQL >FROM dub_sale_events_mvWHEREtimestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}- AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }}- {% if defined(linkId) %} AND link_id = {{ String(linkId) }}+ AND timestamp < {{ DateTime(end, '2025-12-31 00:00:00') }}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}{% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}AND link_id IN (SELECT link_id FROM workspace_links){% end %}@@ -258,7 +261,8 @@ SQL >WHEREtimestamp >= {{ DateTime(start, '2024-06-01 00:00:00') }}AND timestamp < {{ DateTime(end, '2024-06-07 00:00:00') }}- {% if defined(linkId) %} AND link_id = {{ String(linkId) }}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ String(linkId) }}{% elif defined(workspaceId) or defined(partnerId) or defined(programId) %}AND link_id IN (SELECT link_id FROM workspace_links){% end %}@@ -297,6 +301,31 @@ SQL >LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%'){% end %}{% if defined(url) %} AND url = {{ url }} {% 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 }}+ {% end %}+ {% end %}+ {% end %}+ {% end %}),distinct_sales AS (SELECT DISTINCT customer_id, link_id FROM sales),min_timestamps AS (diff --git a/packages/tinybird/pipes/v2_os.pipe b/packages/tinybird/pipes/v2_os.pipeindex e92860a131..53958f915e 100644--- a/packages/tinybird/pipes/v2_os.pipe+++ b/packages/tinybird/pipes/v2_os.pipe@@ -47,13 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -96,12 +98,14 @@ SQL >SELECT os, COUNT(os) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -113,11 +117,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -130,43 +145,60 @@ NODE os_salesSQL >%- SELECT- os,- sales,- amount,- amount AS saleAmount- FROM (- SELECT os, COUNT(os) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY os- ORDER BY amount DESC- ) as subquery+ SELECT os, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT os, COUNT(os) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'+ {% 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 %}+ GROUP BY os+ ORDER BY amount DESC+ ) as subquery@@ -174,56 +206,67 @@ NODE os_sales_with_typeSQL >%- 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 (+ WITH+ sales AS (+ SELECT os, customer_id, link_id, timestamp, amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND os != 'Unknown'+ {% 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 amountFROM salesINNER JOIN min_timestamps USING (customer_id, link_id)diff --git a/packages/tinybird/pipes/v2_referer_urls.pipe b/packages/tinybird/pipes/v2_referer_urls.pipeindex 3aab16f98f..f06e218fd7 100644--- a/packages/tinybird/pipes/v2_referer_urls.pipe+++ b/packages/tinybird/pipes/v2_referer_urls.pipe@@ -36,17 +36,17 @@ NODE referer_urls_clicksSQL >%- SELECT- splitByString('?', referer_url)[1] as refererUrl,- COUNT(*) as clicks+ SELECT splitByString('?', referer_url)[1] as refererUrl, COUNT(*) as clicksFROMdub_click_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -58,11 +58,22 @@ SQL >{% 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(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}@@ -79,12 +90,14 @@ SQL >SELECT splitByString('?', referer_url)[1] as refererUrl, COUNT(referer_url) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -96,11 +109,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -114,44 +138,64 @@ NODE referer_urls_salesSQL >%- SELECT- refererUrl,- sales,- amount,- amount AS saleAmount- FROM (- SELECT splitByString('?', referer_url)[1] as refererUrl, COUNT(referer_url) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY splitByString('?', referer_url)[1]- ORDER BY amount DESC- LIMIT 1000- ) as subquery+ SELECT refererUrl, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT+ splitByString('?', referer_url)[1] as refererUrl,+ COUNT(referer_url) as sales,+ sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'+ {% 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 %}+ GROUP BY splitByString('?', referer_url)[1]+ ORDER BY amount DESC+ LIMIT 1000+ ) as subquery@@ -159,58 +203,72 @@ NODE referer_urls_sales_with_typeSQL >%- 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 (+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND referer != 'Unknown'+ {% 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 amountFROM salesINNER JOIN min_timestamps USING (customer_id, link_id)diff --git a/packages/tinybird/pipes/v2_referers.pipe b/packages/tinybird/pipes/v2_referers.pipeindex 60b17b6560..b58c0c5851 100644--- a/packages/tinybird/pipes/v2_referers.pipe+++ b/packages/tinybird/pipes/v2_referers.pipe@@ -47,13 +47,14 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)- ) %} PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -97,12 +98,14 @@ SQL >SELECT referer, COUNT(referer) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -132,44 +135,61 @@ NODE referers_salesSQL >%- SELECT- referer,- sales,- amount,- amount AS saleAmount- FROM (- SELECT referer, COUNT(referer) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- true- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}- {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}- {% if defined(continent) %} AND continent = {{ continent }} {% end %}- {% if defined(country) %} AND country = {{ country }} {% end %}- {% if defined(region) %} AND region = {{ region }} {% end %}- {% if defined(city) %} AND city = {{ city }} {% end %}- {% if defined(device) %} AND device = {{ device }} {% end %}- {% if defined(browser) %} AND browser = {{ browser }} {% end %}- {% if defined(os) %} AND os = {{ os }} {% end %}- {% if defined(referer) %} AND referer = {{ referer }} {% end %}- {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}- {% if defined(url) %} AND url = {{ url }} {% end %}- {% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}- {% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}- GROUP BY referer- ORDER BY amount DESC- LIMIT 1000- ) as subquery+ SELECT referer, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT referer, COUNT(referer) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY referer+ ORDER BY amount DESC+ LIMIT 1000+ ) as subquery@@ -182,12 +202,14 @@ SQL >SELECT referer, customer_id, link_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}diff --git a/packages/tinybird/pipes/v2_regions.pipe b/packages/tinybird/pipes/v2_regions.pipeindex b687454ca5..dca68494a0 100644--- a/packages/tinybird/pipes/v2_regions.pipe+++ b/packages/tinybird/pipes/v2_regions.pipe@@ -36,44 +36,83 @@ NODE regions_clicksSQL >%- SELECT- CONCAT(country, '-', region) as region,- country,- clicks,- FROM (- SELECT region, country, COUNT(region) as clicks- FROM- dub_click_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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(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(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 url = {{ url }} {% end %}- {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}- {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}- GROUP BY region, country- ORDER BY clicks DESC- LIMIT 1000- ) as subquery+ SELECT CONCAT(country, '-', region) as region, country, clicks,+ FROM+ (+ SELECT region, country, COUNT(region) as clicks+ FROM+ dub_click_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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(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 url = {{ url }} {% end %}+ {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}+ {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}+ GROUP BY region, country+ ORDER BY clicks DESC+ LIMIT 1000+ ) as subquery@@ -81,44 +120,83 @@ NODE regions_leadsSQL >%- SELECT- CONCAT(country, '-', region) as region,- country,- leads,- FROM (- SELECT region, country, COUNT(region) as leads- FROM- dub_lead_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 %}- GROUP BY region, country- ORDER BY leads DESC- LIMIT 1000- ) as subquery+ SELECT CONCAT(country, '-', region) as region, country, leads,+ FROM+ (+ SELECT region, country, COUNT(region) as leads+ FROM+ dub_lead_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY region, country+ ORDER BY leads DESC+ LIMIT 1000+ ) as subquery@@ -126,46 +204,83 @@ NODE regions_salesSQL >%- SELECT- CONCAT(country, '-', region) as region,- country,- sales,- amount,- amount AS saleAmount- FROM (- SELECT region, country, COUNT(region) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY region, country- ORDER BY amount DESC- LIMIT 1000- ) as subquery+ SELECT CONCAT(country, '-', region) as region, country, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT region, country, COUNT(region) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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 %}+ GROUP BY region, country+ ORDER BY amount DESC+ LIMIT 1000+ ) as subquery@@ -173,58 +288,89 @@ NODE regions_sales_with_typeSQL >%- 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 (+ WITH+ sales AS (+ SELECT region, country, customer_id, link_id, timestamp, amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND 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(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(region) as sales, sum(amount) as amountFROM salesINNER JOIN min_timestamps USING (customer_id, link_id)@@ -241,7 +387,6 @@ SQL >-NODE regions_compositeSQL >diff --git a/packages/tinybird/pipes/v2_timeseries.pipe b/packages/tinybird/pipes/v2_timeseries.pipeindex 4f9bcfa939..b8b971dec5 100644--- a/packages/tinybird/pipes/v2_timeseries.pipe+++ b/packages/tinybird/pipes/v2_timeseries.pipe@@ -130,13 +130,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -204,12 +206,15 @@ SQL >uniq(*) as leadsFROMdub_lead_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -270,12 +275,15 @@ SQL >sum(amount) as amountFROMdub_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)+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -322,12 +330,14 @@ SQL >amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}diff --git a/packages/tinybird/pipes/v2_top_links.pipe b/packages/tinybird/pipes/v2_top_links.pipeindex 7684f5eccf..ee90240303 100644--- a/packages/tinybird/pipes/v2_top_links.pipe+++ b/packages/tinybird/pipes/v2_top_links.pipe@@ -1,5 +1,5 @@DESCRIPTION >- Top countries+ Top links endpoint (most updated version with linkIds filter support)TAGS "Dub Endpoints"@@ -47,13 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -97,12 +99,15 @@ SQL >SELECT link_id as link, COUNT(*) AS leadsFROMdub_lead_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -132,44 +137,61 @@ NODE top_links_salesSQL >%- SELECT- link,- sales,- amount,- amount AS saleAmount- FROM (- SELECT link_id as link, COUNT(*) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- true- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}- {% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}- {% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}- {% if defined(continent) %} AND continent = {{ continent }} {% end %}- {% if defined(country) %} AND country = {{ country }} {% end %}- {% if defined(region) %} AND region = {{ region }} {% end %}- {% if defined(city) %} AND city = {{ city }} {% end %}- {% if defined(device) %} AND device = {{ device }} {% end %}- {% if defined(browser) %} AND browser = {{ browser }} {% end %}- {% if defined(os) %} AND os = {{ os }} {% end %}- {% if defined(referer) %} AND referer = {{ referer }} {% end %}- {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}- {% if defined(url) %} AND url = {{ url }} {% end %}- {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}- {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}- GROUP BY link_id- ORDER BY amount DESC- LIMIT 1000- ) as subquery+ SELECT link, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT link_id as link, COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY link_id+ ORDER BY amount DESC+ LIMIT 1000+ ) as subquery@@ -182,12 +204,14 @@ SQL >SELECT link_id, customer_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}diff --git a/packages/tinybird/pipes/v2_top_urls.pipe b/packages/tinybird/pipes/v2_top_urls.pipeindex e6719ad35f..2ce0f69818 100644--- a/packages/tinybird/pipes/v2_top_urls.pipe+++ b/packages/tinybird/pipes/v2_top_urls.pipe@@ -75,14 +75,16 @@ SQL >%SELECT url, COUNT(*) AS leads- FROM+ FROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -94,11 +96,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}@@ -112,44 +125,61 @@ NODE top_urls_salesSQL >%- SELECT- url,- sales,- amount,- amount AS saleAmount- FROM (- SELECT url, COUNT(*) as sales, sum(amount) as amount- FROM- dub_sale_events_mv- {% if not defined(linkId) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %}- PREWHERE link_id in (SELECT link_id from workspace_links)- {% end %}- WHERE- 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 %}- GROUP BY url- ORDER BY amount DESC- LIMIT 1000- ) as subquery+ SELECT url, sales, amount, amount AS saleAmount+ FROM+ (+ SELECT url, COUNT(*) as sales, sum(amount) as amount+ FROM+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 %}+ GROUP BY url+ ORDER BY amount DESC+ LIMIT 1000+ ) as subquery@@ -162,12 +192,14 @@ SQL >SELECT url, customer_id, link_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}diff --git a/packages/tinybird/pipes/v2_triggers.pipe b/packages/tinybird/pipes/v2_triggers.pipeindex 0187f2d6e6..3941e74424 100644--- a/packages/tinybird/pipes/v2_triggers.pipe+++ b/packages/tinybird/pipes/v2_triggers.pipe@@ -47,13 +47,15 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links){% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -96,12 +98,14 @@ SQL >SELECT CASE WHEN qr = 0 THEN 'link' WHEN qr = 1 THEN 'qr' END as trigger, COUNT(qr) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}@@ -113,11 +117,22 @@ SQL >{% if defined(os) %} AND os = {{ os }} {% end %}{% if defined(referer) %} AND referer = {{ referer }} {% end %}{% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}- {% if defined(utm_source) %} AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%') {% end %}- {% if defined(utm_medium) %} AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%') {% end %}- {% if defined(utm_campaign) %} AND url LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%') {% end %}- {% if defined(utm_term) %} AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%') {% end %}- {% if defined(utm_content) %} AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%') {% end %}+ {% if defined(utm_source) %}+ AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')+ {% end %}+ {% if defined(utm_medium) %}+ AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')+ {% end %}+ {% if defined(utm_campaign) %}+ AND url+ LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')+ {% end %}+ {% if defined(utm_term) %}+ AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')+ {% end %}+ {% if defined(utm_content) %}+ AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')+ {% end %}{% if defined(url) %} AND url = {{ url }} {% end %}{% if defined(start) %} AND timestamp >= {{ DateTime(start) }} {% end %}{% if defined(end) %} AND timestamp <= {{ DateTime(end) }} {% end %}@@ -130,46 +145,63 @@ NODE trigger_salesSQL >%- 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- 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 %}- GROUP BY qr- ORDER BY amount DESC- ) as subquery+ 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+ dub_sale_events_mv+ {% if not defined(linkId) and not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}+ GROUP BY qr+ ORDER BY amount DESC+ ) as subquery@@ -182,12 +214,14 @@ SQL >SELECT qr, customer_id, link_id, timestamp, amountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) and (defined(workspaceId) or defined(partnerId) or defined(programId)) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}WHEREtrue- {% if defined(linkId) %} AND link_id = {{ String(linkId) }} {% end %}+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ 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 %}diff --git a/packages/tinybird/pipes/v2_utms.pipe b/packages/tinybird/pipes/v2_utms.pipeindex f11ae800d4..b8d0fc40fb 100644--- a/packages/tinybird/pipes/v2_utms.pipe+++ b/packages/tinybird/pipes/v2_utms.pipe@@ -55,13 +55,16 @@ SQL >FROM dub_lead_events_mvWHERE customer_id = {{ String(customerId) }})- {% elif not defined(linkId) and (+ {% elif not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %}+ AND url != ''{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}{% if defined(country) %} AND country = {{ country }} {% end %}@@ -121,12 +124,14 @@ SQL >SELECT utm, count(*) as leadsFROMdub_lead_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 %}+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -187,12 +192,14 @@ SQL >SELECT utm, count(*) as sales, sum(amount) as saleAmountFROMdub_sale_events_mv- {% if not defined(linkId) and (+ {% if not defined(linkId) and not defined(linkIds) 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 %}+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''{% if defined(customerId) %} AND customer_id = {{ String(customerId) }} {% end %}{% if defined(qr) %} AND qr = {{ Boolean(qr) }} {% end %}{% if defined(continent) %} AND continent = {{ continent }} {% end %}@@ -236,91 +243,93 @@ NODE utms_sales_with_typeSQL >%- 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+ 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 not defined(linkIds) and (+ defined(workspaceId) or defined(partnerId) or defined(programId)+ ) %} PREWHERE link_id in (SELECT link_id from workspace_links) {% end %}+ WHERE+ true+ {% if defined(linkIds) %} AND link_id IN {{ Array(linkIds, 'String') }}+ {% elif defined(linkId) %} AND link_id = {{ linkId }}+ {% end %} AND url != ''+ {% 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 %}- GROUP BY utm- HAVING utm != ''- ORDER BY saleAmount DESC+ {% end %}+ GROUP BY utm+ HAVING utm != ''+ ORDER BY saleAmount DESC