forked from segmentio/gads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathad_group_ad.go
481 lines (451 loc) · 18.2 KB
/
ad_group_ad.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
package gads
import (
"encoding/xml"
)
type AdGroupAdService struct {
Auth
}
type AdGroupAdPolicySummary struct {
TopicEntries []PolicyTopicEntry `xml:"policyTopicEntries,omitempty"`
ReviewState string `xml:"reviewState,omitempty"`
DenormalizedStatus string `xml:"denormalizedStatus,omitempty"`
ApprovalStatus string `xml:"combinedApprovalStatus,omitempty"`
}
type PolicyTopicEntry struct {
Type string `xml:"policyTopicEntryType,omitempty"`
Evidences []PolicyTopicEvidence `xml:"policyTopicEvidences,omitempty"`
PConstraints []PolicyTopicConstraint `xml:"policyTopicConstraints,omitempty"`
ID string `xml:"policyTopicId,omitempty"`
Name string `xml:"policyTopicName,omitempty"`
}
type PolicyTopicEvidence struct {
Type string `xml:"policyTopicEvidenceType,omitempty"`
Text []string `xml:"evidenceTextList,omitempty"`
}
type PolicyTopicConstraint struct {
ConstraintType string `xml:"constraintType,omitempty"`
Type string `xml:"PolicyTopicConstraint.Type,omitempty"`
}
type AppUrl struct {
Url string `xml:"url"`
OsType string `xml:"osType"` // "OS_TYPE_IOS", "OS_TYPE_ANDROID", "UNKNOWN"
}
type TextAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Url string `xml:"url"`
DisplayUrl string `xml:"displayUrl"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
FinalAppUrls []AppUrl `xml:"finalAppUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
DevicePreference int64 `xml:"devicePreference,omitempty"`
Headline string `xml:"headline"`
Description1 string `xml:"description1"`
Description2 string `xml:"description2"`
Status string `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
Labels []Label `xml:"-"`
}
type ExpandedTextAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
HeadlinePart1 string `xml:"headlinePart1"`
HeadlinePart2 string `xml:"headlinePart2"`
Description string `xml:"description"`
Path1 string `xml:"path1"`
Path2 string `xml:"path2"`
ExperimentData *AdGroupExperimentData `xml:"-"`
Status string `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
Labels []Label `xml:"-"`
BaseCampaignId *int64 `xml:"-"`
BaseAdGroupId *int64 `xml:"-"`
}
type ResponsiveDisplayAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
MarketingImage Media `xml:"marketingImage"`
LogoImage Media `xml:"logoImage"`
ShortHeadline string `xml:"shortHeadline"`
LongHeadline string `xml:"longHeadline"`
Description string `xml:"description"`
BusinessName string `xml:"businessName"`
ExperimentData AdGroupExperimentData `xml:"-"`
Status string `xml:"-"`
Labels []Label `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
BaseCampaignId int64 `xml:"-"`
BaseAdGroupId int64 `xml:"-"`
}
type AdGroupExperimentData struct {
ExperimentId int64 `xml:"experimentId"`
ExperimentDeltaStatus string `xml:"experimentDeltaStatus"`
ExperimentDataStatus string `xml:"experimentDataStatus"`
}
type ImageAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Url string `xml:"url"`
DisplayUrl string `xml:"displayUrl"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
FinalAppUrls []AppUrl `xml:"finalAppUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
DevicePreference int64 `xml:"devicePreference,omitempty"`
Image int64 `xml:"imageId"`
Name string `xml:"name"`
AdToCopyImageFrom int64 `xml:"adToCopyImageFrom"`
Status string `xml:"-"`
Labels []Label `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
}
type MobileAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Url string `xml:"url"`
DisplayUrl string `xml:"displayUrl"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
FinalAppUrls []AppUrl `xml:"finalAppUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
DevicePreference int64 `xml:"devicePreference,omitempty"`
Headline string `xml:"headline"`
Description string `xml:"description"`
MarkupLanguages []string `xml:"markupLanguages"`
MobileCarriers []string `xml:"mobileCarriers"`
BusinessName string `xml:"businessName"`
CountryCode string `xml:"countryCode"`
PhoneNumber string `xml:"phoneNumber"`
Status string `xml:"-"`
Labels []Label `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
}
type TemplateElementField struct {
Name string `xml:"name"`
Type string `xml:"type"`
FieldText string `xml:"fieldText"`
FieldMedia string `xml:"fieldMedia"`
}
type TemplateElement struct {
UniqueName string `xml:"uniqueName"`
Fields []TemplateElementField `xml:"fields"`
}
type TemplateAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Url string `xml:"url"`
DisplayUrl string `xml:"displayUrl"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
FinalAppUrls []AppUrl `xml:"finalAppUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
DevicePreference int64 `xml:"devicePreference,omitempty"`
TemplateId int64 `xml:"templateId"`
AdUnionId int64 `xml:"adUnionId>id"`
TemplateElements []TemplateElement `xml:"templateElements"`
Dimensions []Dimensions `xml:"dimensions"`
Name string `xml:"name"`
Duration int64 `xml:"duration"`
originAdId *int64 `xml:"originAdId"`
Status string `xml:"-"`
Labels []Label `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
}
type DynamicSearchAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Url string `xml:"url"`
DisplayUrl string `xml:"displayUrl"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
FinalAppUrls []AppUrl `xml:"finalAppUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
DevicePreference int64 `xml:"devicePreference,omitempty"`
Status string `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
}
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupAdService.ExpandedDynamicSearchAd
type ExpandedDynamicSearchAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Url string `xml:"url"`
DisplayUrl string `xml:"displayUrl"`
FinalUrls []string `xml:"finalUrls,omitempty"`
FinalMobileUrls []string `xml:"finalMobileUrls,omitempty"`
FinalAppUrls []AppUrl `xml:"finalAppUrls,omitempty"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate,omitempty"`
UrlCustomParameters *CustomParameters `xml:"urlCustomParameters,omitempty"`
Type string `xml:"type,omitempty"`
DevicePreference int64 `xml:"devicePreference,omitempty"`
Description string `xml:"description"`
Status string `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
}
type ProductAd struct {
AdGroupId int64 `xml:"-"`
Id int64 `xml:"id,omitempty"`
Type string `xml:"type,omitempty"`
Status string `xml:"-"`
PolicySummary *AdGroupAdPolicySummary `xml:"-"`
}
type AdGroupAdOperations map[string]AdGroupAds
func NewAdGroupAdService(auth *Auth) *AdGroupAdService {
return &AdGroupAdService{Auth: *auth}
}
func NewTextAd(adGroupId int64, url, displayUrl, headline, description1, description2, status string) TextAd {
return TextAd{
AdGroupId: adGroupId,
Url: url,
DisplayUrl: displayUrl,
Headline: headline,
Description1: description1,
Description2: description2,
Status: status,
}
}
type AdGroupAdLabel struct {
AdGroupAdId int64 `xml:"adGroupAdId"`
LabelId int64 `xml:"labelId"`
}
type AdGroupAdLabelOperations map[string][]AdGroupAdLabel
type AdUrlUpgrade struct {
AdId int64 `xml:"adId"`
FinalUrl string `xml:"finalUrl"`
FinalMobileUrl string `xml:"finalMobileUrl"`
TrackingUrlTemplate string `xml:"trackingUrlTemplate"`
}
// Get returns an array of ad's and the total number of ad's matching
// the selector.
//
// Example
//
// ads, totalCount, err := adGroupAdService.Get(
// gads.Selector{
// Fields: []string{
// "AdGroupId",
// "Status",
// "AdGroupCreativeApprovalStatus",
// "AdGroupAdDisapprovalReasons",
// "AdGroupAdTrademarkDisapproved",
// },
// Predicates: []gads.Predicate{
// {"AdGroupId", "EQUALS", []string{adGroupId}},
// },
// },
// )
//
// Selectable fields are
// "AdGroupId", "Id", "Url", "DisplayUrl", "CreativeFinalUrls", "CreativeFinalMobileUrls",
// "CreativeFinalAppUrls", "CreativeTrackingUrlTemplate", "CreativeUrlCustomParameters",
// "DevicePreference", "Status", "AdGroupCreativeApprovalStatus", "AdGroupAdDisapprovalReasons"
// "AdGroupAdTrademarkDisapproved", "Labels"
//
// TextAd
// "Headline", "Description1", "Description2"
//
// ImageAd
// "ImageCreativeName"
//
// filterable fields are
// "AdGroupId", "Id", "Url", "DisplayUrl", "CreativeFinalUrls", "CreativeFinalMobileUrls",
// "CreativeFinalAppUrls", "CreativeTrackingUrlTemplate", "CreativeUrlCustomParameters",
// "DevicePreference", "Status", "AdGroupCreativeApprovalStatus", "AdGroupAdDisapprovalReasons"
// "Labels"
//
// TextAd specific fields
// "Headline", "Description1", "Description2"
//
// ImageAd specific fields
// "ImageCreativeName"
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupAdService#get
//
func (s AdGroupAdService) Get(selector Selector) (adGroupAds AdGroupAds, totalCount int64, err error) {
selector.XMLName = xml.Name{baseUrl, "serviceSelector"}
respBody, err := s.Auth.request(
adGroupAdServiceUrl,
"get",
struct {
XMLName xml.Name
Sel Selector
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "get",
},
Sel: selector,
},
nil,
)
if err != nil {
return adGroupAds, totalCount, err
}
getResp := struct {
Size int64 `xml:"rval>totalNumEntries"`
AdGroupAds AdGroupAds `xml:"rval>entries"`
}{}
err = xml.Unmarshal([]byte(respBody), &getResp)
if err != nil {
return adGroupAds, totalCount, err
}
return getResp.AdGroupAds, getResp.Size, err
}
// Mutate allows you to add, modify and remove ads, returning the
// modified ads.
//
// Example
//
// ads, err := adGroupAdService.Mutate(
// gads.AdGroupAdOperations{
// "ADD": {
// gads.NewTextAd(
// adGroup.Id,
// "https://classdo.com/en",
// "classdo.com",
// "test headline",
// "test line one",
// "test line two",
// "PAUSED",
// ),
// },
// "SET": {
// modifiedAd,
// },
// "REMOVE": {
// adNeedingRemoval,
// },
// },
// )
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupAdService#mutate
//
func (s *AdGroupAdService) Mutate(adGroupAdOperations AdGroupAdOperations) (adGroupAds AdGroupAds, err error) {
type adGroupAdOperation struct {
Action string `xml:"operator"`
AdGroupAd AdGroupAds `xml:"operand"`
}
operations := []adGroupAdOperation{}
for action, adGroupAds := range adGroupAdOperations {
for _, adGroupAd := range adGroupAds {
operations = append(operations,
adGroupAdOperation{
Action: action,
AdGroupAd: []interface{}{adGroupAd},
},
)
}
}
mutation := struct {
XMLName xml.Name
Ops []adGroupAdOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "mutate",
},
Ops: operations,
}
respBody, err := s.Auth.request(adGroupAdServiceUrl, "mutate", mutation, nil)
if err != nil {
return adGroupAds, err
}
mutateResp := struct {
AdGroupAds AdGroupAds `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return adGroupAds, err
}
return mutateResp.AdGroupAds, err
}
// MutateLabel allows you to add and removes labels from ads.
//
// Example
//
// ads, err := adGroupAdService.MutateLabel(
// gads.AdGroupAdLabelOperations{
// "ADD": {
// gads.AdGroupAdLabel{AdGroupAdId: 3200, LabelId: 5353},
// gads.AdGroupAdLabel{AdGroupAdId: 4320, LabelId: 5643},
// },
// "REMOVE": {
// gads.AdGroupAdLabel{AdGroupAdId: 3653, LabelId: 5653},
// },
// }
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupAdService#mutateLabel
//
func (s *AdGroupAdService) MutateLabel(adGroupAdLabelOperations AdGroupAdLabelOperations) (adGroupAdLabels []AdGroupAdLabel, err error) {
type adGroupAdLabelOperation struct {
Action string `xml:"operator"`
AdGroupAdLabel AdGroupAdLabel `xml:"operand"`
}
operations := []adGroupAdLabelOperation{}
for action, adGroupAdLabels := range adGroupAdLabelOperations {
for _, adGroupAdLabel := range adGroupAdLabels {
operations = append(operations,
adGroupAdLabelOperation{
Action: action,
AdGroupAdLabel: adGroupAdLabel,
},
)
}
}
mutation := struct {
XMLName xml.Name
Ops []adGroupAdLabelOperation `xml:"operations"`
}{
XMLName: xml.Name{
Space: baseUrl,
Local: "mutateLabel",
},
Ops: operations}
respBody, err := s.Auth.request(adGroupAdServiceUrl, "mutateLabel", mutation, nil)
if err != nil {
return adGroupAdLabels, err
}
mutateResp := struct {
AdGroupAdLabels []AdGroupAdLabel `xml:"rval>value"`
}{}
err = xml.Unmarshal([]byte(respBody), &mutateResp)
if err != nil {
return adGroupAdLabels, err
}
return mutateResp.AdGroupAdLabels, err
}
// Query is not yet implemented
//
// Relevant documentation
//
// https://developers.google.com/adwords/api/docs/reference/v201809/AdGroupAdService#query
//
func (s *AdGroupAdService) Query(query string) (adGroupAds AdGroupAds, totalCount int64, err error) {
return adGroupAds, totalCount, ERROR_NOT_YET_IMPLEMENTED
}