-
Notifications
You must be signed in to change notification settings - Fork 6
/
analytics.go
53 lines (45 loc) · 1.62 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
package jwplatform
import (
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// AnalyticsQueryParameters define the allowed parameters on the Query action.
type AnalyticsQueryParameters struct {
Source string `url:"source"`
Format string `url:"format"`
}
// AnalyticsResponse is the structure returned via the Query action.
type AnalyticsResponse struct {
Dimensions []string `json:"dimensions"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
Filter string `json:"filter"`
IncludeMetadata bool `json:"include_metadata"`
Metrics []reportMetric `json:"metrics"`
Sort []reportSort `json:"sort"`
Page int `json:"page"`
PageLength int `json:"page_length"`
RelativeTimeframe string `json:"relative_timeframe"`
}
type reportMetric struct {
Field string `json:"field"`
Operation string `json:"operation"`
}
type reportSort struct {
Field string `json:"field"`
Operation string `json:"operation"`
Order string `json:"order"`
}
// AnalyticsClient for interacting with V2 Analytics API.
type AnalyticsClient struct {
v2Client *V2Client
}
// Query the Analytics API
func (c *AnalyticsClient) Query(siteID string, queryParams *AnalyticsQueryParameters) (*AnalyticsResponse, error) {
analyticResponse := &AnalyticsResponse{}
path := fmt.Sprintf("/v2/sites/%s/analytics/queries", siteID)
urlValues, _ := query.Values(queryParams)
err := c.v2Client.Request(http.MethodPost, path, analyticResponse, nil, urlValues)
return analyticResponse, err
}