Skip to content

armgit / dub

Public Imported from GitHub
0

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

10.2 KB
Raw Blame
NODE workspace_links
SQL >

%
SELECT
    link_id,
    -- here we split tag_ids out of the multiIf because arrayJoin explodes rows
    if(
        {{ String(groupBy) }} = 'top_link_tags',
        arrayJoin(tag_ids),
        multiIf(
            {{ String(groupBy) }} = 'top_folders',
            folder_id,
            domain
        )
    ) AS groupByField
FROM dub_links_metadata_latest FINAL
WHERE
    workspace_id
    = {{
        String(
            workspaceId,
            'cl7pj5kq4006835rbjlt2ofka',
            description="The unique ID for the workspace",
            required=True,
        )
    }}
    AND deleted == 0
    AND multiIf(
        {{ String(groupBy) }} = 'top_folders',
        folder_id != '',
        {{ String(groupBy) }} = 'top_link_tags',
        length(tag_ids) > 0,
        domain != ''
    )
    {% if defined(tenantId) %} AND tenant_id = {{ tenantId }} {% end %}
    {% if defined(domain) %} AND domain IN {{ Array(domain, 'String') }} {% end %}
    {% if defined(folderIds) %} AND folder_id IN {{ Array(folderIds, 'String') }}
    {% elif defined(folderId) %} AND folder_id = {{ folderId }}
    {% 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 group_by_link_metadata_clicks
SQL >

%
SELECT wl.groupByField as groupByField, COUNT(*) AS clicks
FROM dub_click_events_mv AS ce
JOIN workspace_links AS wl ON ce.link_id = wl.link_id
WHERE
    workspace_id
    = {{
        String(
            workspaceId,
            'cl7pj5kq4006835rbjlt2ofka',
            description="The unique ID for the workspace",
            required=True,
        )
    }}
    {% if defined(linkIds) %} AND ce.link_id IN {{ Array(linkIds, 'String') }}
    {% elif defined(linkId) %} AND ce.link_id = {{ linkId }}
    {% end %}
    {% if defined(continent) %} AND continent = {{ continent }} {% end %}
    {% if defined(country) %} AND country = {{ country }} {% end %}
    {% if defined(region) %} AND region = {{ region }} {% end %}
    {% if defined(city) %} AND city = {{ city }} {% end %}
    {% if defined(device) %} AND device = {{ device }} {% end %}
    {% if defined(browser) %} AND browser = {{ browser }} {% end %}
    {% if defined(os) %} AND os = {{ os }} {% end %}
    {% if defined(trigger) %} AND trigger = {{ trigger }} {% end %}
    {% if defined(referer) %} AND referer = {{ referer }} {% end %}
    {% if defined(refererUrl) %} AND splitByString('?', referer_url)[1] = {{ refererUrl }} {% end %}
    {% if defined(utm_source) %}
        AND url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
    {% end %}
    {% if defined(utm_medium) %}
        AND url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
    {% end %}
    {% if defined(utm_campaign) %}
        AND url
        LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
    {% end %}
    {% if defined(utm_term) %}
        AND url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
    {% end %}
    {% if defined(utm_content) %}
        AND url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
    {% end %}
    {% if defined(url) %} AND splitByString('?', url)[1] = {{ url }} {% end %}
    {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}
    {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY wl.groupByField
ORDER BY clicks DESC
LIMIT 5000



NODE group_by_link_metadata_leads
SQL >

%
SELECT wl.groupByField as groupByField, COUNT(*) AS leads
FROM dub_lead_events_mv AS le
JOIN workspace_links AS wl ON le.link_id = wl.link_id
WHERE
    workspace_id
    = {{
        String(
            workspaceId,
            'cl7pj5kq4006835rbjlt2ofka',
            description="The unique ID for the workspace",
            required=True,
        )
    }}
    {% if defined(linkIds) %} AND le.link_id IN {{ Array(linkIds, 'String') }}
    {% elif defined(linkId) %} AND le.link_id = {{ linkId }}
    {% end %}
    {% 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 splitByString('?', url)[1] = {{ url }} {% end %}
    {% if defined(start) %} AND timestamp >= {{ DateTime64(start) }} {% end %}
    {% if defined(end) %} AND timestamp <= {{ DateTime64(end) }} {% end %}
GROUP BY wl.groupByField
ORDER BY leads DESC
LIMIT 5000



NODE first_sale_by_link_customer
SQL >

%
SELECT workspace_id, link_id, customer_id, minMerge(first_sale_state) AS first_sale_ts
FROM dub_first_sale_mv
WHERE
    workspace_id
    = {{
        String(
            workspaceId,
            'cl7pj5kq4006835rbjlt2ofka',
            description="The unique ID for the workspace",
            required=True,
        )
    }}
GROUP BY workspace_id, link_id, customer_id



NODE group_by_link_metadata_sales
SQL >

%
SELECT
    wl.groupByField AS groupByField,
    COUNT(*) AS sales,
    SUM(t.amount) AS saleAmount
FROM
(
    SELECT
        se.*,
        if(
            isNull(fs.first_sale_ts),
            'new',
            if(
                toUnixTimestamp64Milli(se.timestamp) = toUnixTimestamp64Milli(fs.first_sale_ts),
                'new',
                'recurring'
            )
        ) AS sale_type
    FROM dub_sale_events_mv AS se
    LEFT JOIN first_sale_by_link_customer AS fs
        USING (workspace_id, link_id, customer_id)
    WHERE
        workspace_id
        = {{
            String(
                workspaceId,
                'cl7pj5kq4006835rbjlt2ofka',
                description="The unique ID for the workspace",
                required=True,
            )
        }}
        {% if defined(linkIds) %} AND se.link_id IN {{ Array(linkIds, 'String') }}
        {% elif defined(linkId) %} AND se.link_id = {{ linkId }}
        {% end %}
        {% if defined(customerId) %} AND se.customer_id = {{ String(customerId) }} {% end %}
        {% if defined(continent) %} AND se.continent = {{ continent }} {% end %}
        {% if defined(country) %} AND se.country = {{ country }} {% end %}
        {% if defined(region) %} AND se.region = {{ region }} {% end %}
        {% if defined(city) %} AND se.city = {{ city }} {% end %}
        {% if defined(device) %} AND se.device = {{ device }} {% end %}
        {% if defined(browser) %} AND se.browser = {{ browser }} {% end %}
        {% if defined(os) %} AND se.os = {{ os }} {% end %}
        {% if defined(trigger) %} AND se.trigger = {{ trigger }} {% end %}
        {% if defined(referer) %} AND se.referer = {{ referer }} {% end %}
        {% if defined(refererUrl) %} AND splitByString('?', se.referer_url)[1] = {{ refererUrl }} {% end %}
        {% if defined(utm_source) %}
            AND se.url LIKE concat('%utm_source=', encodeURLFormComponent({{ String(utm_source) }}), '%')
        {% end %}
        {% if defined(utm_medium) %}
            AND se.url LIKE concat('%utm_medium=', encodeURLFormComponent({{ String(utm_medium) }}), '%')
        {% end %}
        {% if defined(utm_campaign) %}
            AND se.url
            LIKE concat('%utm_campaign=', encodeURLFormComponent({{ String(utm_campaign) }}), '%')
        {% end %}
        {% if defined(utm_term) %}
            AND se.url LIKE concat('%utm_term=', encodeURLFormComponent({{ String(utm_term) }}), '%')
        {% end %}
        {% if defined(utm_content) %}
            AND se.url LIKE concat('%utm_content=', encodeURLFormComponent({{ String(utm_content) }}), '%')
        {% end %}
        {% if defined(url) %} AND splitByString('?', se.url)[1] = {{ url }} {% end %}
        {% if defined(start) %} AND se.timestamp >= {{ DateTime64(start) }} {% end %}
        {% if defined(end) %} AND se.timestamp <= {{ DateTime64(end) }} {% end %}
) AS t
JOIN workspace_links AS wl
    ON t.link_id = wl.link_id
WHERE
    true
    {% if defined(saleType) %} AND t.sale_type = {{ String(saleType) }} {% end %}
GROUP BY wl.groupByField
ORDER BY saleAmount DESC
LIMIT 5000



NODE group_by_link_metadata_composite
SQL >

%
    SELECT c.groupByField as groupByField, c.clicks as clicks, l.leads as leads, s.sales as sales, s.saleAmount as saleAmount
    FROM group_by_link_metadata_clicks AS c
    LEFT JOIN group_by_link_metadata_leads AS l ON c.groupByField = l.groupByField
    LEFT JOIN group_by_link_metadata_sales AS s ON c.groupByField = s.groupByField
    ORDER BY saleAmount DESC



NODE endpoint
SQL >

%
SELECT *
FROM
    {% if eventType == 'clicks' %} group_by_link_metadata_clicks
    {% elif eventType == 'leads' %} group_by_link_metadata_leads
    {% elif eventType == 'sales' %}group_by_link_metadata_sales 
    {% elif eventType == 'composite' %}group_by_link_metadata_composite
    {% else %} group_by_link_metadata_clicks
    {% end %}

TYPE ENDPOINT