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
|
package main
import (
"fmt"
"log"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
analytics "google.golang.org/api/analytics/v3"
)
func main() {
client, err := google.DefaultClient(
oauth2.NoContext,
"https://www.googleapis.com/auth/analytics.readonly")
if err != nil {
log.Fatalf("Unable to read client : %v", err)
}
service, err := analytics.New(client)
if err != nil {
log.Fatalf("Unable to Access Google Analytics: %v", err)
}
result, err := service.Data.Ga.Get("ga:"+os.Getenv("PROFILE_ID"), "yesterday", "today", "ga:sessions").Dimensions("ga:visitorGender,ga:visitorAgeBracket").Do()
if err != nil {
log.Fatalf("Unable to get data: %v", err)
}
for _, row := range result.Rows {
fmt.Printf("%s,%s,%s\n", row[0], row[1], row[2])
}
}
|