forked from emiddleton/gads
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcampaign.go
503 lines (460 loc) · 16.1 KB
/
campaign.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package gads
import (
"encoding/xml"
"fmt"
)
// CampaignService A campaignService holds the connection information for the
// campaign service.
type CampaignService struct {
Auth
}
// NewCampaignService creates a new campaignService
func NewCampaignService(auth *Auth) *CampaignService {
return &CampaignService{Auth: *auth}
}
// ConversionOptimizerEligibility
//
// RejectionReasons can be any of
// "CAMPAIGN_IS_NOT_ACTIVE", "NOT_CPC_CAMPAIGN","CONVERSION_TRACKING_NOT_ENABLED",
// "NOT_ENOUGH_CONVERSIONS", "UNKNOWN"
//
type conversionOptimizerEligibility struct {
Eligible bool `xml:"eligible"` // is eligible for optimization
RejectionReasons []string `xml:"rejectionReasons"` // reason for why campaign is
// not eligible for conversion optimization.
}
type FrequencyCap struct {
Impressions int64 `xml:"impressions"`
TimeUnit string `xml:"timeUnit"`
Level string `xml:"level,omitempty"`
}
type CampaignSetting struct {
XMLName xml.Name `xml:"settings"`
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
// GeoTargetTypeSetting
PositiveGeoTargetType *string `xml:"positiveGeoTargetType,omitempty"`
NegativeGeoTargetType *string `xml:"negativeGeoTargetType,omitempty"`
// RealTimeBiddingSetting
OptIn *bool `xml:"optIn,omitempty"`
// DynamicSearchAdsSetting
DomainName *string `xml:"domainName,omitempty"`
LanguageCode *string `xml:"langaugeCode,omitempty"`
// TrackingSetting
TrackingUrl *string `xml:"trackingUrl,omitempty"`
// ShoppingSetting
MerchantId *int64 `xml:"merchantId,omitempty"`
SalesCountry *string `xml:"salesCountry,omitempty"`
CampaignPriority *int `xml:"campaignPriority"`
EnableLocal *bool `xml:"enableLocal,omitempty"`
}
func NewDynamicSearchAdsSetting(domainName, languageCode string) CampaignSetting {
return CampaignSetting{
Type: "DynamicSearchAdsSetting",
DomainName: &domainName,
LanguageCode: &languageCode,
}
}
func NewGeoTargetTypeSetting(positiveGeoTargetType, negativeGeoTargetType string) CampaignSetting {
return CampaignSetting{
Type: "GeoTargetTypeSetting",
PositiveGeoTargetType: &positiveGeoTargetType,
NegativeGeoTargetType: &negativeGeoTargetType,
}
}
func NewRealTimeBiddingSetting(optIn bool) CampaignSetting {
return CampaignSetting{
Type: "RealTimeBiddingSetting",
OptIn: &optIn,
}
}
func NewTrackingSetting(trackingUrl string) CampaignSetting {
return CampaignSetting{
Type: "TrackingSetting",
TrackingUrl: &trackingUrl,
}
}
func NewShoppingSetting(merchantID int64, salesCountry string, campaignPriority int, enableLocal bool) CampaignSetting {
return CampaignSetting{
Type: "ShoppingSetting",
MerchantId: &merchantID,
SalesCountry: &salesCountry,
CampaignPriority: &campaignPriority,
EnableLocal: &enableLocal,
}
}
type NetworkSetting struct {
TargetGoogleSearch bool `xml:"targetGoogleSearch"`
TargetSearchNetwork bool `xml:"targetSearchNetwork"`
TargetContentNetwork bool `xml:"targetContentNetwork"`
TargetPartnerSearchNetwork bool `xml:"targetPartnerSearchNetwork"`
}
/*
BIDDING SCHEME
*/
// BiddingSchemeInterface interface for bidding scheme
type BiddingSchemeInterface interface {
GetType() string
}
// BiddingScheme struct for ManualCpcBiddingScheme
type BiddingScheme struct {
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
EnhancedCpcEnabled bool `xml:"enhancedCpcEnabled"`
}
// NewBiddingScheme returns new instance of BiddingScheme
func NewBiddingScheme(enhancedCpcEnabled bool) *BiddingScheme {
return &BiddingScheme{Type: `ManualCpcBiddingScheme`, EnhancedCpcEnabled: enhancedCpcEnabled}
}
// GetType return type of bidding scheme
func (s *BiddingScheme) GetType() string {
return s.Type
}
// TargetRoasBiddingScheme struct for TargetRoasBiddingScheme
type TargetRoasBiddingScheme struct {
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
TargetRoas float64 `xml:"targetRoas"`
BidCeiling *int64 `xml:"bidCeiling>microAmount"`
BidFloor *int64 `xml:"bidFloor>microAmount"`
}
// NewTargetRoasBiddingScheme returns new instance of TargetRoasBiddingScheme
func NewTargetRoasBiddingScheme(targetRoas float64, bidCeiling, bidFloor *int64) *TargetRoasBiddingScheme {
return &TargetRoasBiddingScheme{
Type: `TargetRoasBiddingScheme`,
TargetRoas: targetRoas,
BidCeiling: bidCeiling,
BidFloor: bidFloor,
}
}
// GetType return type of bidding scheme
func (s *TargetRoasBiddingScheme) GetType() string {
return s.Type
}
func biddingSchemeUnmarshalXML(dec *xml.Decoder, start xml.StartElement) (BiddingSchemeInterface, error) {
biddingSchemeType, err := findAttr(start.Attr, xml.Name{Space: "http://www.w3.org/2001/XMLSchema-instance", Local: "type"})
if err != nil {
return nil, err
}
switch biddingSchemeType {
case "ManualCpcBiddingScheme":
c := &BiddingScheme{Type: biddingSchemeType}
return c, dec.DecodeElement(c, &start)
case "TargetRoasBiddingScheme":
c := &TargetRoasBiddingScheme{Type: biddingSchemeType}
return c, dec.DecodeElement(c, &start)
default:
if StrictMode {
return nil, fmt.Errorf("unknown bidding scheme type %#v", biddingSchemeType)
}
return nil, nil
}
}
type Bid struct {
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
Amount int64 `xml:"bid>microAmount"`
CpcBidSource *string `xml:"cpcBidSource"`
CpmBidSource *string `xml:"cpmBidSource"`
}
type BiddingStrategyConfiguration struct {
StrategyId int64 `xml:"biddingStrategyId,omitempty"`
StrategyName string `xml:"biddingStrategyName,omitempty"`
StrategyType string `xml:"biddingStrategyType,omitempty"`
StrategySource string `xml:"biddingStrategySource,omitempty"`
Scheme BiddingSchemeInterface `xml:"biddingScheme,omitempty"`
Bids []Bid `xml:"bids"`
}
// UnmarshalXML special unmarshal for the different bidding schemes
func (b *BiddingStrategyConfiguration) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
for token, err := dec.Token(); err == nil; token, err = dec.Token() {
if err != nil {
return err
}
switch start := token.(type) {
case xml.StartElement:
switch start.Name.Local {
case "biddingStrategyId":
if err := dec.DecodeElement(&b.StrategyId, &start); err != nil {
return err
}
case "biddingStrategyName":
if err := dec.DecodeElement(&b.StrategyName, &start); err != nil {
return err
}
case "biddingStrategyType":
if err := dec.DecodeElement(&b.StrategyType, &start); err != nil {
return err
}
case "biddingStrategySource":
if err := dec.DecodeElement(&b.StrategySource, &start); err != nil {
return err
}
case "biddingScheme":
bs, err := biddingSchemeUnmarshalXML(dec, start)
if err != nil {
return err
}
b.Scheme = bs
case "bids":
if err := dec.DecodeElement(&b.Bids, &start); err != nil {
return err
}
}
}
}
return nil
}
type CustomParameter struct {
Key string `xml:"key"`
Value string `xml:"value"`
IsRemove bool `xml:"isRemove"`
}
type CustomParameters struct {
CustomParameters []CustomParameter `xml:"parameters"`
DoReplace bool `xml:"doReplace"`
}
type Campaign struct {
Id int64 `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
Status string `xml:"status,omitempty"` // Status: "ENABLED", "PAUSED", "REMOVED"
ServingStatus *string `xml:"servingStatus,omitempty"` // ServingStatus: "SERVING", "NONE", "ENDED", "PENDING", "SUSPENDED"
StartDate string `xml:"startDate,omitempty"`
EndDate *string `xml:"endDate,omitempty"`
BudgetId int64 `xml:"budget>budgetId,omitempty"`
ConversionOptimizerEligibility *conversionOptimizerEligibility `xml:"conversionOptimizerEligibility,omitempty"`
AdServingOptimizationStatus string `xml:"adServingOptimizationStatus,omitempty"`
FrequencyCap *FrequencyCap `xml:"frequencyCap,omitempty"`
Settings []CampaignSetting `xml:"settings"`
AdvertisingChannelType string `xml:"advertisingChannelType,omitempty"` // "UNKNOWN", "SEARCH", "DISPLAY", "SHOPPING"
AdvertisingChannelSubType *string `xml:"advertisingChannelSubType,omitempty"` // "UNKNOWN", "SEARCH_MOBILE_APP", "DISPLAY_MOBILE_APP", "SEARCH_EXPRESS", "DISPLAY_EXPRESS"
NetworkSetting *NetworkSetting `xml:"networkSetting"`
Labels []Label `xml:"labels,omitempty"`
BiddingStrategyConfiguration *BiddingStrategyConfiguration `xml:"biddingStrategyConfiguration,omitempty"`
ForwardCompatibilityMap *map[string]string `xml:"forwardCompatibilityMap,omitempty"`
TrackingUrlTemplate *string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
BaseCampaignID *int64 `xml:"baseCampaignId,omitempty"`
CampaignTrialType *string `xml:"campaignTrialType,omitempty"`
Errors []error `xml:"-"`
}
type CampaignOperations map[string][]Campaign
type CampaignLabel struct {
CampaignId int64 `xml:"campaignId"`
LabelId int64 `xml:"labelId"`
}
type CampaignLabelOperations map[string][]CampaignLabel
// Get returns an array of Campaign's and the total number of campaign's matching
// the selector.
//
// Example
//
// campaigns, totalCount, err := campaignService.Get(
// gads.Selector{
// Fields: []string{
// "AdGroupId",
// "Status",
// "AdGroupCreativeApprovalStatus",
// "AdGroupAdDisapprovalReasons",
// "AdGroupAdTrademarkDisapproved",
// },
// Predicates: []gads.Predicate{
// {"AdGroupId", "EQUALS", []string{adGroupId}},
// },
// },
// )
//
// Selectable fields are
// "Id", "Name", "Status", "ServingStatus", "StartDate", "EndDate", "AdServingOptimizationStatus",
// "Settings", "AdvertisingChannelType", "AdvertisingChannelSubType", "Labels", "TrackingUrlTemplate",
// "UrlCustomParameters"
//
// filterable fields are
// "Id", "Name", "Status", "ServingStatus", "StartDate", "EndDate", "AdvertisingChannelType",
// "AdvertisingChannelSubType", "Labels", "TrackingUrlTemplate"
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/CampaignService#get
//
func (s *CampaignService) Get(selector Selector) (campaigns []Campaign, totalCount int64, err error) {
selector.XMLName = xml.Name{"", "serviceSelector"}
respBody, err := s.Auth.request(
campaignServiceUrl,
"get",
struct {
XMLName xml.Name
Sel Selector
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "get",
},
Sel: selector,
},
)
if err != nil {
return campaigns, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
Campaigns []Campaign `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return campaigns, totalCount, err
}
return getResp.Campaigns, getResp.Size, err
}
// Mutate allows you to add and modify campaigns, returning the
// campaigns. Note that the "REMOVE" operator is not supported.
// To remove a campaign set its Status to "REMOVED".
//
// Example
//
// campaignNeedingRemoval.Status = "REMOVED"
// ads, err := campaignService.Mutate(
// gads.CampaignOperations{
// "ADD": {
// gads.Campaign{
// Name: "my campaign name",
// Status: "PAUSED",
// StartDate: time.Now().Format("20060102"),
// BudgetId: 321543214,
// AdServingOptimizationStatus: "ROTATE_INDEFINITELY",
// Settings: []gads.CampaignSetting{
// gads.NewRealTimeBiddingSetting(true),
// },
// AdvertisingChannelType: "SEARCH",
// BiddingStrategyConfiguration: &gads.BiddingStrategyConfiguration{
// StrategyType: "MANUAL_CPC",
// },
// },
// campaignNeedingRemoval,
// },
// "SET": {
// modifiedCampaign,
// },
// }
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/CampaignService#mutate
//
func (s *CampaignService) Mutate(campaignOperations CampaignOperations) (campaigns []Campaign, err error) {
type campaignOperation struct {
Action string `xml:"operator"`
Campaign Campaign `xml:"operand"`
}
operations := []campaignOperation{}
for action, campaigns := range campaignOperations {
for _, campaign := range campaigns {
// you can't mutate those fields
// if you want to perform campaign mutate from campaign get
// you can't.
campaign.CampaignTrialType = nil
campaign.AdServingOptimizationStatus = ""
// you can't mutate this field too
//if campaign.BiddingStrategyConfiguration != nil {
// campaign.BiddingStrategyConfiguration.StrategyType = ""
//}
operations = append(operations,
campaignOperation{
Action: action,
Campaign: campaign,
},
)
}
}
mutation := struct {
XMLName xml.Name
Ops []campaignOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "mutate",
},
Ops: operations}
respBody, err := s.Auth.request(campaignServiceUrl, "mutate", mutation)
if err != nil {
return campaigns, err
}
mutateResp := struct {
BaseResponse
Campaigns []Campaign `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return campaigns, err
}
if len(mutateResp.PartialFailureErrors) > 0 {
err = mutateResp.PartialFailureErrors
}
return mutateResp.Campaigns, err
}
// Mutate allows you to add and removes labels from campaigns.
//
// Example
//
// cls, err := campaignService.MutateLabel(
// gads.CampaignOperations{
// "ADD": {
// gads.CampaignLabel{CampaignId: 3200, LabelId: 5353},
// gads.CampaignLabel{CampaignId: 4320, LabelId: 5643},
// },
// "REMOVE": {
// gads.CampaignLabel{CampaignId: 3653, LabelId: 5653},
// },
// }
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/CampaignService#mutateLabel
//
func (s *CampaignService) MutateLabel(campaignLabelOperations CampaignLabelOperations) (campaignLabels []CampaignLabel, err error) {
type campaignLabelOperation struct {
Action string `xml:"operator"`
CampaignLabel CampaignLabel `xml:"operand"`
}
operations := []campaignLabelOperation{}
for action, campaignLabels := range campaignLabelOperations {
for _, campaignLabel := range campaignLabels {
operations = append(operations,
campaignLabelOperation{
Action: action,
CampaignLabel: campaignLabel,
},
)
}
}
mutation := struct {
XMLName xml.Name
Ops []campaignLabelOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "mutateLabel",
},
Ops: operations}
respBody, err := s.Auth.request(campaignServiceUrl, "mutateLabel", mutation)
if err != nil {
return campaignLabels, err
}
mutateResp := struct {
BaseResponse
CampaignLabels []CampaignLabel `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return campaignLabels, err
}
if len(mutateResp.PartialFailureErrors) > 0 {
err = mutateResp.PartialFailureErrors
}
return mutateResp.CampaignLabels, err
}
// Query is not yet implemented
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/CampaignService#query
//
func (s *CampaignService) Query(query string) (campaigns []Campaign, totalCount int64, err error) {
return campaigns, totalCount, ERROR_NOT_YET_IMPLEMENTED
}