How to change Ad Group Audience List targeting from Observation to Targeting?

247 views
Skip to first unread message

Josh Barr

unread,
May 9, 2019, 10:49:06 AM5/9/19
to AdWords API and Google Ads API Forum
I have a little app to remove current audience lists from Ad Groups and add the one I want, but I can't figure out how to update the Targeting Setting from Observation to Targeting. Anyone know how to do that?

This is what I have to add a new Audience List to an Ad Group: 


               
 // create user list criterion. Set eligible makes it target
 
CriterionUserList userListCriterion = new CriterionUserList();
 
Long userListId = 1111L;
 userListCriterion
.setUserListId(userListId);
 userListCriterion
.setUserListEligibleForSearch(true);


 
// Create ad group criterion
 
BiddableAdGroupCriterion adgroupCriterion = new BiddableAdGroupCriterion();
 adgroupCriterion
.setAdGroupId(1111L);
 adgroupCriterion
.setCriterion(userListCriterion);


 
// Create operation
 
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
 operation
.setOperand(adgroupCriterion);
 operation
.setOperator(Operator.ADD);


 
AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] {operation};


 
// Apply the criterion
 
AdGroupCriterionReturnValue result = adgroupCriterionService.mutate(operations);

googleadsapi...@google.com

unread,
May 9, 2019, 4:12:41 PM5/9/19
to josh...@uline.com, AdWords API and Google Ads API Forum
Hi Josh, 

You may use the AdGroupService.mutate() operation with the TargetingSettingDetail and targetAll set to true/false to switch between Observation and Targeting. You may refer to the code snippets available here as a reference. Could you please give this a try?

Thanks,
Bharani, Google Ads API Team
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Message has been deleted

Josh Barr

unread,
May 9, 2019, 4:47:35 PM5/9/19
to AdWords API and Google Ads API Forum
Hi Bharani, thank you for responding! Setting userListsSetting.setTargetAll(false); changed it for me: 

public void updateTargeting(AdWordsServicesInterface adWordsServices, AdWordsSession session, AdGroup adGroup) {
 
 
AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
 
List<Setting> settings = Lists.newArrayList();
 
List<TargetingSettingDetail> details = Lists.newArrayList();
 
TargetingSetting targetingSetting = null;
 
if (adGroup.getSettings() != null) {
   
// Find the existing TargetingSetting if it exists, and keep
   
// track of all other settings.
   
for (Setting setting : adGroup.getSettings()) {
     
if (setting instanceof TargetingSetting) {
       targetingSetting
= (TargetingSetting) setting;
     
} else {
       settings
.add(setting);
     
}
   
}


   
if (targetingSetting != null) {
     
// Copy all the existing TargetingSettingDetails except the one for
     
// USER_INTEREST_AND_LIST.
     
for (TargetingSettingDetail settingDetail : targetingSetting.getDetails()) {
       
if (!CriterionTypeGroup.USER_INTEREST_AND_LIST.equals(
               settingDetail
.getCriterionTypeGroup())) {
         details
.add(settingDetail);
       
}
     
}
   
}
 
}


 
if (targetingSetting == null) {
   targetingSetting
= new TargetingSetting();
 
}


 
// Create a new USER_INTEREST_AND_LIST targeting setting detail and add
 
// it to the details list.
 
TargetingSettingDetail userListsSetting = new TargetingSettingDetail();
 userListsSetting
.setCriterionTypeGroup(CriterionTypeGroup.USER_INTEREST_AND_LIST);


 
// true = "Bid only"; false = "Target and bid"
 userListsSetting
.setTargetAll(false);
 details
.add(userListsSetting);


 targetingSetting
.setDetails(details.toArray(new TargetingSettingDetail[details.size()]));


 
// Add the new TargetingSetting for USER_INTEREST_AND_LIST to the settings list.
 settings
.add(targetingSetting);


 adGroup
.setSettings(settings.toArray(new Setting[settings.size()]));
 
 
// Create operations
 
AdGroupOperation operation = new AdGroupOperation();
 operation
.setOperand(adGroup);
 operation
.setOperator(Operator.SET);
 
 
AdGroupOperation[] operations = new AdGroupOperation[] {operation};
 
 
// Update settings by sending operations
 
try {
 
AdGroupReturnValue result = adGroupService.mutate(operations);
 
} catch (RemoteException e) {
 logger
.error("Error updating ad group settings: " + e);
 
}
 
 
}
Reply all
Reply to author
Forward
0 new messages