-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (90 loc) · 2.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/urfave/cli"
ga "google.golang.org/api/analyticsdata/v1beta"
//"google.golang.org/api/analyticsreporting/v4"
"google.golang.org/api/option"
"log"
"os"
)
type GoogleAnalyticsCache struct {
Monthly []GoogleAnalyticsPV `json:"monthly"`
Weekly []GoogleAnalyticsPV `json:"weekly"`
Yearly []GoogleAnalyticsPV `json:"yearly"`
}
type GoogleAnalyticsPV struct {
Path string `json:"path"`
Pv string `json:"pv"`
Title string `json:"title"`
}
func main() {
app := &cli.App{
Name: "google analytics",
Usage: "ga",
Action: func(cCtx *cli.Context) error {
ctx := context.Background()
// https://pkg.go.dev/google.golang.org/api/analyticsreporting/v4
ars, err := ga.NewService(ctx, option.WithScopes(ga.AnalyticsReadonlyScope))
if err != nil {
return fmt.Errorf("new google analytice service: %w", err)
}
pvWeekly, err := fetchGoogleAnalytics(ars, ctx, "7daysAgo", "today")
if err != nil {
return err
}
pvMonthly, err := fetchGoogleAnalytics(ars, ctx, "30daysAgo", "today")
if err != nil {
return err
}
pvYearly, err := fetchGoogleAnalytics(ars, ctx, "365daysAgo", "today")
if err != nil {
return err
}
output := GoogleAnalyticsCache{
Weekly: pvWeekly,
Monthly: pvMonthly,
Yearly: pvYearly,
}
outputJSON, err := json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("marshal json: %w", err)
}
fmt.Println(string(outputJSON))
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func fetchGoogleAnalytics(ars *ga.Service, ctx context.Context, start, end string) ([]GoogleAnalyticsPV, error) {
resp, err := ars.Properties.BatchRunReports("properties/319098367", &ga.BatchRunReportsRequest{
Requests: []*ga.RunReportRequest{
{
Property: "properties/319098367",
DateRanges: []*ga.DateRange{{StartDate: start, EndDate: end}},
Dimensions: []*ga.Dimension{{Name: "pagePath"}, {Name: "pageTitle"}},
Metrics: []*ga.Metric{{Name: "screenPageViews"}},
OrderBys: []*ga.OrderBy{{Desc: true, Dimension: &ga.DimensionOrderBy{DimensionName: "screenPageViews"}}},
Limit: 50,
},
},
}).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("reporting batch get: %w", err)
}
pvs := make([]GoogleAnalyticsPV, 0, 50)
for _, report := range resp.Reports {
for _, row := range report.Rows {
pvs = append(pvs, GoogleAnalyticsPV{
Path: row.DimensionValues[0].Value,
Pv: row.MetricValues[0].Value,
Title: row.DimensionValues[1].Value,
})
}
}
return pvs, nil
}