Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Add list analytics (!) subscription stats+campaign views on the list overview page.

Kailash Nadh03a9584262fd
diff --git a/cmd/lists.go b/cmd/lists.go
index 67452f13..77c368b8 100644
--- a/cmd/lists.go
+++ b/cmd/lists.go
@@ -5,6 +5,7 @@ import (
"net/http"
"strings"
+ "github.com/jmoiron/sqlx/types"
"github.com/knadh/listmonk/internal/auth"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
@@ -36,11 +37,14 @@ type listsView struct {
type listView struct {
adminView
- IsNew bool
- Tab string
- List models.List
- Campaigns []models.Campaign
- Page models.PageProps
+ Charts types.JSONText
+ CanViewCampaignCharts bool
+ CacheSlowQueries bool
+ IsNew bool
+ Tab string
+ List models.List
+ Campaigns []models.Campaign
+ Page models.PageProps
}
// publicListForm is the minimal list representation used by the forms view.
@@ -125,6 +129,15 @@ func (a *App) ViewList(c echo.Context) error {
Tab: tab,
}
+ if tab == "list" {
+ data.CanViewCampaignCharts = data.Can("campaigns:get", "campaigns:get_all") && data.Can("campaigns:get_analytics")
+ data.CacheSlowQueries = ko.Bool("app.cache_slow_queries")
+ data.Charts, err = a.core.GetListCharts(id, data.CanViewCampaignCharts)
+ if err != nil {
+ return err
+ }
+ }
+
if tab == "list" && data.Can("campaigns:get", "campaigns:get_all") {
data.Campaigns, data.Page, err = a.getViewCampaigns(c, id)
if err != nil {
diff --git a/internal/core/core.go b/internal/core/core.go
index 4f7f64d2..3a7fc5ab 100644
--- a/internal/core/core.go
+++ b/internal/core/core.go
@@ -25,6 +25,7 @@ const (
matDashboardCharts = "mat_dashboard_charts"
matDashboardCounts = "mat_dashboard_counts"
+ matListCharts = "mat_list_charts"
matListSubStats = "mat_list_subscriber_stats"
)
@@ -91,7 +92,7 @@ func New(o *Opt, h *Hooks) *Core {
// RefreshMatViews refreshes all materialized views.
func (c *Core) RefreshMatViews(concurrent bool) error {
- for _, v := range []string{matDashboardCharts, matDashboardCounts, matListSubStats} {
+ for _, v := range []string{matDashboardCharts, matDashboardCounts, matListSubStats, matListCharts} {
_ = c.RefreshMatView(v, true)
}
return nil
diff --git a/internal/core/lists.go b/internal/core/lists.go
index da16d968..213cfaa5 100644
--- a/internal/core/lists.go
+++ b/internal/core/lists.go
@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/gofrs/uuid/v5"
+ "github.com/jmoiron/sqlx/types"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
"github.com/lib/pq"
@@ -222,3 +223,17 @@ func (c *Core) DeleteLists(ids []int, query, typ, optin, status string, tags []s
}
return nil
}
+
+// GetListCharts returns weekly new subscriptions and and campaign view stats.
+func (c *Core) GetListCharts(id int, campaignViews bool) (types.JSONText, error) {
+ if err := c.refreshCache(matListCharts, false); err != nil {
+ return nil, err
+ }
+
+ var out types.JSONText
+ if err := c.q.GetListCharts.Get(&out, id, campaignViews); err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError,
+ c.i18n.Ts("globals.messages.errorFetching", "name", "list charts", "error", pqErrMsg(err)))
+ }
+ return out, nil
+}
diff --git a/internal/migrations/v7.0.0.go b/internal/migrations/v7.0.0.go
index 36d7f772..defb54df 100644
--- a/internal/migrations/v7.0.0.go
+++ b/internal/migrations/v7.0.0.go
@@ -14,5 +14,36 @@ func V7_0_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger
return err
}
+ // List analytics.
+ if _, err := db.Exec(`
+CREATE MATERIALIZED VIEW IF NOT EXISTS mat_list_charts AS
+ -- Aggregate campaign events before joining lists so multi-list campaigns do not
+ -- multiply the work over the raw events. Views belong to campaigns, not lists.
+ WITH views AS (
+ SELECT campaign_id, DATE_TRUNC('week', created_at)::DATE AS date, COUNT(*) AS count
+ FROM campaign_views
+ WHERE created_at >= CURRENT_DATE - INTERVAL '12 months' AND created_at <= NOW()
+ GROUP BY campaign_id, date
+ ), counts AS (
+ SELECT list_id, DATE_TRUNC('week', created_at)::DATE AS date,
+ COUNT(*) AS subscriptions, 0::BIGINT AS campaign_views
+ FROM subscriber_lists
+ WHERE created_at >= CURRENT_DATE - INTERVAL '12 months' AND created_at <= NOW()
+ GROUP BY list_id, date
+ UNION ALL
+ SELECT cl.list_id, v.date, 0::BIGINT, SUM(v.count)::BIGINT
+ FROM views v JOIN campaign_lists cl ON cl.campaign_id = v.campaign_id
+ WHERE cl.list_id IS NOT NULL
+ GROUP BY cl.list_id, v.date
+ )
+ SELECT list_id, date, SUM(subscriptions)::BIGINT AS subscriptions,
+ SUM(campaign_views)::BIGINT AS campaign_views
+ FROM counts
+ GROUP BY list_id, date;
+CREATE UNIQUE INDEX IF NOT EXISTS mat_list_charts_idx ON mat_list_charts (list_id, date);
+ `); err != nil {
+ return err
+ }
+
return nil
}
diff --git a/models/queries.go b/models/queries.go
index 987287f3..78ecbd34 100644
--- a/models/queries.go
+++ b/models/queries.go
@@ -52,6 +52,8 @@ type Queries struct {
DeleteSubscriptionsByQuery string `query:"delete-subscriptions-by-query"`
UnsubscribeSubscribersFromListsByQuery string `query:"unsubscribe-subscribers-from-lists-by-query"`
+ GetListCharts *sqlx.Stmt `query:"get-list-charts"`
+
CreateList *sqlx.Stmt `query:"create-list"`
QueryLists string `query:"query-lists"`
GetLists *sqlx.Stmt `query:"get-lists"`
diff --git a/queries/lists.sql b/queries/lists.sql
index 02caf0b6..af4261ae 100644
--- a/queries/lists.sql
+++ b/queries/lists.sql
@@ -100,3 +100,17 @@ AND CASE
-- Optional list IDs based on user permission.
WHEN $7 = TRUE THEN TRUE ELSE id = ANY($8::INT[])
END;
+
+-- name: get-list-charts
+-- Fill missing weeks, including empty/new lists absent from the cached view.
+WITH weeks AS (
+ SELECT GENERATE_SERIES(DATE_TRUNC('week', CURRENT_DATE - INTERVAL '12 months'),
+ DATE_TRUNC('week', CURRENT_DATE), INTERVAL '1 week')::DATE AS date
+)
+SELECT JSON_BUILD_OBJECT(
+ 'subscriptions', JSON_AGG(JSON_BUILD_OBJECT('date', w.date, 'count', COALESCE(c.subscriptions, 0)) ORDER BY w.date),
+ 'campaign_views', CASE WHEN $2::BOOLEAN THEN
+ JSON_AGG(JSON_BUILD_OBJECT('date', w.date, 'count', COALESCE(c.campaign_views, 0)) ORDER BY w.date)
+ ELSE '[]'::JSON END
+)
+FROM weeks w LEFT JOIN mat_list_charts c ON c.list_id = $1 AND c.date = w.date;
diff --git a/schema.sql b/schema.sql
index 581addbc..d13bc292 100644
--- a/schema.sql
+++ b/schema.sql
@@ -443,3 +443,31 @@ CREATE MATERIALIZED VIEW mat_list_subscriber_stats AS
UNION ALL
SELECT NOW() AS updated_at, 0 AS list_id, NULL AS status, COUNT(id) AS subscriber_count FROM subscribers;
DROP INDEX IF EXISTS mat_list_subscriber_stats_idx; CREATE UNIQUE INDEX mat_list_subscriber_stats_idx ON mat_list_subscriber_stats (list_id, status);
+
+-- Weekly list charts for the latest twelve months.
+DROP MATERIALIZED VIEW IF EXISTS mat_list_charts;
+CREATE MATERIALIZED VIEW mat_list_charts AS
+ -- Aggregate campaign events before joining lists so multi-list campaigns do not
+ -- multiply the work over the raw events. Views belong to campaigns, not lists.
+ WITH views AS (
+ SELECT campaign_id, DATE_TRUNC('week', created_at)::DATE AS date, COUNT(*) AS count
+ FROM campaign_views
+ WHERE created_at >= CURRENT_DATE - INTERVAL '12 months' AND created_at <= NOW()
+ GROUP BY campaign_id, date
+ ), counts AS (
+ SELECT list_id, DATE_TRUNC('week', created_at)::DATE AS date,
+ COUNT(*) AS subscriptions, 0::BIGINT AS campaign_views
+ FROM subscriber_lists
+ WHERE created_at >= CURRENT_DATE - INTERVAL '12 months' AND created_at <= NOW()
+ GROUP BY list_id, date
+ UNION ALL
+ SELECT cl.list_id, v.date, 0::BIGINT, SUM(v.count)::BIGINT
+ FROM views v JOIN campaign_lists cl ON cl.campaign_id = v.campaign_id
+ WHERE cl.list_id IS NOT NULL
+ GROUP BY cl.list_id, v.date
+ )
+ SELECT list_id, date, SUM(subscriptions)::BIGINT AS subscriptions,
+ SUM(campaign_views)::BIGINT AS campaign_views
+ FROM counts
+ GROUP BY list_id, date;
+CREATE UNIQUE INDEX mat_list_charts_idx ON mat_list_charts (list_id, date);
diff --git a/static/admin/assets/css/style.css b/static/admin/assets/css/style.css
index c089dd8f..fd36dc5b 100644
--- a/static/admin/assets/css/style.css
+++ b/static/admin/assets/css/style.css
@@ -1216,12 +1216,16 @@ visual-editor {
/* Sections */
-/* Dashboard */
-section.dashboard {
+section.dashboard,
+section.lists {
.chart-box {
position: relative;
height: 18rem;
}
+}
+
+/* Dashboard */
+section.dashboard {
.stat-card {
display: flex;
flex-direction: column;
diff --git a/static/admin/assets/js/charts.js b/static/admin/assets/js/charts.js
new file mode 100644
index 00000000..2d974254
--- /dev/null
+++ b/static/admin/assets/js/charts.js
@@ -0,0 +1,61 @@
+import Chart from 'chart.js/auto';
+
+const FONT_FAMILY = "'Geist', sans-serif";
+
+// Format an ISO date ('2024-06-01') to a short 'DD Mon' label.
+function label(iso) {
+ // Parse date-only buckets as local dates so western timezones do not show
+ // the previous day.
+ const d = new Date(`${iso}T00:00:00`);
+ if (Number.isNaN(d.getTime())) {
+ return iso;
+ }
+
+ return d.toLocaleDateString(undefined, { day: '2-digit', month: 'short' });
+}
+
+// Draw a filled line chart of {count, date} rows on the given canvas.
+export function lineChart(id, rows) {
+ const canvas = document.getElementById(id);
+ if (!canvas) {
+ return;
+ }
+
+ // Get the primary colour.
+ const el = document.createElement('div');
+ el.style.color = 'var(--primary)';
+ document.documentElement.appendChild(el);
+ const color = getComputedStyle(el).color || '#0055d4';
+ el.remove();
+
+ const data = rows || [];
+ Chart.defaults.font.family = FONT_FAMILY;
+
+ // eslint-disable-next-line no-new
+ new Chart(canvas, {
+ type: 'line',
+ data: {
+ labels: data.map((d) => label(d.date)),
+ datasets: [{
+ data: data.map((d) => d.count),
+ borderColor: color,
+ backgroundColor: `${color.replace('rgb(', 'rgba(').replace(')', ', 0.0314)')}`,
+ borderWidth: 2,
+ fill: true,
+ tension: 0.35,
+ pointBorderWidth: 0.5,
+ pointHoverBorderWidth: 5,
+ }],
+ },
+ options: {
+ responsive: true,
+ maintainAspectRatio: false,
+ plugins: { legend: { display: false } },
+ scales: {
+ x: { grid: { display: false }, border: { display: false } },
+ y: { grid: { display: false }, border: { display: false }, ticks: { precision: 0 } },
+ },
+ },
+ });
+}
+
diff --git a/static/admin/assets/js/views/dashboard.js b/static/admin/assets/js/views/dashboard.js
index ff398761..43fabc84 100644
--- a/static/admin/assets/js/views/dashboard.js
+++ b/static/admin/assets/js/views/dashboard.js
@@ -1,61 +1,4 @@
-import Chart from 'chart.js/auto';
-
-const FONT_FAMILY = "'Geist', sans-serif";
-
-// Format an ISO date ('2024-06-01') to a short 'DD Mon' label.
-function label(iso) {
- const d = new Date(iso);
- if (Number.isNaN(d.getTime())) {
- return iso;
- }
-
- return d.toLocaleDateString(undefined, { day: '2-digit', month: 'short' });
-}
-
-// Draw a filled line chart of {count, date} rows on the given canvas.
-function lineChart(id, rows) {
- const canvas = document.getElementById(id);
- if (!canvas) {
- return;
- }
-
- // Get the primary colour.
- const el = document.createElement('div');
- el.style.color = 'var(--primary)';
- document.documentElement.appendChild(el);
- const color = getComputedStyle(el).color || '#0055d4';
- el.remove();
-
- const data = rows || [];
- Chart.defaults.font.family = FONT_FAMILY;
-
- // eslint-disable-next-line no-new
- new Chart(canvas, {
- type: 'line',
- data: {
- labels: data.map((d) => label(d.date)),
- datasets: [{
- data: data.map((d) => d.count),
- borderColor: color,
- backgroundColor: `${color.replace('rgb(', 'rgba(').replace(')', ', 0.0314)')}`,
- borderWidth: 2,
- fill: true,
- tension: 0.35,
- pointBorderWidth: 0.5,
- pointHoverBorderWidth: 5,
- }],
- },
- options: {
- responsive: true,
- maintainAspectRatio: false,
- plugins: { legend: { display: false } },
- scales: {
- x: { grid: { display: false }, border: { display: false } },
- y: { grid: { display: false }, border: { display: false }, ticks: { precision: 0 } },
- },
- },
- });
-}
+import { lineChart } from '../charts.js';
(() => {
const d = window._dashboardCharts || {};
diff --git a/static/admin/assets/js/views/lists.js b/static/admin/assets/js/views/lists.js
index 54b46102..c4a69008 100644
--- a/static/admin/assets/js/views/lists.js
+++ b/static/admin/assets/js/views/lists.js
@@ -1,4 +1,5 @@
import Alpine from 'alpinejs';
+import { lineChart } from '../charts.js';
import {
api,
urls,
@@ -111,3 +112,7 @@ function component(list = null) {
document.addEventListener('alpine:init', () => {
Alpine.data('listsView', component);
}, { once: true });
+
+const charts = window._listCharts || {};
+lineChart('chart-subscriptions', charts.subscriptions);
+lineChart('chart-list-views', charts.campaign_views);