diff --git a/app/controller/GoogleAdsController.php b/app/controller/GoogleAdsController.php index 45432f8..3dc8e65 100644 --- a/app/controller/GoogleAdsController.php +++ b/app/controller/GoogleAdsController.php @@ -77,6 +77,7 @@ class GoogleAdsController return $this->addCampaignBudget($options); } + public function createGroup(Request $request) { $options = $request->all(); @@ -85,6 +86,14 @@ class GoogleAdsController return $this->addGroup($options); } + public function updateGroup(Request $request) + { + $options = $request->all(); + + // 继续处理 Google Ads API 操作 + return $this->modifyGroup($options); + } + public function createLinkManagerToClient(Request $request) { $options = $request->all(); @@ -183,22 +192,43 @@ class GoogleAdsController return $this->successResponse(['groups_created' => $resourceName]); } + /** + * 更新广告组 + * @throws ApiException + */ + public function modifyGroup($options): Response + { + $adGroupStatus = [ + 0, // UNSPECIFIED + 1, // UNKNOWN + 2, // ENABLED + 3, // PAUSED + 4 // REMOVED + ]; + $options['bid_micro_amount'] = $options['amount'] * 1000000 < 0 ? 0 : $options['amount'] * 1000000; + if (!in_array($options['status'], $adGroupStatus)) { + return $this->errorResponse(101, 'status参数错误'); + } + $resourceName = $this->googleAdsGroupService->runUpdateGroup($options); + return $this->successResponse(['group_updated' => $resourceName]); + } + // 可以加入一些公共方法 protected function successResponse($data): Response { return Json([ - 'code' => 200, - 'status' => 'success', + 'code' => 0, + 'msg' => 'ok', 'data' => $data, ]); } - protected function errorResponse($message): Response + protected function errorResponse($code, $message, $data = []): Response { return Json([ - 'code' => 400, - 'status' => 'error', - 'message' => $message, + 'code' => $code, + 'msg' => $message ?: 'error', + 'data' => $data ]); } diff --git a/app/service/GoogleAdsGroupService.php b/app/service/GoogleAdsGroupService.php index c2eb7d5..45fbc52 100644 --- a/app/service/GoogleAdsGroupService.php +++ b/app/service/GoogleAdsGroupService.php @@ -11,6 +11,7 @@ use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClient; use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\V18\GoogleAdsException; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; +use Google\Ads\GoogleAds\Util\FieldMasks; use Google\Ads\GoogleAds\Util\V18\ResourceNames; use Google\Ads\GoogleAds\V18\Enums\AdGroupStatusEnum\AdGroupStatus; use Google\Ads\GoogleAds\V18\Enums\AdGroupTypeEnum\AdGroupType; @@ -107,15 +108,88 @@ class GoogleAdsGroupService $customerId, $operations )); - $resourceNames = []; + $resourceNames = []; printf("Added %d ad groups:%s", $response->getResults()->count(), PHP_EOL); foreach ($response->getResults() as $addedAdGroup) { /** @var AdGroup $addedAdGroup */ print $addedAdGroup->getResourceName() . PHP_EOL; - $resourceNames[] = $addedAdGroup->getResourceName(); + $resourceNames[] = $addedAdGroup->getResourceName(); } return $resourceNames; } + + + + /** + * This example updates the CPC bid and status for a given ad group. To get ad groups, run + * GetAdGroups.php. + */ + /* @param int $customerId the customer ID + * @param $options + * @return mixed + * @throws ApiException + */ + public function runUpdateGroup($options): mixed + { + $googleAdsClient = $this->googleAdsClient; + // Creates a single shared budget to be used by the campaigns added below. + + $resourceNames = self::updateGroup($googleAdsClient, $options['customer_id'], $options['group_id'], $options['bid_micro_amount'], $options['status']); + + return $resourceNames; + } + + /** + * Runs the updateAdGroup example. + * + * @param GoogleAdsClient $googleAdsClient the Google Ads API client + * @param int $customerId the customer ID + * @param int $adGroupId the ID of ad group to update + * @param int $bidMicroAmount the bid amount in micros to use for the ad group bid + */ + // [START update_ad_group] + public static function updateGroup( + GoogleAdsClient $googleAdsClient, + int $customerId, + int $adGroupId, + $bidMicroAmount, + int $status + ) + { + + // Creates an ad group object with the specified resource name and other changes. + $adGroup = new AdGroup([ + 'resource_name' => ResourceNames::forAdGroup($customerId, $adGroupId), + 'cpc_bid_micros' => $bidMicroAmount, +// 'status' => AdGroupStatus::PAUSED + 'status' => $status + ]); + + // Constructs an operation that will update the ad group with the specified resource name, + // using the FieldMasks utility to derive the update mask. This mask tells the Google Ads + // API which attributes of the ad group you want to change. + $adGroupOperation = new AdGroupOperation(); + $adGroupOperation->setUpdate($adGroup); + $adGroupOperation->setUpdateMask(FieldMasks::allSetFieldsOf($adGroup)); + + // Issues a mutate request to update the ad group. + $adGroupServiceClient = $googleAdsClient->getAdGroupServiceClient(); + $response = $adGroupServiceClient->mutateAdGroups(MutateAdGroupsRequest::build( + $customerId, + [$adGroupOperation] + )); + + // Prints the resource name of the updated ad group. + /** @var AdGroup $updatedAdGroup */ + $updatedAdGroup = $response->getResults()[0]; + printf( + "Updated ad group with resource name: '%s'%s", + $updatedAdGroup->getResourceName(), + PHP_EOL + ); + return $updatedAdGroup->getResourceName(); + } + // [END update_ad_group] } diff --git a/config/route.php b/config/route.php index 883fb31..7cdf79e 100644 --- a/config/route.php +++ b/config/route.php @@ -45,6 +45,9 @@ Route::group('/googleads', function () { Route::group('/group', function () { Route::post('/create', [GoogleAdsController::class, 'createGroup']); }); + Route::group('/group', function () { + Route::post('/update', [GoogleAdsController::class, 'updateGroup']); + }); Route::group('/account_link', function () { Route::post('/create', [GoogleAdsController::class, 'createLinkManagerToClient']); });