High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Fix analytics counts incorrectly summed up per list for campaigns. Closes #3308. Closes #3313.
Kailash Nadh85f78bb6c0f2
diff --git a/cmd/campaign_analytics.go b/cmd/campaign_analytics.goindex 5564997e..b7fd4313 100644--- a/cmd/campaign_analytics.go+++ b/cmd/campaign_analytics.go@@ -278,8 +278,12 @@ func (a *App) getAnalytics(c *echo.Context, resource string) error {return c.JSON(http.StatusOK, okResp{out})}-// sumAnalyticsCounts returns the total count across the given analytics rows.+// sumAnalyticsCounts returns the combined list totals when available or sums up the rows.func sumAnalyticsCounts(rows []models.CampaignAnalyticsCount) int {+ if len(rows) > 0 && rows[0].Total != nil {+ return *rows[0].Total+ }+total := 0for _, r := range rows {total += r.Countdiff --git a/models/templates.go b/models/templates.goindex 68c4e0c0..d6cb5233 100644--- a/models/templates.go+++ b/models/templates.go@@ -89,6 +89,7 @@ type AnalyticsItem struct {}type CampaignAnalyticsCount struct {+ Total *int `db:"total" json:"-"`ListID int `db:"list_id" json:"list_id,omitempty"`CampaignID int `db:"campaign_id" json:"campaign_id"`Count int `db:"count" json:"count"`diff --git a/queries/lists.sql b/queries/lists.sqlindex a71769c9..17c243d6 100644--- a/queries/lists.sql+++ b/queries/lists.sql@@ -118,7 +118,7 @@ FROM weeks w LEFT JOIN mat_list_charts c ON c.list_id = $1 AND c.date = w.date;-- name: get-list-charts-live-- raw: true--- Compute the requested list live when slow-query caching is disabled.+-- Sum up the requested list (live when slow-query shared caching is disabled).-- Executed as a raw unprepared query so that different intervals don't end up using-- the same cached query plan which may be suboptimal. This was discovered through-- a bunch of profile exercises.@@ -174,7 +174,9 @@ WITH counts AS (AND created_at >= $2 AND created_at <= $3GROUP BY campaign_id, "timestamp")-SELECT cl.list_id, SUM(c.count)::BIGINT AS "count", c.timestamp+-- Sum up the combined total (before the join duplicates shared campaigns across lists).+SELECT cl.list_id, SUM(c.count)::BIGINT AS "count", c.timestamp,+ (SELECT SUM(count)::BIGINT FROM counts) AS totalFROM counts c JOIN campaign_lists cl ON cl.campaign_id = c.campaign_idWHERE cl.list_id = ANY($1)GROUP BY cl.list_id, c.timestamp ORDER BY c.timestamp;@@ -213,7 +215,9 @@ WITH uniq AS (SELECT campaign_id, COUNT(*) AS "count", "timestamp"FROM uniq GROUP BY campaign_id, "timestamp")-SELECT cl.list_id, SUM(c.count)::BIGINT AS "count", c.timestamp+-- Sum up the combined total (before the join duplicates shared campaigns across lists).+SELECT cl.list_id, SUM(c.count)::BIGINT AS "count", c.timestamp,+ (SELECT SUM(count)::BIGINT FROM counts) AS totalFROM counts c JOIN campaign_lists cl ON cl.campaign_id = c.campaign_idWHERE cl.list_id = ANY($1)GROUP BY cl.list_id, c.timestamp ORDER BY c.timestamp;