Search in sources :

Example 41 with AdUnitTargeting

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

the class CreateTrafficForecastSegments 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 adjustment service and the network service.
    AdjustmentServiceInterface adjustmentService = adManagerServices.get(session, AdjustmentServiceInterface.class);
    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 (i.e. the whole network).
    AdUnitTargeting adUnitTargeting = new AdUnitTargeting();
    adUnitTargeting.setAdUnitId(rootAdUnitId);
    adUnitTargeting.setIncludeDescendants(true);
    inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] { adUnitTargeting });
    // Create targeting for United States traffic.
    GeoTargeting geoTargeting = new GeoTargeting();
    Location countryLocation = new Location();
    countryLocation.setId(2840L);
    geoTargeting.setTargetedLocations(new Location[] { countryLocation });
    Targeting targeting = new Targeting();
    targeting.setInventoryTargeting(inventoryTargeting);
    targeting.setGeoTargeting(geoTargeting);
    TrafficForecastSegment segment = new TrafficForecastSegment();
    segment.setTargeting(targeting);
    segment.setName("Forecast segment #" + new Random().nextInt(Integer.MAX_VALUE));
    // Create the traffic forecst segment on the server.
    TrafficForecastSegment[] segments = adjustmentService.createTrafficForecastSegments(new TrafficForecastSegment[] { segment });
    for (TrafficForecastSegment createdSegment : segments) {
        System.out.printf("Traffic forecast segment with ID %d and %d forecast adjustments was created.%n", createdSegment.getId(), createdSegment.getActiveForecastAdjustmentCount());
    }
}
Also used : NetworkServiceInterface(com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface) TrafficForecastSegment(com.google.api.ads.admanager.axis.v202205.TrafficForecastSegment) AdUnitTargeting(com.google.api.ads.admanager.axis.v202205.AdUnitTargeting) AdUnitTargeting(com.google.api.ads.admanager.axis.v202205.AdUnitTargeting) Targeting(com.google.api.ads.admanager.axis.v202205.Targeting) GeoTargeting(com.google.api.ads.admanager.axis.v202205.GeoTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202205.InventoryTargeting) Random(java.util.Random) AdjustmentServiceInterface(com.google.api.ads.admanager.axis.v202205.AdjustmentServiceInterface) InventoryTargeting(com.google.api.ads.admanager.axis.v202205.InventoryTargeting) GeoTargeting(com.google.api.ads.admanager.axis.v202205.GeoTargeting) Location(com.google.api.ads.admanager.axis.v202205.Location)

Example 42 with AdUnitTargeting

use of com.google.api.ads.admanager.axis.v202205.AdUnitTargeting 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)

Aggregations

Random (java.util.Random)24 Before (org.junit.Before)10 AdUnitTargeting (com.google.api.ads.admanager.axis.v202108.AdUnitTargeting)9 InventoryTargeting (com.google.api.ads.admanager.axis.v202108.InventoryTargeting)9 AdUnitTargeting (com.google.api.ads.admanager.axis.v202111.AdUnitTargeting)9 InventoryTargeting (com.google.api.ads.admanager.axis.v202111.InventoryTargeting)9 AdUnitTargeting (com.google.api.ads.admanager.axis.v202202.AdUnitTargeting)9 InventoryTargeting (com.google.api.ads.admanager.axis.v202202.InventoryTargeting)9 AdUnitTargeting (com.google.api.ads.admanager.axis.v202205.AdUnitTargeting)9 InventoryTargeting (com.google.api.ads.admanager.axis.v202205.InventoryTargeting)9 Targeting (com.google.api.ads.admanager.axis.v202108.Targeting)8 Targeting (com.google.api.ads.admanager.axis.v202111.Targeting)8 Targeting (com.google.api.ads.admanager.axis.v202202.Targeting)8 Targeting (com.google.api.ads.admanager.axis.v202205.Targeting)8 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202108.NetworkServiceInterface)7 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202111.NetworkServiceInterface)7 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202202.NetworkServiceInterface)7 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202205.NetworkServiceInterface)7 CreativePlaceholder (com.google.api.ads.admanager.axis.v202108.CreativePlaceholder)5 Goal (com.google.api.ads.admanager.axis.v202108.Goal)5