-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalytics.go
131 lines (105 loc) · 3.41 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
package mailersend
import (
"context"
"fmt"
"net/http"
)
const analyticsBasePath = "/analytics"
type AnalyticsService service
// analyticsActivityRoot - format of analytics response
type analyticsActivityRoot struct {
Data analyticsData `json:"data"`
}
type analyticsData struct {
DateFrom string `json:"date_from"`
DateTo string `json:"date_to"`
GroupBy string `json:"group_by"`
Stats []analyticsStats `json:"stats"`
}
type analyticsStats struct {
Date string `json:"date"`
Queued int `json:"queued,omitempty"`
Sent int `json:"sent,omitempty"`
Delivered int `json:"delivered,omitempty"`
SoftBounced int `json:"soft_bounced,omitempty"`
HardBounced int `json:"hard_bounced,omitempty"`
Junk int `json:"junk,omitempty"`
Opened int `json:"opened,omitempty"`
Clicked int `json:"clicked,omitempty"`
Unsubscribed int `json:"unsubscribed,omitempty"`
SpamComplaints int `json:"spam_complaints,omitempty"`
}
type opensRoot struct {
Data openData `json:"data"`
}
type openData struct {
DateFrom int `json:"date_from"`
DateTo int `json:"date_to"`
Stats []openStats `json:"stats"`
}
type openStats struct {
Name string `json:"name"`
Count int `json:"count"`
}
// AnalyticsOptions - modifies the behavior of AnalyticsService methods
type AnalyticsOptions struct {
DomainID string `url:"domain_id,omitempty"`
RecipientID []int64 `url:"recipient_id,omitempty"`
DateFrom int64 `url:"date_from"`
DateTo int64 `url:"date_to"`
GroupBy string `url:"group_by,omitempty"`
Tags []string `url:"tags[],omitempty"`
Event []string `url:"event[],omitempty"`
}
func (s *AnalyticsService) GetActivityByDate(ctx context.Context, options *AnalyticsOptions) (*analyticsActivityRoot, *Response, error) {
path := fmt.Sprintf("%s/date", analyticsBasePath)
req, err := s.client.newRequest("GET", path, options)
if err != nil {
return nil, nil, err
}
root := new(analyticsActivityRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *AnalyticsService) GetOpensByCountry(ctx context.Context, options *AnalyticsOptions) (*opensRoot, *Response, error) {
path := fmt.Sprintf("%s/country", analyticsBasePath)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(opensRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *AnalyticsService) GetOpensByUserAgent(ctx context.Context, options *AnalyticsOptions) (*opensRoot, *Response, error) {
path := fmt.Sprintf("%s/ua-name", analyticsBasePath)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(opensRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *AnalyticsService) GetOpensByReadingEnvironment(ctx context.Context, options *AnalyticsOptions) (*opensRoot, *Response, error) {
path := fmt.Sprintf("%s/ua-type", analyticsBasePath)
req, err := s.client.newRequest(http.MethodGet, path, options)
if err != nil {
return nil, nil, err
}
root := new(opensRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}