Skip to content

Commit

Permalink
Add view and click counts to campaign API response and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Dec 18, 2018
1 parent a4135be commit f54170d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions frontend/my/src/Campaigns.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ class Campaigns extends React.PureComponent {
<Row><Col className="label" span={10}>Rate</Col><Col span={12}>{ Math.round(rate, 2) } / min</Col></Row>
}

<Row><Col className="label" span={10}>Views</Col><Col span={12}>0</Col></Row>
<Row><Col className="label" span={10}>Clicks</Col><Col span={12}>0</Col></Row>
<Row><Col className="label" span={10}>Views</Col><Col span={12}>{ record.views }</Col></Row>
<Row><Col className="label" span={10}>Clicks</Col><Col span={12}>{ record.clicks }</Col></Row>
<br />
<Row><Col className="label" span={10}>Created</Col><Col span={12}>{ dayjs(record.created_at).format(cs.DateFormat) }</Col></Row>

Expand Down
2 changes: 2 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down
5 changes: 4 additions & 1 deletion queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,18 @@ 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
FROM campaign_lists WHERE campaign_lists.campaign_id = campaigns.id
) 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
Expand Down

0 comments on commit f54170d

Please sign in to comment.