创建实验
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
实验是一种界面,用于管理与基准广告系列相关的实验广告系列。实验广告系列是功能完善的广告系列,可以投放广告并产生点击次数、费用和其他指标。
使用 Google Ads API 运行实验的第一步是创建 Experiment
。此资源定义了您要运行的实验的一些关键信息,例如名称和实验类型。在此步骤中,您无需指定参与实验的任何广告系列。
以下是 Experiment
的一些关键字段的概览:
name
:每个实验都必须具有唯一的名称。
description
:一个可选字段,可供您日后引用。不会影响实验的运行方式。
suffix
:后缀将附加到实验组广告系列名称的末尾,以便您将它们与对照组广告系列区分开来。
实验组页面将进一步说明这些概念。
type
:要运行的实验类型。此处有多种类型,但大多数都是系统实验。对于自定义实验,您需要指定 SEARCH_CUSTOM
或 DISPLAY_CUSTOM
。
status
:创建实验时,请将此字段设置为 SETUP
。稍后,当您开始实验后,您可以通过此字段查看实验的当前状态
start_date
和 end_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.V21.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: GoogleAdsClient, customer_id: str
) -> str:
"""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: ExperimentOperation = client.get_type(
"ExperimentOperation"
)
experiment: 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: ExperimentServiceClient = client.get_service(
"ExperimentService"
)
response: MutateExperimentsResponse = experiment_service.mutate_experiments(
customer_id=customer_id, operations=[experiment_operation]
)
experiment_resource_name: str = 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::V21::Resources::Experiment->new({
# Name must be unique.
name => "Example Experiment #" . uniqid(),
type => SEARCH_CUSTOM,
suffix => "[experiment]",
status => SETUP
});
my $operation =
Google::Ads::GoogleAds::V21::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;
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eExperiments in Google Ads allow you to create and manage experimental campaigns to test variations against a base campaign.\u003c/p\u003e\n"],["\u003cp\u003eExperiment campaigns function as independent campaigns, accumulating performance data like clicks, costs, and other metrics.\u003c/p\u003e\n"],["\u003cp\u003eThe initial step involves defining the experiment's parameters, such as name, type, and schedule, using the \u003ccode\u003eExperiment\u003c/code\u003e resource.\u003c/p\u003e\n"],["\u003cp\u003eTo distinguish them from the base campaign, experiment campaigns will have a defined suffix appended to their names.\u003c/p\u003e\n"],["\u003cp\u003eChanges to the original campaign can be automatically synchronized with experiment campaigns by enabling the \u003ccode\u003esync_enabled\u003c/code\u003e option.\u003c/p\u003e\n"]]],[],null,["# Create experiments\n\nExperiments are an interface to manage experimental campaigns related to a base\ncampaign. Experiment campaigns are full-fledged campaigns that can serve ads\nand accrue clicks, cost, and other metrics.\n\nThe first step in running an experiment using the Google Ads API is to create an\n[`Experiment`](/google-ads/api/reference/rpc/v21/Experiment). This resource defines some key\ninformation about the experiment you want to run, such as a name and experiment\ntype. You do not specify any of the campaigns involved in the experiment at\nthis step.\n\nHere's an overview of some key fields for an `Experiment`:\n\n- `name`: Each experiment must have a unique name.\n- `description`: An optional field that you can use to reference later. Does not affect how the experiment runs.\n- `suffix`: The suffix will be appended to the end of the names of the treatment campaigns so you can distinguish them from the control campaign. These concepts will be explained further in the [experiment\n arms](/google-ads/api/docs/experiments/experiment-arms) page.\n- `type`: What type of experiment to run. There are many types here, but most of them are system experiments. For your custom experiments, you'll want to specify either `SEARCH_CUSTOM` or `DISPLAY_CUSTOM`.\n- `status`: When creating an experiment, set this field to `SETUP`. Later on, once you begin the experiment, this field will let you check what it's currently doing\n- `start_date` and `end_date`: Specify when the experiment should start and end.\n- `sync_enabled`: Disabled by default. If set to `true`, changes made to the original campaign while your experiment is running are automatically copied to the experiment campaign. [Learn\n more](//support.google.com/google-ads/answer/10575537).\n\nHere's an example of creating an experiment:\n\n\n### Java\n\n```java\nprivate String createExperimentResource(GoogleAdsClient googleAdsClient, long customerId) {\n ExperimentOperation operation =\n ExperimentOperation.newBuilder()\n .setCreate(\n Experiment.newBuilder()\n // Name must be unique.\n .setName(\"Example Experiment #\" + getPrintableDateTime())\n .setType(ExperimentType.SEARCH_CUSTOM)\n .setSuffix(\"[experiment]\")\n .setStatus(ExperimentStatus.SETUP)\n .build())\n .build();\n\n try (ExperimentServiceClient experimentServiceClient =\n googleAdsClient.getLatestVersion().createExperimentServiceClient()) {\n MutateExperimentsResponse response =\n experimentServiceClient.mutateExperiments(\n Long.toString(customerId), ImmutableList.of(operation));\n String experiment = response.getResults(0).getResourceName();\n System.out.printf(\"Created experiment with resource name '%s'%n\", experiment);\n return experiment;\n }\n}https://github.com/googleads/google-ads-java/blob/3c3c1041c2a0ab81553e3b2a79876256649397ed/google-ads-examples/src/main/java/com/google/ads/googleads/examples/campaignmanagement/CreateExperiment.java#L126-L148\n \n```\n\n### C#\n\n```c#\n/// \u003csummary\u003e\n/// Creates the experiment.\n/// \u003c/summary\u003e\n/// \u003cparam name=\"client\"\u003eThe Google Ads client.\u003c/param\u003e\n/// \u003cparam name=\"customerId\"\u003eThe customer ID for which the call is made.\u003c/param\u003e\n/// \u003creturns\u003eThe resource name of the newly created experiment.\u003c/returns\u003e\nprivate static string CreateAnExperiment(GoogleAdsClient client, long customerId)\n{\n // Get the ExperimentService.\n ExperimentServiceClient experimentService = client.GetService(\n Services.V21.ExperimentService);\n\n // Creates the experiment.\n Experiment experiment = new Experiment()\n {\n // Name must be unique.\n Name = $\"Example Experiment #{ExampleUtilities.GetRandomString()}\",\n Type = ExperimentType.SearchCustom,\n Suffix = \"[experiment]\",\n Status = ExperimentStatus.Setup\n };\n\n // Creates the operation.\n ExperimentOperation operation = new ExperimentOperation()\n {\n Create = experiment\n };\n\n // Makes the API call.\n MutateExperimentsResponse response = experimentService.MutateExperiments(\n customerId.ToString(), new[] { operation });\n\n // Displays the result.\n string experimentResourceName = response.Results.First().ResourceName;\n\n Console.WriteLine($\"Created experiment with resource name \" +\n $\"'{experimentResourceName}'.\");\n return experimentResourceName;\n}\nhttps://github.com/googleads/google-ads-dotnet/blob/ada966e1983b655e82172b6c3e7d9b091b522377/Google.Ads.GoogleAds/examples/CampaignManagement/CreateExperiment.cs#L114-L153\n \n```\n\n### PHP\n\n```php\nprivate static function createExperimentResource(\n ExperimentServiceClient $experimentServiceClient,\n int $customerId\n): string {\n // Creates an experiment and its operation.\n $experiment = new Experiment([\n // Name must be unique.\n 'name' =\u003e 'Example Experiment #' . Helper::getPrintableDatetime(),\n 'type' =\u003e ExperimentType::SEARCH_CUSTOM,\n 'suffix' =\u003e '[experiment]',\n 'status' =\u003e ExperimentStatus::SETUP\n ]);\n $experimentOperation = new ExperimentOperation(['create' =\u003e $experiment]);\n\n // Issues a request to create the experiment.\n $response = $experimentServiceClient-\u003emutateExperiments(\n MutateExperimentsRequest::build($customerId, [$experimentOperation])\n );\n $experimentResourceName = $response-\u003egetResults()[0]-\u003egetResourceName();\n print \"Created experiment with resource name '$experimentResourceName'\" . PHP_EOL;\n\n return $experimentResourceName;\n} \nhttps://github.com/googleads/google-ads-php/blob/be0249c30c27b4760387bec6682b82c9f4167761/examples/CampaignManagement/CreateExperiment.php#L147-L169\n\n \n```\n\n### Python\n\n```python\ndef create_experiment_resource(\n client: GoogleAdsClient, customer_id: str\n) -\u003e str:\n \"\"\"Creates a new experiment resource.\n\n Args:\n client: an initialized GoogleAdsClient instance.\n customer_id: a client customer ID.\n\n Returns:\n the resource name for the new experiment.\n \"\"\"\n experiment_operation: ExperimentOperation = client.get_type(\n \"ExperimentOperation\"\n )\n experiment: Experiment = experiment_operation.create\n\n experiment.name = f\"Example Experiment #{uuid.uuid4()}\"\n experiment.type_ = client.enums.ExperimentTypeEnum.SEARCH_CUSTOM\n experiment.suffix = \"[experiment]\"\n experiment.status = client.enums.ExperimentStatusEnum.SETUP\n\n experiment_service: ExperimentServiceClient = client.get_service(\n \"ExperimentService\"\n )\n response: MutateExperimentsResponse = experiment_service.mutate_experiments(\n customer_id=customer_id, operations=[experiment_operation]\n )\n\n experiment_resource_name: str = response.results[0].resource_name\n print(f\"Created experiment with resource name {experiment_resource_name}\")\n\n return experiment_resource_name \nhttps://github.com/googleads/google-ads-python/blob/d0595698b8a7de6cc00684b467462601037c9db9/examples/campaign_management/create_experiment.py#L86-L118\n \n```\n\n### Ruby\n\n```ruby\ndef create_experiment_resource(client, customer_id)\n operation = client.operation.create_resource.experiment do |e|\n # Name must be unique.\n e.name = \"Example Experiment #{(Time.new.to_f * 1000).to_i}\"\n e.type = :SEARCH_CUSTOM\n e.suffix = '[experiment]'\n e.status = :SETUP\n end\n\n response = client.service.experiment.mutate_experiments(\n customer_id: customer_id,\n operations: [operation],\n )\n\n experiment = response.results.first.resource_name\n puts \"Created experiment with resource name #{experiment}.\"\n\n experiment\nend \nhttps://github.com/googleads/google-ads-ruby/blob/2752563c7ffd15a4d2238116869f64aea3011cc3/examples/campaign_management/create_experiment.rb#L43-L61\n\n \n```\n\n### Perl\n\n```perl\nsub create_experiment_resource {\n my ($api_client, $customer_id) = @_;\n\n my $experiment = Google::Ads::GoogleAds::V21::Resources::Experiment-\u003enew({\n # Name must be unique.\n name =\u003e \"Example Experiment #\" . uniqid(),\n type =\u003e SEARCH_CUSTOM,\n suffix =\u003e \"[experiment]\",\n status =\u003e SETUP\n });\n\n my $operation =\n Google::Ads::GoogleAds::V21::Services::ExperimentService::ExperimentOperation\n -\u003enew({\n create =\u003e $experiment\n });\n\n my $response = $api_client-\u003eExperimentService()-\u003emutate({\n customerId =\u003e $customer_id,\n operations =\u003e [$operation]});\n\n my $resource_name = $response-\u003e{results}[0]{resourceName};\n printf \"Created experiment with resource name '%s'.\\n\", $resource_name;\n return $resource_name;\n}https://github.com/googleads/google-ads-perl/blob/9abffd69cd856633dfdcee5c636fe9cd0eb4b5ed/examples/campaign_management/create_experiment.pl#L70-L94\n \n```\n\n\u003cbr /\u003e"]]