use of com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface in project googleads-java-lib by googleads.
the class GetCurrentNetwork 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 {
// Get the NetworkService.
NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class);
// Get the current network.
Network network = networkService.getCurrentNetwork();
System.out.printf("Current network has network code '%s' and display name '%s'.%n", network.getNetworkCode(), network.getDisplayName());
}
use of com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface in project googleads-java-lib by googleads.
the class GetDefaultThirdPartyDataDeclaration 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 Exception {
// Get the NetworkService.
NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class);
// Get the PublisherQueryLanguageService.
PublisherQueryLanguageServiceInterface pqlService = adManagerServices.get(session, PublisherQueryLanguageServiceInterface.class);
// Get the current network's default third party data declaration.
ThirdPartyDataDeclaration declaration = networkService.getDefaultThirdPartyDataDeclaration();
if (declaration == null) {
System.out.println("No default ad technology partners have been set on this network.");
} else if (DeclarationType.NONE.equals(declaration.getDeclarationType()) || declaration.getThirdPartyCompanyIds().length == 0) {
System.out.println("This network has specified that there are no ad technology providers " + " associated with its reservation creatives by default.");
} else {
System.out.printf("This network has specified %d ad technology provider(s) associated with its reservation" + " creatives by default:%n", declaration.getThirdPartyCompanyIds().length);
ResultSet companies = pqlService.select(new StatementBuilder().select("name, id").from("rich_media_ad_company").where("id in (:ids)").withBindVariableValue("ids", ImmutableSet.copyOf(Longs.asList(declaration.getThirdPartyCompanyIds()))).toStatement());
System.out.println(Pql.resultSetToString(companies));
}
}
use of com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface 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);
}
use of com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface in project googleads-java-lib by googleads.
the class GetTrafficData 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) 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 the date range. Include the previous and next 7 days.
Interval interval = new Interval(Instant.now().plus(Duration.standardDays(-7)), Instant.now().plus(Duration.standardDays(7)));
DateRange dateRange = new DateRange();
dateRange.setStartDate(DateTimes.toDateTime(interval.getStart()).getDate());
dateRange.setEndDate(DateTimes.toDateTime(interval.getEnd()).getDate());
// Request the traffic data.
TrafficDataRequest trafficDataRequest = new TrafficDataRequest();
trafficDataRequest.setRequestedDateRange(dateRange);
trafficDataRequest.setTargeting(targeting);
TrafficDataResponse trafficData = forecastService.getTrafficData(trafficDataRequest);
// Read the historical traffic data.
TimeSeries historicalTimeSeries = trafficData.getHistoricalTimeSeries();
if (historicalTimeSeries != null) {
Date historicalStartDate = historicalTimeSeries.getTimeSeriesDateRange().getStartDate();
DateTime historicalStart = new DateTime(historicalStartDate.getYear(), historicalStartDate.getMonth(), historicalStartDate.getDay(), 0, 0, 0);
for (int i = 0; i < historicalTimeSeries.getValues().length; i++) {
System.out.printf("%s: %d historical ad opportunities%n", historicalStart.plus(Duration.standardDays(i)).toString(DateTimeFormat.longDate()), historicalTimeSeries.getValues()[i]);
}
}
// Read the forecasted traffic data.
TimeSeries forecastedTimeSeries = trafficData.getForecastedTimeSeries();
if (forecastedTimeSeries != null) {
Date forecastedStartDate = forecastedTimeSeries.getTimeSeriesDateRange().getStartDate();
DateTime forecastedStart = new DateTime(forecastedStartDate.getYear(), forecastedStartDate.getMonth(), forecastedStartDate.getDay(), 0, 0, 0);
for (int i = 0; i < forecastedTimeSeries.getValues().length; i++) {
System.out.printf("%s: %d forecasted ad opportunities%n", forecastedStart.plus(Duration.standardDays(i)).toString(DateTimeFormat.longDate()), forecastedTimeSeries.getValues()[i]);
}
}
}
use of com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface in project googleads-java-lib by googleads.
the class CreateAdUnits 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 {
// Get the InventoryService.
InventoryServiceInterface inventoryService = adManagerServices.get(session, InventoryServiceInterface.class);
// Get the NetworkService.
NetworkServiceInterface networkService = adManagerServices.get(session, NetworkServiceInterface.class);
// Set the parent ad unit's ID for all ad units to be created under.
String parentAdUnitId = networkService.getCurrentNetwork().getEffectiveRootAdUnitId();
// Create a 300x250 web ad unit size.
Size webSize = new Size();
webSize.setWidth(300);
webSize.setHeight(250);
webSize.setIsAspectRatio(false);
AdUnitSize webAdUnitSize = new AdUnitSize();
webAdUnitSize.setSize(webSize);
webAdUnitSize.setEnvironmentType(EnvironmentType.BROWSER);
// Create a 640x360v video ad unit size with a companion.
Size videoSize = new Size();
videoSize.setWidth(640);
videoSize.setHeight(360);
videoSize.setIsAspectRatio(false);
AdUnitSize videoAdUnitSize = new AdUnitSize();
videoAdUnitSize.setSize(videoSize);
videoAdUnitSize.setCompanions(new AdUnitSize[] { webAdUnitSize });
videoAdUnitSize.setEnvironmentType(EnvironmentType.VIDEO_PLAYER);
// Create a web ad unit.
AdUnit webAdUnit = new AdUnit();
webAdUnit.setName("web_ad_unit_" + new Random().nextInt(Integer.MAX_VALUE));
webAdUnit.setDescription(webAdUnit.getName());
webAdUnit.setParentId(parentAdUnitId);
webAdUnit.setTargetWindow(AdUnitTargetWindow.BLANK);
webAdUnit.setAdUnitSizes(new AdUnitSize[] { webAdUnitSize });
// Create a video ad unit.
AdUnit videoAdUnit = new AdUnit();
videoAdUnit.setName("video_ad_unit_" + new Random().nextInt(Integer.MAX_VALUE));
videoAdUnit.setDescription(videoAdUnit.getName());
videoAdUnit.setParentId(parentAdUnitId);
videoAdUnit.setTargetWindow(AdUnitTargetWindow.BLANK);
videoAdUnit.setAdUnitSizes(new AdUnitSize[] { videoAdUnitSize });
// Create the ad units on the server.
AdUnit[] adUnits = inventoryService.createAdUnits(new AdUnit[] { webAdUnit, videoAdUnit });
for (AdUnit adUnit : adUnits) {
System.out.printf("An ad unit with ID '%s', name '%s' was created.%n", adUnit.getId(), adUnit.getName());
}
}
Aggregations