From d20d709b8a54f7b4c201403119fc410ab0306f27 Mon Sep 17 00:00:00 2001 From: hgc Date: Tue, 17 Dec 2024 10:41:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B9=BF=E5=91=8A=E7=B3=BB=E5=88=97=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/GoogleAdsController.php | 20 ++++++- app/service/GoogleAdsCampaignService.php | 67 ++++++++++++++++++++++++ config/route.php | 3 ++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/app/controller/GoogleAdsController.php b/app/controller/GoogleAdsController.php index 96a12ad..996d471 100644 --- a/app/controller/GoogleAdsController.php +++ b/app/controller/GoogleAdsController.php @@ -52,6 +52,13 @@ class GoogleAdsController // 继续处理 Google Ads API 操作 return $this->removeCampaign($options); } + public function updateCampaign(Request $request) + { + $options = $request->all(); + + // 继续处理 Google Ads API 操作 + return $this->modifyCampaign($options); + } public function createCampaignBudget(Request $request) { @@ -128,13 +135,22 @@ class GoogleAdsController return $this->successResponse(['campaigns_list' => $resourceName]); } /** - * 关联广告客户ID + * 删除广告系列 * @throws ApiException */ public function removeCampaign($options): Response { $resourceName = $this->googleAdsCampaignService->runRemoveCampaign($options); - return $this->successResponse(['campaigns_deleted' => $resourceName]); + return $this->successResponse(['campaign_deleted' => $resourceName]); + } + /** + * 更新广告系列 + * @throws ApiException + */ + public function modifyCampaign($options): Response + { + $resourceName = $this->googleAdsCampaignService->runUpdateCampaign($options); + return $this->successResponse(['campaign_updated' => $resourceName]); } // 可以加入一些公共方法 diff --git a/app/service/GoogleAdsCampaignService.php b/app/service/GoogleAdsCampaignService.php index c000028..746c2a2 100644 --- a/app/service/GoogleAdsCampaignService.php +++ b/app/service/GoogleAdsCampaignService.php @@ -11,6 +11,7 @@ use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\V18\GoogleAdsException; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\Lib\V18\GoogleAdsServerStreamDecorator; +use Google\Ads\GoogleAds\Util\FieldMasks; use Google\Ads\GoogleAds\Util\V18\ResourceNames; use Google\Ads\GoogleAds\V18\Common\ManualCpc; use Google\Ads\GoogleAds\V18\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType; @@ -276,4 +277,70 @@ class GoogleAdsCampaignService ); return $removedCampaign->getResourceName(); } + + /** + * This example updates a campaign by setting the status to `PAUSED`. To get campaigns, run + * GetCampaigns.php. + */ + /* @param int $customerId the customer ID + * @param $options + * @return mixed + * @throws ApiException + */ + public function runUpdateCampaign($options): mixed + { + $googleAdsClient = $this->googleAdsClient; + // Creates a single shared budget to be used by the campaigns added below. + $resourceName = self::updateCampaign($googleAdsClient, $options['customer_id'], $options['campaign_id'],$options['status']); + + return $resourceName; + } + + /** + * Runs the updateCampaign example. + * + * @param GoogleAdsClient $googleAdsClient the Google Ads API client + * @param int $customerId the customer ID + * @param int $campaignId the ID of campaign to update + */ + public static function updateCampaign( + GoogleAdsClient $googleAdsClient, + int $customerId, + int $campaignId, + int $status + ) + { + + // Creates a campaign object with the specified resource name and other changes. + $campaign = new Campaign([ +// 'resource_name' => ResourceNames::forCampaign($customerId, $campaignId), +// 'status' => CampaignStatus::PAUSED + 'resource_name' => ResourceNames::forCampaign($customerId, $campaignId), + 'status' => $status, + ]); + + // Constructs an operation that will update the campaign 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 campaign you want to change. + $campaignOperation = new CampaignOperation(); + $campaignOperation->setUpdate($campaign); + $campaignOperation->setUpdateMask(FieldMasks::allSetFieldsOf($campaign)); + + // Issues a mutate request to update the campaign. + $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); + $response = $campaignServiceClient->mutateCampaigns(MutateCampaignsRequest::build( + $customerId, + [$campaignOperation] + )); + + // Prints the resource name of the updated campaign. + /** @var Campaign $updatedCampaign */ + $updatedCampaign = $response->getResults()[0]; + printf( + "Updated campaign with resource name: '%s'%s", + $updatedCampaign->getResourceName(), + PHP_EOL + ); + return $updatedCampaign->getResourceName(); + } } diff --git a/config/route.php b/config/route.php index 7328f01..6eb65c5 100644 --- a/config/route.php +++ b/config/route.php @@ -34,6 +34,9 @@ Route::group('/googleads', function () { Route::group('/campaign', function () { Route::post('/list', [GoogleAdsController::class, 'listCampaigns']); }); + Route::group('/campaign', function () { + Route::post('/update', [GoogleAdsController::class, 'updateCampaign']); + }); Route::group('/campaign', function () { Route::post('/delete', [GoogleAdsController::class, 'deleteCampaign']); });