From f54170d5090eafcec3f924fd98cd5ed4cc283989 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Tue, 18 Dec 2018 23:46:29 +0530 Subject: [PATCH] Add view and click counts to campaign API response and UI --- frontend/my/src/Campaigns.js | 4 ++-- models/models.go | 2 ++ queries.sql | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/frontend/my/src/Campaigns.js b/frontend/my/src/Campaigns.js index 789c3e2..b3473c6 100644 --- a/frontend/my/src/Campaigns.js +++ b/frontend/my/src/Campaigns.js @@ -225,8 +225,8 @@ class Campaigns extends React.PureComponent { Rate{ Math.round(rate, 2) } / min } - Views0 - Clicks0 + Views{ record.views } + Clicks{ record.clicks }
Created{ dayjs(record.created_at).format(cs.DateFormat) } diff --git a/models/models.go b/models/models.go index 01cca2d..ff19a54 100644 --- a/models/models.go +++ b/models/models.go @@ -136,6 +136,8 @@ type Campaign struct { MessengerID string `db:"messenger" json:"messenger"` Lists types.JSONText `json:"lists"` + View int `db:"views" json:"views"` + Clicks int `db:"clicks" json:"clicks"` // TemplateBody is joined in from templates by the next-campaigns query. TemplateBody string `db:"template_body" json:"-"` Tpl *template.Template `json:"-"` diff --git a/queries.sql b/queries.sql index 712bb38..84b9305 100644 --- a/queries.sql +++ b/queries.sql @@ -255,7 +255,7 @@ INSERT INTO campaign_lists (campaign_id, list_id, list_name) -- name: get-campaigns -- Here, 'lists' is returned as an aggregated JSON array from campaign_lists because -- the list reference may have been deleted. -SELECT campaigns.*, ( +SELECT campaigns.*, COUNT(campaign_views.campaign_id) AS views, COUNT(link_clicks.campaign_id) AS clicks, ( SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(l)), '[]') FROM ( SELECT COALESCE(campaign_lists.list_id, 0) AS id, campaign_lists.list_name AS name @@ -263,7 +263,10 @@ SELECT campaigns.*, ( ) l ) AS lists FROM campaigns +LEFT JOIN campaign_views ON (campaign_views.campaign_id = campaigns.id) +LEFT JOIN link_clicks ON (link_clicks.campaign_id = campaigns.id) WHERE ($1 = 0 OR id = $1) AND status=(CASE WHEN $2 != '' THEN $2::campaign_status ELSE status END) +GROUP BY campaigns.id ORDER BY created_at DESC OFFSET $3 LIMIT $4; -- name: get-campaign-for-preview