コード例 #1
0
  public static void runExample(DfpServices dfpServices, DfpSession session) throws Exception {
    // Get the PlacementService.
    PlacementServiceInterface placementService =
        dfpServices.get(session, PlacementServiceInterface.class);

    // Create a statement to get all placements.
    StatementBuilder statementBuilder =
        new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

    // Default for total result set size.
    int totalResultSetSize = 0;

    do {
      // Get placements by statement.
      PlacementPage page =
          placementService.getPlacementsByStatement(statementBuilder.toStatement());

      if (page.getResults() != null) {
        totalResultSetSize = page.getTotalResultSetSize();
        int i = page.getStartIndex();
        for (Placement placement : page.getResults()) {
          System.out.printf(
              "%d) Placement with ID \"%d\" and name \"%s\" was found.\n",
              i++, placement.getId(), placement.getName());
        }
      }

      statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);

    System.out.printf("Number of results found: %d\n", totalResultSetSize);
  }
コード例 #2
0
  public static void runExample(DfpServices dfpServices, DfpSession session, long placementId)
      throws Exception {
    // Get the PlacementService.
    PlacementServiceInterface placementService =
        dfpServices.get(session, PlacementServiceInterface.class);

    // Create a statement to only select a single placement by ID.
    StatementBuilder statementBuilder =
        new StatementBuilder()
            .where("id = :id")
            .orderBy("id ASC")
            .limit(1)
            .withBindVariableValue("id", placementId);

    // Get the placement.
    PlacementPage page = placementService.getPlacementsByStatement(statementBuilder.toStatement());

    Placement placement = Iterables.getOnlyElement(Arrays.asList(page.getResults()));

    // Enable AdSense targeting.
    placement.setTargetingDescription(
        (placement.getDescription() == null || placement.getDescription().isEmpty())
            ? "Insert description here."
            : placement.getDescription());
    placement.setTargetingAdLocation("All images on sports pages.");
    placement.setTargetingSiteName("https://news.google.com");
    placement.setIsAdSenseTargetingEnabled(true);

    // Update the placement on the server.
    Placement[] placements = placementService.updatePlacements(new Placement[] {placement});

    for (Placement updatedPlacement : placements) {
      System.out.printf(
          "Placement with ID \"%d\" and name \"%s\" was updated.\n",
          updatedPlacement.getId(), updatedPlacement.getName());
    }
  }