创建实验

实验是一个界面,用于管理与基本广告系列相关的实验广告系列。实验广告系列是成熟的广告系列,可以投放广告并产生点击次数、费用和其他指标。

使用 Google Ads API 运行实验的第一步是创建 Experiment。此资源定义了您要运行的实验的一些关键信息,例如名称和实验类型。在此步骤中,您未指定实验所涉及的任何广告系列。

下面简要介绍了 Experiment 的一些关键字段:

  • name:每个实验都必须有唯一的名称。
  • description:可选字段,稍后可供您引用。也不会影响实验的运行方式。
  • suffix:后缀将附加到实验组广告系列的名称末尾,以便将其与对照组广告系列区分开来。这些概念将在实验组页面中详细介绍。
  • type:要运行的实验类型。实验类型多种多样,但大多数是系统实验。对于自定义实验,您需要指定 SEARCH_CUSTOMDISPLAY_CUSTOM
  • status:创建实验时,请将此字段设为 SETUP。稍后,实验开始后,您可以通过此字段查看当前实验
  • start_dateend_date:指定实验的开始和结束时间。
  • sync_enabled:默认处于停用状态。如果设置为 true,则在实验运行期间对原始广告系列所做的更改会自动复制到实验广告系列。了解详情

以下是创建实验的示例:

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.V16.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::V16::Resources::Experiment->new({
    # Name must be unique.
    name   => "Example Experiment #" . uniqid(),
    type   => SEARCH_CUSTOM,
    suffix => "[experiment]",
    status => SETUP
  });

  my $operation =
    Google::Ads::GoogleAds::V16::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;
}