Search in sources :

Example 56 with Targeting

use of com.google.api.ads.admanager.axis.v202205.Targeting in project googleads-java-lib by googleads.

the class UpdateCustomTargetingKeys method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param customTargetingKeyId the ID of the custom targeting key 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 customTargetingKeyId) throws RemoteException {
    // Get the CustomTargetingService.
    CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
    // Create a statement to get custom targeting keys.
    StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", customTargetingKeyId);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get custom targeting keys by statement.
        CustomTargetingKeyPage page = customTargetingService.getCustomTargetingKeysByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            CustomTargetingKey[] customTargetingKeys = page.getResults();
            totalResultSetSize = page.getTotalResultSetSize();
            // name.
            for (CustomTargetingKey customTargetingKey : customTargetingKeys) {
                if (customTargetingKey.getDisplayName() == null) {
                    customTargetingKey.setDisplayName(customTargetingKey.getName());
                }
                customTargetingKey.setDisplayName(customTargetingKey.getDisplayName() + " (Deprecated)");
            }
            // Update the custom targeting keys on the server.
            customTargetingKeys = customTargetingService.updateCustomTargetingKeys(customTargetingKeys);
            for (CustomTargetingKey updatedCustomTargetingKey : customTargetingKeys) {
                System.out.printf("Custom targeting key with ID %d, name '%s', and display name " + "'%s' was updated.%n", updatedCustomTargetingKey.getId(), updatedCustomTargetingKey.getName(), updatedCustomTargetingKey.getDisplayName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
}
Also used : CustomTargetingServiceInterface(com.google.api.ads.admanager.axis.v202205.CustomTargetingServiceInterface) CustomTargetingKey(com.google.api.ads.admanager.axis.v202205.CustomTargetingKey) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder) CustomTargetingKeyPage(com.google.api.ads.admanager.axis.v202205.CustomTargetingKeyPage)

Example 57 with Targeting

use of com.google.api.ads.admanager.axis.v202205.Targeting in project googleads-java-lib by googleads.

the class UpdateCustomTargetingValues method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param customTargetingValueId the ID of the custom targeting value 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 customTargetingValueId) throws RemoteException {
    // Get the CustomTargetingService.
    CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
    // Create a statement to get custom targeting values.
    StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", customTargetingValueId);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get custom targeting values by statement.
        CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            CustomTargetingValue[] customTargetingValues = page.getResults();
            // name.
            for (CustomTargetingValue customTargetingValue : customTargetingValues) {
                if (customTargetingValue.getDisplayName() == null) {
                    customTargetingValue.setDisplayName(customTargetingValue.getName());
                }
                customTargetingValue.setDisplayName(customTargetingValue.getDisplayName() + " (Deprecated)");
            }
            // Update the custom targeting values on the server.
            customTargetingValues = customTargetingService.updateCustomTargetingValues(customTargetingValues);
            for (CustomTargetingValue updatedCustomTargetingValue : customTargetingValues) {
                System.out.printf("Custom targeting value with ID %d, name '%s', and display name " + "'%s' was updated.%n", updatedCustomTargetingValue.getId(), updatedCustomTargetingValue.getName(), updatedCustomTargetingValue.getDisplayName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
}
Also used : CustomTargetingValue(com.google.api.ads.admanager.axis.v202205.CustomTargetingValue) CustomTargetingServiceInterface(com.google.api.ads.admanager.axis.v202205.CustomTargetingServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder) CustomTargetingValuePage(com.google.api.ads.admanager.axis.v202205.CustomTargetingValuePage)

Example 58 with Targeting

use of com.google.api.ads.admanager.axis.v202205.Targeting 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]);
        }
    }
}
Also used : TrafficDataResponse(com.google.api.ads.admanager.axis.v202205.TrafficDataResponse) NetworkServiceInterface(com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface) TimeSeries(com.google.api.ads.admanager.axis.v202205.TimeSeries) AdUnitTargeting(com.google.api.ads.admanager.axis.v202205.AdUnitTargeting) Targeting(com.google.api.ads.admanager.axis.v202205.Targeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202205.InventoryTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202205.InventoryTargeting) Date(com.google.api.ads.admanager.axis.v202205.Date) DateTime(org.joda.time.DateTime) ForecastServiceInterface(com.google.api.ads.admanager.axis.v202205.ForecastServiceInterface) DateRange(com.google.api.ads.admanager.axis.v202205.DateRange) AdUnitTargeting(com.google.api.ads.admanager.axis.v202205.AdUnitTargeting) TrafficDataRequest(com.google.api.ads.admanager.axis.v202205.TrafficDataRequest) Interval(org.joda.time.Interval)

Example 59 with Targeting

use of com.google.api.ads.admanager.axis.v202205.Targeting in project googleads-java-lib by googleads.

the class GetAllTargetingPresets 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 TargetingPresetService.
    TargetingPresetServiceInterface targetingPresetService = adManagerServices.get(session, TargetingPresetServiceInterface.class);
    // Create a statement to get all targeting presets.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get targeting presets by statement.
        TargetingPresetPage page = targetingPresetService.getTargetingPresetsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (TargetingPreset targetingPreset : page.getResults()) {
                System.out.printf("%d) Targeting preset with ID %d and name '%s' was found.%n", i++, targetingPreset.getId(), targetingPreset.getName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : TargetingPresetServiceInterface(com.google.api.ads.admanager.axis.v202205.TargetingPresetServiceInterface) TargetingPresetPage(com.google.api.ads.admanager.axis.v202205.TargetingPresetPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder) TargetingPreset(com.google.api.ads.admanager.axis.v202205.TargetingPreset)

Aggregations

Random (java.util.Random)22 LinkedHashSet (java.util.LinkedHashSet)10 Set (java.util.Set)10 Before (org.junit.Before)10 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder)9 Targeting (com.google.api.ads.admanager.axis.v202108.Targeting)9 Targeting (com.google.api.ads.admanager.axis.v202111.Targeting)9 Targeting (com.google.api.ads.admanager.axis.v202202.Targeting)9 CustomTargetingServiceInterface (com.google.api.ads.admanager.axis.v202205.CustomTargetingServiceInterface)9 Targeting (com.google.api.ads.admanager.axis.v202205.Targeting)9 AdUnitTargeting (com.google.api.ads.admanager.axis.v202108.AdUnitTargeting)8 InventoryTargeting (com.google.api.ads.admanager.axis.v202108.InventoryTargeting)8 AdUnitTargeting (com.google.api.ads.admanager.axis.v202111.AdUnitTargeting)8 InventoryTargeting (com.google.api.ads.admanager.axis.v202111.InventoryTargeting)8 AdUnitTargeting (com.google.api.ads.admanager.axis.v202202.AdUnitTargeting)8 InventoryTargeting (com.google.api.ads.admanager.axis.v202202.InventoryTargeting)8 AdUnitTargeting (com.google.api.ads.admanager.axis.v202205.AdUnitTargeting)7 InventoryTargeting (com.google.api.ads.admanager.axis.v202205.InventoryTargeting)7 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202108.NetworkServiceInterface)6 CreativePlaceholder (com.google.api.ads.admanager.axis.v202108.CreativePlaceholder)5