إنشاء شرائح الجمهور المستندة إلى بيانات المستخدِمين وتعديلها

يصف هذا الدليل الطرق التي يمكنك استخدامها لإنشاء شريحة بيانات أو تعديلها.

قبل البدء

قبل المتابعة، عليك إعداد المصادقة.

إنشاء شريحة بيانات جديدة

يمكنك إنشاء شريحة بيانات جديدة باستخدام الـ curators.dataSegments.create طريقة. بعد الإنشاء، تكون شريحة جمهورك المستندة إلى بيانات المستخدمين في الحالة ACTIVE، ما يتيح للصفقات أو الحِزم التي يتم عرضها في عرض الأسعار في الوقت الفعلي استهداف شريحة جمهورك المستندة إلى بيانات المستخدمين.

لإتاحة شرائح البيانات لمخطّطي الوسائط من أجل استهداف الصفقات أو الحِزم، عليك تفعيل ظهور الشريحة في علامة التبويب إعدادات شريحة البيانات ضمن واجهة مستخدم "التنظيم".

يرسل المثال التالي طلب POST إلى طريقة curators.dataSegments.create مع نص JSON يحتوي على كائن DataSegment. ينشئ هذا الطلب شريحة بيانات بالإعدادات المحدّدة.

راحة

طلب

curl --request POST \
'https://authorizedbuyersmarketplace.googleapis.com/v1beta/curators/ACCOUNT_ID/dataSegments' \
--header 'Authorization: Bearer ACCESS_TOKEN' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"cpmFee":{"nanos":100,"units":0,"currencyCode":"USD"},"name":"curators/ACCOUNT_ID/dataSegments/test-data-segment-2"}' \
--compressed

غيِّر القيم في السلسلة على الشكل التالي:

  • ACCOUNT_ID: رقم تعريف حسابك
  • ACCESS_TOKEN: رمز الدخول

الردّ

{
  "name": "curators/ACCOUNT_ID/dataSegments/test-data-segment-2",
  "createTime": "2025-08-14T17:45:08.744379Z",
  "updateTime": "2025-08-14T17:45:08.744379Z",
  "cpmFee": {
    "currencyCode": "USD",
    "nanos": 100
  },
  "state": "ACTIVE"
}

جافا

/*
 * Copyright (c) 2026 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
 * or implied. See the License for the specific language governing permissions and limitations
under
 * the License.
 */

package com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.curators.dataSegments;

import com.google.api.services.authorizedbuyersmarketplace.v1beta.AuthorizedBuyersMarketplace;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.DataSegment;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.Money;
import com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.Utils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

public class CreateDataSegment {

  private CreateDataSegment() {}

  /**
   * Executes the create operation for a data segment.
   *
   * @param marketplaceClient the initialized Marketplace API client.
   * @param accountId the account ID of the curator that is creating the data segment
   * @param dataSegmentId the id of the data segment to be created
   * @param cpmFeeCurrencyCode the currency code of the data segment's CPM fee
   * @param cpmFeeUnits the whole units of the data segment's CPM fee
   * @param cpmFeeNanos the nanos of the data segment's CPM fee, representing a fraction of the
   *     specified currency.
   */
  public static void execute(
      AuthorizedBuyersMarketplace marketplaceClient,
      Long accountId,
      String dataSegmentId,
      String cpmFeeCurrencyCode,
      Long cpmFeeUnits,
      Integer cpmFeeNanos) throws IOException {

    // Create the required objects to call the create method.
    DataSegment dataSegmentToCreate =
        new DataSegment()
            .setName(String.format("curators/%d/dataSegments/%s", accountId, dataSegmentId))
            .setCpmFee(
                new Money()
                    .setCurrencyCode(cpmFeeCurrencyCode)
                    .setUnits(cpmFeeUnits)
                    .setNanos(cpmFeeNanos));

    String parent = String.format("curators/%d", accountId);

    // Call the API to create the data segment.
    DataSegment createdDataSegment =
        marketplaceClient.curators().dataSegments().create(parent, dataSegmentToCreate).execute();

    System.out.println("Successfully created data segment:");
    Utils.jsonPrettyPrint(createdDataSegment);
  }

  /**
   * Creates and configures the ArgumentParser for this sample.
   *
   * @return the configured ArgumentParser.
   */
  private static ArgumentParser createArgumentParser() {
    ArgumentParser parser =
        ArgumentParsers.newFor("CreateDataSegment")
            .build()
            .defaultHelp(true)
            .description("Creates a new data segment for the given curator account.");

    // Required arguments.
    parser
        .addArgument("-a", "--account_id")
        .help("The account ID of the curator that will create the curated package.")
        .required(true)
        .type(Long.class);
    parser
        .addArgument("-d", "--data_segment_id")
        .help(
            "The identifier to be used in the name of the data segment to be created."
                + " The final name will be curators/{account_id}/dataSegments/{data_segment_id}")
        .required(true);
    parser
        .addArgument("--cpm_fee_currency_code")
        .help("The three-letter currency code defined in ISO 4217. For example, \"USD\".")
        .required(true);
    parser
        .addArgument("--cpm_fee_units")
        .help(
            "The whole units of the CPM fee. For example, if `currencyCode` were set to \"USD\", "
                + "a value of \"1\" would be $1 USD for 1,000 views. ")
        .type(Long.class)
        .required(true);
    parser
        .addArgument("--cpm_fee_nanos")
        .help(
            "The nano units of the CPM fee, representing a fraction of the specified "
                + "currency. For example, if `currencyCode` were set to \"USD\", a value of "
                + "\"20000000\" would be $0.02 USD for 1,000 views. ")
        .type(Integer.class)
        .required(true);

    return parser;
  }

  public static void main(String[] args) {
    ArgumentParser parser = createArgumentParser();
    Namespace parsedArgs = null;
    try {
      parsedArgs = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
      parser.handleError(e);
      System.exit(1);
    }

    AuthorizedBuyersMarketplace client = null;
    try {
      client = Utils.getMarketplaceClient();
    } catch (IOException ex) {
      System.out.printf("Unable to create Marketplace API service:%n%s", ex);
      System.out.println("Did you specify a valid path to a service account key file?");
      System.exit(1);
    } catch (GeneralSecurityException ex) {
      System.out.printf("Unable to establish secure HttpTransport:%n%s", ex);
      System.exit(1);
    }

    // Parse arguments.
    Long accountId = parsedArgs.getLong("account_id");
    String dataSegmentId = parsedArgs.getString("data_segment_id");
    String cpmFeeCurrencyCode = parsedArgs.getString("cpm_fee_currency_code");
    Long cpmFeeUnits = parsedArgs.getLong("cpm_fee_units");
    Integer cpmFeeNanos = parsedArgs.getInt("cpm_fee_nanos");

    try {
      execute(client, accountId, dataSegmentId, cpmFeeCurrencyCode, cpmFeeUnits, cpmFeeNanos);
    } catch (Exception e) {
      System.out.printf("Marketplace API returned error response:%n%s", e);
      System.exit(1);
    }
  }
}

تعديل شريحة بيانات حالية

لتعديل شريحة بيانات حالية، يمكنك استخدام الـ curators.dataSegments.patch طريقة. على سبيل المثال، استخدِم هذه الطريقة لتعديل الـ cpmFee حقل لشريحة البيانات.

يرسل المثال التالي طلب PATCH إلى طريقة curators.dataSegments.patch مع نص JSON يحتوي على كائن DataSegment. يعدّل هذا الطلب شريحة البيانات المحدّدة، مع تغيير الحقول المحدّدة فقط في مَعلمة طلب البحث updateMask.

راحة

طلب

curl --request PATCH \
  'https://authorizedbuyersmarketplace.googleapis.com/v1beta/curators/ACCOUNT_ID/dataSegments/test-data-segment-2?updateMask=cpmFee.nanos' \
  --header 'Authorization: Bearer ACCESS_TOKEN' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json'  \
  --data '{"cpmFee":{"nanos":50}}' \
  --compressed

الردّ

{
  "name": "curators/ACCOUNT_ID/dataSegments/test-data-segment-2",
  "createTime": "2025-08-14T17:45:08.744Z",
  "updateTime": "2025-08-14T17:56:02.200619Z",
  "cpmFee": {
    "currencyCode": "USD",
    "nanos": 50
  },
  "state": "ACTIVE"
}

جافا

/*
 * Copyright (c) 2026 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
 * or implied. See the License for the specific language governing permissions and limitations
under
 * the License.
 */

package com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.curators.dataSegments;

import com.google.api.services.authorizedbuyersmarketplace.v1beta.AuthorizedBuyersMarketplace;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.DataSegment;
import com.google.api.services.authorizedbuyersmarketplace.v1beta.model.Money;
import com.google.api.services.samples.authorizedbuyers.marketplace.v1beta.Utils;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

public class PatchDataSegment {

  private PatchDataSegment() {}

  /**
   * Executes the patch operation for a data segment.
   *
   * @param marketplaceClient the initialized Marketplace API client.
   * @param accountId the account ID of the curator that created the data segment.
   * @param dataSegmentId the resource ID of the data segment to update.
   * @param cpmFeeUnits the modified value of `DataSegment.cpmFee.units` (or null if the field is
   *     not being modified).
   * @param cpmFeeNanos the modified value of `DataSegment.cpmFee.nanos` (or null if the field is
   *     not being modified).
   */
  public static void execute(
      AuthorizedBuyersMarketplace marketplaceClient,
      Long accountId,
      String dataSegmentId,
      Long cpmFeeUnits,
      Integer cpmFeeNanos)
      throws IOException {

    // Construct resources needed for the patch request.
    String name = String.format("curators/%d/dataSegments/%s", accountId, dataSegmentId);

    List<String> patchedFields = new ArrayList<>();
    DataSegment patchedDataSegment = new DataSegment();
    Money cpmFee = new Money();
    if (cpmFeeUnits != null) {
      cpmFee.setUnits(cpmFeeUnits);
      patchedFields.add("cpmFee.units");
    }
    if (cpmFeeNanos != null) {
      cpmFee.setNanos(cpmFeeNanos);
      patchedFields.add("cpmFee.nanos");
    }
    patchedDataSegment.setCpmFee(cpmFee);
    String updateMask = String.join(",", patchedFields);

    // Patch the data segment.
    DataSegment returnedDataSegment =
        marketplaceClient
            .curators()
            .dataSegments()
            .patch(name, patchedDataSegment)
            .setUpdateMask(updateMask)
            .execute();

    System.out.println("Successfully patched data segment:");
    Utils.jsonPrettyPrint(returnedDataSegment);
  }

  /**
   * Creates and configures the ArgumentParser for this sample.
   *
   * @return the configured ArgumentParser.
   */
  private static ArgumentParser createArgumentParser() {
    ArgumentParser parser =
        ArgumentParsers.newFor("PatchDataSegment")
            .build()
            .defaultHelp(true)
            .description(
                "Updates a specific data segment. To modify the state of a "
                    + "data segment, use the curators.dataSegments.activate or "
                    + "curators.dataSegments.deactivate methods instead.");

    // Required arguments.
    parser
        .addArgument("-a", "--account_id")
        .help("The account ID of the curator that created the data segment.")
        .required(true)
        .type(Long.class);
    parser
        .addArgument("-d", "--data_segment_id")
        .help("The resource ID of the data segment to update.")
        .required(true);

    // Optional arguments.
    // (Note: the currency code of the cpm fee is not modifiable, and thus is not included here.)
    parser
        .addArgument("--cpm_fee_units")
        .help(
            "The modified value of `DataSegment.cpmFee.units`. This represents the whole units of "
                + "the CPM fee. For example, if `currencyCode` were set to \"USD\", "
                + "a value of \"1\" would be $1 USD for 1,000 views. ")
        .type(Long.class);
    parser
        .addArgument("--cpm_fee_nanos")
        .help(
            "The modified value of `DataSegment.cpmFee.nanos`. This represents the nano units of "
                + "the CPM fee, representing a fraction of the specified currency. "
                + "For example, if `currencyCode` were set to \"USD\", a value of "
                + "\"20000000\" would be $0.02 USD for 1,000 views. ")
        .type(Integer.class);

    return parser;
  }

  public static void main(String[] args) {
    ArgumentParser parser = createArgumentParser();
    Namespace parsedArgs = null;
    try {
      parsedArgs = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
      parser.handleError(e);
      System.exit(1);
    }

    AuthorizedBuyersMarketplace client = null;
    try {
      client = Utils.getMarketplaceClient();
    } catch (IOException ex) {
      System.out.printf("Unable to create Marketplace API service:%n%s", ex);
      System.out.println("Did you specify a valid path to a service account key file?");
      System.exit(1);
    } catch (GeneralSecurityException ex) {
      System.out.printf("Unable to establish secure HttpTransport:%n%s", ex);
      System.exit(1);
    }

    // Get parsed arguments.
    Long accountId = parsedArgs.getLong("account_id");
    String dataSegmentId = parsedArgs.getString("data_segment_id");
    Long cpmFeeUnits = parsedArgs.getLong("cpm_fee_units");
    Integer cpmFeeNanos = parsedArgs.getInt("cpm_fee_nanos");

    try {
      execute(client, accountId, dataSegmentId, cpmFeeUnits, cpmFeeNanos);
    } catch (Exception e) {
      System.out.printf("Marketplace API returned error response:%n%s", e);
      System.exit(1);
    }
  }
}

الخطوات التالية