diff options
author | Matthieu <matthieu.cneude@gmail.com> | 2019-01-27 20:23:36 +0100 |
---|---|---|
committer | Matthieu <matthieu.cneude@gmail.com> | 2019-01-27 20:23:36 +0100 |
commit | dad857b9ebfadd8363cbd7a2aa4eeb2e4c34514b (patch) | |
tree | 1c99c80141de9a010f73ec7c572c8804f9b38e27 | |
parent | a4173872feb6961dafa5d02a10ff65990e6f5d29 (diff) |
Add ga metrics: new session vs return sessions
Fix colors of the widgets
-rwxr-xr-x | cmd/devdash/devdash | bin | 13784882 -> 13812444 bytes | |||
-rw-r--r-- | internal/ga_widget.go | 69 | ||||
-rw-r--r-- | internal/plateform/ga.go | 31 | ||||
-rw-r--r-- | internal/plateform/termui.go | 37 | ||||
-rw-r--r-- | internal/tui.go | 23 |
5 files changed, 144 insertions, 16 deletions
diff --git a/cmd/devdash/devdash b/cmd/devdash/devdash Binary files differindex cc7bf67..17a340b 100755 --- a/cmd/devdash/devdash +++ b/cmd/devdash/devdash diff --git a/internal/ga_widget.go b/internal/ga_widget.go index 26c2a02..4bb42db 100644 --- a/internal/ga_widget.go +++ b/internal/ga_widget.go @@ -9,9 +9,10 @@ import ( ) const ( - realtime = "ga.realtime" - users = "ga.users" - pages = "ga.pages" + realtime = "ga.realtime" + users = "ga.users" + pages = "ga.pages" + new_returning = "ga.new_returning" ) type gaWidget struct { @@ -41,7 +42,9 @@ func (g *gaWidget) CreateWidgets(widget Widget, tui *Tui) (err error) { case users: err = g.gaUsers(widget) case pages: - err = g.getFirstPages(widget, 3) + err = g.TopContents(widget) + case new_returning: + err = g.NewVsReturningSessions(widget) default: return errors.Errorf("can't find the widget %s", widget.Name) } @@ -85,7 +88,7 @@ func (g *gaWidget) gaUsers(widget Widget) error { endDate = widget.Options["end_date"] } - rep, err := g.client.GetUserReport(g.viewID, startDate, endDate) + rep, err := g.client.UserReport(g.viewID, startDate, endDate) if err != nil { return err } @@ -113,6 +116,7 @@ func (g *gaWidget) gaUsers(widget Widget) error { Data: u, Dimensions: dates, BarWidth: 6, + Bd: 5, Bdlabel: "Weekly users", Size: widget.Size, }) @@ -120,8 +124,8 @@ func (g *gaWidget) gaUsers(widget Widget) error { return nil } -func (g *gaWidget) getFirstPages(widget Widget, count int) error { - rep, err := g.client.GetTopContent(g.viewID) +func (g *gaWidget) TopContents(widget Widget) error { + rep, err := g.client.TopContents(g.viewID) if err != nil { return err } @@ -170,8 +174,59 @@ func (g *gaWidget) getFirstPages(widget Widget, count int) error { g.tui.AddTable(tableAttr{ Data: table, + Bd: 5, BdLabel: "Most page viewed", }) return nil } + +func (g *gaWidget) NewVsReturningSessions(widget Widget) error { + rep, err := g.client.NewVsReturningSessions(g.viewID) + if err != nil { + return err + } + + var dim []string + var u []int + for _, v := range rep.Reports { + for l := 0; l < len(v.Data.Rows); l++ { + p := v.Data.Rows[l].Dimensions[0] + dim = append(dim, p) + for m := 0; m < len(v.Data.Rows[l].Metrics); m++ { + value := v.Data.Rows[l].Metrics[m].Values[0] + if v, err := strconv.ParseInt(value, 0, 0); err == nil { + u = append(u, int(v)) + } + if err != nil { + return err + } + } + } + } + + s := len(u) / 2 + sessions := u[:s] + new := u[s:] + + var data [8][]int + + for i := 0; i < 8; i++ { + for j := 0; j < len(sessions); j++ { + data[i] = append(data[i], 0) + } + } + + data[3] = sessions + data[4] = new + + g.tui.AddStackedBarChart(stackedBarChartAttr{ + Data: data, + Dimensions: dim, + BarWidth: 6, + Bd: 5, + Bdlabel: "Session vs New", + }) + + return nil +} diff --git a/internal/plateform/ga.go b/internal/plateform/ga.go index 3c37045..4a6b5f1 100644 --- a/internal/plateform/ga.go +++ b/internal/plateform/ga.go @@ -58,7 +58,7 @@ func NewGaClient(keyfile string) (*Client, error) { // GetReport queries the Analytics Reporting API V4 using the // Analytics Reporting API V4 service object. -func (c *Client) GetUserReport(viewID string, startDate string, endDate string) (*ga.GetReportsResponse, error) { +func (c *Client) UserReport(viewID string, startDate string, endDate string) (*ga.GetReportsResponse, error) { req := &ga.GetReportsRequest{ ReportRequests: []*ga.ReportRequest{ { @@ -102,9 +102,9 @@ func (c *Client) RealTimeUsers(viewID string) (string, error) { return resp.TotalsForAllResults[metric], nil } -// GetReport queries the Analytics Reporting API V4 using the +// TopContents queries the Analytics Reporting API V4 using the // Analytics Reporting API V4 service object. -func (c *Client) GetTopContent(viewID string) (*ga.GetReportsResponse, error) { +func (c *Client) TopContents(viewID string) (*ga.GetReportsResponse, error) { req := &ga.GetReportsRequest{ ReportRequests: []*ga.ReportRequest{ { @@ -127,3 +127,28 @@ func (c *Client) GetTopContent(viewID string) (*ga.GetReportsResponse, error) { return c.service.Reports.BatchGet(req).Do() } + +// TopContents queries the Analytics Reporting API V4 using the +// Analytics Reporting API V4 service object. +func (c *Client) NewVsReturningSessions(viewID string) (*ga.GetReportsResponse, error) { + req := &ga.GetReportsRequest{ + ReportRequests: []*ga.ReportRequest{ + { + ViewId: viewID, + DateRanges: []*ga.DateRange{ + {StartDate: "7daysAgo", EndDate: "today"}, + }, + Metrics: []*ga.Metric{ + {Expression: "ga:sessions"}, + }, + Dimensions: []*ga.Dimension{ + {Name: "ga:userType"}, + {Name: "ga:month"}, + {Name: "ga:day"}, + }, + }, + }, + } + + return c.service.Reports.BatchGet(req).Do() +} diff --git a/internal/plateform/termui.go b/internal/plateform/termui.go index fb277ed..71713d8 100644 --- a/internal/plateform/termui.go +++ b/internal/plateform/termui.go @@ -110,7 +110,13 @@ func (t *termUI) Text(text string, fg uint16, size int) { t.body.AddRows(termui.NewCol(size, 0, pro)) } -func (t *termUI) BarChart(data []int, dimensions []string, barWidth int, bdLabel string) { +func (t *termUI) BarChart( + data []int, + dimensions []string, + barWidth int, + bd uint16, + bdLabel string, +) { bc := termui.NewBarChart() bc.BorderLabel = bdLabel bc.Data = data @@ -122,15 +128,42 @@ func (t *termUI) BarChart(data []int, dimensions []string, barWidth int, bdLabel bc.TextColor = termui.ColorGreen bc.BarColor = termui.ColorBlue bc.NumColor = termui.ColorWhite + bc.BorderFg = termui.Attribute(bd) + + t.widgets = append(t.widgets, bc) +} + +func (t *termUI) StackedBarChart( + data [8][]int, + dimensions []string, + barWidth int, + bd uint16, + bdLabel string, +) { + bc := termui.NewMBarChart() + bc.BorderLabel = bdLabel + bc.Data = data + bc.BarWidth = barWidth + bc.DataLabels = dimensions + bc.Width = 200 + bc.Height = 20 + bc.TextColor = termui.ColorGreen + bc.BorderFg = termui.Attribute(bd) + bc.SetMax(10) t.widgets = append(t.widgets, bc) } -func (t *termUI) Table(data [][]string, bdLabel string) { +func (t *termUI) Table( + data [][]string, + bd uint16, + bdLabel string, +) { ta := termui.NewTable() ta.Rows = data ta.BorderLabel = bdLabel ta.FgColor = termui.ColorGreen + ta.BorderFg = termui.Attribute(bd) ta.SetSize() t.widgets = append(t.widgets, ta) diff --git a/internal/tui.go b/internal/tui.go index 6d8a67c..2e0514c 100644 --- a/internal/tui.go +++ b/internal/tui.go @@ -17,8 +17,9 @@ type renderer interface { type drawer interface { Text(text string, fg uint16, size int) TextBox(data string, fg uint16, bd uint16, bdlabel string, h int) - BarChart(data []int, dimensions []string, barWidth int, bdLabel string) - Table(data [][]string, bdLabel string) + BarChart(data []int, dimensions []string, barWidth int, bd uint16, bdLabel string) + StackedBarChart(data [8][]int, dimensions []string, barWidth int, bd uint16, bdLabel string) + Table(data [][]string, bd uint16, bdLabel string) AddCol(size int) AddRow() error } @@ -54,12 +55,22 @@ type barChartAttr struct { Data []int Dimensions []string BarWidth int + Bd uint16 Bdlabel string Size string } +type stackedBarChartAttr struct { + Data [8][]int + Dimensions []string + BarWidth int + Bd uint16 + Bdlabel string +} + type tableAttr struct { Data [][]string + Bd uint16 BdLabel string } @@ -88,11 +99,15 @@ func (t *Tui) AddTextBox(attr textBoxAttr) { } func (t *Tui) AddBarChart(attr barChartAttr) { - t.instance.BarChart(attr.Data, attr.Dimensions, attr.BarWidth, attr.Bdlabel) + t.instance.BarChart(attr.Data, attr.Dimensions, attr.BarWidth, attr.Bd, attr.Bdlabel) +} + +func (t *Tui) AddStackedBarChart(attr stackedBarChartAttr) { + t.instance.StackedBarChart(attr.Data, attr.Dimensions, attr.BarWidth, attr.Bd, attr.Bdlabel) } func (t *Tui) AddTable(attr tableAttr) { - t.instance.Table(attr.Data, attr.BdLabel) + t.instance.Table(attr.Data, attr.Bd, attr.BdLabel) } func (t *Tui) AddKQuit(key string) { |