Utwórz eksperyment

Java

// Copyright 2022 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
//
//     https://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.ads.googleads.examples.campaignmanagement;

import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime;

import com.beust.jcommander.Parameter;
import com.google.ads.googleads.examples.utils.ArgumentNames;
import com.google.ads.googleads.examples.utils.CodeSampleParams;
import com.google.ads.googleads.lib.GoogleAdsClient;
import com.google.ads.googleads.lib.utils.FieldMasks;
import com.google.ads.googleads.v11.enums.ExperimentStatusEnum.ExperimentStatus;
import com.google.ads.googleads.v11.enums.ExperimentTypeEnum.ExperimentType;
import com.google.ads.googleads.v11.errors.GoogleAdsError;
import com.google.ads.googleads.v11.errors.GoogleAdsException;
import com.google.ads.googleads.v11.resources.Campaign;
import com.google.ads.googleads.v11.resources.Experiment;
import com.google.ads.googleads.v11.resources.ExperimentArm;
import com.google.ads.googleads.v11.services.CampaignOperation;
import com.google.ads.googleads.v11.services.CampaignServiceClient;
import com.google.ads.googleads.v11.services.ExperimentArmOperation;
import com.google.ads.googleads.v11.services.ExperimentArmServiceClient;
import com.google.ads.googleads.v11.services.ExperimentOperation;
import com.google.ads.googleads.v11.services.ExperimentServiceClient;
import com.google.ads.googleads.v11.services.GoogleAdsServiceClient;
import com.google.ads.googleads.v11.services.GoogleAdsServiceClient.SearchPagedResponse;
import com.google.ads.googleads.v11.services.MutateExperimentArmsResponse;
import com.google.ads.googleads.v11.services.MutateExperimentsResponse;
import com.google.ads.googleads.v11.utils.ResourceNames;
import com.google.common.collect.ImmutableList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * This example creates a new experiment, experiment arms, and demonstrates how to modify the draft
 * campaign as well as begin the experiment.
 */
public class CreateExperiment {

  private static class CreateExperimentParams extends CodeSampleParams {

    @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
    private Long customerId;

    @Parameter(names = ArgumentNames.BASE_CAMPAIGN_ID, required = true)
    private Long baseCampaignId;
  }

  public static void main(String[] args) {
    CreateExperimentParams params = new CreateExperimentParams();
    if (!params.parseArguments(args)) {
      throw new IllegalArgumentException("Invalid or missing command line arguments");
    }

    GoogleAdsClient googleAdsClient = null;
    try {
      googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
    } catch (FileNotFoundException fnfe) {
      System.err.printf(
          "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
      System.exit(1);
    } catch (IOException ioe) {
      System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
      System.exit(1);
    }

    try {
      new CreateExperiment().runExample(googleAdsClient, params.customerId, params.baseCampaignId);
    } catch (GoogleAdsException gae) {
      // GoogleAdsException is the base class for most exceptions thrown by an API request.
      // Instances of this exception have a message and a GoogleAdsFailure that contains a
      // collection of GoogleAdsErrors that indicate the underlying causes of the
      // GoogleAdsException.
      System.err.printf(
          "Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
          gae.getRequestId());
      int i = 0;
      for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
        System.err.printf("  Error %d: %s%n", i++, googleAdsError);
      }
      System.exit(1);
    }
  }

  /**
   * Runs the example.
   *
   * @param googleAdsClient the googleAdsClient.
   * @param customerId the customer ID.
   * @param baseCampaignId the ID of the campaign on which to base the experiment.
   */
  private void runExample(GoogleAdsClient googleAdsClient, long customerId, long baseCampaignId) {
    String experiment = createExperimentResource(googleAdsClient, customerId);
    String treatmentArm = createExperimentArms(googleAdsClient, customerId, baseCampaignId, experiment);
    String draftCampaign = fetchDraftCampaign(googleAdsClient, customerId, treatmentArm);

    modifyDraftCampaign(googleAdsClient, customerId, draftCampaign);

    // When you're done setting up the experiment and arms and modifying the draft campaign, this
    // will begin the experiment.
    try (ExperimentServiceClient experimentServiceClient =
        googleAdsClient.getLatestVersion().createExperimentServiceClient()) {
      experimentServiceClient.scheduleExperimentAsync(experiment);
    }
  }

  /** Creates a campaign experiment. */
  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;
    }
  }

  /** Creates control and experiment arms for the experiment. */
  private String createExperimentArms(
      GoogleAdsClient googleAdsClient, long customerId, long campaignId, String experiment) {
    List<ExperimentArmOperation> operations = new ArrayList<>();
    operations.add(
        ExperimentArmOperation.newBuilder()
            .setCreate(
                // The "control" arm references an already-existing campaign.
                ExperimentArm.newBuilder()
                    .setControl(true)
                    .addCampaigns(ResourceNames.campaign(customerId, campaignId))
                    .setTrial(experiment)
                    .setName("control arm")
                    .setTrafficSplit(40)
                    .build())
            .build());
    operations.add(
        ExperimentArmOperation.newBuilder()
            .setCreate(
                // The non-"control" arm, also called a "treatment" arm, will automatically
                // generate draft campaigns that you can modify before starting the experiment.
                ExperimentArm.newBuilder()
                    .setControl(false)
                    .setTrial(experiment)
                    .setName("experiment arm")
                    .setTrafficSplit(60)
                    .build())
            .build());

    try (ExperimentArmServiceClient experimentArmServiceClient =
        googleAdsClient.getLatestVersion().createExperimentArmServiceClient()) {
      MutateExperimentArmsResponse response =
          experimentArmServiceClient.mutateExperimentArms(Long.toString(customerId), operations);

      // Results always return in the order that you specify them in the request. Since we created
      // the treatment arm last, it will be the last result.  If you don't remember which arm is the
      // treatment arm, you can always filter the query in the next section with
      // `experiment_arm.control = false`.
      String controlArm = response.getResults(0).getResourceName();
      String treatmentArm = response.getResults(response.getResultsCount() - 1).getResourceName();

      System.out.printf("Created control arm with resource name '%s'%n", controlArm);
      System.out.printf("Created treatment arm with resource name '%s'%n", treatmentArm);

      return treatmentArm;
    }
  }

  /** Retrieves the draft campaign. */
  private String fetchDraftCampaign(
      GoogleAdsClient googleAdsClient, long customerId, String treatmentArm) {
    // The `in_design_campaigns` represent campaign drafts, which you can modify before starting the
    // experiment.
    String query =
        String.format(
            "SELECT experiment_arm.in_design_campaigns "
                + "FROM experiment_arm "
                + "WHERE experiment_arm.resource_name = '%s'",
            treatmentArm);

    try (GoogleAdsServiceClient googleAdsServiceClient =
        googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
      SearchPagedResponse response =
          googleAdsServiceClient.search(Long.toString(customerId), query);

      // In design campaigns returns as an array, but for now it can only ever contain a single ID,
      // so we just grab the first one.
      String draftCampaign =
          response.iterateAll().iterator().next().getExperimentArm().getInDesignCampaigns(0);
      System.out.printf("Found draft campaign with resource name '%s'%n", draftCampaign);
      return draftCampaign;
    }
  }

  /** Modifies the draft campaign. */
  private void modifyDraftCampaign(
      GoogleAdsClient googleAdsClient, long customerId, String draftCampaign) {
    Campaign.Builder campaignBuilder = Campaign.newBuilder().setResourceName(draftCampaign);

    // You can change anything you like about the campaign. These are the changes you're testing by
    // doing this experiment. Here we just change the name for illustrative purposes, but generally
    // you may want to change more meaningful parts of the campaign.
    //
    // You can also change underlying resources, such as ad groups and keywords, just as you
    // would for any other campaign. When searching with the GoogleAdsService, be sure to
    // include a PARAMETERS clause with `include_drafts = true` when searching for these
    // draft entities.
    Campaign campaign =
        campaignBuilder.setName("Modified Campaign Name " + getPrintableDateTime()).build();

    CampaignOperation operation =
        CampaignOperation.newBuilder()
            .setUpdate(campaign)
            .setUpdateMask(FieldMasks.allSetFieldsOf(campaign))
            .build();

    try (CampaignServiceClient campaignServiceClient =
        googleAdsClient.getLatestVersion().createCampaignServiceClient()) {
      campaignServiceClient.mutateCampaigns(Long.toString(customerId), ImmutableList.of(operation));

      System.out.printf("Updated the name for campaign '%s'%n", draftCampaign);
    }
  }
}

      

C#

// Copyright 2022 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.

using CommandLine;
using Google.Ads.Gax.Examples;
using Google.Ads.Gax.Util;
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V11.Errors;
using Google.Ads.GoogleAds.V11.Resources;
using Google.Ads.GoogleAds.V11.Services;
using System;
using System.Linq;
using static Google.Ads.GoogleAds.V11.Enums.ExperimentStatusEnum.Types;
using static Google.Ads.GoogleAds.V11.Enums.ExperimentTypeEnum.Types;

namespace Google.Ads.GoogleAds.Examples.V11
{
    /// <summary>
    /// This code example creates a new experiment, experiment arms, and demonstrates
    /// how to modify the draft campaign as well as begin the experiment.
    /// </summary>
    public class CreateExperiment : ExampleBase
    {
        /// <summary>
        /// Command line options for running the <see cref="CreateExperiment"/> example.
        /// </summary>
        public class Options : OptionsBase
        {
            /// <summary>
            /// The customer ID for which the call is made.
            /// </summary>
            [Option("customerId", Required = true, HelpText =
                "The customer ID for which the call is made.")]
            public long CustomerId { get; set; }

            /// <summary>
            /// Id of the campaign for which the control arm is created.
            /// </summary>
            [Option("baseCampaignId", Required = true, HelpText =
                "Id of the campaign for which the control arm is created.")]
            public long BaseCampaignId { get; set; }
        }

        /// <summary>
        /// Main method, to run this code example as a standalone application.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        public static void Main(string[] args)
        {
            Options options = ExampleUtilities.ParseCommandLine<Options>(args);

            CreateExperiment codeExample = new CreateExperiment();
            Console.WriteLine(codeExample.Description);
            codeExample.Run(new GoogleAdsClient(), options.CustomerId, options.BaseCampaignId);
        }

        /// <summary>
        /// Returns a description about the code example.
        /// </summary>
        public override string Description =>
            "This code example creates a new experiment, experiment arms, and demonstrates how " +
            "to modify the draft campaign as well as begin the experiment.";

        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        /// <param name="baseCampaignId">Id of the campaign for which the control arm is created.</param>
        public void Run(GoogleAdsClient client, long customerId, long baseCampaignId)
        {
            // Get the ExperimentService.
            ExperimentServiceClient experimentService = client.GetService(
                Services.V11.ExperimentService);

            try
            {
                string experimentResourceName = CreateAnExperiment(client, customerId);
                string controlArmResourceName, treatmentArmResourceName;

                (controlArmResourceName, treatmentArmResourceName) = CreateExperimentArms(
                    client, customerId, baseCampaignId, experimentResourceName);

                string draftCampaignResourceName = FetchDraftCampaign(
                    client, customerId, treatmentArmResourceName);

                ModifyDraftCampaign(client, customerId, draftCampaignResourceName);

                // When you're done setting up the experiment and arms and modifying the draft
                // campaign, this will begin the experiment.
                experimentService.ScheduleExperiment(experimentResourceName);
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }

        /// <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.V11.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;
        }


        /// <summary>
        /// Creates the experiment arms.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        /// <param name="baseCampaignId">ID of the campaign for which the control arm is
        /// created.</param>
        /// <param name="experimentResourceName">Resource name of the experiment.</param>
        /// <returns>The resource names of the control and treatment arms.</returns>
        private static (string, string) CreateExperimentArms(GoogleAdsClient client,
            long customerId, long baseCampaignId, string experimentResourceName)
        {
            // Get the ExperimentArmService.
            ExperimentArmServiceClient experimentService = client.GetService(
                Services.V11.ExperimentArmService);

            // Create the control arm. The control arm references an already-existing campaign.
            ExperimentArmOperation controlArmOperation = new ExperimentArmOperation()
            {
                Create = new ExperimentArm()
                {
                    Control = true,
                    Campaigns = {
                        ResourceNames.Campaign(customerId, baseCampaignId)
                    },
                    Trial = experimentResourceName,
                    Name = "Control Arm",
                    TrafficSplit = 40
                }
            };

            // Create the non-control arm. The non-"control" arm, also called a "treatment" arm,
            // will automatically generate draft campaigns that you can modify before starting the
            // experiment.
            ExperimentArmOperation treatmentArmOperation = new ExperimentArmOperation()
            {
                Create = new ExperimentArm()
                {
                    Control = false,
                    Trial = experimentResourceName,
                    Name = "Experiment Arm",
                    TrafficSplit = 60
                }
            };

            // Makes the API call.
            MutateExperimentArmsResponse response = experimentService.MutateExperimentArms(
                customerId.ToString(), new[] { controlArmOperation, treatmentArmOperation });

            // Results always return in the order that you specify them in the request.
            // Since we created the treatment arm last, it will be the last result.  If
            // you don't remember which arm is the treatment arm, you can always filter
            // the query in the FetchDraftCampaign method with `experiment_arm.control = false`.
            string controlArmResourceName = response.Results.First().ResourceName;
            string treatmentArmResourceName = response.Results.Last().ResourceName;

            Console.WriteLine($"Created control arm with resource name " +
                $"'{controlArmResourceName}.");
            Console.WriteLine($"Created treatment arm with resource name" +
              $" '{treatmentArmResourceName}'.");
            return (controlArmResourceName, treatmentArmResourceName);
        }


        /// <summary>
        /// Fetches the draft campaign.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        /// <param name="treatmentArmResourceName">Name of the treatment arm resource.</param>
        /// <returns>Resource name of the draft campaign</returns>
        private static string FetchDraftCampaign(GoogleAdsClient client, long customerId,
            string treatmentArmResourceName)
        {
            // Get the GoogleAdsService.
            GoogleAdsServiceClient googleAdsService = client.GetService(
                Services.V11.GoogleAdsService);

            // Creates the query.
            // The `in_design_campaigns` represent campaign drafts, which you can modify
            // before starting the experiment.
            string query = $"SELECT experiment_arm.in_design_campaigns FROM experiment_arm " +
                $"WHERE experiment_arm.resource_name = '{treatmentArmResourceName}'";

            // Makes the API call.
            // In design campaigns returns as an array, but for now it can only ever
            // contain a single ID, so we just grab the first one.
            string draftCampaignResourceName = googleAdsService.Search(
                customerId.ToString(), query).First().ExperimentArm.InDesignCampaigns.First();

            Console.WriteLine($"Found draft campaign with resource name " +
                $"'{draftCampaignResourceName}'.");

            return draftCampaignResourceName;
        }


        /// <summary>
        /// Modifies the draft campaign.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        /// <param name="draftCampaignResourceName">Resource name of the draft campaign.</param>
        private static void ModifyDraftCampaign(GoogleAdsClient client, long customerId,
            string draftCampaignResourceName)
        {
            // Get the CampaignService.
            CampaignServiceClient campaignService = client.GetService(
                Services.V11.CampaignService);

            // You can change anything you like about the campaign. These are the changes you're
            // testing by doing this experiment. Here we just change the name for illustrative
            // purposes, but generally you may want to change more meaningful parts of the campaign.
            //
            // You can also change underlying resources, such as ad groups and keywords, just as
            // you would for any other campaign. When searching with the GoogleAdsService, be sure
            // to include a PARAMETERS clause with `include_drafts = true` when searching for
            // these draft entities.
            Campaign campaign = new Campaign()
            {
                ResourceName = draftCampaignResourceName,
                Name = $"Modified Campaign Name #{ExampleUtilities.GetRandomString()}"
            };

            // Creates an operation.
            CampaignOperation operation = new CampaignOperation()
            {
                Update = campaign,
                UpdateMask = FieldMasks.AllSetFieldsOf(campaign)
            };

            // Makes the API call.
            MutateCampaignsResponse response = campaignService.MutateCampaigns(
                customerId.ToString(), new[] { operation });

            // Displays the result.
            Console.WriteLine($"Updated the name for campaign {draftCampaignResourceName}.");
        }
    }
}
      

PHP

<?php

/**
 * Copyright 2022 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
 *
 *     https://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.
 */

namespace Google\Ads\GoogleAds\Examples\CampaignManagement;

require __DIR__ . '/../../vendor/autoload.php';

use GetOpt\GetOpt;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Examples\Utils\Helper;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V11\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V11\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V11\GoogleAdsException;
use Google\Ads\GoogleAds\Util\FieldMasks;
use Google\Ads\GoogleAds\Util\V11\ResourceNames;
use Google\Ads\GoogleAds\V11\Enums\CampaignExperimentTrafficSplitTypeEnum\CampaignExperimentTrafficSplitType;
use Google\Ads\GoogleAds\V11\Enums\ExperimentStatusEnum\ExperimentStatus;
use Google\Ads\GoogleAds\V11\Enums\ExperimentTypeEnum\ExperimentType;
use Google\Ads\GoogleAds\V11\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V11\Resources\Campaign;
use Google\Ads\GoogleAds\V11\Resources\CampaignExperiment;
use Google\Ads\GoogleAds\V11\Resources\Experiment;
use Google\Ads\GoogleAds\V11\Resources\ExperimentArm;
use Google\Ads\GoogleAds\V11\Services\CampaignOperation;
use Google\Ads\GoogleAds\V11\Services\ExperimentArmOperation;
use Google\Ads\GoogleAds\V11\Services\ExperimentOperation;
use Google\Ads\GoogleAds\V11\Services\ExperimentServiceClient;
use Google\Ads\GoogleAds\V11\Services\GoogleAdsRow;
use Google\ApiCore\ApiException;

/**
 * This example creates a new experiment, experiment arms, and demonstrates how to modify the draft
 * campaign as well as begin the experiment.
 */
class CreateExperiment
{
    private const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE';
    private const BASE_CAMPAIGN_ID = 'INSERT_BASE_CAMPAIGN_ID_HERE';

    public static function main()
    {
        // Either pass the required parameters for this example on the command line, or insert them
        // into the constants above.
        $options = (new ArgumentParser())->parseCommandArguments([
            ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT,
            ArgumentNames::BASE_CAMPAIGN_ID => GetOpt::REQUIRED_ARGUMENT
        ]);

        // Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

        // Construct a Google Ads client configured from a properties file and the
        // OAuth2 credentials above.
        $googleAdsClient = (new GoogleAdsClientBuilder())
            ->fromFile()
            ->withOAuth2Credential($oAuth2Credential)
            ->build();

        try {
            self::runExample(
                $googleAdsClient,
                $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID,
                $options[ArgumentNames::BASE_CAMPAIGN_ID] ?: self::BASE_CAMPAIGN_ID
            );
        } catch (GoogleAdsException $googleAdsException) {
            printf(
                "Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
                $googleAdsException->getRequestId(),
                PHP_EOL,
                PHP_EOL
            );
            foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
                /** @var GoogleAdsError $error */
                printf(
                    "\t%s: %s%s",
                    $error->getErrorCode()->getErrorCode(),
                    $error->getMessage(),
                    PHP_EOL
                );
            }
            exit(1);
        } catch (ApiException $apiException) {
            printf(
                "ApiException was thrown with message '%s'.%s",
                $apiException->getMessage(),
                PHP_EOL
            );
            exit(1);
        }
    }

    /**
     * Runs the example.
     *
     * @param GoogleAdsClient $googleAdsClient the Google Ads API client
     * @param int $customerId the client customer ID
     * @param int $campaignId the campaign ID
     */
    public static function runExample(
        GoogleAdsClient $googleAdsClient,
        int $customerId,
        int $campaignId
    ) {
        $experimentServiceClient = $googleAdsClient->getExperimentServiceClient();

        $experimentResourceName =
            self::createExperimentResource($experimentServiceClient, $customerId);
        $treatmentArmResourceName = self::createExperimentArms(
            $googleAdsClient,
            $customerId,
            $campaignId,
            $experimentResourceName
        );
        $draftCampaignResourceName =
            self::fetchDraftCampaign($googleAdsClient, $customerId, $treatmentArmResourceName);
        self::modifyDraftCampaign($googleAdsClient, $customerId, $draftCampaignResourceName);

        // When you're done setting up the experiment and arms and modifying the draft campaign,
        // this will begin the experiment.
        $experimentServiceClient->scheduleExperiment($experimentResourceName);
    }

    /**
     * Creates an experiment resource.
     *
     * @param ExperimentServiceClient $experimentServiceClient the experiment service client
     * @param int $customerId the customer ID
     * @return string the created experiment's resource name
     */
    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(
            $customerId,
            [$experimentOperation]
        );
        $experimentResourceName = $response->getResults()[0]->getResourceName();
        print "Created experiment with resource name '$experimentResourceName'" . PHP_EOL;

        return $experimentResourceName;
    }

    /**
     * Creates experiment arms and returns the treatment arm resource name, which will be used in
     * the next step.
     *
     * @param GoogleAdsClient $googleAdsClient the Google Ads API client
     * @param int $customerId the customer ID
     * @param int $campaignId the campaign ID
     * @param string $experimentResourceName the experiment's resource name
     * @return string the treatment arm's resource name
     */
    private static function createExperimentArms(
        GoogleAdsClient $googleAdsClient,
        int $customerId,
        int $campaignId,
        string $experimentResourceName
    ): string {
        $operations = [];
        $experimentArm1 = new ExperimentArm([
            // The "control" arm references an already-existing campaign.
            'control' => true,
            'campaigns' => [ResourceNames::forCampaign($customerId, $campaignId)],
            'trial' => $experimentResourceName,
            'name' => 'control arm',
            'traffic_split' => 40
        ]);
        $operations[] = new ExperimentArmOperation(['create' => $experimentArm1]);
        $experimentArm2 = new ExperimentArm([
            // The non-"control" arm, also called a "treatment" arm, will automatically
            // generate draft campaigns that you can modify before starting the
            // experiment.
            'control' => false,
            'trial' => $experimentResourceName,
            'name' => 'experiment arm',
            'traffic_split' => 60
        ]);
        $operations[] = new ExperimentArmOperation(['create' => $experimentArm2]);

        // Issues a request to create the experiment arms.
        $experimentArmServiceClient = $googleAdsClient->getExperimentArmServiceClient();
        $response = $experimentArmServiceClient->mutateExperimentArms($customerId, $operations);
        // Results always return in the order that you specify them in the request.
        // Since we created the treatment arm last, it will be the last result.  If
        // you don't remember which arm is the treatment arm, you can always filter
        // the query in the next section with `experiment_arm.control = false`.
        $controlArmResourceName = $response->getResults()[0]->getResourceName();
        $treatmentArmResourceName =
            $response->getResults()[count($operations) - 1]->getResourceName();
        print "Created control arm with resource name '$controlArmResourceName'" . PHP_EOL;
        print "Created treatment arm with resource name '$treatmentArmResourceName'" . PHP_EOL;

        return $treatmentArmResourceName;
    }

    /**
     * Fetches the draft campaign of the experiment treatment arm.
     *
     * @param GoogleAdsClient $googleAdsClient the Google Ads API client
     * @param int $customerId the customer ID
     * @param string $treatmentArmResourceName the treatment arm's resource name
     * @return string the draft campaign's resource name
     */
    private static function fetchDraftCampaign(
        GoogleAdsClient $googleAdsClient,
        int $customerId,
        string $treatmentArmResourceName
    ): string {
        // Fetches information about the in design campaigns and prints out its resource
        // name. The `in_design_campaigns` represent campaign drafts, which you can modify
        // before starting the experiment.
        $query = "SELECT experiment_arm.in_design_campaigns FROM experiment_arm"
            . " WHERE experiment_arm.resource_name = '$treatmentArmResourceName'";
        $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
        $response = $googleAdsServiceClient->search($customerId, $query);

        // In design campaigns returns as an array, but for now it can only ever contain a single
        // ID, so we just grab the first one.
        /** @var GoogleAdsRow $googleAdsRow */
        $googleAdsRow = $response->getIterator()->current();
        $draftCampaignResourceName = $googleAdsRow->getExperimentArm()->getInDesignCampaigns()[0];
        print "Found draft campaign with resource name '$draftCampaignResourceName'" . PHP_EOL;

        return $draftCampaignResourceName;
    }

    /**
     * Modifies the draft campaign to simulate the experiment where you're testing changing
     * attributes of the campaign.
     *
     * @param GoogleAdsClient $googleAdsClient the Google Ads API client
     * @param int $customerId the customer ID
     * @param string $draftCampaignResourceName the draft campaign's resource name
     */
    private static function modifyDraftCampaign(
        GoogleAdsClient $googleAdsClient,
        int $customerId,
        string $draftCampaignResourceName
    ): void {
        // You can change anything you like about the campaign. These are the changes you're testing
        // by doing this experiment. Here we just change the name for illustrative purposes, but
        // generally you may want to change more meaningful parts of the campaign.
        $updatedCampaign = new Campaign([
            'resource_name' => $draftCampaignResourceName,
            'name' => 'Modified Campaign Name ' . Helper::getShortPrintableDatetime()
        ]);
        $campaignOperation = new CampaignOperation();
        $campaignOperation->setUpdate($updatedCampaign);
        $campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($updatedCampaign));

        // Issues a request to update the campaign.
        $campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
        $campaignServiceClient->mutateCampaigns($customerId, [$campaignOperation]);

        print "Updated the name for the campaign '$draftCampaignResourceName'" . PHP_EOL;
    }
}

CreateExperiment::main();

      

Python

# Encoding: utf-8
#
# Copyright 2022 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
#
#     https://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.
"""This example creates a new experiment and experiment arms.

It also demonstrates how to modify the draft campaign as well as begin the
experiment.
"""

import argparse
import sys
import uuid
from venv import create

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
from google.api_core import protobuf_helpers


def main(client, customer_id, base_campaign_id):
    """The main method that creates all necessary entities for the example.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        base_campaign_id: the campaign ID to associate with the control arm of
            the experiment.
    """
    experiment = create_experiment_resource(client, customer_id)
    treatment_arm = create_experiment_arms(
        client, customer_id, base_campaign_id, experiment
    )
    draft_campaign = fetch_draft_campaign(client, customer_id, treatment_arm)

    modify_draft_campaign(client, customer_id, draft_campaign)

    # When you're done setting up the experiment and arms and modifying the
    # draft campaign, this will begin the experiment.
    experiment_service = client.get_service("ExperimentService")
    experiment_service.schedule_experiment(resource_name=experiment)


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


def create_experiment_arms(client, customer_id, base_campaign_id, experiment):
    """Creates a control and treatment experiment arms.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        base_campaign_id: the campaign ID to associate with the control arm of
            the experiment.
        experiment: the resource name for an experiment.

    Returns:
        the resource name for the new treatment experiment arm.
    """
    operations = []

    campaign_service = client.get_service("CampaignService")

    # The "control" arm references an already-existing campaign.
    operation_1 = client.get_type("ExperimentArmOperation")
    exa_1 = operation_1.create
    exa_1.control = True
    exa_1.campaigns.append(
        campaign_service.campaign_path(customer_id, base_campaign_id)
    )
    exa_1.trial = experiment
    exa_1.name = "control arm"
    exa_1.traffic_split = 40
    operations.append(operation_1)

    # The non-"control" arm, also called a "treatment" arm, will automatically
    # generate draft campaigns that you can modify before starting the
    # experiment.
    operation_2 = client.get_type("ExperimentArmOperation")
    exa_2 = operation_2.create
    exa_2.control = False
    exa_2.trial = experiment
    exa_2.name = "experiment arm"
    exa_2.traffic_split = 60
    operations.append(operation_2)

    experiment_arm_service = client.get_service("ExperimentArmService")
    response = experiment_arm_service.mutate_experiment_arms(
        customer_id=customer_id, operations=operations
    )

    # Results always return in the order that you specify them in the request.
    # Since we created the treatment arm last, it will be the last result.  If
    # you don't remember which arm is the treatment arm, you can always filter
    # the query in the next section with `experiment_arm.control = false`.
    control_arm = response.results[0].resource_name
    treatment_arm = response.results[1].resource_name

    print(f"Created control arm with resource name {control_arm}")
    print(f"Created treatment arm with resource name {treatment_arm}")

    return treatment_arm


def fetch_draft_campaign(client, customer_id, treatment_arm):
    """Retrieves the in-design campaigns for an experiment.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        treatment_arm: the resource name for the treatment arm of an experiment.

    Returns:
        the resource name for the in-design campaign.
    """
    # The `in_design_campaigns` represent campaign drafts, which you can modify
    # before starting the experiment.
    ga_service = client.get_service("GoogleAdsService")

    query = f"""
      SELECT
        experiment_arm.in_design_campaigns
      FROM experiment_arm
      WHERE experiment_arm.resource_name = '{treatment_arm}'"""

    request = client.get_type("SearchGoogleAdsRequest")
    request.customer_id = customer_id
    request.query = query

    response = ga_service.search(request=request)

    # In design campaigns returns as a list, but for now it can only ever
    # contain a single ID, so we just grab the first one.
    draft_campaign = response.results[0].experiment_arm.in_design_campaigns[0]

    print(f"Found draft campaign with resource name {draft_campaign}")

    return draft_campaign


def modify_draft_campaign(client, customer_id, draft_campaign):
    """Modifies the given in-design campaign.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
        draft_campaign: the resource name for an in-design campaign.
    """
    campaign_service = client.get_service("CampaignService")
    campaign_operation = client.get_type("CampaignOperation")
    campaign = campaign_operation.update
    campaign.resource_name = draft_campaign

    # You can change anything you like about the campaign. These are the changes
    # you're testing by doing this experiment. Here we just change the name for
    # illustrative purposes, but generally you may want to change more
    # meaningful parts of the campaign.
    campaign.name = f"Modified Campaign Name #{uuid.uuid4()}"

    client.copy_from(
        campaign_operation.update_mask,
        protobuf_helpers.field_mask(None, campaign._pb),
    )

    campaign_service.mutate_campaigns(
        customer_id=customer_id, operations=[campaign_operation]
    )

    print(f"Updated name for campaign {draft_campaign}")


if __name__ == "__main__":
    # GoogleAdsClient will read the google-ads.yaml configuration file in the
    # home directory if none is specified.
    googleads_client = GoogleAdsClient.load_from_storage(version="v11")

    parser = argparse.ArgumentParser(
        description=("Create a campaign experiment based on a campaign draft.")
    )
    # The following argument(s) need to be provided to run the example.
    parser.add_argument(
        "-c",
        "--customer_id",
        type=str,
        required=True,
        help="The Google Ads customer ID.",
    )
    parser.add_argument(
        "-i",
        "--base_campaign_id",
        type=str,
        required=True,
        help="The campaign id.",
    )
    args = parser.parse_args()

    try:
        main(googleads_client, args.customer_id, args.base_campaign_id)
    except GoogleAdsException as ex:
        print(
            f'Request with ID "{ex.request_id}" failed with status '
            f'"{ex.error.code().name}" and includes the following errors:'
        )
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)

      

Ruby

#!/usr/bin/env ruby
# Encoding: utf-8
#
# Copyright 2022 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
#
#     https://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.
#
# This example creates a new experiment, experiment arms, and demonstrates
# how to modify the draft campaign as well as begin the experiment.

require 'optparse'
require 'google/ads/google_ads'

def create_experiment(customer_id, campaign_id)
  # GoogleAdsClient will read a config file from
  # ENV['HOME']/google_ads_config.rb when called without parameters
  client = Google::Ads::GoogleAds::GoogleAdsClient.new

  experiment = create_experiment_resource(client, customer_id)
  treatment_arm = create_experiment_arms(client, customer_id, campaign_id, experiment)
  draft_campaign = fetch_draft_campaign(client, customer_id, treatment_arm)

  modify_draft_campaign(client, customer_id, draft_campaign)

  # When you're done setting up the experiment and arms and modifying the draft
  # campaign, this will begin the experiment.
  response = client.service.experiment.schedule_experiment(
    # This is from the very first step above.
    resource_name: experiment,
  )
end

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

def create_experiment_arms(client, customer_id, campaign_id, experiment)
  operations = []
  operations << client.operation.create_resource.experiment_arm do |ea|
    # The "control" arm references an already-existing campaign.
    ea.control = true
    ea.campaigns << client.path.campaign(customer_id, campaign_id)
    ea.trial = experiment
    ea.name = 'control arm'
    ea.traffic_split = 40
  end
  operations << client.operation.create_resource.experiment_arm do |ea|
    # The non-"control" arm, also called a "treatment" arm, will automatically
    # generate draft campaigns that you can modify before starting the
    # experiment.
    ea.control = false
    ea.trial = experiment
    ea.name = 'experiment arm'
    ea.traffic_split = 60
  end

  response = client.service.experiment_arm.mutate_experiment_arms(
    customer_id: customer_id,
    operations: operations,
  )

  # Results always return in the order that you specify them in the request.
  # Since we created the treatment arm last, it will be the last result.  If
  # you don't remember which arm is the treatment arm, you can always filter
  # the query in the next section with `experiment_arm.control = false`.
  control_arm = response.results.first.resource_name
  treatment_arm = response.results.last.resource_name

  puts "Created control arm with resource name #{control_arm}."
  puts "Created treatment arm with resource name #{treatment_arm}."

  treatment_arm
end

def fetch_draft_campaign(client, customer_id, treatment_arm)
  # The `in_design_campaigns` represent campaign drafts, which you can modify
  # before starting the experiment.
  query = <<~QUERY
    SELECT experiment_arm.in_design_campaigns
    FROM experiment_arm
    WHERE experiment_arm.resource_name = "#{treatment_arm}"
  QUERY

  response = client.service.google_ads.search(
    customer_id: customer_id,
    query: query
  )

  # In design campaigns returns as an array, but for now it can only ever
  # contain a single ID, so we just grab the first one.
  draft_campaign = response.first.experiment_arm.in_design_campaigns.first

  puts "Found draft campaign with resource name #{draft_campaign}."

  draft_campaign
end

def modify_draft_campaign(client, customer_id, draft_campaign)
  operation = client.operation.update_resource.campaign(draft_campaign) do |c|
    # In this block you can change anything you like about the campaign. These
    # are the changes you're testing by doing this experiment. Here we just
    # change the name for illustrative purposes, but generally you may want to
    # change more meaningful parts of the campaign.
    #
    # You can also change underlying resources, such as ad groups and keywords,
    # just as you would for any other campaign. When searching with the
    # GoogleAdsService, be sure to include a PARAMETERS clause with
    # `include_drafts = true` when searching for these draft entities.
    c.name = "Modified Campaign Name #{(Time.new.to_f * 1000).to_i}"
  end

  response = client.service.campaign.mutate_campaigns(
    customer_id: customer_id,
    operations: [operation],
  )

  puts "Updated the name for campaign #{draft_campaign}."
end

if __FILE__ == $0
  PAGE_SIZE = 1000

  options = {}

  # Running the example with -h will print the command line usage.
  OptionParser.new do |opts|
    opts.banner = sprintf('Usage: %s [options]', File.basename(__FILE__))

    opts.separator ''
    opts.separator 'Options:'

    opts.on('-C', '--customer-id CUSTOMER-ID', String, 'Customer ID') do |v|
      options[:customer_id] = v
    end

    opts.on('-c', '--base-campaign BASE-CAMPAIGN', String, 'Base Campaign ID') do |v|
      options[:campaign_id] = v
    end

    opts.separator ''
    opts.separator 'Help:'

    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end
  end.parse!

  begin
    create_experiment(
      options.fetch(:customer_id).tr("-", ""),
      options.fetch(:campaign_id),
    )
  rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e
    e.failure.errors.each do |error|
      STDERR.printf("Error with message: %s\n", error.message)
      if error.location
        error.location.field_path_elements.each do |field_path_element|
          STDERR.printf("\tOn field: %s\n", field_path_element.field_name)
        end
      end
      error.error_code.to_h.each do |k, v|
        next if v == :UNSPECIFIED
        STDERR.printf("\tType: %s\n\tCode: %s\n", k, v)
      end
    end
    raise
  end
end

      

Perl

#!/usr/bin/perl -w
#
# Copyright 2022, 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.
#
# This example creates a new experiment, experiment arms, and demonstrates
# how to modify the draft campaign as well as begin the experiment.

use strict;
use warnings;
use utf8;

use FindBin qw($Bin);
use lib "$Bin/../../lib";
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::Utils::GoogleAdsHelper;
use Google::Ads::GoogleAds::Utils::FieldMasks;
use Google::Ads::GoogleAds::V11::Enums::ExperimentStatusEnum qw(SETUP);
use Google::Ads::GoogleAds::V11::Enums::ExperimentTypeEnum qw(SEARCH_CUSTOM);
use Google::Ads::GoogleAds::V11::Resources::Campaign;
use Google::Ads::GoogleAds::V11::Resources::Experiment;
use Google::Ads::GoogleAds::V11::Resources::ExperimentArm;
use Google::Ads::GoogleAds::V11::Services::CampaignService::CampaignOperation;
use
  Google::Ads::GoogleAds::V11::Services::ExperimentService::ExperimentOperation;
use
  Google::Ads::GoogleAds::V11::Services::ExperimentArmService::ExperimentArmOperation;
use Google::Ads::GoogleAds::V11::Utils::ResourceNames;

use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Cwd qw(abs_path);
use Data::Uniqid qw(uniqid);
use POSIX qw(strftime);

my $customer_id;
my $campaign_id;

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

  my $experiment = create_experiment_resource($api_client, $customer_id);
  my $treatment_arm =
    create_experiment_arms($api_client, $customer_id, $campaign_id,
    $experiment);
  my $draft_campaign =
    fetch_draft_campaign($api_client, $customer_id, $treatment_arm);

  modify_draft_campaign($api_client, $customer_id, $draft_campaign);

  # When you're done setting up the experiment and arms and modifying the draft
  # campaign, this will begin the experiment.
  my $response = $api_client->ExperimentService()->schedule_experiment({
    # This is from the very first step above.
    resourceName => $experiment
  });

  return 1;
}

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

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

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

sub create_experiment_arms {
  my ($api_client, $customer_id, $campaign_id, $experiment) = @_;

  my $operations = [];
  push @$operations,
    Google::Ads::GoogleAds::V11::Services::ExperimentArmService::ExperimentArmOperation
    ->new({
      create => Google::Ads::GoogleAds::V11::Resources::ExperimentArm->new({
          # The "control" arm references an already-existing campaign.
          control   => "true",
          campaigns => [
            Google::Ads::GoogleAds::V11::Utils::ResourceNames::campaign(
              $customer_id, $campaign_id
            )
          ],
          trial        => $experiment,
          name         => "control arm",
          trafficSplit => 40
        })});

  push @$operations,
    Google::Ads::GoogleAds::V11::Services::ExperimentArmService::ExperimentArmOperation
    ->new({
      create => Google::Ads::GoogleAds::V11::Resources::ExperimentArm->new({
          # The non-"control" arm, also called a "treatment" arm, will automatically
          # generate draft campaigns that you can modify before starting the
          # experiment.
          control      => "false",
          trial        => $experiment,
          name         => "experiment arm",
          trafficSplit => 60
        })});

  my $response = $api_client->ExperimentArmService()->mutate({
    customerId => $customer_id,
    operations => $operations
  });

  # Results always return in the order that you specify them in the request.
  # Since we created the treatment arm last, it will be the last result.  If
  # you don't remember which arm is the treatment arm, you can always filter
  # the query in the next section with `experiment_arm.control = false`.
  my $control_arm   = $response->{results}[0]{resourceName};
  my $treatment_arm = $response->{results}[1]{resourceName};

  printf "Created control arm with resource name '%s'.\n",   $control_arm;
  printf "Created treatment arm with resource name '%s'.\n", $treatment_arm;
  return $treatment_arm;
}

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

  # The 'in_design_campaigns' represent campaign drafts, which you can modify
  # before starting the experiment.
  my $query =
    "SELECT experiment_arm.in_design_campaigns FROM experiment_arm " .
    "WHERE experiment_arm.resource_name = '$treatment_arm'";

  my $response = $api_client->GoogleAdsService()->search({
    customerId => $customer_id,
    query      => $query
  });

  # In design campaigns returns as an array, but for now it can only ever
  # contain a single ID, so we just grab the first one.
  my $draft_campaign =
    $response->{results}[0]->{experimentArm}{inDesignCampaigns}[0];

  printf "Found draft campaign with resource name '%s'.\n", $draft_campaign;
  return $draft_campaign;
}

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

  # In this block you can change anything you like about the campaign. These
  # are the changes you're testing by doing this experiment. Here we just
  # change the name for illustrative purposes, but generally you may want to
  # change more meaningful parts of the campaign.
  #
  # You can also change underlying resources, such as ad groups and keywords,
  # just as you would for any other campaign. When searching with the
  # GoogleAdsService, be sure to include a PARAMETERS clause with
  # `include_drafts = true` when searching for these draft entities.
  my $updated_campaign = Google::Ads::GoogleAds::V11::Resources::Campaign->new({
      resourceName => $draft_campaign,
      name => "Modified Campaign Name " . strftime("%Y%m%d", localtime(time))});

  my $operation =
    Google::Ads::GoogleAds::V11::Services::CampaignService::CampaignOperation->
    new({
      update     => $updated_campaign,
      updateMask => all_set_fields_of($updated_campaign)});

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

  printf "Updated the name for the campaign '%s'.\n", $draft_campaign;

  return 1;
}

# Don't run the example if the file is being included.
if (abs_path($0) ne abs_path(__FILE__)) {
  return 1;
}

# Get Google Ads Client, credentials will be read from ~/googleads.properties.
my $api_client = Google::Ads::GoogleAds::Client->new();

# By default examples are set to die on any server returned fault.
$api_client->set_die_on_faults(1);

# Parameters passed on the command line will override any parameters set in code.
GetOptions(
  "customer_id=s"      => \$customer_id,
  "base_campaign_id=i" => \$campaign_id
);

# Print the help message if the parameters are not initialized in the code nor
# in the command line.
pod2usage(2) if not check_params($customer_id, $campaign_id);

# Call the example.
create_experiment($api_client, $customer_id =~ s/-//gr, $campaign_id);

=pod

=head1 NAME

create_experiment

=head1 DESCRIPTION

This example creates a new experiment, experiment arms, and demonstrates how to modify the draft
campaign as well as begin the experiment.

=head1 SYNOPSIS

create_experiment.pl [options]

    -help                            Show the help message.
    -customer_id                     The Google Ads customer ID.
    -base_campaign_id                   The base campaign ID.

=cut