func c_google_analytics(clientid string, secret string, tokenstr string, sites []conf.GoogleAnalyticsSite) (opentsdb.MultiDataPoint, error) { var md opentsdb.MultiDataPoint var mErr multiError c, err := analyticsClient(clientid, secret, tokenstr) if err != nil { return nil, err } svc, err := analytics.New(c) if err != nil { return nil, err } dimensions := []string{"browser", "trafficType", "deviceCategory", "operatingSystem"} for _, site := range sites { getPageviews(&md, svc, site) if site.Detailed { if err = getActiveUsers(&md, svc, site); err != nil { mErr = append(mErr, err) } for _, dimension := range dimensions { if err = getActiveUsersByDimension(&md, svc, site, dimension); err != nil { mErr = append(mErr, err) } } } } if len(mErr) == 0 { return md, nil } else { return md, mErr } }
func c_google_analytics(clientid string, secret string, tokenstr string, sites []conf.GoogleAnalyticsSite) (opentsdb.MultiDataPoint, error) { var md opentsdb.MultiDataPoint var mErr multiError c, err := googleAPIClient(clientid, secret, tokenstr, []string{analytics.AnalyticsScope}) if err != nil { return nil, err } svc, err := analytics.New(c) if err != nil { return nil, err } // dimension: max records we want to fetch // "source" has a very long tail so we limit it to something sane // TODO: Dimensions we want and associated attributes should eventually be // setup in configuration. dimensions := map[string]int{"browser": -1, "trafficType": -1, "source": 10, "deviceCategory": -1, "operatingSystem": -1} for _, site := range sites { getPageviews(&md, svc, site) if site.Detailed { if err = getActiveUsers(&md, svc, site); err != nil { mErr = append(mErr, err) } for dimension, topN := range dimensions { if err = getActiveUsersByDimension(&md, svc, site, dimension, topN); err != nil { mErr = append(mErr, err) } } } } if len(mErr) == 0 { return md, nil } else { return md, mErr } }
func (mg *MetricGA) run() bool { log.Printf("Google Metric") ctx := context.Background() authBytes, err := ioutil.ReadFile("./Realtime Metrics-6b2636a4f45d.json") if err != nil { log.Printf("error reading file: %s", err) return false } auth, err := google.JWTConfigFromJSON(authBytes, analytics.AnalyticsReadonlyScope) if err != nil { log.Printf("Error authenticating with google: %s", err) return false } client := auth.Client(ctx) if err != nil { log.Printf("Error authenticating with google: %s", err) return false } srv, err := analytics.New(client) realtime := analytics.NewDataRealtimeService(srv) request := realtime.Get("ga:775859", "rt:activeUsers") data, err := request.Do() if err != nil { log.Printf("Error getting metrics: %s", err) } count := data.Rows[0][0] i, err := strconv.ParseInt(count, 10, 8) if err == nil { mg.value = uint8(i) return true } return false }