-
Notifications
You must be signed in to change notification settings - Fork 54
/
basic_operation_test.go
559 lines (530 loc) · 13.6 KB
/
basic_operation_test.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package gads
import (
"fmt"
"golang.org/x/net/context"
"time"
)
func ExampleCampaignService_Get() {
// load credentials from
authConf, _ := NewCredentials(context.TODO())
cs := NewCampaignService(&authConf.Auth)
// This example illustrates how to retrieve all the campaigns for an account.
var pageSize int64 = 500
var offset int64 = 0
paging := Paging{
Offset: offset,
Limit: pageSize,
}
totalCount := 0
for {
campaigns, totalCount, err := cs.Get(
Selector{
Fields: []string{
"Id",
"Name",
"Status",
},
Ordering: []OrderBy{
{"Name", "ASCENDING"},
},
Paging: &paging,
},
)
if err != nil {
fmt.Printf("Error occured finding campaigns")
}
for _, c := range campaigns {
fmt.Printf("Campaign ID %d, name '%s' and status '%s'", c.Id, c.Name, c.Status)
}
// Increment values to request the next page.
offset += pageSize
paging.Offset = offset
if totalCount < offset {
break
}
}
fmt.Printf("\tTotal number of campaigns found: %d.", totalCount)
}
func ExampleCampaignService_Mutate() {
// load credentials from
authConf, err := NewCredentials(context.TODO())
cs := NewCampaignService(&authConf.Auth)
var budgetId int64 = 1
// This example illustrates how to create campaigns.
campaigns, err := cs.Mutate(
CampaignOperations{
"ADD": {
Campaign{
Name: fmt.Sprintf("Interplanetary Cruise #%d", time.Now().Unix()),
Status: "ACTIVE",
BiddingStrategyConfiguration: &BiddingStrategyConfiguration{
StrategyType: "MANUAL_CPC",
},
// Budget (required) - note only the budget ID is required
BudgetId: budgetId,
AdvertisingChannelType: "SEARCH",
// Optional Fields:
StartDate: time.Now().Format("20060102"),
AdServingOptimizationStatus: "ROTATE",
NetworkSetting: &NetworkSetting{
TargetGoogleSearch: true,
TargetSearchNetwork: true,
TargetContentNetwork: false,
TargetPartnerSearchNetwork: false,
},
Settings: []CampaignSetting{
NewGeoTargetTypeSetting(
"DONT_CARE",
"DONT_CARE",
),
},
FrequencyCap: &FrequencyCap{
Impressions: 5,
TimeUnit: "DAY",
Level: "ADGROUP",
},
},
Campaign{
Name: fmt.Sprintf("Interplanetary Cruise banner #%d", time.Now().Unix()),
Status: "PAUSED",
BiddingStrategyConfiguration: &BiddingStrategyConfiguration{
StrategyType: "MANUAL_CPC",
},
// Budget (required) - note only the budget ID is required
BudgetId: budgetId,
AdvertisingChannelType: "DISPLAY",
},
},
},
)
if err != nil {
fmt.Printf("Error occured creating campaign.")
}
for _, c := range campaigns {
fmt.Printf("Campaign with name '%s' and ID %d was added.", c.Name, c.Id)
}
// This example illustrates how to update a campaign, setting its status to 'PAUSED'
campaigns, err = cs.Mutate(
CampaignOperations{
"SET": {
Campaign{
Id: campaigns[0].Id,
Status: "PAUSED",
},
},
},
)
if err != nil {
fmt.Printf("No campaigns were updated.")
} else {
fmt.Printf("Campaign ID %d was successfully updated, status was set to '%s'.", campaigns[0].Id, campaigns[0].Status)
}
// This example removes a campaign by setting the status to 'REMOVED'.
campaigns, err = cs.Mutate(
CampaignOperations{
"SET": {
Campaign{
Id: campaigns[0].Id,
Status: "REMOVED",
},
},
},
)
if err != nil {
fmt.Printf("No campaigns were updated.")
} else {
fmt.Printf("Campaign ID %d was removed.", campaigns[0].Id)
}
}
func ExampleAdGroupService_Get() {
authConf, _ := NewCredentials(context.TODO())
ags := NewAdGroupService(&authConf.Auth)
// This example illustrates how to retrieve all the ad groups for a campaign.
campaignId := "3"
var pageSize int64 = 500
var offset int64 = 0
paging := Paging{
Offset: offset,
Limit: pageSize,
}
totalCount := 0
for {
adGroups, totalCount, err := ags.Get(
Selector{
Fields: []string{
"Id",
"Name",
},
Ordering: []OrderBy{
{"Name", "ASCENDING"},
},
Predicates: []Predicate{
{"CampaignId", "IN", []string{campaignId}},
},
Paging: &paging,
},
)
if err != nil {
fmt.Printf("Error occured finding ad group")
}
for _, ag := range adGroups {
fmt.Printf("Ad group name is '%s' and ID is %d", ag.Id, ag.Name)
}
// Increment values to request the next page.
offset += pageSize
paging.Offset = offset
if totalCount < offset {
break
}
}
fmt.Printf("\tCampaign ID %d has %d ad group(s).", campaignId, totalCount)
}
func ExampleAdGroupService_Mutate() {
authConf, err := NewCredentials(context.TODO())
ags := NewAdGroupService(&authConf.Auth)
var campaignId int64 = 1
// This example illustrates how to create ad groups.
adGroups, err := ags.Mutate(
AdGroupOperations{
"ADD": {
AdGroup{
Name: fmt.Sprintf("Earth to Mars Cruises #%d", time.Now().Unix()),
Status: "ENABLED",
CampaignId: campaignId,
BiddingStrategyConfiguration: []BiddingStrategyConfiguration{
{
Bids: []Bid{
Bid{
Type: "CpcBid",
Amount: 10000000,
},
},
},
},
Settings: []AdSetting{
AdSetting{
Details: []TargetSettingDetail{
TargetSettingDetail{
CriterionTypeGroup: "PLACEMENT",
TargetAll: true,
},
TargetSettingDetail{
CriterionTypeGroup: "VERTICAL",
TargetAll: false,
},
},
},
},
},
AdGroup{
Name: fmt.Sprintf("Earth to Pluto Cruises #%d", time.Now().Unix()),
Status: "ENABLED",
CampaignId: campaignId,
BiddingStrategyConfiguration: []BiddingStrategyConfiguration{
{
Bids: []Bid{
Bid{
Type: "CpcBid",
Amount: 10000000,
},
},
},
},
},
},
},
)
if err != nil {
fmt.Printf("")
} else {
for _, ag := range adGroups {
fmt.Printf("Ad group ID %d was successfully added.", ag.Id)
}
}
// This example illustrates how to update an ad group
adGroups, err = ags.Mutate(
AdGroupOperations{
"SET": {
AdGroup{
Id: adGroups[0].Id,
Status: "PAUSE",
},
},
},
)
if err != nil {
fmt.Printf("No ad groups were updated.")
} else {
fmt.Printf("Ad group id %d was successfully updated.", adGroups[0].Id)
}
// This example removes an ad group by setting the status to 'REMOVED'.
adGroups, err = ags.Mutate(
AdGroupOperations{
"SET": {
AdGroup{
Id: adGroups[0].Id,
Status: "REMOVE",
},
},
},
)
if err != nil {
fmt.Printf("No ad groups were updated.")
} else {
fmt.Printf("Ad group id %d was successfully removed.", adGroups[0].Id)
}
}
func ExampleAdGroupCriterionService_Get() {
authConf, _ := NewCredentials(context.TODO())
agcs := NewAdGroupCriterionService(&authConf.Auth)
// This example illustrates how to retrieve all keywords for an ad group.
adGroupId := "1"
var pageSize int64 = 500
var offset int64 = 0
paging := Paging{
Offset: offset,
Limit: pageSize,
}
for {
adGroupCriterions, totalCount, err := agcs.Get(
Selector{
Fields: []string{
"Id",
"CriteriaType",
"KeywordText",
},
Ordering: []OrderBy{
{"Id", "ASCENDING"},
},
Predicates: []Predicate{
{"AdGroupId", "EQUALS", []string{adGroupId}},
{"CriteriaType", "EQUALS", []string{"KEYWORD"}},
},
Paging: &paging,
},
)
if err != nil {
fmt.Printf("Error occured finding ad group criterion")
}
for _, agc := range adGroupCriterions {
kc := agc.(BiddableAdGroupCriterion).Criterion.(KeywordCriterion)
fmt.Printf("Keyword ID %d, type '%s' and text '%s'", kc.Id, kc.MatchType, kc.Text)
}
// Increment values to request the next page.
offset += pageSize
paging.Offset = offset
if totalCount < offset {
fmt.Printf("\tAd group ID %d has %d keyword(s).", totalCount)
break
}
}
}
func ExampleAdGroupCriterionService_Mutate() {
authConf, err := NewCredentials(context.TODO())
agcs := NewAdGroupCriterionService(&authConf.Auth)
var adGroupId int64 = 1
// This example illustrates how to add multiple keywords to a given ad group.
adGroupCriterions, err := agcs.Mutate(
AdGroupCriterionOperations{
"ADD": {
BiddableAdGroupCriterion{
AdGroupId: adGroupId,
Criterion: KeywordCriterion{
Text: "mars cruise",
MatchType: "BROAD",
},
UserStatus: "PAUSED",
DestinationUrl: "http://example.com/mars",
},
BiddableAdGroupCriterion{
AdGroupId: adGroupId,
Criterion: KeywordCriterion{
Text: "space hotel",
MatchType: "BROAD",
},
},
},
},
)
if err != nil {
fmt.Printf("No keywords were added.")
} else {
fmt.Printf("Added %d keywords to ad group ID %d:", len(adGroupCriterions), adGroupId)
for _, agc := range adGroupCriterions {
k := agc.(BiddableAdGroupCriterion).Criterion.(KeywordCriterion)
fmt.Printf("\tKeyword ID is %d and type is '%s'", k.Id, k.MatchType)
}
}
// This example updates the bid of a keyword.
keywordCriterion := adGroupCriterions[0].(BiddableAdGroupCriterion).Criterion.(KeywordCriterion)
biddingStrategyConfigurations := BiddingStrategyConfiguration{
Bids: []Bid{
Bid{
Type: "CpcBid",
Amount: 10000000,
},
},
}
adGroupCriterions, err = agcs.Mutate(
AdGroupCriterionOperations{
"SET": {
BiddableAdGroupCriterion{
AdGroupId: adGroupId,
Criterion: keywordCriterion,
BiddingStrategyConfiguration: &biddingStrategyConfigurations,
},
},
},
)
biddableAdGroupCriterion := adGroupCriterions[0].(BiddableAdGroupCriterion)
keywordCriterion = biddableAdGroupCriterion.Criterion.(KeywordCriterion)
if err != nil {
fmt.Printf("No keywords were updated.")
} else {
fmt.Printf("Keyword ID %d was successfully updated, current bids are:", keywordCriterion.Id)
for _, bid := range biddableAdGroupCriterion.BiddingStrategyConfiguration.Bids {
fmt.Printf("\tType: '%s', value: %d", bid.Type, bid.Amount)
}
}
// This example removes a keyword using the 'REMOVE' operator.
adGroupCriterions, err = agcs.Mutate(
AdGroupCriterionOperations{
"REMOVE": {
BiddableAdGroupCriterion{
AdGroupId: adGroupId,
Criterion: keywordCriterion,
},
},
},
)
if err != nil {
fmt.Printf("No keywords were removed.")
} else {
biddableAdGroupCriterion := adGroupCriterions[0].(BiddableAdGroupCriterion)
keywordCriterion = biddableAdGroupCriterion.Criterion.(KeywordCriterion)
fmt.Printf("Keyword ID %d was successfully removed.", keywordCriterion.Id)
}
}
func ExampleAdGroupAdService_Get() {
authConf, _ := NewCredentials(context.TODO())
agas := NewAdGroupAdService(&authConf.Auth)
// This example illustrates how to retrieve all text ads for an ad group.
adGroupId := "1"
var pageSize int64 = 500
var offset int64 = 0
paging := Paging{
Offset: offset,
Limit: pageSize,
}
var totalCount int64 = 0
for {
adGroupAds, totalCount, err := agas.Get(
Selector{
Fields: []string{
"Id",
"Status",
"AdType",
},
Ordering: []OrderBy{
{"Id", "ASCENDING"},
},
Predicates: []Predicate{
{"AdGroupId", "IN", []string{adGroupId}},
{"Status", "IN", []string{"ENABLED", "PAUSED", "DISABLED"}},
{"AdType", "EQUALS", []string{"TEXT_AD"}},
},
Paging: &paging,
},
)
if err != nil {
fmt.Printf("Error occured finding ad group ad")
}
for _, aga := range adGroupAds {
ta := aga.(TextAd)
fmt.Printf("Ad ID is %d, type is 'TextAd' and status is '%s'", ta.Id, ta.Status)
}
// Increment values to request the next page.
offset += pageSize
paging.Offset = offset
if totalCount < offset {
break
}
}
fmt.Printf("\tAd group ID %d has %d ad(s).", totalCount)
}
func ExampleAdGroupAdService_Mutate() {
authConf, err := NewCredentials(context.TODO())
agas := NewAdGroupAdService(&authConf.Auth)
// This example illustrates how to add text ads to a given ad group.
var adGroupId int64 = 1
adGroupAds, err := agas.Mutate(
AdGroupAdOperations{
"ADD": {
NewTextAd(
adGroupId,
"http://www.example.com",
"example.com",
"Luxury Cruise to Mars",
"Visit the Red Planet in style.",
"Low-gravity fun for everyone!",
"ACTIVE",
),
NewTextAd(
adGroupId,
"http://www.example.com",
"www.example.com",
"Luxury Cruise to Mars",
"Enjoy your stay at Red Planet.",
"Buy your tickets now!",
"ACTIVE",
),
},
},
)
if err != nil {
fmt.Printf("No ads were added.")
} else {
fmt.Printf("Added %d ad(s) to ad group ID %d:", len(adGroupAds), adGroupId)
for _, ada := range adGroupAds {
ta := ada.(TextAd)
fmt.Printf("\tAd ID %d, type 'TextAd' and status '%s'", ta.Id, ta.Status)
}
}
// This example illustrates how to update an ad, setting its status to 'PAUSED'.
textAdId := adGroupAds[0].(TextAd).Id
adGroupAds, err = agas.Mutate(
AdGroupAdOperations{
"SET": {
TextAd{
AdGroupId: adGroupId,
Id: textAdId,
Status: "PAUSED",
},
},
},
)
if err != nil {
fmt.Printf("No ads were updated.")
} else {
textAd := adGroupAds[0].(TextAd)
fmt.Printf("Ad ID %d was successfully updated, status set to '%s'.", textAd.Id, textAd.Status)
}
// This example removes an ad using the 'REMOVE' operator.
adGroupAds, err = agas.Mutate(
AdGroupAdOperations{
"SET": {
TextAd{
AdGroupId: adGroupId,
Id: textAdId,
Status: "REMOVE",
},
},
},
)
if err != nil {
fmt.Printf("No ads were removed.")
} else {
textAd := adGroupAds[0].(TextAd)
fmt.Printf("Ad ID %d was successfully removed.", textAd.Id)
}
}