广告系列操作
This commit is contained in:
		
							parent
							
								
									ab8e40d9cf
								
							
						
					
					
						commit
						c35d6af3b8
					
				| @ -38,6 +38,20 @@ class GoogleAdsController | |||||||
|         // 继续处理 Google Ads API 操作
 |         // 继续处理 Google Ads API 操作
 | ||||||
|         return $this->addCampaign($options); |         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) |     public function createCampaignBudget(Request $request) | ||||||
|     { |     { | ||||||
| @ -71,8 +85,8 @@ class GoogleAdsController | |||||||
|         // 例如使用 $options['customer_id'] 和 $options['feed_id']
 |         // 例如使用 $options['customer_id'] 和 $options['feed_id']
 | ||||||
|         // 调用相应的 Google Ads 服务
 |         // 调用相应的 Google Ads 服务
 | ||||||
| //        $budgetResourceName   = $this->googleAdsCampaignBudgetService->createCampaignBudget();
 | //        $budgetResourceName   = $this->googleAdsCampaignBudgetService->createCampaignBudget();
 | ||||||
|         $campaignResourceName = $this->googleAdsCampaignService->runAddCampaign(4060397299, $options); |         $resourceName = $this->googleAdsCampaignService->runAddCampaign(4060397299, $options); | ||||||
|         return $this->successResponse(['campaignResourceName' => $campaignResourceName]); |         return $this->successResponse(['campaign_resource_name' => $resourceName]); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -81,8 +95,8 @@ class GoogleAdsController | |||||||
|      */ |      */ | ||||||
|     public function addCampaignBudget($options): Response |     public function addCampaignBudget($options): Response | ||||||
|     { |     { | ||||||
|         $budgetResourceName = $this->googleAdsCampaignService->runAddCampaignBudget(0, $options); |         $resourceName = $this->googleAdsCampaignService->runAddCampaignBudget(0, $options); | ||||||
|         return $this->successResponse(['budgetResourceName' => $budgetResourceName]); |         return $this->successResponse(['budget_resource_name' => $resourceName]); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -91,8 +105,8 @@ class GoogleAdsController | |||||||
|      */ |      */ | ||||||
|     public function addLinkManagerToClient($options): Response |     public function addLinkManagerToClient($options): Response | ||||||
|     { |     { | ||||||
|         $linkResourceName = $this->googleAdsAccountService->runLinkManagerToClient($options); |         $resourceName = $this->googleAdsAccountService->runLinkManagerToClient($options); | ||||||
|         return $this->successResponse(['linkResourceName' => $linkResourceName]); |         return $this->successResponse(['link_resource_name' => $resourceName]); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -101,8 +115,26 @@ class GoogleAdsController | |||||||
|      */ |      */ | ||||||
|     public function listAccessibleCustomers(): Response |     public function listAccessibleCustomers(): Response | ||||||
|     { |     { | ||||||
|         $linkResourceName = $this->googleAdsAccountService->runListAccessibleCustomers(); |         $resourceName = $this->googleAdsAccountService->runListAccessibleCustomers(); | ||||||
|         return $this->successResponse(['linkResourceName' => $linkResourceName]); |         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]); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // 可以加入一些公共方法
 |     // 可以加入一些公共方法
 | ||||||
|  | |||||||
| @ -10,6 +10,8 @@ use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClient; | |||||||
| use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder; | use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder; | ||||||
| use Google\Ads\GoogleAds\Lib\V18\GoogleAdsException; | use Google\Ads\GoogleAds\Lib\V18\GoogleAdsException; | ||||||
| use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; | 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\Common\ManualCpc; | ||||||
| use Google\Ads\GoogleAds\V18\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType; | use Google\Ads\GoogleAds\V18\Enums\AdvertisingChannelTypeEnum\AdvertisingChannelType; | ||||||
| use Google\Ads\GoogleAds\V18\Enums\BudgetDeliveryMethodEnum\BudgetDeliveryMethod; | 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\Resources\CampaignBudget; | ||||||
| use Google\Ads\GoogleAds\V18\Services\CampaignBudgetOperation; | use Google\Ads\GoogleAds\V18\Services\CampaignBudgetOperation; | ||||||
| use Google\Ads\GoogleAds\V18\Services\CampaignOperation; | 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\MutateCampaignsRequest; | ||||||
| use Google\Ads\GoogleAds\V18\Services\MutateCampaignBudgetsRequest; | use Google\Ads\GoogleAds\V18\Services\MutateCampaignBudgetsRequest; | ||||||
|  | use Google\Ads\GoogleAds\V18\Services\SearchGoogleAdsStreamRequest; | ||||||
| use Google\ApiCore\ApiException; | use Google\ApiCore\ApiException; | ||||||
| 
 | 
 | ||||||
| class GoogleAdsCampaignService | class GoogleAdsCampaignService | ||||||
| @ -32,7 +36,7 @@ class GoogleAdsCampaignService | |||||||
| 
 | 
 | ||||||
|     public function __construct() |     public function __construct() | ||||||
|     { |     { | ||||||
|         $this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID'); | //        $this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
 | ||||||
| 
 | 
 | ||||||
|         // OAuth2 Token Authentication
 |         // OAuth2 Token Authentication
 | ||||||
|         $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); |         $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); | ||||||
| @ -123,9 +127,9 @@ class GoogleAdsCampaignService | |||||||
|      */ |      */ | ||||||
|     public function runAddCampaignBudget(int $customerId, $options): mixed |     public function runAddCampaignBudget(int $customerId, $options): mixed | ||||||
|     { |     { | ||||||
|         if(!$customerId){ | //        if(!$customerId){
 | ||||||
|             $customerId = $this->customerId; | //            $customerId = $this->customerId;
 | ||||||
|         } | //        }
 | ||||||
|         $googleAdsClient = $this->googleAdsClient; |         $googleAdsClient = $this->googleAdsClient; | ||||||
|         // Creates a single shared budget to be used by the campaigns added below.
 |         // 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); | ||||||
| @ -169,6 +173,107 @@ class GoogleAdsCampaignService | |||||||
| 
 | 
 | ||||||
|         return $addedBudget->getResourceName(); |         return $addedBudget->getResourceName(); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     // [END add_campaigns]
 |     // [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(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -31,6 +31,12 @@ Route::group('/googleads', function () { | |||||||
|     Route::group('/campaign', function () { |     Route::group('/campaign', function () { | ||||||
|         Route::post('/create', [GoogleAdsController::class, 'createCampaign']); |         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::group('/campaign_budget', function () { | ||||||
|         Route::post('/create', [GoogleAdsController::class, 'createCampaignBudget']); |         Route::post('/create', [GoogleAdsController::class, 'createCampaignBudget']); | ||||||
|     }); |     }); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user