diff --git a/examples/CampaignManagement/CreateSearchAdoptAiMaxExperiment.php b/examples/CampaignManagement/CreateSearchAdoptAiMaxExperiment.php new file mode 100644 index 000000000..2dc284a8a --- /dev/null +++ b/examples/CampaignManagement/CreateSearchAdoptAiMaxExperiment.php @@ -0,0 +1,213 @@ +parseCommandArguments([ + ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT, + ArgumentNames::CAMPAIGN_ID => GetOpt::REQUIRED_ARGUMENT + ]); + + $customerId = $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID; + $campaignId = $options[ArgumentNames::CAMPAIGN_ID] ?: self::CAMPAIGN_ID; + + // Build the Google Ads client from the auth config file. + $googleAdsClient = (new GoogleAdsClientBuilder()) + ->fromFile() + ->withOAuth2Credential((new OAuth2TokenBuilder())->fromFile()->build()) + ->build(); + + try { + self::runExample($googleAdsClient, (int)$customerId, (int)$campaignId); + } catch (GoogleAdsException $googleAdsException) { + printf( + "Request with ID '%s' failed with status '%s' and includes the following errors:%s", + $googleAdsException->getRequestId(), + $googleAdsException->getStatus(), + PHP_EOL + ); + foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) { + printf("\tError with message \"%s\".%s", $error->getMessage(), PHP_EOL); + if ($error->getLocation()) { + foreach ($error->getLocation()->getFieldPathElements() as $fieldPathElement) { + printf("\t\tOn field: %s%s", $fieldPathElement->getFieldName(), PHP_EOL); + } + } + } + exit(1); + } catch (ApiException $apiException) { + printf("ApiException was thrown with message '%s'.%s", $apiException->getMessage(), PHP_EOL); + exit(1); + } + } + + /** + * Runs the example logic. + * + * @param GoogleAdsClient $googleAdsClient the Google Ads API client + * @param int $customerId the customer ID + * @param int $campaignId the campaign ID to run the experiment on + */ + public static function runExample( + GoogleAdsClient $googleAdsClient, + int $customerId, + int $campaignId + ) { + $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); + + // [START create_search_adopt_ai_max_experiment_1] + // Create the experiment resource name using a temporary ID. + $experimentResourceName = ResourceNames::forExperiment($customerId, -1); + + // 1. Create the experiment operation. + $experiment = new Experiment([ + 'resource_name' => $experimentResourceName, + 'name' => 'ADOPT_AI_MAX Experiment #' . uniqid(), + 'type' => ExperimentType::ADOPT_AI_MAX + ]); + $experimentOperation = new MutateOperation([ + 'experiment_operation' => new ExperimentOperation(['create' => $experiment]) + ]); + + // Get the shared campaign resource path. + $campaignResourceName = ResourceNames::forCampaign($customerId, $campaignId); + + // 2. Create the control arm operation. Both arms in an intra-campaign experiment + // reference the same base campaign. + $controlArm = new ExperimentArm([ + 'experiment' => $experimentResourceName, + 'name' => 'Control Arm', + 'control' => true, + 'traffic_split' => 50, + 'campaigns' => [$campaignResourceName] + ]); + $controlArmOperation = new MutateOperation([ + 'experiment_arm_operation' => new ExperimentArmOperation(['create' => $controlArm]) + ]); + + // 3. Create the treatment arm operation. + $treatmentArm = new ExperimentArm([ + 'experiment' => $experimentResourceName, + 'name' => 'Treatment Arm', + 'control' => false, + 'traffic_split' => 50, + 'campaigns' => [$campaignResourceName] + ]); + $treatmentArmOperation = new MutateOperation([ + 'experiment_arm_operation' => new ExperimentArmOperation(['create' => $treatmentArm]) + ]); + + // 4. Create a campaign operation with an update mask to enable AI Max and + // configure asset automation settings. + $assetAutomationSettings = []; + $automationTypes = [ + AssetAutomationType::TEXT_ASSET_AUTOMATION, + AssetAutomationType::FINAL_URL_EXPANSION_TEXT_ASSET_AUTOMATION + ]; + + foreach ($automationTypes as $automationType) { + $assetAutomationSettings[] = new AssetAutomationSetting([ + 'asset_automation_type' => $automationType, + 'asset_automation_status' => AssetAutomationStatus::OPTED_IN + ]); + } + + $campaign = new Campaign([ + 'resource_name' => $campaignResourceName, + 'ai_max_setting' => new AiMaxSetting(['enable_ai_max' => true]), + 'asset_automation_settings' => $assetAutomationSettings + ]); + + $campaignOperation = new CampaignOperation([ + 'update' => $campaign, + 'update_mask' => FieldMasks::allSetFieldsOf($campaign) + ]); + $campaignMutateOperation = new MutateOperation([ + 'campaign_operation' => $campaignOperation + ]); + + // Send all mutate operations in a single Mutate request. + $mutateOperations = [ + $experimentOperation, + $controlArmOperation, + $treatmentArmOperation, + $campaignMutateOperation + ]; + + $response = $googleAdsServiceClient->mutate( + MutateGoogleAdsRequest::build($customerId, $mutateOperations) + ); + // [END create_search_adopt_ai_max_experiment_1] + + // Print the results. + // The results will be returned in the same order as the mutate operations. + $responses = $response->getMutateOperationResponses(); + + $experimentResult = $responses[0]->getExperimentResult(); + $controlArmResult = $responses[1]->getExperimentArmResult(); + $treatmentArmResult = $responses[2]->getExperimentArmResult(); + $campaignResult = $responses[3]->getCampaignResult(); + + printf("Created experiment: %s%s", $experimentResult->getResourceName(), PHP_EOL); + printf("Created control arm: %s%s", $controlArmResult->getResourceName(), PHP_EOL); + printf("Created treatment arm: %s%s", $treatmentArmResult->getResourceName(), PHP_EOL); + printf("Updated campaign to enable AI Max: %s%s", $campaignResult->getResourceName(), PHP_EOL); + } +} + +CreateSearchAdoptAiMaxExperiment::main(); \ No newline at end of file