use of com.google.api.ads.admanager.axis.v202111.Company in project googleads-java-lib by googleads.
the class UpdateCompanies method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param companyId the ID of the company 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 companyId) throws RemoteException {
// Get the CompanyService.
CompanyServiceInterface companyService = adManagerServices.get(session, CompanyServiceInterface.class);
// Create a statement to only select a single company by ID.
StatementBuilder statementBuilder = new StatementBuilder().where("id = :id").orderBy("id ASC").limit(1).withBindVariableValue("id", companyId);
// Get the company.
CompanyPage page = companyService.getCompaniesByStatement(statementBuilder.toStatement());
Company company = Iterables.getOnlyElement(Arrays.asList(page.getResults()));
// Update the comment.
company.setComment(company.getComment() + " Updated.");
// Update the company on the server.
Company[] companies = companyService.updateCompanies(new Company[] { company });
for (Company updatedCompany : companies) {
System.out.printf("Company with ID %d, name '%s', and comment '%s' was updated.%n", updatedCompany.getId(), updatedCompany.getName(), updatedCompany.getComment());
}
}
use of com.google.api.ads.admanager.axis.v202111.Company in project googleads-java-lib by googleads.
the class CreateCompanies 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 CompanyService.
CompanyServiceInterface companyService = adManagerServices.get(session, CompanyServiceInterface.class);
// Create an advertiser.
Company advertiser = new Company();
advertiser.setName("Advertiser #" + new Random().nextInt(Integer.MAX_VALUE));
advertiser.setType(CompanyType.ADVERTISER);
// Create an agency.
Company agency = new Company();
agency.setName("Agency #" + new Random().nextInt(Integer.MAX_VALUE));
agency.setType(CompanyType.AGENCY);
// Create the companies on the server.
Company[] companies = companyService.createCompanies(new Company[] { advertiser, agency });
for (Company createdCompany : companies) {
System.out.printf("A company with ID %d, name '%s', and type '%s' was created.%n", createdCompany.getId(), createdCompany.getName(), createdCompany.getType());
}
}
use of com.google.api.ads.admanager.axis.v202111.Company in project googleads-java-lib by googleads.
the class GetAdvertisers 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 {
CompanyServiceInterface companyService = adManagerServices.get(session, CompanyServiceInterface.class);
// Create a statement to select companies.
StatementBuilder statementBuilder = new StatementBuilder().where("type = :type").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("type", CompanyType.ADVERTISER.toString());
// Retrieve a small amount of companies at a time, paging through
// until all companies have been retrieved.
int totalResultSetSize = 0;
do {
CompanyPage page = companyService.getCompaniesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
// Print out some information for each company.
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (Company company : page.getResults()) {
System.out.printf("%d) Company with ID %d, name '%s', and type '%s' was found.%n", i++, company.getId(), company.getName(), company.getType());
}
}
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.v202111.Company 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.v202111.Company 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