广告系列 更新操作

This commit is contained in:
hgc 2024-12-17 10:41:06 +08:00
parent c35d6af3b8
commit d20d709b8a
3 changed files with 88 additions and 2 deletions

View File

@ -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]);
}
// 可以加入一些公共方法

View File

@ -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();
}
}

View File

@ -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']);
});