use of com.google.api.ads.admanager.axis.v202205.Creative in project googleads-java-lib by googleads.
the class DeactivateCreativeWrappersForLabel method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param labelId the ID of the creative wrapper label to deactivate.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long labelId) throws RemoteException {
// Get the CreativeWrapperService.
CreativeWrapperServiceInterface creativeWrapperService = adManagerServices.get(session, CreativeWrapperServiceInterface.class);
// Create a statement to select the active creative wrappers for the
// given label.
StatementBuilder statementBuilder = new StatementBuilder().where("WHERE status = :status AND labelId = :labelId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("status", CreativeWrapperStatus.ACTIVE.toString()).withBindVariableValue("labelId", labelId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get creative wrappers by statement.
CreativeWrapperPage page = creativeWrapperService.getCreativeWrappersByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (CreativeWrapper creativeWrapper : page.getResults()) {
System.out.printf("%d) Creative wrapper with ID %d applying to label ID" + " %d will be deactivated.%n", i++, creativeWrapper.getId(), creativeWrapper.getLabelId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of creative wrappers to be deactivated: %d%n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
DeactivateCreativeWrappers action = new DeactivateCreativeWrappers();
// Perform action.
UpdateResult result = creativeWrapperService.performCreativeWrapperAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of creative wrappers deactivated: %d%n", result.getNumChanges());
} else {
System.out.println("No creative wrappers were deactivated.");
}
}
}
use of com.google.api.ads.admanager.axis.v202205.Creative in project googleads-java-lib by googleads.
the class GetActiveCreativeWrappers method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
CreativeWrapperServiceInterface creativeWrapperService = adManagerServices.get(session, CreativeWrapperServiceInterface.class);
// Create a statement to select creative wrappers.
StatementBuilder statementBuilder = new StatementBuilder().where("status = :status").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("status", CreativeWrapperStatus.ACTIVE.toString());
// Retrieve a small amount of creative wrappers at a time, paging through
// until all creative wrappers have been retrieved.
int totalResultSetSize = 0;
do {
CreativeWrapperPage page = creativeWrapperService.getCreativeWrappersByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
// Print out some information for each creative wrapper.
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (CreativeWrapper creativeWrapper : page.getResults()) {
System.out.printf("%d) Creative wrapper with ID %d and label ID %d was found.%n", i++, creativeWrapper.getId(), creativeWrapper.getLabelId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
use of com.google.api.ads.admanager.axis.v202205.Creative in project googleads-java-lib by googleads.
the class UpdateCreativeWrappers method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param creativeWrapperId the ID of the creative wrapper to update.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long creativeWrapperId) throws RemoteException {
// Get the CreativeWrapperService.
CreativeWrapperServiceInterface creativeWrapperService = adManagerServices.get(session, CreativeWrapperServiceInterface.class);
// Create a statement to only select a single creative wrapper by ID.
StatementBuilder statementBuilder = new StatementBuilder().where("id = :id").orderBy("id ASC").limit(1).withBindVariableValue("id", creativeWrapperId);
// Get the creative wrapper.
CreativeWrapperPage page = creativeWrapperService.getCreativeWrappersByStatement(statementBuilder.toStatement());
CreativeWrapper creativeWrapper = Iterables.getOnlyElement(Arrays.asList(page.getResults()));
// Update the creative wrapper ordering.
creativeWrapper.setOrdering(CreativeWrapperOrdering.OUTER);
// Update the creative wrapper on the server.
CreativeWrapper[] creativeWrappers = creativeWrapperService.updateCreativeWrappers(new CreativeWrapper[] { creativeWrapper });
for (CreativeWrapper updatedCreativeWrapper : creativeWrappers) {
System.out.printf("Creative wrapper with ID %d and wrapping order '%s' was updated.%n", updatedCreativeWrapper.getId(), updatedCreativeWrapper.getOrdering());
}
}
use of com.google.api.ads.admanager.axis.v202205.Creative in project googleads-java-lib by googleads.
the class CreateCustomCreatives method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param advertiserId the ID of the advertiser (company) that all creatives will be assigned to.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
* @throws IOException if unable to get media data from the URL.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long advertiserId) throws IOException {
// Get the CreativeService.
CreativeServiceInterface creativeService = adManagerServices.get(session, CreativeServiceInterface.class);
// Create creative size.
Size size = new Size();
size.setWidth(600);
size.setHeight(315);
size.setIsAspectRatio(false);
// Create a custom creative.
CustomCreative customCreative = new CustomCreative();
customCreative.setName("Custom creative #" + new Random().nextInt(Integer.MAX_VALUE));
customCreative.setAdvertiserId(advertiserId);
customCreative.setDestinationUrl("http://google.com");
customCreative.setSize(size);
// Set the custom creative image asset.
CustomCreativeAsset customCreativeAsset = new CustomCreativeAsset();
customCreativeAsset.setMacroName("IMAGE_ASSET");
CreativeAsset asset = new CreativeAsset();
asset.setAssetByteArray(Media.getMediaDataFromUrl("https://goo.gl/3b9Wfh"));
// Filenames must be unique.
asset.setFileName(String.format("image%s.jpg", new Random().nextInt(Integer.MAX_VALUE)));
customCreativeAsset.setAsset(asset);
customCreative.setCustomCreativeAssets(new CustomCreativeAsset[] { customCreativeAsset });
// Set the HTML snippet using the custom creative asset macro.
customCreative.setHtmlSnippet("<a href='%%CLICK_URL_UNESC%%%%DEST_URL%%'>" + "<img src='%%FILE:" + customCreativeAsset.getMacroName() + "%%'/>" + "</a><br>Click above for great deals!");
// Create the creative on the server.
Creative[] creatives = creativeService.createCreatives(new Creative[] { customCreative });
for (Creative createdCreative : creatives) {
System.out.printf("A creative with ID %d, name '%s', and type '%s'" + " was created and can be previewed at: %s%n", createdCreative.getId(), createdCreative.getName(), createdCreative.getClass().getSimpleName(), createdCreative.getPreviewUrl());
}
}
use of com.google.api.ads.admanager.axis.v202205.Creative in project googleads-java-lib by googleads.
the class GetAvailabilityForecast method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param advertiserId the ID of the advertiser (company) to forecast for. Setting an advertiser
* will cause the forecast to apply the appropriate unified blocking rules.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long advertiserId) throws RemoteException {
// Get the ForecastService.
ForecastServiceInterface forecastService = adManagerServices.get(session, ForecastServiceInterface.class);
// Get the NetworkService.
NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class);
// Get the root ad unit ID used to target the whole site.
String rootAdUnitId = networkService.getCurrentNetwork().getEffectiveRootAdUnitId();
// Create inventory targeting.
InventoryTargeting inventoryTargeting = new InventoryTargeting();
// Create ad unit targeting for the root ad unit.
AdUnitTargeting adUnitTargeting = new AdUnitTargeting();
adUnitTargeting.setAdUnitId(rootAdUnitId);
adUnitTargeting.setIncludeDescendants(true);
inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] { adUnitTargeting });
// Create targeting.
Targeting targeting = new Targeting();
targeting.setInventoryTargeting(inventoryTargeting);
// Create a line item.
LineItem lineItem = new LineItem();
lineItem.setTargeting(targeting);
lineItem.setLineItemType(LineItemType.SPONSORSHIP);
// Set the roadblocking type.
lineItem.setRoadblockingType(RoadblockingType.ONE_OR_MORE);
// Set the creative rotation type.
lineItem.setCreativeRotationType(CreativeRotationType.OPTIMIZED);
// Create creative placeholder size.
Size size = new Size();
size.setWidth(300);
size.setHeight(250);
size.setIsAspectRatio(false);
// Create the creative placeholder.
CreativePlaceholder creativePlaceholder = new CreativePlaceholder();
creativePlaceholder.setSize(size);
// Set the size of creatives that can be associated with this line item.
lineItem.setCreativePlaceholders(new CreativePlaceholder[] { creativePlaceholder });
// Set the length of the line item to run.
lineItem.setStartDateTimeType(StartDateTimeType.IMMEDIATELY);
lineItem.setEndDateTime(DateTimes.toDateTime(Instant.now().plus(Duration.standardDays(30L)), "America/New_York"));
// Set the cost type.
lineItem.setCostType(CostType.CPM);
// Set the line item to use 50% of the impressions.
Goal goal = new Goal();
goal.setGoalType(GoalType.DAILY);
goal.setUnitType(UnitType.IMPRESSIONS);
goal.setUnits(50L);
lineItem.setPrimaryGoal(goal);
// Get forecast for prospective line item.
ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem();
prospectiveLineItem.setAdvertiserId(advertiserId);
prospectiveLineItem.setLineItem(lineItem);
AvailabilityForecastOptions options = new AvailabilityForecastOptions();
options.setIncludeContendingLineItems(true);
options.setIncludeTargetingCriteriaBreakdown(true);
AvailabilityForecast forecast = forecastService.getAvailabilityForecast(prospectiveLineItem, options);
long matched = forecast.getMatchedUnits();
double availablePercent = (forecast.getAvailableUnits() / (matched * 1.0)) * 100;
String unitType = forecast.getUnitType().toString().toLowerCase();
System.out.printf("%d %s matched.%n", matched, unitType);
System.out.printf("%.2f%% %s available.%n", availablePercent, unitType);
if (forecast.getPossibleUnits() != null) {
double possiblePercent = (forecast.getPossibleUnits() / (matched * 1.0)) * 100;
System.out.printf("%.2f%% %s possible.%n", possiblePercent, unitType);
}
System.out.printf("%d contending line items.%n", forecast.getContendingLineItems() == null ? 0 : forecast.getContendingLineItems().length);
}
Aggregations