-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcampaigns.go
386 lines (330 loc) · 12.9 KB
/
campaigns.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package mailerlite
import (
"context"
"fmt"
"net/http"
)
const campaignEndpoint = "/campaigns"
// CampaignService defines an interface for campaign-related operations.
type CampaignService interface {
List(ctx context.Context, options *ListCampaignOptions) (*RootCampaigns, *Response, error)
Get(ctx context.Context, campaignID string) (*RootCampaign, *Response, error)
Create(ctx context.Context, campaign *CreateCampaign) (*RootCampaign, *Response, error)
Update(ctx context.Context, campaignID string, campaign *UpdateCampaign) (*RootCampaign, *Response, error)
Schedule(ctx context.Context, campaignID string, campaign *ScheduleCampaign) (*RootCampaign, *Response, error)
Cancel(ctx context.Context, campaignID string) (*RootCampaign, *Response, error)
Subscribers(ctx context.Context, options *ListCampaignSubscriberOptions) (*RootCampaignSubscribers, *Response, error)
Languages(ctx context.Context) (*RootCampaignLanguages, *Response, error)
Delete(ctx context.Context, campaignID string) (*Response, error)
}
// campaignService implements CampaignService.
type campaignService struct {
*service
}
// RootCampaigns - campaigns response
type RootCampaigns struct {
Data []Campaign `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
// RootCampaign - single campaign response
type RootCampaign struct {
Data Campaign `json:"data"`
}
type RootCampaignSubscribers struct {
Data []CampaignSubscriber `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type RootCampaignLanguages struct {
Data []CampaignLanguage
}
type Campaign struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
MissingData []interface{} `json:"missing_data"`
Settings CampaignSettings `json:"settings"`
Filter [][]CampaignFilter `json:"filter"`
FilterForHumans [][]string `json:"filter_for_humans"`
DeliverySchedule string `json:"delivery_schedule"`
LanguageID string `json:"language_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ScheduledFor string `json:"scheduled_for"`
QueuedAt string `json:"queued_at"`
StartedAt string `json:"started_at"`
FinishedAt string `json:"finished_at"`
StoppedAt interface{} `json:"stopped_at"`
DefaultEmailID string `json:"default_email_id"`
Emails []Email `json:"emails"`
UsedInAutomations bool `json:"used_in_automations"`
TypeForHumans string `json:"type_for_humans"`
Stats Stats `json:"stats"`
IsStopped bool `json:"is_stopped"`
HasWinner interface{} `json:"has_winner"`
WinnerVersionForHuman interface{} `json:"winner_version_for_human"`
WinnerSendingTimeForHumans interface{} `json:"winner_sending_time_for_humans"`
WinnerSelectedManuallyAt interface{} `json:"winner_selected_manually_at"`
UsesEcommerce bool `json:"uses_ecommerce"`
UsesSurvey bool `json:"uses_survey"`
CanBeScheduled bool `json:"can_be_scheduled"`
Warnings []interface{} `json:"warnings"`
InitialCreatedAt interface{} `json:"initial_created_at"`
IsCurrentlySendingOut bool `json:"is_currently_sending_out"`
}
type CampaignSettings struct {
TrackOpens string `json:"track_opens"`
UseGoogleAnalytics string `json:"use_google_analytics"`
EcommerceTracking string `json:"ecommerce_tracking"`
}
type CampaignFilter struct {
Operator string `json:"operator"`
Args []interface{} `json:"args"`
}
type Email struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
EmailableID string `json:"emailable_id"`
EmailableType string `json:"emailable_type"`
Type string `json:"type"`
From string `json:"from"`
FromName string `json:"from_name"`
Name string `json:"name"`
Subject string `json:"subject"`
PlainText string `json:"plain_text"`
ScreenshotURL string `json:"screenshot_url"`
PreviewURL string `json:"preview_url"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
IsDesigned bool `json:"is_designed"`
LanguageID float64 `json:"language_id"`
IsWinner bool `json:"is_winner"`
Stats Stats `json:"stats"`
SendAfter interface{} `json:"send_after"`
TrackOpens bool `json:"track_opens"`
}
type Stats struct {
Sent int `json:"sent"`
OpensCount int `json:"opens_count"`
UniqueOpensCount int `json:"unique_opens_count"`
OpenRate OpenRate `json:"open_rate"`
ClicksCount int `json:"clicks_count"`
UniqueClicksCount int `json:"unique_clicks_count"`
ClickRate ClickRate `json:"click_rate"`
UnsubscribesCount int `json:"unsubscribes_count"`
UnsubscribeRate UnsubscribeRate `json:"unsubscribe_rate"`
SpamCount int `json:"spam_count"`
SpamRate SpamRate `json:"spam_rate"`
HardBouncesCount int `json:"hard_bounces_count"`
HardBounceRate HardBounceRate `json:"hard_bounce_rate"`
SoftBouncesCount int `json:"soft_bounces_count"`
SoftBounceRate SoftBounceRate `json:"soft_bounce_rate"`
ForwardsCount int `json:"forwards_count"`
ClickToOpenRate ClickToOpenRate `json:"click_to_open_rate"`
}
// ListCampaignOptions - modifies the behavior of campaignService.List method
type ListCampaignOptions struct {
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
// GetCampaignOptions - modifies the behavior of campaignService.Get method
type GetCampaignOptions struct {
ID int `json:"id,omitempty"`
}
type UpdateCampaign CreateCampaign
// CreateCampaign - modifies the behavior of campaignService.Create method
type CreateCampaign struct {
Name string `json:"name"`
LanguageID int `json:"language_id,omitempty"`
Type string `json:"type"`
Emails []Emails `json:"emails"`
Groups []string `json:"groups,omitempty"`
Segments []string `json:"segments,omitempty"`
AbSettings *AbSettings `json:"ab_settings,omitempty"`
ResendSettings *ResendSettings `json:"resend_settings,omitempty"`
}
type Emails struct {
Subject string `json:"subject"`
FromName string `json:"from_name"`
From string `json:"from"`
Content string `json:"content"`
}
type AbSettings struct {
TestType string `json:"test_type"`
SelectWinnerBy string `json:"select_winner_by"`
AfterTimeAmount int `json:"after_time_amount"`
AfterTimeUnit string `json:"after_time_unit"`
TestSplit int `json:"test_split"`
BValue BValue `json:"b_value"`
}
type BValue struct {
Subject string `json:"subject,omitempty"`
FromName string `json:"from_name,omitempty"`
From string `json:"from,omitempty"`
}
type ResendSettings struct {
TestType string `json:"test_type"`
SelectWinnerBy string `json:"select_winner_by"`
BValue BValue `json:"b_value"`
}
// ScheduleCampaign - modifies the behavior of campaignService.Schedule method
type ScheduleCampaign struct {
Delivery string `json:"delivery"`
Schedule *Schedule `json:"schedule,omitempty"`
Resend *Resend `json:"resend,omitempty"`
}
type Schedule struct {
Date string `json:"date"`
Hours string `json:"hours"`
Minutes string `json:"minutes"`
TimezoneID int `json:"timezone_id,omitempty"`
}
type Resend struct {
Delivery string `json:"delivery"`
Date string `json:"date"`
Hours string `json:"hours"`
Minutes string `json:"minutes"`
TimezoneID int `json:"timezone_id,omitempty"`
}
type CampaignSubscriber struct {
ID string `json:"id"`
OpensCount int `json:"opens_count"`
ClicksCount int `json:"clicks_count"`
Subscriber Subscriber `json:"subscriber"`
}
type CampaignLanguage struct {
Id string `json:"id"`
Shortcode string `json:"shortcode"`
Iso639 string `json:"iso639"`
Name string `json:"name"`
Direction string `json:"direction"`
}
type ListCampaignSubscriberOptions struct {
CampaignID string `url:"-"`
Filters *[]Filter `json:"filters,omitempty"`
Page int `url:"page,omitempty"`
Sort string `url:"sort,omitempty"`
Limit int `url:"limit,omitempty"`
}
// List - list of campaigns
func (s *campaignService) List(ctx context.Context, options *ListCampaignOptions) (*RootCampaigns, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, campaignEndpoint, options)
if err != nil {
return nil, nil, err
}
root := new(RootCampaigns)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
// Get - get a single campaign ID
func (s *campaignService) Get(ctx context.Context, campaignID string) (*RootCampaign, *Response, error) {
path := fmt.Sprintf("%s/%s", campaignEndpoint, campaignID)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(RootCampaign)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *campaignService) Create(ctx context.Context, campaign *CreateCampaign) (*RootCampaign, *Response, error) {
req, err := s.client.newRequest(http.MethodPost, campaignEndpoint, campaign)
if err != nil {
return nil, nil, err
}
root := new(RootCampaign)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *campaignService) Update(ctx context.Context, campaignID string, campaign *UpdateCampaign) (*RootCampaign, *Response, error) {
path := fmt.Sprintf("%s/%s", campaignEndpoint, campaignID)
req, err := s.client.newRequest(http.MethodPut, path, campaign)
if err != nil {
return nil, nil, err
}
root := new(RootCampaign)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *campaignService) Schedule(ctx context.Context, campaignID string, campaign *ScheduleCampaign) (*RootCampaign, *Response, error) {
path := fmt.Sprintf("%s/%s/schedule", campaignEndpoint, campaignID)
req, err := s.client.newRequest(http.MethodPost, path, campaign)
if err != nil {
return nil, nil, err
}
root := new(RootCampaign)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
// Cancel - cancel a single campaign
func (s *campaignService) Cancel(ctx context.Context, campaignID string) (*RootCampaign, *Response, error) {
path := fmt.Sprintf("%s/%s/cancel", campaignEndpoint, campaignID)
req, err := s.client.newRequest(http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}
root := new(RootCampaign)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
// Subscribers - get subscribers activity of a campaign
func (s *campaignService) Subscribers(ctx context.Context, options *ListCampaignSubscriberOptions) (*RootCampaignSubscribers, *Response, error) {
path := fmt.Sprintf("%s/%s/reports/subscriber-activity", campaignEndpoint, options.CampaignID)
req, err := s.client.newRequest(http.MethodPost, path, options)
if err != nil {
return nil, nil, err
}
root := new(RootCampaignSubscribers)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *campaignService) Languages(ctx context.Context) (*RootCampaignLanguages, *Response, error) {
path := fmt.Sprintf("%s/languages", campaignEndpoint)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(RootCampaignLanguages)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *campaignService) Delete(ctx context.Context, campaignID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", campaignEndpoint, campaignID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
res, err := s.client.do(ctx, req, nil)
if err != nil {
return res, err
}
return res, nil
}