use of com.google.api.ads.adwords.axis.v201809.cm.PageFeed in project googleads-java-lib by googleads.
the class AddDynamicPageFeed method updateCampaignDsaSetting.
/**
* Updates the campaign DSA setting to add DSA pagefeeds.
*/
private static void updateCampaignDsaSetting(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long campaignId, DSAFeedDetails feedDetails) throws ApiException, RemoteException {
// Get the CampaignService.
CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
Selector selector = new SelectorBuilder().fields(CampaignField.Id, CampaignField.Settings).equalsId(campaignId).build();
CampaignPage campaignPage = campaignService.get(selector);
if (campaignPage.getEntries() == null || campaignPage.getTotalNumEntries() == 0) {
throw new IllegalArgumentException("No campaign found with ID: " + campaignId);
}
Campaign campaign = campaignPage.getEntries(0);
if (campaign.getSettings() == null) {
throw new IllegalArgumentException("Campaign with ID " + campaignId + " is not a DSA campaign.");
}
DynamicSearchAdsSetting dsaSetting = (DynamicSearchAdsSetting) Arrays.stream(campaign.getSettings()).filter(DynamicSearchAdsSetting.class::isInstance).findFirst().orElse(null);
if (dsaSetting == null) {
throw new IllegalArgumentException("Campaign with ID " + campaignId + " is not a DSA campaign.");
}
// Use a page feed to specify precisely which URLs to use with your
// Dynamic Search Ads.
PageFeed pageFeed = new PageFeed();
pageFeed.setFeedIds(new long[] { feedDetails.feedId });
dsaSetting.setPageFeed(pageFeed);
// Optional: Specify whether only the supplied URLs should be used with your
// Dynamic Search Ads.
dsaSetting.setUseSuppliedUrlsOnly(true);
Campaign updatedCampaign = new Campaign();
updatedCampaign.setId(campaignId);
updatedCampaign.setSettings(campaign.getSettings());
CampaignOperation operation = new CampaignOperation();
operation.setOperand(updatedCampaign);
operation.setOperator(Operator.SET);
updatedCampaign = campaignService.mutate(new CampaignOperation[] { operation }).getValue(0);
System.out.printf("DSA page feed for campaign ID %d was updated with feed ID %d.%n", updatedCampaign.getId(), feedDetails.feedId);
}
Aggregations