Skip to content

Fix analytics when combining folder scope with excluded links#3867

Open
pepeladeira wants to merge 3 commits into
mainfrom
analytics-folder-negative-link-filter
Open

Fix analytics when combining folder scope with excluded links#3867
pepeladeira wants to merge 3 commits into
mainfrom
analytics-folder-negative-link-filter

Conversation

@pepeladeira
Copy link
Copy Markdown
Collaborator

@pepeladeira pepeladeira commented May 6, 2026

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved consistency and accuracy of analytics result filtering when using multiple filter parameters together across all query types and aggregation modes.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
dub Ready Ready Preview May 11, 2026 1:51pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

📝 Walkthrough

Walkthrough

This PR implements workspace-scoped link ID filtering by normalizing workspace-link parameters in the analytics TypeScript layer, tightening the early-return condition for all-time aggregates, and propagating consistent workspace_links constraint logic across all Tinybird query pipes.

Changes

Workspace-Scoped Link ID Filtering

Layer / File(s) Summary
Data Normalization & Predicate
apps/web/lib/analytics/get-analytics.ts
Extract folderId, partnerId, partnerTagId from params and compute workspace-scoped dimension filters. Tighten all-time aggregates early-return to require exclusive linkId scope via workspaceScopeIsExclusiveLinkIdsIn predicate (checks groupBy === "count", interval === "all", no temporal bounds, no root/program/customer filters, no other dimensional filters).
Placeholder & Dimension Usage
apps/web/lib/analytics/get-analytics.ts
Use normalizedLinkId.values for SQL placeholders in early-return branch. Destructure workspaceLinkDims to populate dimension operators/values in tinybirdParams instead of reconstructing filters locally.
Link ID + Workspace Scoping
packages/tinybird/pipes/v4_count.pipe, packages/tinybird/pipes/v4_events.pipe, packages/tinybird/pipes/v4_group_by.pipe, packages/tinybird/pipes/v4_timeseries.pipe
Apply consistent constraint across all event queries (click_events, lead_events, sale_events, and their aggregates): when linkIdOperator is defined alongside any workspace-scoping parameter (programId, partnerId, groupId, partnerTagId, tenantId, folderId, domain, tagId, root), add AND link_id IN (SELECT link_id FROM workspace_links) in both NOT IN and IN branches to ensure results are restricted to the workspace link set.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

🐰 A rabbit bounds through filtered links,
Where workspace scopes stay crisp and tight,
Each count, event, roll—now linked just right,
Joins to workspace_links with SQL winks!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: enabling analytics to correctly handle the combination of folder scope filtering with excluded links.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch analytics-folder-negative-link-filter

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/tinybird/pipes/v4_group_by.pipe (1)

141-170: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add the linkId filtering fix to v4_group_by_link_metadata.pipe event nodes

The group_by_link_metadata_clicks, group_by_link_metadata_leads, and group_by_link_metadata_sales nodes have linkId filtering logic that should apply the same fix as v4_group_by.pipe. When combining scope filters (folderId, domain, partnerId, groupId, partnerTagId, tenantId, tagId, programId, root) with excluded linkId filtering (NOT IN), add AND link_id IN (SELECT link_id FROM workspace_links) to ensure correct data accuracy.

Example: clicks node
{% if defined(linkId) %}
    {% if defined(linkIdOperator) and String(linkIdOperator) == 'NOT IN' %}
        AND ce.link_id NOT IN {{ Array(linkId, 'String') }}
        {% if defined(programId) or defined(partnerId) or defined(groupId) or defined(partnerTagId) or defined(tenantId) or defined(folderId) or defined(domain) or defined(tagId) or defined(root) %}
            AND ce.link_id IN (SELECT link_id FROM workspace_links)
        {% end %}
    {% else %}
        AND ce.link_id IN {{ Array(linkId, 'String') }}
        {% if defined(programId) or defined(partnerId) or defined(groupId) or defined(partnerTagId) or defined(tenantId) or defined(folderId) or defined(domain) or defined(tagId) or defined(root) %}
            AND ce.link_id IN (SELECT link_id FROM workspace_links)
        {% end %}
    {% end %}
{% end %}

Apply the same pattern to the leads and sales nodes, adjusting the table alias (le.link_id and se.link_id respectively).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/tinybird/pipes/v4_group_by.pipe` around lines 141 - 170, The
clicks/leads/sales event nodes (group_by_link_metadata_clicks,
group_by_link_metadata_leads, group_by_link_metadata_sales) need the same linkId
filtering fix as v4_group_by.pipe: when linkId is defined and linkIdOperator ==
'NOT IN' and any scope filter (programId, partnerId, groupId, partnerTagId,
tenantId, folderId, domain, tagId, programId, root) is present, add "AND
<alias>.link_id IN (SELECT link_id FROM workspace_links)"; likewise add that
same IN-check when linkId is defined and not NOT IN; update the conditions
around linkId, linkIdOperator and the scope checks in each node replacing the
plain aliases (ce.link_id, le.link_id, se.link_id) accordingly so the NOT IN
branch and the IN branch both append the workspace_links IN clause when any
scope filter is present.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/tinybird/pipes/v4_group_by.pipe`:
- Around line 141-170: The clicks/leads/sales event nodes
(group_by_link_metadata_clicks, group_by_link_metadata_leads,
group_by_link_metadata_sales) need the same linkId filtering fix as
v4_group_by.pipe: when linkId is defined and linkIdOperator == 'NOT IN' and any
scope filter (programId, partnerId, groupId, partnerTagId, tenantId, folderId,
domain, tagId, programId, root) is present, add "AND <alias>.link_id IN (SELECT
link_id FROM workspace_links)"; likewise add that same IN-check when linkId is
defined and not NOT IN; update the conditions around linkId, linkIdOperator and
the scope checks in each node replacing the plain aliases (ce.link_id,
le.link_id, se.link_id) accordingly so the NOT IN branch and the IN branch both
append the workspace_links IN clause when any scope filter is present.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: add15672-91b3-46e9-ad33-f568426b1abb

📥 Commits

Reviewing files that changed from the base of the PR and between b1f9ca9 and 1c16722.

📒 Files selected for processing (5)
  • apps/web/lib/analytics/get-analytics.ts
  • packages/tinybird/pipes/v4_count.pipe
  • packages/tinybird/pipes/v4_events.pipe
  • packages/tinybird/pipes/v4_group_by.pipe
  • packages/tinybird/pipes/v4_timeseries.pipe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant