/
aggregate_query.go
151 lines (126 loc) · 5.67 KB
/
aggregate_query.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
package plausible
// AggregateQuery represents an API query for aggregate information over a period of time for a given list of metrics.
// In an aggregate query, the Period field and the Metrics field are mandatory, all the others are optional.
type AggregateQuery struct {
// Period to consider for the aggregate query. The result will include results over this period of time.
// This field is mandatory.
Period TimePeriod
// Metrics to be included in the aggregation result.
// This field is mandatory.
Metrics Metrics
// Filters is a filter over properties to narrow down the aggregation results.
// This field is optional.
Filters Filter
// ComparePreviousPeriod tells whether to include a comparison with the previous period in the query result.
// This field is optional and will default to false.
ComparePreviousPeriod bool
}
// Validate tells whether the query is valid or not.
// If the query is not valid, a string explaining why the query is not valid will be returned.
func (aq *AggregateQuery) Validate() (ok bool, invalidReason string) {
if aq.Period.IsEmpty() {
return false, "a period must be specified for an aggregate query"
}
if aq.Metrics.IsEmpty() {
return false, "at least one metric must be specified for an aggregate query"
}
return true, ""
}
func (aq *AggregateQuery) toQueryArgs() QueryArgs {
queryArgs := QueryArgs{}
queryArgs.Merge(aq.Period.toQueryArgs())
queryArgs.Merge(aq.Metrics.toQueryArgs())
if !aq.Filters.IsEmpty() {
queryArgs.Merge(aq.Filters.toQueryArgs())
}
if aq.ComparePreviousPeriod {
queryArgs.Add(QueryArg{Name: "compare", Value: "previous_period"})
}
return queryArgs
}
// AggregateResult represents the result of an aggregate query.
type AggregateResult struct {
// BounceRate represents the bounce rate result for the query.
// Only use this field if you included the BounceRate metric in your query.
BounceRate float64 `json:"bounce_rate"`
// BounceRateChange represents the bounce rate change compared to the previous period.
// Only use this field if you included the BounceRate metric in your query and ComparePreviousPeriod was set to true.
BounceRateChange float64 `json:"bounce_rate_change"`
// Pageviews represents the page view result for the query.
// Only use this field if you included the PageViews metric in your query.
Pageviews int `json:"pageviews"`
// PageviewsChange represents change in the number of pageviews compared to the previous period.
// Only use this field if you included the PageViews metric in your query and ComparePreviousPeriod was set to true.
PageviewsChange int `json:"pageviews_change"`
// VisitDuration represents the visit duration result for the query.
// Only use this field if you included the VisitDuration metric in your query
VisitDuration float64 `json:"visit_duration"`
// VisitDurationChange represents the visit duration change compared to the previous period.
// Only use this field if you included the VisitVisitDuration metric in your query and ComparePreviousPeriod was set to true.
VisitDurationChange float64 `json:"visit_duration_change"`
// Visitors represents the number of visitors result for the query.
// Only use this field if you included the Visitors metric in your query.
Visitors int `json:"visitors"`
// VisitorsChange represents the change in the number of visitors compared to the previous period.
// Only use this field if you included the Visitors metric in your query and ComparePreviousPeriod was set to true.
VisitorsChange int `json:"visitors_change"`
// Visits represents the visits result for the query.
// Only use this field if you included the Visits metric in your query
Visits int `json:"visits"`
// VisitsChange represents the visits change compared to the previous period.
// Only use this field if you included the Visits metric in your query and ComparePreviousPeriod was set to true.
VisitsChange int `json:"visits_change"`
// Events represents the events result for the query.
// Only use this field if you included the Events metric in your query
Events int `json:"events"`
// EventsChange represents the events change compared to the previous period.
// Only use this field if you included the Events metric in your query and ComparePreviousPeriod was set to true.
EventsChange int `json:"events_change"`
}
type rawAggregateResult struct {
Result *struct {
BounceRate struct {
Change float64 `json:"change"`
Value float64 `json:"value"`
} `json:"bounce_rate,omitempty"`
Events struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"events,omitempty"`
Pageviews struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"pageviews,omitempty"`
VisitDuration struct {
Change float64 `json:"change"`
Value float64 `json:"value"`
} `json:"visit_duration,omitempty"`
Visitors struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"visitors,omitempty"`
Visits struct {
Change int `json:"change"`
Value int `json:"value"`
} `json:"visits,omitempty"`
} `json:"results,omitempty"`
}
func (r *rawAggregateResult) toAggregateResult() AggregateResult {
var res AggregateResult
if r.Result == nil {
return res
}
res.BounceRate = r.Result.BounceRate.Value
res.BounceRateChange = r.Result.BounceRate.Change
res.Events = r.Result.Events.Value
res.EventsChange = r.Result.Events.Change
res.Pageviews = r.Result.Pageviews.Value
res.PageviewsChange = r.Result.Pageviews.Change
res.VisitDuration = r.Result.VisitDuration.Value
res.VisitDurationChange = r.Result.VisitDuration.Change
res.Visitors = r.Result.Visitors.Value
res.VisitorsChange = r.Result.Visitors.Change
res.Visits = r.Result.Visits.Value
res.VisitsChange = r.Result.Visits.Change
return res
}