Tạo thử nghiệm

Thử nghiệm là một giao diện để quản lý các chiến dịch thử nghiệm liên quan đến một chiến dịch cơ sở. Chiến dịch thử nghiệm là những chiến dịch đầy đủ chức năng có thể phân phát quảng cáo và tích luỹ số lượt nhấp, chi phí cũng như các chỉ số khác.

Bước đầu tiên để chạy thử nghiệm bằng API Google Ads là tạo một Experiment. Tài nguyên này xác định một số thông tin chính về thử nghiệm mà bạn muốn chạy, chẳng hạn như tên và loại thử nghiệm. Bạn không chỉ định bất kỳ chiến dịch nào tham gia thử nghiệm ở bước này.

Dưới đây là thông tin tổng quan về một số trường chính của Experiment:

  • name: Mỗi thử nghiệm phải có một tên riêng biệt.
  • description: Một trường không bắt buộc mà bạn có thể sử dụng để tham chiếu sau. Không ảnh hưởng đến cách chạy thử nghiệm.
  • suffix: Hậu tố sẽ được thêm vào cuối tên của các chiến dịch thử nghiệm để bạn có thể phân biệt các chiến dịch này với chiến dịch đối chứng. Các khái niệm này sẽ được giải thích thêm trong trang nhóm thử nghiệm.
  • type: Loại thử nghiệm cần chạy. Có nhiều loại thử nghiệm ở đây, nhưng hầu hết đều là thử nghiệm hệ thống. Đối với các thử nghiệm tuỳ chỉnh, bạn nên chỉ định SEARCH_CUSTOM hoặc DISPLAY_CUSTOM.
  • status: Khi tạo thử nghiệm, hãy đặt trường này thành SETUP. Sau đó, khi bạn bắt đầu thử nghiệm, trường này sẽ cho phép bạn kiểm tra trạng thái của thử nghiệm
  • start_dateend_date: Chỉ định thời điểm bắt đầu và kết thúc thử nghiệm.
  • sync_enabled: Tắt theo mặc định. Nếu bạn đặt thành true, các thay đổi đối với chiến dịch gốc trong khi thử nghiệm đang chạy sẽ tự động được sao chép sang chiến dịch thử nghiệm. Tìm hiểu thêm.

Dưới đây là ví dụ về cách tạo thử nghiệm:

Java

private String createExperimentResource(GoogleAdsClient googleAdsClient, long customerId) {
  ExperimentOperation operation =
      ExperimentOperation.newBuilder()
          .setCreate(
              Experiment.newBuilder()
                  // Name must be unique.
                  .setName("Example Experiment #" + getPrintableDateTime())
                  .setType(ExperimentType.SEARCH_CUSTOM)
                  .setSuffix("[experiment]")
                  .setStatus(ExperimentStatus.SETUP)
                  .build())
          .build();

  try (ExperimentServiceClient experimentServiceClient =
      googleAdsClient.getLatestVersion().createExperimentServiceClient()) {
    MutateExperimentsResponse response =
        experimentServiceClient.mutateExperiments(
            Long.toString(customerId), ImmutableList.of(operation));
    String experiment = response.getResults(0).getResourceName();
    System.out.printf("Created experiment with resource name '%s'%n", experiment);
    return experiment;
  }
}
      

C#

/// <summary>
/// Creates the experiment.
/// </summary>
/// <param name="client">The Google Ads client.</param>
/// <param name="customerId">The customer ID for which the call is made.</param>
/// <returns>The resource name of the newly created experiment.</returns>
private static string CreateAnExperiment(GoogleAdsClient client, long customerId)
{
    // Get the ExperimentService.
    ExperimentServiceClient experimentService = client.GetService(
        Services.V19.ExperimentService);

    // Creates the experiment.
    Experiment experiment = new Experiment()
    {
        // Name must be unique.
        Name = $"Example Experiment #{ExampleUtilities.GetRandomString()}",
        Type = ExperimentType.SearchCustom,
        Suffix = "[experiment]",
        Status = ExperimentStatus.Setup
    };

    // Creates the operation.
    ExperimentOperation operation = new ExperimentOperation()
    {
        Create = experiment
    };

    // Makes the API call.
    MutateExperimentsResponse response = experimentService.MutateExperiments(
        customerId.ToString(), new[] { operation });

    // Displays the result.
    string experimentResourceName = response.Results.First().ResourceName;

    Console.WriteLine($"Created experiment with resource name " +
        $"'{experimentResourceName}'.");
    return experimentResourceName;
}

      

PHP

private static function createExperimentResource(
    ExperimentServiceClient $experimentServiceClient,
    int $customerId
): string {
    // Creates an experiment and its operation.
    $experiment = new Experiment([
        // Name must be unique.
        'name' => 'Example Experiment #' . Helper::getPrintableDatetime(),
        'type' => ExperimentType::SEARCH_CUSTOM,
        'suffix' => '[experiment]',
        'status' => ExperimentStatus::SETUP
    ]);
    $experimentOperation = new ExperimentOperation(['create' => $experiment]);

    // Issues a request to create the experiment.
    $response = $experimentServiceClient->mutateExperiments(
        MutateExperimentsRequest::build($customerId, [$experimentOperation])
    );
    $experimentResourceName = $response->getResults()[0]->getResourceName();
    print "Created experiment with resource name '$experimentResourceName'" . PHP_EOL;

    return $experimentResourceName;
}
      

Python

def create_experiment_resource(client, customer_id):
    """Creates a new experiment resource.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.

    Returns:
        the resource name for the new experiment.
    """
    experiment_operation = client.get_type("ExperimentOperation")
    experiment = experiment_operation.create

    experiment.name = f"Example Experiment #{uuid.uuid4()}"
    experiment.type_ = client.enums.ExperimentTypeEnum.SEARCH_CUSTOM
    experiment.suffix = "[experiment]"
    experiment.status = client.enums.ExperimentStatusEnum.SETUP

    experiment_service = client.get_service("ExperimentService")
    response = experiment_service.mutate_experiments(
        customer_id=customer_id, operations=[experiment_operation]
    )

    experiment_resource_name = response.results[0].resource_name
    print(f"Created experiment with resource name {experiment_resource_name}")

    return experiment_resource_name
      

Ruby

def create_experiment_resource(client, customer_id)
  operation = client.operation.create_resource.experiment do |e|
    # Name must be unique.
    e.name = "Example Experiment #{(Time.new.to_f * 1000).to_i}"
    e.type = :SEARCH_CUSTOM
    e.suffix = '[experiment]'
    e.status = :SETUP
  end

  response = client.service.experiment.mutate_experiments(
    customer_id: customer_id,
    operations: [operation],
  )

  experiment = response.results.first.resource_name
  puts "Created experiment with resource name #{experiment}."

  experiment
end
      

Perl

sub create_experiment_resource {
  my ($api_client, $customer_id) = @_;

  my $experiment = Google::Ads::GoogleAds::V19::Resources::Experiment->new({
    # Name must be unique.
    name   => "Example Experiment #" . uniqid(),
    type   => SEARCH_CUSTOM,
    suffix => "[experiment]",
    status => SETUP
  });

  my $operation =
    Google::Ads::GoogleAds::V19::Services::ExperimentService::ExperimentOperation
    ->new({
      create => $experiment
    });

  my $response = $api_client->ExperimentService()->mutate({
      customerId => $customer_id,
      operations => [$operation]});

  my $resource_name = $response->{results}[0]{resourceName};
  printf "Created experiment with resource name '%s'.\n", $resource_name;
  return $resource_name;
}