|
| 1 | +package com.microsoft.bingads.examples.v13; |
| 2 | + |
| 3 | +import com.microsoft.bingads.*; |
| 4 | +import com.microsoft.bingads.v13.campaignmanagement.*; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.UnsupportedEncodingException; |
| 13 | +import java.net.URL; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.rmi.RemoteException; |
| 16 | +import java.util.Base64; |
| 17 | +import java.util.Calendar; |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +public class ResponsiveAdRecommendation extends ExampleBase { |
| 22 | + public static void main(java.lang.String[] args) { |
| 23 | + |
| 24 | + // To run this example you'll need to provide a valid Ad Final URL |
| 25 | + String AdFinalURL = "https://contoso.com"; |
| 26 | + |
| 27 | + // Set to false to disable cleanup of created entities at the end |
| 28 | + boolean DoCleanup = true; |
| 29 | + |
| 30 | + try { |
| 31 | + authorizationData = getAuthorizationData(); |
| 32 | + |
| 33 | + CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient<ICampaignManagementService>( |
| 34 | + authorizationData, |
| 35 | + API_ENVIRONMENT, |
| 36 | + ICampaignManagementService.class); |
| 37 | + |
| 38 | + CreateResponsiveAdRecommendationRequest request = new CreateResponsiveAdRecommendationRequest(); |
| 39 | + |
| 40 | + request.setFinalUrls(new ArrayOfstring(Arrays.asList(AdFinalURL))); |
| 41 | + |
| 42 | + System.out.println("Getting ad recommendation for URL " + AdFinalURL + " ..."); |
| 43 | + |
| 44 | + CreateResponsiveAdRecommendationResponse recommendationResponse = |
| 45 | + CampaignManagementExampleHelper.CampaignManagementService.getService().createResponsiveAdRecommendation(request); |
| 46 | + |
| 47 | + ResponsiveAd ad = recommendationResponse.getResponsiveAd(); |
| 48 | + |
| 49 | + List<AdRecommendationImageSuggestion> imageSuggestions = recommendationResponse |
| 50 | + .getImageSuggestions() |
| 51 | + .getAdRecommendationImageSuggestions(); |
| 52 | + |
| 53 | + // select a few images from the suggested list. This example picks first 5 images |
| 54 | + List<AdRecommendationImageSuggestion> selectedImages = imageSuggestions.subList(0, 5); |
| 55 | + |
| 56 | + // add selected images to your media library |
| 57 | + saveImages(selectedImages); |
| 58 | + |
| 59 | + ad.setImages(new ArrayOfAssetLink(selectedImages.stream().map(x -> x.getAssetLink()).collect(Collectors.toList()))); |
| 60 | + |
| 61 | + ad.setBusinessName("Contoso"); |
| 62 | + ad.setCallToActionLanguage(LanguageName.ENGLISH); |
| 63 | + |
| 64 | + // Create an Audience campaign with one ad group and a responsive ad. |
| 65 | + |
| 66 | + ArrayOfCampaign campaigns = new ArrayOfCampaign(); |
| 67 | + Campaign campaign = new Campaign(); |
| 68 | + campaign.setBudgetType(BudgetLimitType.DAILY_BUDGET_STANDARD); |
| 69 | + // CampaignType must be set for Audience campaigns |
| 70 | + ArrayList<CampaignType> campaignTypes = new ArrayList<CampaignType>(); |
| 71 | + campaignTypes.add(CampaignType.AUDIENCE); |
| 72 | + campaign.setCampaignType(campaignTypes); |
| 73 | + campaign.setDailyBudget(50.00); |
| 74 | + ArrayOfstring languages = new ArrayOfstring(); |
| 75 | + languages.getStrings().add("All"); |
| 76 | + campaign.setLanguages(languages); |
| 77 | + campaign.setName("Ad recommendation test " + new java.util.Date()); |
| 78 | + campaign.setTimeZone("PacificTimeUSCanadaTijuana"); |
| 79 | + campaigns.getCampaigns().add(campaign); |
| 80 | + |
| 81 | + outputStatusMessage("-----\nAddCampaigns:"); |
| 82 | + AddCampaignsResponse addCampaignsResponse = CampaignManagementExampleHelper.addCampaigns( |
| 83 | + authorizationData.getAccountId(), |
| 84 | + campaigns); |
| 85 | + ArrayOfNullableOflong campaignIds = addCampaignsResponse.getCampaignIds(); |
| 86 | + ArrayOfBatchError campaignErrors = addCampaignsResponse.getPartialErrors(); |
| 87 | + outputStatusMessage("CampaignIds:"); |
| 88 | + CampaignManagementExampleHelper.outputArrayOfNullableOflong(campaignIds); |
| 89 | + outputStatusMessage("PartialErrors:"); |
| 90 | + CampaignManagementExampleHelper.outputArrayOfBatchError(campaignErrors); |
| 91 | + |
| 92 | + // Add an ad group within the campaign. |
| 93 | + |
| 94 | + ArrayOfAdGroup adGroups = new ArrayOfAdGroup(); |
| 95 | + AdGroup adGroup = new AdGroup(); |
| 96 | + adGroup.setName("Holiday Sale"); |
| 97 | + adGroup.setStartDate(null); |
| 98 | + Calendar calendar = Calendar.getInstance(); |
| 99 | + adGroup.setEndDate(new com.microsoft.bingads.v13.campaignmanagement.Date()); |
| 100 | + adGroup.getEndDate().setDay(31); |
| 101 | + adGroup.getEndDate().setMonth(12); |
| 102 | + adGroup.getEndDate().setYear(calendar.get(Calendar.YEAR)); |
| 103 | + Bid CpcBid = new Bid(); |
| 104 | + CpcBid.setAmount(0.09); |
| 105 | + adGroup.setCpcBid(CpcBid); |
| 106 | + // Network cannot be set for ad groups in Audience campaigns |
| 107 | + adGroup.setNetwork(null); |
| 108 | + adGroups.getAdGroups().add(adGroup); |
| 109 | + |
| 110 | + outputStatusMessage("-----\nAddAdGroups:"); |
| 111 | + AddAdGroupsResponse addAdGroupsResponse = CampaignManagementExampleHelper.addAdGroups( |
| 112 | + campaignIds.getLongs().get(0), |
| 113 | + adGroups, |
| 114 | + false); |
| 115 | + ArrayOfNullableOflong adGroupIds = addAdGroupsResponse.getAdGroupIds(); |
| 116 | + ArrayOfBatchError adGroupErrors = addAdGroupsResponse.getPartialErrors(); |
| 117 | + outputStatusMessage("AdGroupIds:"); |
| 118 | + CampaignManagementExampleHelper.outputArrayOfNullableOflong(adGroupIds); |
| 119 | + outputStatusMessage("PartialErrors:"); |
| 120 | + CampaignManagementExampleHelper.outputArrayOfBatchError(adGroupErrors); |
| 121 | + |
| 122 | + // Add a responsive ad within the ad group. |
| 123 | + |
| 124 | + ArrayOfAd ads = new ArrayOfAd(); |
| 125 | + |
| 126 | + ads.getAds().add(ad); |
| 127 | + |
| 128 | + outputStatusMessage("-----\nAddAds:"); |
| 129 | + AddAdsResponse addAdsResponse = CampaignManagementExampleHelper.addAds( |
| 130 | + adGroupIds.getLongs().get(0), |
| 131 | + ads); |
| 132 | + ArrayOfNullableOflong adIds = addAdsResponse.getAdIds(); |
| 133 | + ArrayOfBatchError adErrors = addAdsResponse.getPartialErrors(); |
| 134 | + outputStatusMessage("AdIds:"); |
| 135 | + CampaignManagementExampleHelper.outputArrayOfNullableOflong(adIds); |
| 136 | + outputStatusMessage("PartialErrors:"); |
| 137 | + CampaignManagementExampleHelper.outputArrayOfBatchError(adErrors); |
| 138 | + |
| 139 | + System.out.println("\nCreated campaign: " + campaign.getName()); |
| 140 | + |
| 141 | + if (!DoCleanup) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + // Delete the account's media. |
| 146 | + |
| 147 | + ArrayOflong mediaIds = new ArrayOflong(ad.getImages().getAssetLinks().stream().map(x -> x.getAsset().getId()).collect(Collectors.toList())); |
| 148 | + |
| 149 | + outputStatusMessage("-----\nDeleteMedia:"); |
| 150 | + CampaignManagementExampleHelper.deleteMedia( |
| 151 | + authorizationData.getAccountId(), |
| 152 | + mediaIds); |
| 153 | + |
| 154 | + for (java.lang.Long id : mediaIds.getLongs()) { |
| 155 | + outputStatusMessage(String.format("Deleted Media Id %s", id)); |
| 156 | + } |
| 157 | + |
| 158 | + // Delete the campaign and everything it contains e.g., ad groups and ads. |
| 159 | + |
| 160 | + outputStatusMessage("-----\nDeleteCampaigns:"); |
| 161 | + ArrayOflong deleteCampaignIds = new ArrayOflong(); |
| 162 | + deleteCampaignIds.getLongs().add(campaignIds.getLongs().get(0)); |
| 163 | + CampaignManagementExampleHelper.deleteCampaigns( |
| 164 | + authorizationData.getAccountId(), |
| 165 | + deleteCampaignIds); |
| 166 | + outputStatusMessage(String.format("Deleted CampaignId %d", deleteCampaignIds.getLongs().get(0))); |
| 167 | + } catch (Exception ex) { |
| 168 | + String faultXml = ExampleExceptionHelper.getBingAdsExceptionFaultXml(ex, System.out); |
| 169 | + outputStatusMessage(faultXml); |
| 170 | + String message = ExampleExceptionHelper.handleBingAdsSDKException(ex, System.out); |
| 171 | + outputStatusMessage(message); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Get image media that can be managed with the Campaign Management API. |
| 176 | + |
| 177 | + static Image getImageMedia( |
| 178 | + java.lang.String mediaType, |
| 179 | + java.lang.String imageFileName) throws UnsupportedEncodingException, IOException { |
| 180 | + Image image = new Image(); |
| 181 | + image.setData(getBmpBase64String(imageFileName)); |
| 182 | + image.setMediaType(mediaType); |
| 183 | + image.setType("Image"); |
| 184 | + |
| 185 | + return image; |
| 186 | + } |
| 187 | + |
| 188 | + // Get the image media as base64 string. |
| 189 | + |
| 190 | + static java.lang.String getBmpBase64String( |
| 191 | + java.lang.String imageFileName) throws UnsupportedEncodingException, IOException { |
| 192 | + File fi = new File(imageFileName); |
| 193 | + byte[] imageBytes = Files.readAllBytes(fi.toPath()); |
| 194 | + java.lang.String base64String = new java.lang.String(Base64.getEncoder().encode(imageBytes), "UTF-8"); |
| 195 | + return base64String; |
| 196 | + } |
| 197 | + |
| 198 | + private static void saveImages(List<AdRecommendationImageSuggestion> imageSuggestions) throws RemoteException, Exception { |
| 199 | + ArrayOfMedia mediaToAdd = new ArrayOfMedia(); |
| 200 | + |
| 201 | + for (AdRecommendationImageSuggestion imageSuggestion : imageSuggestions) { |
| 202 | + Image image = imageSuggestion.getImage(); |
| 203 | + |
| 204 | + byte[] imageBytes = downloadBytes(new URL(imageSuggestion.getImageUrl())); |
| 205 | + |
| 206 | + String imageBase64 = new String(Base64.getEncoder().encode(imageBytes)); |
| 207 | + |
| 208 | + image.setData(imageBase64); |
| 209 | + |
| 210 | + mediaToAdd.getMedias().add(image); |
| 211 | + } |
| 212 | + |
| 213 | + ArrayOflong mediaIds = CampaignManagementExampleHelper.addMedia( |
| 214 | + authorizationData.getAccountId(), |
| 215 | + mediaToAdd).getMediaIds(); |
| 216 | + |
| 217 | + for (int i = 0; i < mediaIds.getLongs().size(); i++) { |
| 218 | + imageSuggestions.get(i).getAssetLink().getAsset().setId(mediaIds.getLongs().get(i)); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private static byte[] downloadBytes(URL url) throws IOException { |
| 223 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 224 | + |
| 225 | + byte[] buffer = new byte[4096]; |
| 226 | + |
| 227 | + int bytesRead; |
| 228 | + |
| 229 | + InputStream inputStream = null; |
| 230 | + |
| 231 | + try { |
| 232 | + inputStream = url.openStream(); |
| 233 | + |
| 234 | + while ((bytesRead = inputStream.read(buffer)) > 0) { |
| 235 | + outputStream.write(buffer, 0, bytesRead); |
| 236 | + } |
| 237 | + |
| 238 | + return outputStream.toByteArray(); |
| 239 | + } finally { |
| 240 | + if (inputStream != null) { |
| 241 | + inputStream.close(); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments