-
Notifications
You must be signed in to change notification settings - Fork 96
/
AddPerformanceMaxForTravelGoalsCampaign.cs
1043 lines (948 loc) · 48.4 KB
/
AddPerformanceMaxForTravelGoalsCampaign.cs
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using CommandLine;
using Google.Ads.Gax.Examples;
using Google.Ads.Gax.Util;
using Google.Ads.GoogleAds.Config;
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V17.Common;
using Google.Ads.GoogleAds.V17.Errors;
using Google.Ads.GoogleAds.V17.Resources;
using Google.Ads.GoogleAds.V17.Services;
using Google.Protobuf;
using System;
using System.Collections.Generic;
using static Google.Ads.GoogleAds.V17.Enums.AdvertisingChannelTypeEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AssetFieldTypeEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AssetGroupStatusEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.AssetSetTypeEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.BudgetDeliveryMethodEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.CampaignStatusEnum.Types;
using static Google.Ads.GoogleAds.V17.Enums.HotelAssetSuggestionStatusEnum.Types;
namespace Google.Ads.GoogleAds.Examples.V17
{
/// <summary>
/// This example shows how to create a Performance Max for travel goals campaign. It also uses
/// TravelAssetSuggestionService to fetch suggested assets for creating an asset group. In case
/// there are not enough assets for the asset group (required by Performance Max), this example
/// will create more assets to fulfill the requirements.
///
/// <p>For more information about Performance Max campaigns, see
/// https://developers.google.com/google-ads/api/docs/performance-max/overview.</p>
/// <p>Prerequisites:</p>
///
/// <ul>
/// <li>You must have at least one conversion action in the account. For more about conversion
/// actions, see
/// https://developers.google.com/google-ads/api/docs/conversions/overview#conversion_actions.
/// </li>
/// </ul>
///
/// <p>Notes:</p>
///
/// <ul>
/// <li>This example uses the default customer conversion goals. For an example of setting
/// campaign-specific conversion goals, see AddPerformanceMaxRetailCampaign.cs.</li>
/// <li>To learn how to create asset group signals, see AddPerformanceMaxCampaign.cs.</li>
/// </ul>
/// </summary>
public class AddPerformanceMaxForTravelGoalsCampaign : ExampleBase
{
/// <summary>
/// Command line options for running the
/// <see cref="AddPerformanceMaxForTravelGoalsCampaign"/> example.
/// </summary>
public class Options : OptionsBase
{
/// <summary>
/// The Google Ads customer ID.
/// </summary>
[Option("customerId", Required = true, HelpText =
"The Google Ads customer ID.")]
public long CustomerId { get; set; }
/// <summary>
/// The place ID of a hotel property. A place ID uniquely identifies a place in the
/// Google Places database. See
/// https://developers.google.com/places/web-service-place-id to learn more.
/// </summary>
[Option("placeId", Required = true, HelpText =
"The place ID of a hotel property. A place ID uniquely identifies a place in the" +
"Google Places database. See " +
"https://developers.google.com/places/web-service-place-id to learn more.")]
public string PlaceId { get; set; }
}
/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
/// <param name="args">The command line arguments.</param>
public static void Main(string[] args)
{
Options options = ExampleUtilities.ParseCommandLine<Options>(args);
AddPerformanceMaxForTravelGoalsCampaign codeExample =
new AddPerformanceMaxForTravelGoalsCampaign();
Console.WriteLine(codeExample.Description);
codeExample.Run(new GoogleAdsClient(), options.CustomerId, options.PlaceId);
}
// Minimum requirements of assets required in a Performance Max asset group.
// See https://developers.google.com/google-ads/api/docs/performance-max/assets for details.
private Dictionary<AssetFieldType, int> MIN_REQUIRED_TEXT_ASSET_COUNTS =
new Dictionary<AssetFieldType, int>()
{
{ AssetFieldType.Headline, 3 },
{ AssetFieldType.LongHeadline, 1 },
{ AssetFieldType.Description, 2 },
{ AssetFieldType.BusinessName, 1 },
};
private Dictionary<AssetFieldType, int> MIN_REQUIRED_IMAGE_ASSET_COUNTS =
new Dictionary<AssetFieldType, int>()
{
{ AssetFieldType.MarketingImage, 1 },
{ AssetFieldType.SquareMarketingImage, 1 },
{ AssetFieldType.Logo, 1 },
};
// Texts and URLs used to create text and image assets when the TravelAssetSuggestionService
// doesn't return enough assets required for creating an asset group.
private Dictionary<AssetFieldType, List<string>> DEFAULT_TEXT_ASSETS_INFO =
new Dictionary<AssetFieldType, List<string>>()
{
{ AssetFieldType.Headline, new List<string>()
{
"Hotel", "Travel Reviews", "Book travel"
}
},
{ AssetFieldType.LongHeadline, new List<string>() { "Travel the World" } },
{ AssetFieldType.Description, new List<string>()
{
"Great deal for your beloved hotel",
"Best rate guaranteed"
}
},
{ AssetFieldType.BusinessName, new List<string>() { "Interplanetary cruises" } },
};
private Dictionary<AssetFieldType, List<string>> DEFAULT_IMAGE_ASSETS_INFO =
new Dictionary<AssetFieldType, List<string>>()
{
{ AssetFieldType.MarketingImage, new List<string>()
{
"https://gaagl.page.link/Eit5"
}
},
{ AssetFieldType.SquareMarketingImage, new List<string>()
{
"https://gaagl.page.link/bjYi"
}
},
{ AssetFieldType.Logo, new List<string>()
{
"https://gaagl.page.link/bjYi"
}
},
};
// We specify temporary IDs that are specific to a single mutate request. Temporary IDs are always
// negative and unique within one mutate request.
//
// <p>See https://developers.google.com/google-ads/api/docs/mutating/best-practices for
// further details.
//
// <p>These temporary IDs are fixed because they are used in multiple places.
private int ASSET_TEMPORARY_ID = -1;
private int BUDGET_TEMPORARY_ID = -2;
private int CAMPAIGN_TEMPORARY_ID = -3;
private int ASSET_GROUP_TEMPORARY_ID = -4;
// There are also entities that will be created in the same request but do not
// need to be fixed temporary IDs because they are referenced only once.
private long temporaryId = -5;
/// <summary>
/// Returns a description about the code example.
/// </summary>
public override string Description => "This example shows how to create a Performance " +
" Max for travel goals campaign. It also uses TravelAssetSuggestionService to fetch " +
"suggested assets for creating an asset group. In case there are not enough assets " +
"for the asset group (required by Performance Max), this example will create more " +
"assets to fulfill the requirements.\n" +
"For more information about Performance Max campaigns, see " +
"https://developers.google.com/google-ads/api/docs/performance-max/overview.\n" +
"Prerequisites:\n" +
"You must have at least one conversion action in the account. For more about " +
"conversion actions, see " +
"https://developers.google.com/google-ads/api/docs/conversions/overview#conversion_actions.\n" +
"Notes:\n" +
"- This example uses the default customer conversion goals. For an example of " +
"setting campaign-specific conversion goals, see AddPerformanceMaxRetailCampaign.cs.\n" +
"- To learn how to create asset group signals, see AddPerformanceMaxCampaign.cs.";
/// <summary>
/// Runs the code example.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <param name="placeId">The place ID of a hotel property.</param>
public void Run(GoogleAdsClient client, long customerId, string placeId)
{
HotelAssetSuggestion hotelAssetSuggestion =
GetHotelAssetSuggestion(client, customerId, placeId);
// Performance Max campaigns require that repeated assets such as headlines and
// descriptions be created before the campaign. For the list of required assets for a
// Performance Max campaign, see
// https://developers.google.com/google-ads/api/docs/performance-max/assets.
// This step is the same for any type of Performance Max campaign.
// Creates the headlines using the hotel asset suggestion.
List<string> headlineAssetResourceNames = CreateMultipleTextAssets(
client, customerId, AssetFieldType.Headline, hotelAssetSuggestion);
// Creates the descriptions using the hotel asset suggestion.
List<string> descriptionAssetResourceNames = CreateMultipleTextAssets(
client, customerId, AssetFieldType.Description, hotelAssetSuggestion);
// Creates a hotel property asset set, which will be used later to link with a newly
// created campaign.
string hotelPropertyAssetSetResourceName = CreateHotelAssetSet(client, customerId);
// Creates a hotel property asset and link it with the previously created hotel property
// asset set. This asset will also be linked to an asset group in the later steps.
// In the real-world scenario, you'd need to create many assets for all your hotel
// properties. We use one hotel property here for simplicity.
// Both asset and asset set need to be created before creating a campaign, so we cannot
// bundle them with other mutate operations below.
string hotelPropertyAssetResourceName = CreateHotelAsset(
client, customerId, placeId, hotelPropertyAssetSetResourceName);
// It's important to create the entities below in this order because they depend on
// each other.
// The methods below create and return mutate operations that we later provide to the
// GoogleAdsService.Mutate method in order to create the entities in a single request.
// Since the entities for a Performance Max campaign are closely tied to one-another,
// it's considered a best practice to create them in a single Mutate request so they
// all complete successfully or fail entirely, leaving no orphaned entities. See:
// https://developers.google.com/google-ads/api/docs/mutating/overview.
List<MutateOperation> mutateOperations = new List<MutateOperation>();
mutateOperations.Add(CreateCampaignBudgetOperation(customerId));
mutateOperations.Add(CreateCampaignOperation(customerId,
hotelPropertyAssetSetResourceName));
mutateOperations.AddRange(
CreateAssetGroupOperations(
customerId,
hotelPropertyAssetResourceName,
headlineAssetResourceNames,
descriptionAssetResourceNames,
hotelAssetSuggestion,
client.Config
)
);
// Issues a mutate request to create everything and prints the results.
GoogleAdsServiceClient googleAdsServiceClient =
client.GetService(Services.V17.GoogleAdsService);
MutateGoogleAdsResponse response =
googleAdsServiceClient.Mutate(customerId.ToString(), mutateOperations);
Console.WriteLine("Created the following entities for a campaign budget, a campaign, " +
"and an asset group for Performance Max for travel goals:");
PrintResponseDetails(response);
}
/// <summary>
/// Returns hotel asset suggestion obtained from TravelAssetsSuggestionService.
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <param name="placeId">The place ID of a hotel property.</param>
/// <returns>The hotel asset suggestion.</returns>
/// </summary>
// [START get_hotel_asset_suggestion]
private HotelAssetSuggestion GetHotelAssetSuggestion(GoogleAdsClient client,
long customerId, string placeId)
{
// Get the TravelAssetSuggestionService client.
TravelAssetSuggestionServiceClient travelAssetSuggestionService =
client.GetService(Services.V17.TravelAssetSuggestionService);
SuggestTravelAssetsRequest request = new SuggestTravelAssetsRequest
{
CustomerId = customerId.ToString(),
LanguageOption = "en-US",
};
request.PlaceIds.Add(placeId);
SuggestTravelAssetsResponse response = travelAssetSuggestionService.SuggestTravelAssets(
request
);
Console.WriteLine($"Fetched a hotel asset suggestion for the place ID {placeId}");
return response.HotelAssetSuggestions[0];
}
// [END get_hotel_asset_suggestion]
///<summary>
/// Creates multiple text assets and returns the list of resource names. The hotel asset
/// suggestion is used to create a text asset first. If the number of created text assets is
/// still fewer than the minimum required number of assets of the specified asset field
/// type, adds more text assets to fulfill the requirement.
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <param name="assetFieldType">The asset field type that the text assets will be created
/// for.</param>
/// <param name="hotelAssetSuggestion">The hotel asset suggestion.</param>
/// <returns>The resource names of the created text assets.</returns>
/// </summary>
private List<string> CreateMultipleTextAssets(GoogleAdsClient client, long customerId,
AssetFieldType assetFieldType, HotelAssetSuggestion hotelAssetSuggestion)
{
// Uses the GoogleAdService to create multiple text assets in a single request.
List<MutateOperation> mutateOperations = new List<MutateOperation>();
// First, adds all the text assets of the specified asset field type.
// Filters to only the specified asset field type.
// Constructs a mutate operation to create the asset.
// Adds the operation to the list.
if (hotelAssetSuggestion.Status == HotelAssetSuggestionStatus.Success)
{
foreach (HotelTextAsset asset in hotelAssetSuggestion.TextAssets)
{
if (asset.AssetFieldType == assetFieldType)
{
MutateOperation operation = new MutateOperation
{
AssetOperation = new AssetOperation
{
Create = new Asset
{
TextAsset = new TextAsset
{
Text = asset.Text
}
}
}
};
mutateOperations.Add(operation);
}
}
}
// If the added assets are still less than the minimum required assets for the asset
// field type, add more text assets using the default texts.
int i = 0;
while (mutateOperations.Count < MIN_REQUIRED_TEXT_ASSET_COUNTS[assetFieldType])
{
string text = DEFAULT_TEXT_ASSETS_INFO[assetFieldType][i++];
MutateOperation operation = new MutateOperation
{
AssetOperation = new AssetOperation {
Create = new Asset {
TextAsset = new TextAsset {
Text = text
}
}
}
};
mutateOperations.Add(operation);
}
GoogleAdsServiceClient googleAdsService =
client.GetService(Services.V17.GoogleAdsService);
MutateGoogleAdsResponse response =
googleAdsService.Mutate(customerId.ToString(), mutateOperations);
List<string> assetResourceNames = new List<string>();
foreach (MutateOperationResponse operationResponse in response.MutateOperationResponses)
{
MutateAssetResult assetResult = operationResponse.AssetResult;
assetResourceNames.Add(assetResult.ResourceName);
}
Console.WriteLine($"The following assets were created for the asset field type " +
$"{assetFieldType}");
PrintResponseDetails(response);
return assetResourceNames;
}
/// <summary>
/// Creates a hotel property asset set.
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The Google Ads customer ID.</param>
/// <returns> The created hotel property asset set resource name.</returns>
/// </summary>
// [START create_hotel_asset_set]
private string CreateHotelAssetSet(GoogleAdsClient client, long customerId)
{
AssetSetOperation operation = new AssetSetOperation()
{
Create = new AssetSet {
Name = "My Hotel property asset set #" + ExampleUtilities.GetRandomString(),
Type = AssetSetType.HotelProperty
}
};
AssetSetServiceClient assetSetService = client.GetService(Services.V17.AssetSetService);
MutateAssetSetsResponse response = assetSetService.MutateAssetSets(
customerId.ToString(),
new List<AssetSetOperation> { operation }
);
string assetResourceName = response.Results[0].ResourceName;
Console.WriteLine($"Created an asset set with resource name: {assetResourceName}");
return assetResourceName;
}
// [END create_hotel_asset_set]
/// <summary>
/// Creates a hotel property asset using the specified place ID. The place ID must belong
/// to a hotel property. Then, links it to the specified asset set.
///
/// <p>See https://developers.google.com/places/web-service/place-id to search for a hotel
/// place ID.</p>
/// <param name="client">The Google Ads API client.</param>
/// <param name="customerId">The client customer ID.</param>
/// <param name="placeId">The place ID for a hotel.</param>
/// <param name="hotelPropertyAssetSetResourceName">The hotel asset set resource
/// name.</param>
/// <returns>The created hotel property asset resource name.</returns>
/// </summary>
// [START create_hotel_asset]
private string CreateHotelAsset(
GoogleAdsClient client, long customerId, string placeId,
string hotelPropertyAssetSetResourceName)
{
// Uses the GoogleAdService to create an asset and asset set asset in a single request.
List<MutateOperation> mutateOperations = new List<MutateOperation>();
string assetResourceName = ResourceNames.Asset(customerId, ASSET_TEMPORARY_ID);
// Creates a mutate operation for a hotel property asset.
Asset hotelPropertyAsset = new Asset()
{
ResourceName = assetResourceName,
HotelPropertyAsset = new HotelPropertyAsset
{
PlaceId = placeId
}
};
mutateOperations.Add(new MutateOperation
{
AssetOperation = new AssetOperation
{
Create = hotelPropertyAsset
}
});
// Creates a mutate operation for an asset set asset.
AssetSetAsset assetSetAsset = new AssetSetAsset
{
Asset = assetResourceName,
AssetSet = hotelPropertyAssetSetResourceName
};
mutateOperations.Add(new MutateOperation
{
AssetSetAssetOperation = new AssetSetAssetOperation
{
Create = assetSetAsset
}
});
// Issues a mutate request to create all entities.
GoogleAdsServiceClient googleAdsServiceClient =
client.GetService(Services.V17.GoogleAdsService);
MutateGoogleAdsResponse response =
googleAdsServiceClient.Mutate(customerId.ToString(), mutateOperations);
Console.WriteLine("Created the following entities for the hotel asset:");
PrintResponseDetails(response);
return response.MutateOperationResponses[0].AssetResult.ResourceName;
}
// [END create_hotel_asset]
/// <summary>
/// Creates a mutate operation that creates a new campaign budget.
/// <p>A temporary ID will be assigned to this campaign budget so that it can be referenced
/// by other objects being created in the same mutate request.</p>
/// <param name="customerId">The client customer ID.</param>
/// <returns>A mutate operation that creates a campaign budget.</returns>
/// </summary>
private MutateOperation CreateCampaignBudgetOperation(long customerId)
{
CampaignBudget campaignBudget = new CampaignBudget
{
Name = "Performance Max for travel goals campaign budget #" +
ExampleUtilities.GetRandomString(),
// The budget period already defaults to DAILY.
AmountMicros = 500000,
DeliveryMethod = BudgetDeliveryMethod.Standard,
// A Performance Max campaign cannot use a shared campaign budget.
ExplicitlyShared = false,
// Sets a temporary ID in the budget's resource name, so it can be referenced
// by the campaign in later steps.
ResourceName = ResourceNames.CampaignBudget(customerId, BUDGET_TEMPORARY_ID)
};
return new MutateOperation
{
CampaignBudgetOperation = new CampaignBudgetOperation
{
Create = campaignBudget
}
};
}
/// <summary>
/// Creates a mutate operation that creates a new Performance Max campaign. Links the
/// specified hotel property asset set to this campaign.
/// <p>A temporary ID will be assigned to this campaign so that it can be referenced by
/// other objects being created in the same mutate request.</p>
/// <param name="customerId">The client customer ID.</param>
/// <param name="hotelPropertyAssetSetResourceName"> The resource name of the hotel property
/// asset set.</param>
/// <returns>A mutate operation that creates a campaign.</returns>
/// </summary>
// [START create_campaign]
private MutateOperation CreateCampaignOperation(long customerId,
string hotelPropertyAssetSetResourceName)
{
Campaign performanceMaxCampaign = new Campaign
{
Name = "Performance Max for travel goals campaign #" +
ExampleUtilities.GetRandomString(),
// Sets the campaign status as PAUSED. The campaign is the only entity in
// the mutate request that should have its status set.
Status = CampaignStatus.Paused,
// All Performance Max campaigns have an advertising_channel_type of
// PERFORMANCE_MAX. The advertising_channel_sub_type should not be set.
AdvertisingChannelType = AdvertisingChannelType.PerformanceMax,
// To create a Performance Max for travel goals campaign, you need to set
// `hotel_property_asset_set`.
HotelPropertyAssetSet = hotelPropertyAssetSetResourceName,
// Bidding strategy must be set directly on the campaign.
// Setting a portfolio bidding strategy by resource name is not supported.
// Max Conversion and Maximize Conversion Value are the only strategies
// supported for Performance Max campaigns.
// An optional ROAS (Return on Advertising Spend) can be set for
// maximize_conversion_value. The ROAS value must be specified as a ratio in
// the API. It is calculated by dividing "total value" by "total spend".
// For more information on Maximize Conversion Value, see the support
// article: http://support.google.com/google-ads/answer/7684216.
// A targetRoas of 3.5 corresponds to a 350% return on ad spend.
MaximizeConversionValue = new MaximizeConversionValue
{
TargetRoas = 3.5
},
// Assigns the resource name with a temporary ID.
ResourceName = ResourceNames.Campaign(customerId, CAMPAIGN_TEMPORARY_ID),
// Sets the budget using the given budget resource name.
CampaignBudget = ResourceNames.CampaignBudget(customerId, BUDGET_TEMPORARY_ID)
};
return new MutateOperation
{
CampaignOperation = new CampaignOperation
{
Create = performanceMaxCampaign
}
};
}
// [END create_campaign]
/// <summary>
/// Creates a mutate operation that creates a new asset group.
/// <param name="customerId">The client customer ID.</param>
/// <param name="hotelPropertyAssetResourceName"> The resource name of the hotel property
/// asset.</param>
/// <param name="headlineAssetResourceNames">The resource names for headline
/// assets.</param>
/// <param name="descriptionAssetResourceNames">The resource names for description
/// assets.</param>
/// <param name="hotelAssetSuggestion">The hotel asset suggestion.</param>
/// <param name="config">The Google Ads configuration.</param>
/// <returns>A mutate operation that creates an asset group.</returns>
/// </summary>
private List<MutateOperation> CreateAssetGroupOperations(
long customerId,
string hotelPropertyAssetResourceName,
List<string> headlineAssetResourceNames,
List<string> descriptionAssetResourceNames,
HotelAssetSuggestion hotelAssetSuggestion,
GoogleAdsConfig config
)
{
List<MutateOperation> mutateOperations = new List<MutateOperation>();
// Creates a new mutate operation that creates an asset group using suggested
// information when available.
string assetGroupName;
List<string> assetGroupFinalUrls = new List<string>();
if (hotelAssetSuggestion.Status == HotelAssetSuggestionStatus.Success)
{
assetGroupName = hotelAssetSuggestion.HotelName;
assetGroupFinalUrls.Add(hotelAssetSuggestion.FinalUrl);
}
else
{
assetGroupName = "Performance Max for travel goals asset group #"
+ ExampleUtilities.GetRandomString();
assetGroupFinalUrls.Add("https://www.example.com");
}
string assetGroupResourceName = ResourceNames.AssetGroup(customerId,
ASSET_GROUP_TEMPORARY_ID);
AssetGroup assetGroup = new AssetGroup
{
ResourceName = assetGroupResourceName,
Name = assetGroupName,
Campaign = ResourceNames.Campaign(customerId, CAMPAIGN_TEMPORARY_ID),
Status = AssetGroupStatus.Paused
};
assetGroup.FinalUrls.AddRange(assetGroupFinalUrls);
mutateOperations.Add(new MutateOperation
{
AssetGroupOperation = new AssetGroupOperation
{
Create = assetGroup
}
});
// An asset group is linked to an asset by creating a new asset group asset
// and providing:
// - the resource name of the asset group
// - the resource name of the asset
// - the field_type of the asset in this asset group
//
// To learn more about asset groups, see
// https://developers.google.com/google-ads/api/docs/performance-max/asset-groups.
// Headline and description assets were created at the first step of this example.
// So, we just need to link them with the created asset group.
List<AssetGroupAsset> assetGroupAssets = new List<AssetGroupAsset>();
foreach (string headlineAssetResourceName in headlineAssetResourceNames)
{
assetGroupAssets.Add(new AssetGroupAsset
{
Asset = headlineAssetResourceName,
AssetGroup = assetGroupResourceName,
FieldType = AssetFieldType.Headline
});
}
foreach (string descriptionAssetResourceName in descriptionAssetResourceNames)
{
assetGroupAssets.Add(new AssetGroupAsset
{
Asset = descriptionAssetResourceName,
AssetGroup = assetGroupResourceName,
FieldType = AssetFieldType.Description
});
}
foreach (AssetGroupAsset assetGroupAsset in assetGroupAssets)
{
mutateOperations.Add(new MutateOperation
{
AssetGroupAssetOperation = new AssetGroupAssetOperation
{
Create = assetGroupAsset
}
});
}
// [START link_hotel_asset]
// Link the previously created hotel property asset to the asset group. In the
// real-world scenario, you'd need to do this step several times for each hotel property
// asset.
AssetGroupAsset hotelPropertyAssetGroupAsset = new AssetGroupAsset
{
Asset = hotelPropertyAssetResourceName,
AssetGroup = assetGroupResourceName,
FieldType = AssetFieldType.HotelProperty
};
// Adds an operation to link the hotel property asset to the asset group.
mutateOperations.Add(new MutateOperation
{
AssetGroupAssetOperation = new AssetGroupAssetOperation
{
Create = hotelPropertyAssetGroupAsset
}
});
// [END link_hotel_asset]
// Creates the rest of the required text assets and links them to the asset group.
mutateOperations.AddRange(
CreateOperationsForTextAssetsAndAssetGroupAssets(
customerId, hotelAssetSuggestion, assetGroupResourceName
)
);
// Creates the image assets and links them to the asset group. Some optional image
// assets suggested by the TravelAssetSuggestionService might be created too.
mutateOperations.AddRange(
CreateOperationsForImageAssetsAndAssetGroupAssets(
customerId, hotelAssetSuggestion, assetGroupResourceName, config
)
);
if (hotelAssetSuggestion.Status == HotelAssetSuggestionStatus.Success)
{
// Creates a new mutate operation for a suggested call-to-action asset and links it
// to the asset group.
Asset callToActionAsset = new Asset
{
ResourceName = ResourceNames.Asset(customerId, temporaryId),
Name = "Suggested call-to-action asset #" + ExampleUtilities.GetRandomString(),
CallToActionAsset = new CallToActionAsset
{
CallToAction = hotelAssetSuggestion.CallToAction
}
};
// Adds an operation to create the call-to-action asset.
mutateOperations.Add(new MutateOperation
{
AssetOperation = new AssetOperation
{
Create = callToActionAsset
}
});
AssetGroupAsset callToActionAssetGroupAsset = new AssetGroupAsset
{
Asset = callToActionAsset.ResourceName,
AssetGroup = assetGroupResourceName,
FieldType = AssetFieldType.CallToActionSelection
};
// Adds an operation to link the call-to-action asset to the asset group.
mutateOperations.Add(new MutateOperation
{
AssetGroupAssetOperation = new AssetGroupAssetOperation
{
Create = callToActionAssetGroupAsset
}
});
temporaryId--;
}
return mutateOperations;
}
/// <summary>
/// Creates text assets required for an asset group using the suggested hotel text assets.
/// It adds more text assets to fulfill the requirements if the suggested hotel text assets
/// are not enough.
/// <param name="customerId">The client customer ID.</param>
/// <param name="hotelAssetSuggestion">The hotel asset suggestion.</param>
/// <param name="assetGroupResourceName">The resource name of the asset group.</param>
/// <returns>A list of mutate operations that create text assets and asset group
/// assets.</returns>
/// </summary>
private List<MutateOperation> CreateOperationsForTextAssetsAndAssetGroupAssets(
long customerId,
HotelAssetSuggestion hotelAssetSuggestion,
string assetGroupResourceName
)
{
// Creates mutate operations for the suggested text assets except for headlines and
// descriptions, which were created previously.
List<MutateOperation> mutateOperations = new List<MutateOperation>();
// Creates a map of asset field type to list of text values to create.
Dictionary<AssetFieldType, List<string>> textByFieldType =
new Dictionary<AssetFieldType, List<string>>();
if (hotelAssetSuggestion.Status == HotelAssetSuggestionStatus.Success)
{
// Adds text values of suggested text assets.
foreach (HotelTextAsset hotelTextAsset in hotelAssetSuggestion.TextAssets)
{
AssetFieldType assetFieldType = hotelTextAsset.AssetFieldType;
if (assetFieldType == AssetFieldType.Headline ||
assetFieldType == AssetFieldType.Description)
{
// Headlines and descriptions were already created at the first step of this
// code example.
continue;
}
Console.WriteLine($"A text asset with text {hotelTextAsset.Text} is " +
$"suggested for the asset field type {assetFieldType}");
List<string> existingTexts = null;
if (!textByFieldType.TryGetValue(assetFieldType, out existingTexts))
{
existingTexts = textByFieldType[assetFieldType] = new List<string>();
}
existingTexts.Add(hotelTextAsset.Text);
}
}
// Collects more text values by field type to fulfill the requirements.
foreach (AssetFieldType assetFieldType in MIN_REQUIRED_TEXT_ASSET_COUNTS.Keys)
{
if (assetFieldType == AssetFieldType.Headline ||
assetFieldType == AssetFieldType.Description)
{
// Headlines and descriptions were already created at the first step of
// this code example.
continue;
}
List<string> existingTexts = null;
if (!textByFieldType.TryGetValue(assetFieldType, out existingTexts))
{
existingTexts = textByFieldType[assetFieldType] = new List<string>();
}
int i = 0;
while (textByFieldType[assetFieldType].Count <
MIN_REQUIRED_TEXT_ASSET_COUNTS[assetFieldType])
{
string textFromDefaults = DEFAULT_TEXT_ASSETS_INFO[assetFieldType][i++];
Console.WriteLine($"A default text '{textFromDefaults}' is used to create a " +
$"text asset for the asset field type '{assetFieldType}'");
existingTexts.Add(textFromDefaults);
}
}
// Converts the list of text values by field type into AssetOperations and
// AssetGroupAssetOperations.
foreach (AssetFieldType assetFieldType in textByFieldType.Keys)
{
foreach (string text in textByFieldType[assetFieldType])
{
// Builds the asset.
Asset asset = new Asset
{
ResourceName = ResourceNames.Asset(customerId, temporaryId--),
TextAsset = new TextAsset
{
Text = text
}
};
// Adds an operation to create the Asset.
mutateOperations.Add(new MutateOperation
{
AssetOperation = new AssetOperation
{
Create = asset
}
});
//Builds the AssetGroupAsset.
AssetGroupAsset assetGroupAsset = new AssetGroupAsset
{
Asset = asset.ResourceName,
AssetGroup = assetGroupResourceName,
FieldType = assetFieldType
};
// Adds an operation to link the Asset to the AssetGroup.
mutateOperations.Add(new MutateOperation
{
AssetGroupAssetOperation = new AssetGroupAssetOperation
{
Create = assetGroupAsset
}
});
}
}
return mutateOperations;
}
/// <summary>
/// Creates image assets required for an asset group using the suggested hotel image assets.
/// It adds more image assets to fulfill the requirements if the suggested hotel image
/// assets are not enough.
/// <param name="customerId">The client customer ID.</param>
/// <param name="hotelAssetSuggestion">The hotel asset suggestion.</param>
/// <param name="assetGroupResourceName">The resource name of the asset group.</param>
/// <param name="config">The Google Ads config.</param>
/// <returns>A list of mutate operations that create image assets and asset group
/// assets.</returns>
/// </summary>
private List<MutateOperation> CreateOperationsForImageAssetsAndAssetGroupAssets(
long customerId,
HotelAssetSuggestion hotelAssetSuggestion,
string assetGroupResourceName,
GoogleAdsConfig config
)
{
// Creates mutate operations for the suggested image assets.
List<MutateOperation> mutateOperations = new List<MutateOperation>();
// Creates a map of asset field type to list of image URLs for which this method will
// create assets and asset group assets.
Dictionary<AssetFieldType, List<string>> imageUrlsByFieldType =
new Dictionary<AssetFieldType, List<string>>();
if (hotelAssetSuggestion.Status == HotelAssetSuggestionStatus.Success)
{
// Adds URLs of suggested image assets.
foreach (HotelImageAsset hotelImageAsset in hotelAssetSuggestion.ImageAssets)
{
AssetFieldType assetFieldType = hotelImageAsset.AssetFieldType;
Console.WriteLine($"An image asset with URL '{hotelImageAsset.Uri} is " +
$"suggested for the asset field type {assetFieldType}");
List<string> existingImageUrls = null;
if (!imageUrlsByFieldType.TryGetValue(assetFieldType, out existingImageUrls))
{
existingImageUrls = imageUrlsByFieldType[assetFieldType] =
new List<string>();
}
existingImageUrls.Add(hotelImageAsset.Uri);
}
}
// Collects more image URLs by field type to fulfill the requirements.
foreach (AssetFieldType assetFieldType in MIN_REQUIRED_IMAGE_ASSET_COUNTS.Keys)
{
List<string> existingImageUrls = null;
if (!imageUrlsByFieldType.TryGetValue(assetFieldType, out existingImageUrls))
{
existingImageUrls = imageUrlsByFieldType[assetFieldType] =
new List<string>();
}
int i = 0;
while (imageUrlsByFieldType[assetFieldType].Count <
MIN_REQUIRED_IMAGE_ASSET_COUNTS[assetFieldType])
{
string imageUrlFromDefaults = DEFAULT_IMAGE_ASSETS_INFO[assetFieldType][i++];
Console.WriteLine($"A default image URL '{imageUrlFromDefaults} is used to " +
$"create an image asset for the asset field type {assetFieldType}");
existingImageUrls.Add(imageUrlFromDefaults);
}
}
// Converts the list of URLs by field type into AssetOperations and
// AssetGroupAssetOperations.
foreach (AssetFieldType assetFieldType in imageUrlsByFieldType.Keys)
{
foreach (string imageUrl in imageUrlsByFieldType[assetFieldType])
{
// Builds the image asset.
Asset asset = new Asset
{
ResourceName = ResourceNames.Asset(customerId, temporaryId--),
// Provide a unique friendly name to identify your asset.
// When there is an existing image asset with the same content but a
// different name, the new name will be dropped silently.
Name = $"{assetFieldType}{ExampleUtilities.GetRandomString()}",
ImageAsset = new ImageAsset
{
Data = ByteString.CopyFrom(
MediaUtilities.GetAssetDataFromUrl(imageUrl, config)
)
}
};
// Adds an operation to create the asset.
mutateOperations.Add(new MutateOperation
{
AssetOperation = new AssetOperation
{
Create = asset
}
});
// Builds the AssetGroupAsset
AssetGroupAsset assetGroupAsset = new AssetGroupAsset
{
Asset = asset.ResourceName,
AssetGroup = assetGroupResourceName,
FieldType = assetFieldType
};
// Adds an operation to link the Asset to the AssetGroup.
mutateOperations.Add(new MutateOperation
{
AssetGroupAssetOperation = new AssetGroupAssetOperation
{
Create = assetGroupAsset
}
});
}
}
return mutateOperations;
}
/// <summary>
/// Prints the details of a MutateGoogleAdsResponse. Parses the "response" field name and
/// uses it to extract the new entity's name and resource name.
/// <param name="response">The mutate Google Ads response.</param>
/// </summary>
private void PrintResponseDetails(MutateGoogleAdsResponse response) {