-
Notifications
You must be signed in to change notification settings - Fork 17
/
analytics.go
215 lines (181 loc) · 7.5 KB
/
analytics.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
package gav3
import (
"context"
"fmt"
"time"
"github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/model"
"github.com/blackcowmoo/grafana-google-analytics-dataSource/pkg/setting"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/patrickmn/go-cache"
)
// GoogleAnalyticsv3DataSource handler for google sheets
type GoogleAnalytics struct {
Cache *cache.Cache
}
func (ga *GoogleAnalytics) Query(ctx context.Context, config *setting.DatasourceSecretSettings, query backend.DataQuery) (*data.Frames, error) {
client, err := NewGoogleClient(ctx, config.JWT)
if err != nil {
log.DefaultLogger.Error("Query: Fail NewGoogleClient", "error", err.Error())
return nil, err
}
queryModel, err := GetQueryModel(query)
if err != nil {
log.DefaultLogger.Error("Failed to read query: %w", "error", err)
return nil, fmt.Errorf("failed to read query: %w", err)
}
if len(queryModel.AccountID) < 1 {
log.DefaultLogger.Error("Query", "error", "Required AccountID")
return nil, fmt.Errorf("Required AccountID")
}
if len(queryModel.WebPropertyID) < 1 {
log.DefaultLogger.Error("Query", "error", "Required WebPropertyID")
return nil, fmt.Errorf("Required WebPropertyID")
}
if len(queryModel.ProfileID) < 1 {
log.DefaultLogger.Error("Query", "error", "Required ProfileID")
return nil, fmt.Errorf("Required ProfileID")
}
report, err := client.getReport(*queryModel)
if err != nil {
log.DefaultLogger.Error("Query", "error", err)
return nil, err
}
return transformReportsResponseToDataFrames(report, queryModel.RefID, queryModel.Timezone)
}
func (ga *GoogleAnalytics) GetTimezone(ctx context.Context, config *setting.DatasourceSecretSettings, accountId string, webPropertyId string, profileId string) (string, error) {
client, err := NewGoogleClient(ctx, config.JWT)
if err != nil {
return "", fmt.Errorf("failed to create Google API client: %w", err)
}
cacheKey := fmt.Sprintf("analytics:account:%s:webproperty:%s:profile:%s:timezone", accountId, webPropertyId, profileId)
if item, _, found := ga.Cache.GetWithExpiration(cacheKey); found {
return item.(string), nil
}
profile, err := client.getProfile(accountId, webPropertyId, profileId)
if err != nil {
return "", err
}
timezone := profile.Timezone
ga.Cache.Set(cacheKey, timezone, 60*time.Second)
return timezone, nil
}
func (ga *GoogleAnalytics) CheckHealth(ctx context.Context, config *setting.DatasourceSecretSettings) (*backend.CheckHealthResult, error) {
var status = backend.HealthStatusOk
var message = "Success"
client, err := NewGoogleClient(ctx, config.JWT)
if err != nil {
log.DefaultLogger.Error("CheckHealth: Fail NewGoogleClient", "error", err.Error())
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "CheckHealth: Fail NewGoogleClient" + err.Error(),
}, nil
}
accountSummaries, err := ga.GetAccountSummaries(ctx, config)
if err != nil {
log.DefaultLogger.Error("CheckHealth: Fail getAllProfilesList", "error", err.Error())
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "CheckHealth: Fail getProfileList" + err.Error(),
}, nil
}
if len(accountSummaries) == 0 {
log.DefaultLogger.Error("CheckHealth: Not Exist Valid Profile")
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "CheckHealth: Not Exist Valid Profile",
}, nil
}
testData := model.QueryModel{AccountID: accountSummaries[0].Account, WebPropertyID: accountSummaries[0].PropertySummaries[0].Property, ProfileID: accountSummaries[0].PropertySummaries[0].ProfileSummaries[0].Profile, StartDate: "yesterday", EndDate: "today", RefID: "a", Metrics: []string{"ga:sessions"}, TimeDimension: "ga:date", Dimensions: []string{"ga:date"}, PageSize: GaReportMaxResult, PageToken: "", UseNextPage: false, Timezone: "UTC", FiltersExpression: "", Offset: GaDefaultIdx}
res, err := client.getReport(testData)
if err != nil {
log.DefaultLogger.Error("CheckHealth: GET request to analyticsreporting/v4 returned error", "error", err.Error())
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: "CheckHealth: Test Request Fail" + err.Error(),
}, nil
}
if res != nil {
log.DefaultLogger.Debug("HTTPStatusCode", "status", res.HTTPStatusCode)
log.DefaultLogger.Debug("res", res)
}
printResponse(res)
return &backend.CheckHealthResult{
Status: status,
Message: message,
}, nil
}
// remove no profile account
func (ga *GoogleAnalytics) GetAccountSummaries(ctx context.Context, config *setting.DatasourceSecretSettings) ([]*model.AccountSummary, error) {
client, err := NewGoogleClient(ctx, config.JWT)
if err != nil {
return nil, fmt.Errorf("failed to create Google API client: %w", err)
}
cacheKey := fmt.Sprintf("analytics:accountsummaries:%s", config.JWT)
if item, _, found := ga.Cache.GetWithExpiration(cacheKey); found {
return item.([]*model.AccountSummary), nil
}
rawAccountSummaries, err := client.getAccountSummaries(GaDefaultIdx)
if err != nil {
return nil, err
}
log.DefaultLogger.Debug("UA GetAccountSummaries raw accounts", "debug", rawAccountSummaries)
var accounts []*model.AccountSummary
for _, rawAccountSummary := range rawAccountSummaries {
if len(rawAccountSummary.WebProperties) == 0 {
continue
}
var account = &model.AccountSummary{
Account: rawAccountSummary.Id,
DisplayName: rawAccountSummary.Name,
}
var propertySummaries = make([]*model.PropertySummary, 0)
for _, rawPropertySummary := range rawAccountSummary.WebProperties {
if len(rawPropertySummary.Profiles) == 0 {
continue
}
var propertySummary = &model.PropertySummary{
Property: rawPropertySummary.Id,
DisplayName: rawPropertySummary.Name,
Parent: rawAccountSummary.Id,
}
propertySummaries = append(propertySummaries, propertySummary)
var profileSummaries = make([]*model.ProfileSummary, 0)
for _, rawProfileSummaries := range rawPropertySummary.Profiles {
var profileSummary = &model.ProfileSummary{
Profile: rawProfileSummaries.Id,
DisplayName: rawProfileSummaries.Name,
Parent: rawPropertySummary.Id,
Type: rawProfileSummaries.Type,
}
profileSummaries = append(profileSummaries, profileSummary)
}
propertySummary.ProfileSummaries = profileSummaries
}
if len(propertySummaries) > 0 {
account.PropertySummaries = propertySummaries
accounts = append(accounts, account)
}
}
log.DefaultLogger.Debug("UA GetAccountSummaries accounts", "debug", accounts)
ga.Cache.Set(cacheKey, accounts, 60*time.Second)
return accounts, nil
}
func (ga *GoogleAnalytics) GetServiceLevel(ctx context.Context, config *setting.DatasourceSecretSettings, accountId string, webPropertyId string) (string, error) {
return "", nil
}
func (ga *GoogleAnalytics) GetRealtimeDimensions(ctx context.Context, config *setting.DatasourceSecretSettings, propertyId string) ([]model.MetadataItem, error) {
cacheKey := "ga:metadata:" + propertyId + ":realtime-dimensions"
if dimensions, _, found := ga.Cache.GetWithExpiration(cacheKey); found {
return dimensions.([]model.MetadataItem), nil
}
return nil, nil
}
func (ga *GoogleAnalytics) GetRealTimeMetrics(ctx context.Context, config *setting.DatasourceSecretSettings, propertyId string) ([]model.MetadataItem, error) {
cacheKey := "ga:metadata:" + propertyId + ":realtime-metrics"
if metrics, _, found := ga.Cache.GetWithExpiration(cacheKey); found {
return metrics.([]model.MetadataItem), nil
}
return nil, nil
}