广告系列操作
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(); | ||||||
| @ -57,7 +61,7 @@ class GoogleAdsCampaignService | |||||||
|     { |     { | ||||||
|         $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); | ||||||
| 
 | 
 | ||||||
|         // Configures the campaign network options.
 |         // Configures the campaign network options.
 | ||||||
|         $networkSettings = new NetworkSettings([ |         $networkSettings = new NetworkSettings([ | ||||||
| @ -75,7 +79,7 @@ class GoogleAdsCampaignService | |||||||
|             // [START add_campaigns_1]
 |             // [START add_campaigns_1]
 | ||||||
|             $campaign = new Campaign([ |             $campaign = new Campaign([ | ||||||
| //                'name' => 'Interplanetary Cruise #' . Helper::getPrintableDatetime(),
 | //                '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, |                 'advertising_channel_type' => AdvertisingChannelType::SEARCH, | ||||||
|                 // Recommendation: Set the campaign to PAUSED when creating it to prevent
 |                 // Recommendation: Set the campaign to PAUSED when creating it to prevent
 | ||||||
|                 // the ads from immediately serving. Set to ENABLED once you've added
 |                 // 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.
 |         // Issues a mutate request to add campaigns.
 | ||||||
|         $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); |         $campaignServiceClient = $googleAdsClient->getCampaignServiceClient(); | ||||||
|         $response = $campaignServiceClient->mutateCampaigns( |         $response              = $campaignServiceClient->mutateCampaigns( | ||||||
|             MutateCampaignsRequest::build($customerId, $campaignOperations) |             MutateCampaignsRequest::build($customerId, $campaignOperations) | ||||||
|         ); |         ); | ||||||
| //        printf("Added %d campaigns:%s", $response->getResults()->count(), PHP_EOL);
 | //        printf("Added %d campaigns:%s", $response->getResults()->count(), PHP_EOL);
 | ||||||
| @ -110,7 +114,7 @@ class GoogleAdsCampaignService | |||||||
| //            print "{$addedCampaign->getResourceName()}" . PHP_EOL;
 | //            print "{$addedCampaign->getResourceName()}" . PHP_EOL;
 | ||||||
|             $resourceNames[] = $addedCampaign->getResourceName(); |             $resourceNames[] = $addedCampaign->getResourceName(); | ||||||
|         } |         } | ||||||
|          return $resourceNames; |         return $resourceNames; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -123,12 +127,12 @@ 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); | ||||||
| 
 | 
 | ||||||
|         return $budgetResourceName; |         return $budgetResourceName; | ||||||
|     } |     } | ||||||
| @ -142,13 +146,13 @@ class GoogleAdsCampaignService | |||||||
|      * @throws ApiException |      * @throws ApiException | ||||||
|      */ |      */ | ||||||
|     // [START add_campaigns]
 |     // [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.
 |         // Creates a campaign budget.
 | ||||||
|         $budget = new CampaignBudget([ |         $budget = new CampaignBudget([ | ||||||
| //            'name' => 'Interplanetary Cruise Budget #' . Helper::getPrintableDatetime(),
 | //            '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,
 | //            'delivery_method' => BudgetDeliveryMethod::STANDARD,
 | ||||||
|             'amount_micros' => $options['amount'] * 1000000 |             'amount_micros' => $options['amount'] * 1000000 | ||||||
|         ]); |         ]); | ||||||
| @ -159,7 +163,7 @@ class GoogleAdsCampaignService | |||||||
| 
 | 
 | ||||||
|         // Issues a mutate request.
 |         // Issues a mutate request.
 | ||||||
|         $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient(); |         $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient(); | ||||||
|         $response = $campaignBudgetServiceClient->mutateCampaignBudgets( |         $response                    = $campaignBudgetServiceClient->mutateCampaignBudgets( | ||||||
|             MutateCampaignBudgetsRequest::build($customerId, [$campaignBudgetOperation]) |             MutateCampaignBudgetsRequest::build($customerId, [$campaignBudgetOperation]) | ||||||
|         ); |         ); | ||||||
| 
 | 
 | ||||||
| @ -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