-
Notifications
You must be signed in to change notification settings - Fork 80
/
campaigns.go
319 lines (261 loc) Β· 9.38 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
package gochimp3
import (
"errors"
"fmt"
)
const (
campaigns_path = "/campaigns"
single_campaign_path = campaigns_path + "/%s"
campaign_content_path = single_campaign_path + "/content"
send_test_path = single_campaign_path + "/actions/test"
send_path = single_campaign_path + "/actions/send"
CAMPAIGN_TYPE_REGULAR = "regular"
CAMPAIGN_TYPE_PLAINTEXT = "plaintext"
CAMPAIGN_TYPE_ABSPLIT = "absplit" // deprecated by mailchimp
CAMPAIGN_TYPE_RSS = "rss"
CAMPAIGN_TYPE_VARIATE = "variate"
CAMPAIGN_SEND_TYPE_HTML = "html"
CAMPAIGN_SEND_TYPE_PLAINTEXT = "plaintext"
CONDITION_MATCH_ANY = "any"
CONDITION_MATCH_ALL = "all"
CONDITION_TYPE_INTERESTS = "Interests"
CONDITION_OP_CONTAINS = "interestcontains"
)
type CampaignQueryParams struct {
ExtendedQueryParams
Type string
Status string
BeforeSendTime string
SinceSendTime string
BeforeCreateTime string
SinceCreateTime string
ListId string
FolderId string
SortField string
SortDir string
}
func (q CampaignQueryParams) Params() map[string]string {
m := q.ExtendedQueryParams.Params()
m["type"] = q.Type
m["status"] = q.Status
m["before_send_time"] = q.BeforeSendTime
m["since_send_time"] = q.SinceSendTime
m["before_create_time"] = q.BeforeCreateTime
m["since_create_time"] = q.SinceCreateTime
m["list_id"] = q.ListId
m["folder_id"] = q.FolderId
m["sort_field"] = q.SortField
m["sort_dir"] = q.SortDir
return m
}
type ListOfCampaigns struct {
baseList
Campaigns []CampaignResponse `json:"campaigns"`
}
type CampaignCreationRecipients struct {
ListId string `json:"list_id"`
SegmentOptions CampaignCreationSegmentOptions `json:"segment_opts"`
}
type CampaignCreationSegmentOptions struct {
SavedSegmentId int `json:"saved_segment_id"`
Match string `json:"match"` // one of CONDITION_MATCH_*
// this accepts various payloads. See http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#create-post_campaigns
Conditions interface{} `json:"conditions"`
}
type InterestsCondition struct {
ConditionType string `json:"condition_type"`
Field string `json:"field"`
Op string `json:"op"`
Value []string `json:"value"`
}
type CampaignCreationSettings struct {
SubjectLine string `json:"subject_line"`
PreviewText string `json:"preview_text"`
Title string `json:"title"`
FromName string `json:"from_name"`
ReplyTo string `json:"reply_to"`
UseConversation bool `json:"use_conversation"`
ToName string `json:"to_name"`
FolderId string `json:"folder_id"`
Authenticate bool `json:"authenticate"`
AutoFooter bool `json:"auto_footer"`
InlineCss bool `json:"inline_css"`
AutoTweet bool `json:"auto_tweet"`
FbComments bool `json:"fb_comments"`
TemplateId uint `json:"template_id"`
}
type CampaignCreationRequest struct {
Type string `json:"type"` // must be one of the CAMPAIGN_TYPE_* consts
Recipients CampaignCreationRecipients `json:"recipients"`
Settings CampaignCreationSettings `json:"settings"`
// variate_settings not implemented
Tracking CampaignTracking `json:"tracking"`
// rss_opts not implemented
// social_card not implemented
}
type CampaignResponseRecipients struct {
ListId string `json:"list_id"`
ListName string `json:"list_name"`
SegmentText string `json:"segment_text"`
RecipientCount int `json:"recipient_count"`
}
type CampaignResponseSettings struct {
SubjectLine string `json:"subject_line"`
PreviewText string `json:"preview_text"`
Title string `json:"title"`
FromName string `json:"from_name"`
ReplyTo string `json:"reply_to"`
UseConversation bool `json:"use_conversation"`
ToName string `json:"to_name"`
FolderId string `json:"folder_id"`
Authenticate bool `json:"authenticate"`
AutoFooter bool `json:"auto_footer"`
InlineCss bool `json:"inline_css"`
AutoTweet bool `json:"auto_tweet"`
FbComments bool `json:"fb_comments"`
Timewarp bool `json:"timewarp"`
TemplateId uint `json:"template_id"`
DragAndDrop bool `json:"drag_and_drop"`
}
type CampaignTracking struct {
Opens bool `json:"opens"`
HtmlClicks bool `json:"html_clicks"`
TextClicks bool `json:"text_clicks"`
GoalTracking bool `json:"goal_tracking"`
Ecomm360 bool `json:"ecomm360"`
GoogleAnalytics string `json:"google_analytics"`
Clicktale string `json:"clicktale"`
}
type CampaignEcommerce struct {
TotalOrders int `json:"total_orders"`
TotalSpent int `json:"total_spent"`
TotalRevenue int `json:"total_revenue"`
}
type CampaignReportSummary struct {
Opens int `json:"opens"`
UniqueOpens int `json:"unique_opens"`
OpenRate float32 `json:"open_rate"`
Clicks int `json:"clicks"`
SubscriberClicks int `json:"subscriber_clicks"`
ClickRate float32 `json:"click_rate"`
Ecommerce CampaignEcommerce `json:"ecommerce"`
}
type CampaignDeliveryStatus struct {
Enabled bool `json:"enabled"`
}
type CampaignResponse struct {
withLinks
ID string `json:"id"`
WebID uint `json:"web_id"`
Type string `json:"type"`
CreateTime string `json:"create_time"`
ArchiveUrl string `json:"archive_url"`
LongArchiveUrl string `json:"long_archive_url"`
Status string `json:"status"`
EmailsSent uint `json:"emails_sent"`
SendTime string `json:"send_time"`
ContentType string `json:"content_type"`
NeedsBlockRefresh bool `json:"needs_block_refresh"`
Recipients CampaignResponseRecipients `json:"recipients"`
Settings CampaignResponseSettings `json:"settings"`
Tracking CampaignTracking `json:"tracking"`
ReportSummary CampaignReportSummary `json:"report_summary"`
DeliveryStatus CampaignDeliveryStatus `json:"delivery_status"`
api *API
}
func (campaign CampaignResponse) CanMakeRequest() error {
if campaign.ID == "" {
return errors.New("No ID provided on campaign")
}
return nil
}
func (api *API) GetCampaigns(params *CampaignQueryParams) (*ListOfCampaigns, error) {
response := new(ListOfCampaigns)
err := api.Request("GET", campaigns_path, params, nil, response)
if err != nil {
return nil, err
}
for _, l := range response.Campaigns {
l.api = api
}
return response, nil
}
func (api *API) GetCampaign(id string, params *BasicQueryParams) (*CampaignResponse, error) {
endpoint := fmt.Sprintf(single_campaign_path, id)
response := new(CampaignResponse)
response.api = api
return response, api.Request("GET", endpoint, params, nil, response)
}
func (api *API) CreateCampaign(body *CampaignCreationRequest) (*CampaignResponse, error) {
response := new(CampaignResponse)
response.api = api
return response, api.Request("POST", campaigns_path, nil, body, response)
}
func (api *API) UpdateCampaign(id string, body *CampaignCreationRequest) (*CampaignResponse, error) {
endpoint := fmt.Sprintf(single_campaign_path, id)
response := new(CampaignResponse)
response.api = api
return response, api.Request("PATCH", endpoint, nil, body, response)
}
func (api *API) DeleteCampaign(id string) (bool, error) {
endpoint := fmt.Sprintf(single_campaign_path, id)
return api.RequestOk("DELETE", endpoint)
}
// ------------------------------------------------------------------------------------------------
// Actions
// ------------------------------------------------------------------------------------------------
type TestEmailRequest struct {
TestEmails []string `json:"test_emails"`
SendType string `json:"send_type"` // one of the CAMPAIGN_SEND_TYPE_* constants
}
type SendCampaignRequest struct {
CampaignId string `json:"campaign_id"`
}
func (api *API) SendTestEmail(id string, body *TestEmailRequest) (bool, error) {
endpoint := fmt.Sprintf(send_test_path, id)
err := api.Request("POST", endpoint, nil, body, nil)
if err != nil {
return false, err
}
return true, nil
}
func (api *API) SendCampaign(id string, body *SendCampaignRequest) (bool, error) {
endpoint := fmt.Sprintf(send_path, id)
err := api.Request("POST", endpoint, nil, body, nil)
if err != nil {
return false, err
}
return true, nil
}
// ------------------------------------------------------------------------------------------------
// Campaign Content Updates
// ------------------------------------------------------------------------------------------------
type CampaignContentTemplateRequest struct {
ID uint `json:"id,omitempty"`
Sections map[string]string `json:"sections,omitempty"`
}
type CampaignContentUpdateRequest struct {
PlainText string `json:"plain_text"`
Html string `json:"html"`
Url string `json:"url"`
Template *CampaignContentTemplateRequest `json:"template,omitempty"`
}
type CampaignContentResponse struct {
withLinks
PlainText string `json:"plain_text"`
Html string `json:"html"`
ArchiveHtml string `json:"archive_html"`
api *API
}
func (api *API) GetCampaignContent(id string, params *BasicQueryParams) (*CampaignContentResponse, error) {
endpoint := fmt.Sprintf(campaign_content_path, id)
response := new(CampaignContentResponse)
response.api = api
return response, api.Request("GET", endpoint, nil, params, response)
}
func (api *API) UpdateCampaignContent(id string, body *CampaignContentUpdateRequest) (*CampaignContentResponse, error) {
endpoint := fmt.Sprintf(campaign_content_path, id)
response := new(CampaignContentResponse)
response.api = api
return response, api.Request("PUT", endpoint, nil, body, response)
}