Search in sources :

Example 51 with Targeting

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

the class CreateVideoLineItems method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param orderId the ID of the order that the line item will belong to.
 * @param targetedVideoAdUnitId the ID of the d unit that the line item will target.
 * @param contentId the ID of the video content that the line item will target.
 * @param contentBundleId the ID of the video content bundle that the line item will target.
 * @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 orderId, String targetedVideoAdUnitId, long contentId, long contentBundleId, long cmsMetadataValueId) throws RemoteException {
    // Get the LineItemService.
    LineItemServiceInterface lineItemService = adManagerServices.get(session, LineItemServiceInterface.class);
    // Create content targeting.
    ContentTargeting contentTargeting = new ContentTargeting();
    contentTargeting.setTargetedContentIds(new long[] { contentId });
    contentTargeting.setTargetedVideoContentBundleIds(new long[] { contentBundleId });
    // Target only video players
    RequestPlatformTargeting requestPlatformTargeting = new RequestPlatformTargeting();
    requestPlatformTargeting.setTargetedRequestPlatforms(new RequestPlatform[] { RequestPlatform.VIDEO_PLAYER });
    // Create inventory targeting.
    InventoryTargeting inventoryTargeting = new InventoryTargeting();
    inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] { new AdUnitTargeting(targetedVideoAdUnitId, true) });
    // Create video position targeting.
    VideoPosition videoPosition = new VideoPosition();
    videoPosition.setPositionType(VideoPositionType.PREROLL);
    VideoPositionTarget videoPositionTarget = new VideoPositionTarget();
    videoPositionTarget.setVideoPosition(videoPosition);
    VideoPositionTargeting videoPositionTargeting = new VideoPositionTargeting();
    videoPositionTargeting.setTargetedPositions(new VideoPositionTarget[] { videoPositionTarget });
    // Create custom targeting for CmsMetadataValues.
    CmsMetadataCriteria contentCustomCriteria = new CmsMetadataCriteria();
    contentCustomCriteria.setCmsMetadataValueIds(new long[] { cmsMetadataValueId });
    contentCustomCriteria.setOperator(CmsMetadataCriteriaComparisonOperator.EQUALS);
    CustomCriteriaSet customCriteriaSet = new CustomCriteriaSet();
    customCriteriaSet.setChildren(new CustomCriteriaNode[] { contentCustomCriteria });
    customCriteriaSet.setLogicalOperator(CustomCriteriaSetLogicalOperator.AND);
    // Create targeting.
    Targeting targeting = new Targeting();
    targeting.setContentTargeting(contentTargeting);
    targeting.setInventoryTargeting(inventoryTargeting);
    targeting.setVideoPositionTargeting(videoPositionTargeting);
    targeting.setRequestPlatformTargeting(requestPlatformTargeting);
    targeting.setCustomTargeting(customCriteriaSet);
    // Create local line item object.
    LineItem lineItem = new LineItem();
    lineItem.setName("Video line item #" + new Random().nextInt(Integer.MAX_VALUE));
    lineItem.setOrderId(orderId);
    lineItem.setTargeting(targeting);
    // Allow the line item to be booked even if there is not enough inventory.
    lineItem.setAllowOverbook(true);
    // Set the line item type to SPONSORSHIP.
    lineItem.setLineItemType(LineItemType.SPONSORSHIP);
    // Set the environment type to video.
    lineItem.setEnvironmentType(EnvironmentType.VIDEO_PLAYER);
    // Set the creative rotation type to optimized.
    lineItem.setCreativeRotationType(CreativeRotationType.OPTIMIZED);
    // Create the master creative placeholder.
    CreativePlaceholder creativeMasterPlaceholder = new CreativePlaceholder();
    creativeMasterPlaceholder.setSize(new Size(640, 360, false));
    // Create companion creative placeholders.
    CreativePlaceholder companionCreativePlaceholder = new CreativePlaceholder();
    companionCreativePlaceholder.setSize(new Size(300, 250, false));
    // Set companion creative placeholders.
    creativeMasterPlaceholder.setCompanions(new CreativePlaceholder[] { companionCreativePlaceholder });
    // Set the size of creatives that can be associated with this line item.
    lineItem.setCreativePlaceholders(new CreativePlaceholder[] { creativeMasterPlaceholder });
    // Set delivery of video companions to optional.
    lineItem.setCompanionDeliveryOption(CompanionDeliveryOption.OPTIONAL);
    // Set the maximum video creative length for this line item to 15 seconds.
    lineItem.setVideoMaxDuration(15000L);
    // 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 per day to $1.
    lineItem.setCostType(CostType.CPD);
    lineItem.setCostPerUnit(new Money("USD", 1000000L));
    // Set the percentage to be 100%.
    Goal goal = new Goal();
    goal.setGoalType(GoalType.DAILY);
    goal.setUnits(100L);
    lineItem.setPrimaryGoal(goal);
    // Create the line item on the server.
    LineItem[] lineItems = lineItemService.createLineItems(new LineItem[] { lineItem });
    for (LineItem createdLineItem : lineItems) {
        System.out.printf("A video line item with ID %d and name '%s' was created.%n", createdLineItem.getId(), createdLineItem.getName());
    }
}
Also used : RequestPlatformTargeting(com.google.api.ads.admanager.axis.v202202.RequestPlatformTargeting) RequestPlatformTargeting(com.google.api.ads.admanager.axis.v202202.RequestPlatformTargeting) ContentTargeting(com.google.api.ads.admanager.axis.v202202.ContentTargeting) AdUnitTargeting(com.google.api.ads.admanager.axis.v202202.AdUnitTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202202.InventoryTargeting) Targeting(com.google.api.ads.admanager.axis.v202202.Targeting) VideoPositionTargeting(com.google.api.ads.admanager.axis.v202202.VideoPositionTargeting) LineItemServiceInterface(com.google.api.ads.admanager.axis.v202202.LineItemServiceInterface) Size(com.google.api.ads.admanager.axis.v202202.Size) LineItem(com.google.api.ads.admanager.axis.v202202.LineItem) ContentTargeting(com.google.api.ads.admanager.axis.v202202.ContentTargeting) InventoryTargeting(com.google.api.ads.admanager.axis.v202202.InventoryTargeting) VideoPositionTargeting(com.google.api.ads.admanager.axis.v202202.VideoPositionTargeting) CustomCriteriaSet(com.google.api.ads.admanager.axis.v202202.CustomCriteriaSet) VideoPosition(com.google.api.ads.admanager.axis.v202202.VideoPosition) CreativePlaceholder(com.google.api.ads.admanager.axis.v202202.CreativePlaceholder) Money(com.google.api.ads.admanager.axis.v202202.Money) Goal(com.google.api.ads.admanager.axis.v202202.Goal) AdUnitTargeting(com.google.api.ads.admanager.axis.v202202.AdUnitTargeting) Random(java.util.Random) VideoPositionTarget(com.google.api.ads.admanager.axis.v202202.VideoPositionTarget) CmsMetadataCriteria(com.google.api.ads.admanager.axis.v202202.CmsMetadataCriteria)

Example 52 with Targeting

use of com.google.api.ads.admanager.axis.v202205.Targeting 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 53 with Targeting

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

the class GetAllCustomTargetingKeysAndValues 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 CustomTargetingService.
    CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
    // Get all custom targeting keys.
    List<Long> customTargetingKeyIds = getAllCustomTargetingKeyIds(adManagerServices, session);
    // Create a statement to get all custom targeting values for a custom
    // targeting key.
    StatementBuilder statementBuilder = new StatementBuilder().where("customTargetingKeyId = :customTargetingKeyId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    int totalResultsCounter = 0;
    for (Long customTargetingKeyId : customTargetingKeyIds) {
        // Set the custom targeting key ID to select from.
        statementBuilder.withBindVariableValue("customTargetingKeyId", customTargetingKeyId);
        // Default for total result set size and offset.
        int totalResultSetSize = 0;
        statementBuilder.offset(0);
        do {
            // Get custom targeting values by statement.
            CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
            if (page.getResults() != null) {
                totalResultSetSize = page.getTotalResultSetSize();
                for (CustomTargetingValue customTargetingValue : page.getResults()) {
                    System.out.printf("%d) Custom targeting value with ID %d, belonging to key " + "with ID %d, name '%s' and display name '%s' was found.%n", totalResultsCounter++, customTargetingValue.getId(), customTargetingValue.getCustomTargetingKeyId(), customTargetingValue.getName(), customTargetingValue.getDisplayName());
                }
            }
            statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
        } while (statementBuilder.getOffset() < totalResultSetSize);
    }
    System.out.printf("Number of results found: %d%n", totalResultsCounter);
}
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 54 with Targeting

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

the class GetAllCustomTargetingKeysAndValues method getAllCustomTargetingKeyIds.

/**
 * Gets all custom targeting key IDs.
 */
private static List<Long> getAllCustomTargetingKeyIds(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    List<Long> customTargetingKeyIds = new ArrayList<>();
    // Get the CustomTargetingService.
    CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
    // Create a statement to get all custom targeting keys.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // 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) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (CustomTargetingKey customTargetingKey : page.getResults()) {
                System.out.printf("%d) Custom targeting key with ID %d, name '%s', and " + "display name '%s' was found.%n", i++, customTargetingKey.getId(), customTargetingKey.getName(), customTargetingKey.getDisplayName());
                customTargetingKeyIds.add(customTargetingKey.getId());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    return customTargetingKeyIds;
}
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) ArrayList(java.util.ArrayList) CustomTargetingKeyPage(com.google.api.ads.admanager.axis.v202205.CustomTargetingKeyPage)

Example 55 with Targeting

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

the class GetPredefinedCustomTargetingKeysAndValues 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 {
    CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
    // Get all predefined custom targeting keys.
    List<Long> customTargetingKeyIds = getPredefinedCustomTargetingKeyIds(adManagerServices, session);
    // Create a statement to select custom targeting values.
    StatementBuilder statementBuilder = new StatementBuilder().where("customTargetingKeyId = :customTargetingKeyId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    int totalResultsCounter = 0;
    for (Long customTargetingKeyId : customTargetingKeyIds) {
        // Set the custom targeting key ID to select from.
        statementBuilder.withBindVariableValue("customTargetingKeyId", customTargetingKeyId);
        // Retrieve a small amount of custom targeting values at a time, paging through
        // until all custom targeting values have been retrieved.
        int totalResultSetSize = 0;
        statementBuilder.offset(0);
        do {
            CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
            if (page.getResults() != null) {
                // Print out some information for each custom targeting value.
                totalResultSetSize = page.getTotalResultSetSize();
                for (CustomTargetingValue customTargetingValue : page.getResults()) {
                    System.out.printf("%d) Custom targeting value with ID %d, name '%s', display name '%s', " + "and custom targeting key ID %d was found.%n", totalResultsCounter++, customTargetingValue.getId(), customTargetingValue.getName(), customTargetingValue.getDisplayName(), customTargetingValue.getCustomTargetingKeyId());
                }
            }
            statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
        } while (statementBuilder.getOffset() < totalResultSetSize);
    }
    System.out.printf("Number of results found: %d%n", totalResultsCounter);
}
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)

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