/
gagocli.go
232 lines (189 loc) · 5.38 KB
/
gagocli.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"flag"
"github.com/MarkEdmondson1234/gago/gago"
"fmt"
"io/ioutil"
"os"
"github.com/olebedev/config"
)
type argFlags struct {
config string
auth string
view string
start string
end string
metrics string
dimensions string
antisample bool
maxRows int64
output string
verbose bool
}
func parseArgs() (string, argFlags) {
//report args
reportCmd := flag.NewFlagSet("reports", flag.ExitOnError)
var view = reportCmd.String("view", "", "The Google Analytics ViewId to run config for")
var start = reportCmd.String("start", "", "The start date (YYYY-mm-dd) to run config for")
var end = reportCmd.String("end", "", "The end date (YYYY-mm-dd) to run config for")
var antisample = reportCmd.Bool("antisample", false, "Whether to run anti-sampling")
var metrics = reportCmd.String("mets", "", "The metrics ('ga:users,ga:sessions') to run config for")
var dimensions = reportCmd.String("dims", "", "The dimensions ('ga:date,ga:sourceMedium') to run config for")
var maxRows = reportCmd.Int64("max", 1000, "The amount of rows to fetch. Use -1 to fetch all rows")
var config = reportCmd.String("c", "", "Optional config.yml specifying arguments")
var auth = reportCmd.String("a", "", "File path to auth.json service file. Or set via GAGO_AUTH environment argument")
var output = reportCmd.String("o", "", "If used will write CSV output to this file")
var verbose = reportCmd.Bool("v", false, "Verbose output.")
//account args
accSumCmd := flag.NewFlagSet("accounts", flag.ExitOnError)
var auth2 = accSumCmd.String("a", "", "File path to auth.json service file. Or set via GAGO_AUTH environment argument")
if len(os.Args) < 2 {
usage()
}
args := argFlags{}
switch os.Args[1] {
case "accounts":
accSumCmd.Parse(os.Args[2:])
args.auth = *auth2
if args.auth == "" {
if os.Getenv("GAGO_AUTH") == "" {
fmt.Println("Must supply auth json file via -a or GAGO_AUTH environment arg")
os.Exit(1)
}
args.auth = os.Getenv("GAGO_AUTH")
}
case "reports":
reportCmd.Parse(os.Args[2:])
args.view = *view
args.start = *start
args.end = *end
args.antisample = *antisample
args.metrics = *metrics
args.dimensions = *dimensions
args.maxRows = *maxRows // default 1000
args.auth = *auth
args.config = *config
args.verbose = *verbose
args.output = *output
if args.auth == "" {
if os.Getenv("GAGO_AUTH") == "" {
fmt.Println("Must supply auth json file via -a or GAGO_AUTH environment arg")
os.Exit(1)
}
args.auth = os.Getenv("GAGO_AUTH")
}
cfg := readConfigYaml(*config)
if args.view == "" {
viewid, err := cfg.String("view")
checkError("No viewId passed to fetch data for", err)
args.view = viewid
}
if args.start == "" {
start, err := cfg.String("start")
checkError("No start passed to fetch data for", err)
args.start = start
}
if args.end == "" {
end, err := cfg.String("end")
checkError("No end passed to fetch data for", err)
args.end = end
}
if !args.antisample {
as, _ := cfg.Bool("antisample")
// if nothing, its ok as default is nothing
args.antisample = as
}
if args.metrics == "" {
mets, err := cfg.String("metrics")
checkError("No metrics passed", err)
args.metrics = mets
}
if args.dimensions == "" {
dims, _ := cfg.String("dimensions")
args.dimensions = dims
}
// will use flag default of 1000 if not in config or flags
if args.maxRows == 1000 {
mr, _ := cfg.Int("maxRows")
if mr != 0 {
args.maxRows = int64(mr)
}
}
default:
fmt.Println("Command not recognised:", os.Args[1])
usage()
}
return os.Args[1], args
}
func usage() {
fmt.Println(usageText)
os.Exit(0)
}
var usageText = `gagocli [subcommand...] [arguments...]
subcommand:
reports - Download data from Google Analytics API v4
accounts - Get account summary of accounts, webproperties and viewIds
Use -h to get help on subcommand e.g. gagocli report -h
`
func readConfigYaml(filename string) *config.Config {
if filename == "" {
cfg, _ := config.ParseYaml("")
return cfg
}
file, _ := ioutil.ReadFile(filename)
yamlString := string(file)
cfg, err := config.ParseYaml(yamlString)
if err != nil {
panic(err)
}
cfgg, err := cfg.Get("gago")
if err != nil {
fmt.Println("Incorrect gago configuration in file:", filename)
os.Exit(1)
}
return cfgg
}
func main() {
cmd, flags := parseArgs()
analyticsreportingService, analyticsService := gago.Authenticate(flags.auth)
switch cmd {
case "accounts":
gago.GetAccountSummary(analyticsService)
case "reports":
var req = gago.GoogleAnalyticsRequest{
Service: analyticsreportingService,
ViewID: flags.view,
Start: flags.start,
End: flags.end,
Dimensions: flags.dimensions,
Metrics: flags.metrics,
MaxRows: flags.maxRows,
AntiSample: flags.antisample,
Verbose: flags.verbose}
report := gago.GoogleAnalytics(req)
var output *os.File
if len(flags.output) > 0 {
// write to csv
file, err := os.Create(flags.output)
if err != nil {
fmt.Println("Couldn't open file to write to: ", flags.output)
os.Exit(1)
}
defer file.Close()
output = file
} else {
// print to console
output = os.Stdout
}
gago.WriteCSV(report, output)
default:
fmt.Println("Command not recognised:", os.Args[1])
os.Exit(1)
}
}
func checkError(s string, err error) {
if err != nil {
fmt.Println(s)
os.Exit(1)
}
}