Skip to content

fix(analytics): correct per-campaign unique view counting#3024

Open
wucm667 wants to merge 1 commit intoknadh:masterfrom
wucm667:fix/campaign-analytics-unique-counts
Open

fix(analytics): correct per-campaign unique view counting#3024
wucm667 wants to merge 1 commit intoknadh:masterfrom
wucm667:fix/campaign-analytics-unique-counts

Conversation

@wucm667
Copy link
Copy Markdown

@wucm667 wucm667 commented Apr 30, 2026

Fixes #3023

Bug

When privacy.individual_tracking is enabled, requesting analytics for
multiple campaigns at once via /api/campaigns/analytics/views?id=X&id=Y&id=Z
returns incorrect results. Campaigns are missing from the response if the
same subscriber has viewed more than one of the queried campaigns.

Root Cause

The get-campaign-analytics-unique-counts query in queries/campaigns.sql
uses DISTINCT ON(subscriber_id) which deduplicates across ALL campaigns
globally, instead of per-campaign. This means if subscriber A viewed both
campaign 1 and campaign 2, only the oldest event survives, and campaign 2
disappears from the results entirely.

Fix

Change DISTINCT ON(subscriber_id) to DISTINCT ON(subscriber_id, campaign_id)
and update ORDER BY to include campaign_id. This preserves one event per
subscriber per campaign, which is the correct behavior for unique tracking.

When individual_tracking is enabled, DISTINCT ON(subscriber_id)
deduplicated views globally across all campaigns, causing campaigns
to be missing from multi-campaign analytics results if the same
subscriber viewed more than one campaign.

Change to DISTINCT ON(subscriber_id, campaign_id) to preserve
one event per subscriber per campaign, which is the correct
behavior for unique tracking.

Fixes knadh#3023

Signed-off-by: wucm667 <stevenwucongmin@gmail.com>
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.

Analytics: unique view counts incorrect when querying multiple campaigns with individual_tracking enabled

1 participant