forked from jessfraz/junk
/
accounts.go
81 lines (68 loc) · 2.59 KB
/
accounts.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
package main
import (
"fmt"
analytics "github.com/google/google-api-go-client/analytics/v3"
)
func getAccounts(s *analytics.Service) (accountItems []*analytics.Account, err error) {
// get accounts
accountsListCall := s.Management.Accounts.List()
accounts, err := accountsListCall.Do()
if err != nil {
return accountItems, fmt.Errorf("Calling accounts list failed: %s", err)
}
return accounts.Items, nil
}
func getProperties(s *analytics.Service, accounts []*analytics.Account) (properties []*analytics.Webproperty, err error) {
for _, account := range accounts {
// get properties
propertiesListCall := s.Management.Webproperties.List(account.Id)
propertyList, err := propertiesListCall.Do()
if err != nil {
return properties, fmt.Errorf("Calling properties list for account (%s) failed: %s", account.Id, err)
}
properties = append(properties, propertyList.Items...)
}
return properties, nil
}
func getPropertiesByAccount(s *analytics.Service, accountID string) (properties []*analytics.Webproperty, err error) {
// get properties
propertiesListCall := s.Management.Webproperties.List(accountID)
propertiesList, err := propertiesListCall.Do()
if err != nil {
return properties, fmt.Errorf("Calling properties list for account (%s) failed: %s", accountID, err)
}
return propertiesList.Items, nil
}
func getProfiles(s *analytics.Service, properties []*analytics.Webproperty) (profiles []*analytics.Profile, err error) {
for _, property := range properties {
// get profiles
if property.ProfileCount > 0 {
profilesListCall := s.Management.Profiles.List(property.AccountId, property.Id)
profilesList, err := profilesListCall.Do()
if err != nil {
return profiles, fmt.Errorf("Calling profiles list for account (%s) & property (%s) failed: %s", property.AccountId, property.Id, err)
}
profiles = append(profiles, profilesList.Items...)
}
}
return profiles, nil
}
func getPropertyProfiles(s *analytics.Service, propertyID string) (profiles []*analytics.Profile, err error) {
profilesListCall := s.Management.Profiles.List("", propertyID)
profilesList, err := profilesListCall.Do()
if err != nil {
return profiles, fmt.Errorf("Calling profiles list for account (%s) & property (%s) failed: %s", "", propertyID, err)
}
return profilesList.Items, nil
}
func getAccountProfiles(s *analytics.Service, accountID string) (profiles []*analytics.Profile, err error) {
properties, err := getPropertiesByAccount(s, accountID)
if err != nil {
return profiles, err
}
profiles, err = getProfiles(s, properties)
if err != nil {
return profiles, err
}
return profiles, nil
}