广告系列操作

This commit is contained in:
hgc 2024-12-16 22:00:14 +08:00
parent ab8e40d9cf
commit c35d6af3b8
3 changed files with 163 additions and 20 deletions

View File

@ -38,6 +38,20 @@ class GoogleAdsController
// 继续处理 Google Ads API 操作
return $this->addCampaign($options);
}
public function listCampaigns(Request $request)
{
$options = $request->all();
// 继续处理 Google Ads API 操作
return $this->getCampaigns($options);
}
public function deleteCampaign(Request $request)
{
$options = $request->all();
// 继续处理 Google Ads API 操作
return $this->removeCampaign($options);
}
public function createCampaignBudget(Request $request)
{
@ -71,8 +85,8 @@ class GoogleAdsController
// 例如使用 $options['customer_id'] 和 $options['feed_id']
// 调用相应的 Google Ads 服务
// $budgetResourceName = $this->googleAdsCampaignBudgetService->createCampaignBudget();
$campaignResourceName = $this->googleAdsCampaignService->runAddCampaign(4060397299, $options);
return $this->successResponse(['campaignResourceName' => $campaignResourceName]);
$resourceName = $this->googleAdsCampaignService->runAddCampaign(4060397299, $options);
return $this->successResponse(['campaign_resource_name' => $resourceName]);
}
/**
@ -81,8 +95,8 @@ class GoogleAdsController
*/
public function addCampaignBudget($options): Response
{
$budgetResourceName = $this->googleAdsCampaignService->runAddCampaignBudget(0, $options);
return $this->successResponse(['budgetResourceName' => $budgetResourceName]);
$resourceName = $this->googleAdsCampaignService->runAddCampaignBudget(0, $options);
return $this->successResponse(['budget_resource_name' => $resourceName]);
}
/**
@ -91,8 +105,8 @@ class GoogleAdsController
*/
public function addLinkManagerToClient($options): Response
{
$linkResourceName = $this->googleAdsAccountService->runLinkManagerToClient($options);
return $this->successResponse(['linkResourceName' => $linkResourceName]);
$resourceName = $this->googleAdsAccountService->runLinkManagerToClient($options);
return $this->successResponse(['link_resource_name' => $resourceName]);
}
/**
@ -101,8 +115,26 @@ class GoogleAdsController
*/
public function listAccessibleCustomers(): Response
{
$linkResourceName = $this->googleAdsAccountService->runListAccessibleCustomers();
return $this->successResponse(['linkResourceName' => $linkResourceName]);
$resourceName = $this->googleAdsAccountService->runListAccessibleCustomers();
return $this->successResponse(['links_resource_name' => $resourceName]);
}
/**
* get campaigns
* @throws ApiException
*/
public function getCampaigns($options): Response
{
$resourceName = $this->googleAdsCampaignService->runListCampaigns($options['customer_id']);
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]);
}
// 可以加入一些公共方法

View File

@ -10,6 +10,8 @@ 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\Lib\V18\GoogleAdsServerStreamDecorator;
use Google\Ads\GoogleAds\Util\V18\ResourceNames;
use Google\Ads\GoogleAds\V18\Common\ManualCpc;
use Google\Ads\GoogleAds\V18\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType;
use Google\Ads\GoogleAds\V18\Enums\BudgetDeliveryMethodEnum\BudgetDeliveryMethod;
@ -20,8 +22,10 @@ use Google\Ads\GoogleAds\V18\Resources\Campaign\NetworkSettings;
use Google\Ads\GoogleAds\V18\Resources\CampaignBudget;
use Google\Ads\GoogleAds\V18\Services\CampaignBudgetOperation;
use Google\Ads\GoogleAds\V18\Services\CampaignOperation;
use Google\Ads\GoogleAds\V18\Services\GoogleAdsRow;
use Google\Ads\GoogleAds\V18\Services\MutateCampaignsRequest;
use Google\Ads\GoogleAds\V18\Services\MutateCampaignBudgetsRequest;
use Google\Ads\GoogleAds\V18\Services\SearchGoogleAdsStreamRequest;
use Google\ApiCore\ApiException;
class GoogleAdsCampaignService
@ -32,7 +36,7 @@ class GoogleAdsCampaignService
public function __construct()
{
$this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
// $this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
// OAuth2 Token Authentication
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
@ -57,7 +61,7 @@ class GoogleAdsCampaignService
{
$googleAdsClient = $this->googleAdsClient;
// Creates a single shared budget to be used by the campaigns added below.
$budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId,$options);
$budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId, $options);
// Configures the campaign network options.
$networkSettings = new NetworkSettings([
@ -75,7 +79,7 @@ class GoogleAdsCampaignService
// [START add_campaigns_1]
$campaign = new Campaign([
// 'name' => 'Interplanetary Cruise #' . Helper::getPrintableDatetime(),
'name' => $options['campaign_name'].rand(10,99).' #'. Helper::getPrintableDatetime(),
'name' => $options['campaign_name'] . rand(10, 99) . ' #' . Helper::getPrintableDatetime(),
'advertising_channel_type' => AdvertisingChannelType::SEARCH,
// Recommendation: Set the campaign to PAUSED when creating it to prevent
// the ads from immediately serving. Set to ENABLED once you've added
@ -100,7 +104,7 @@ class GoogleAdsCampaignService
// Issues a mutate request to add campaigns.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
$response = $campaignServiceClient->mutateCampaigns(
MutateCampaignsRequest::build($customerId, $campaignOperations)
);
// printf("Added %d campaigns:%s", $response->getResults()->count(), PHP_EOL);
@ -110,7 +114,7 @@ class GoogleAdsCampaignService
// print "{$addedCampaign->getResourceName()}" . PHP_EOL;
$resourceNames[] = $addedCampaign->getResourceName();
}
return $resourceNames;
return $resourceNames;
}
/**
@ -123,12 +127,12 @@ class GoogleAdsCampaignService
*/
public function runAddCampaignBudget(int $customerId, $options): mixed
{
if(!$customerId){
$customerId = $this->customerId;
}
// if(!$customerId){
// $customerId = $this->customerId;
// }
$googleAdsClient = $this->googleAdsClient;
// Creates a single shared budget to be used by the campaigns added below.
$budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId,$options);
$budgetResourceName = self::addCampaignBudget($googleAdsClient, $customerId, $options);
return $budgetResourceName;
}
@ -142,13 +146,13 @@ class GoogleAdsCampaignService
* @throws ApiException
*/
// [START add_campaigns]
private static function addCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId,$options)
private static function addCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId, $options)
{
// Creates a campaign budget.
$budget = new CampaignBudget([
// 'name' => 'Interplanetary Cruise Budget #' . Helper::getPrintableDatetime(),
'name' => $options['name'].rand(10,99).' #' . Helper::getPrintableDatetime(),
'name' => $options['name'] . rand(10, 99) . ' #' . Helper::getPrintableDatetime(),
// 'delivery_method' => BudgetDeliveryMethod::STANDARD,
'amount_micros' => $options['amount'] * 1000000
]);
@ -159,7 +163,7 @@ class GoogleAdsCampaignService
// Issues a mutate request.
$campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
$response = $campaignBudgetServiceClient->mutateCampaignBudgets(
$response = $campaignBudgetServiceClient->mutateCampaignBudgets(
MutateCampaignBudgetsRequest::build($customerId, [$campaignBudgetOperation])
);
@ -169,6 +173,107 @@ class GoogleAdsCampaignService
return $addedBudget->getResourceName();
}
// [END add_campaigns]
/* @param int $customerId the customer ID
* @param $options
* @return mixed
* @throws ApiException
*/
public function runListCampaigns(int $customerId): mixed
{
$googleAdsClient = $this->googleAdsClient;
// Creates a single shared budget to be used by the campaigns added below.
$campaignsResourceName = self::getCampaigns($googleAdsClient, $customerId);
return $campaignsResourceName;
}
/**
* Runs the example.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
*/
// [START get_campaigns]
public static function getCampaigns(GoogleAdsClient $googleAdsClient, int $customerId)
{
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
// Creates a query that retrieves all campaigns.
$query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
// Issues a search stream request.
/** @var GoogleAdsServerStreamDecorator $stream */
$stream = $googleAdsServiceClient->searchStream(
SearchGoogleAdsStreamRequest::build($customerId, $query)
);
$resourceNames = [];
// Iterates over all rows in all messages and prints the requested field values for
// the campaign in each row.
foreach ($stream->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId(),
$googleAdsRow->getCampaign()->getName(),
PHP_EOL
);
$resourceNames[$googleAdsRow->getCampaign()->getId()] = $googleAdsRow->getCampaign()->getName();
}
return $resourceNames;
}
// [END get_campaigns]
/* @param int $customerId the customer ID
* @param $options
* @return mixed
* @throws ApiException
*/
public function runRemoveCampaign($options): mixed
{
$googleAdsClient = $this->googleAdsClient;
// Creates a single shared budget to be used by the campaigns added below.
$resourceName = self::removeCampaign($googleAdsClient, $options['customer_id'], $options['campaign_id']);
return $resourceName;
}
/**
* Runs the example.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
* @param int $campaignId the ID of the campaign to remove
*/
public static function removeCampaign(
GoogleAdsClient $googleAdsClient,
int $customerId,
int $campaignId
)
{
// Creates the resource name of a campaign to remove.
$campaignResourceName = ResourceNames::forCampaign($customerId, $campaignId);
// Creates a campaign operation.
$campaignOperation = new CampaignOperation();
$campaignOperation->setRemove($campaignResourceName);
// Issues a mutate request to remove the campaign.
$campaignServiceClient = $googleAdsClient->getCampaignServiceClient();
$response = $campaignServiceClient->mutateCampaigns(
MutateCampaignsRequest::build($customerId, [$campaignOperation])
);
/** @var Campaign $removedCampaign */
$removedCampaign = $response->getResults()[0];
printf(
"Removed campaign with resource name '%s'%s",
$removedCampaign->getResourceName(),
PHP_EOL
);
return $removedCampaign->getResourceName();
}
}

View File

@ -31,6 +31,12 @@ Route::group('/googleads', function () {
Route::group('/campaign', function () {
Route::post('/create', [GoogleAdsController::class, 'createCampaign']);
});
Route::group('/campaign', function () {
Route::post('/list', [GoogleAdsController::class, 'listCampaigns']);
});
Route::group('/campaign', function () {
Route::post('/delete', [GoogleAdsController::class, 'deleteCampaign']);
});
Route::group('/campaign_budget', function () {
Route::post('/create', [GoogleAdsController::class, 'createCampaignBudget']);
});