132 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			132 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace app\service;
 | |
| 
 | |
| use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClient;
 | |
| use Google\Ads\GoogleAds\V18\Services\AdGroupOperation;
 | |
| use Google\Ads\GoogleAds\V18\Services\AdOperation;
 | |
| use Google\Ads\GoogleAds\V18\Services\CampaignBudgetOperation;
 | |
| use Google\Ads\GoogleAds\V18\Services\CampaignOperation;
 | |
| 
 | |
| class GoogleAdsService
 | |
| {
 | |
|     protected $googleAdsClient;
 | |
| 
 | |
|     public function __construct()
 | |
|     {
 | |
|         $config = include __DIR__ . '/../../config/google_ads_php.ini';
 | |
|         $this->googleAdsClient = new GoogleAdsClient([
 | |
|             'developerToken' => $config['developer_token'],
 | |
|             'clientId' => $config['client_id'],
 | |
|             'clientSecret' => $config['client_secret'],
 | |
|             'refreshToken' => $config['refresh_token'],
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     // 创建广告预算
 | |
|     public function createCampaignBudget($customerId, $budgetName, $amountMicros)
 | |
|     {
 | |
|         $campaignBudgetService = $this->googleAdsClient->getCampaignBudgetServiceClient();
 | |
| 
 | |
|         $campaignBudget = new \Google\Ads\GoogleAds\V18\Resources\CampaignBudget([
 | |
|             'name' => $budgetName,
 | |
|             'amountMicros' => $amountMicros,
 | |
|             'deliveryMethod' => 'STANDARD',
 | |
|             'status' => 'ENABLED',
 | |
|         ]);
 | |
| 
 | |
|         $operation = new CampaignBudgetOperation();
 | |
|         $operation->setCreate($campaignBudget);
 | |
| 
 | |
|         try {
 | |
|             $response = $campaignBudgetService->mutateCampaignBudgets($customerId, [$operation]);
 | |
|             return $response->getResults()[0]->getResourceName();
 | |
|         } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
 | |
|             return $e->getMessage();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 创建广告系列
 | |
|     public function createCampaign($customerId, $budgetId, $campaignName)
 | |
|     {
 | |
|         $campaignService = $this->googleAdsClient->getCampaignServiceClient();
 | |
| 
 | |
|         $campaign = new \Google\Ads\GoogleAds\V18\Resources\Campaign([
 | |
|             'name' => $campaignName,
 | |
|             'advertisingChannelType' => 'SEARCH',
 | |
|             'status' => 'PAUSED',
 | |
|             'startDate' => '2024-12-01',
 | |
|             'endDate' => '2024-12-31',
 | |
|             'manualCpc' => new \Google\Ads\GoogleAds\V18\Resources\ManualCpc([
 | |
|                 'enhancedCpcEnabled' => true
 | |
|             ]),
 | |
|             'campaignBudget' => "customers/{$customerId}/campaignBudgets/{$budgetId}"
 | |
|         ]);
 | |
| 
 | |
|         $operation = new CampaignOperation();
 | |
|         $operation->setCreate($campaign);
 | |
| 
 | |
|         try {
 | |
|             $response = $campaignService->mutateCampaigns($customerId, [$operation]);
 | |
|             return $response->getResults()[0]->getResourceName();
 | |
|         } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
 | |
|             return $e->getMessage();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 创建广告组
 | |
|     public function createAdGroup($customerId, $campaignId, $adGroupName, $cpcBidMicros)
 | |
|     {
 | |
|         $adGroupService = $this->googleAdsClient->getAdGroupServiceClient();
 | |
| 
 | |
|         $adGroup = new \Google\Ads\GoogleAds\V18\Resources\AdGroup([
 | |
|             'name' => $adGroupName,
 | |
|             'campaign' => "customers/{$customerId}/campaigns/{$campaignId}",
 | |
|             'status' => 'PAUSED',
 | |
|             'cpcBidMicros' => $cpcBidMicros,
 | |
|             'startDate' => '2024-12-01',
 | |
|             'endDate' => '2024-12-31',
 | |
|         ]);
 | |
| 
 | |
|         $operation = new AdGroupOperation();
 | |
|         $operation->setCreate($adGroup);
 | |
| 
 | |
|         try {
 | |
|             $response = $adGroupService->mutateAdGroups($customerId, [$operation]);
 | |
|             return $response->getResults()[0]->getResourceName();
 | |
|         } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
 | |
|             return $e->getMessage();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     // 创建广告
 | |
|     public function createAd($customerId, $adGroupId, $adName, $headline, $description, $finalUrls)
 | |
|     {
 | |
|         $adService = $this->googleAdsClient->getAdServiceClient();
 | |
| 
 | |
|         // 创建文本广告
 | |
|         $textAd = new \Google\Ads\GoogleAds\V18\Resources\AdTextAdInfo([
 | |
|             'headline' => $headline,
 | |
|             'description' => $description,
 | |
|             'finalUrls' => $finalUrls,
 | |
|         ]);
 | |
| 
 | |
|         $ad = new \Google\Ads\GoogleAds\V18\Resources\Ad([
 | |
|             'name' => $adName,
 | |
|             'adGroup' => "customers/{$customerId}/adGroups/{$adGroupId}",
 | |
|             'status' => 'PAUSED',
 | |
|             'textAd' => $textAd,
 | |
|         ]);
 | |
| 
 | |
|         $operation = new AdOperation();
 | |
|         $operation->setCreate($ad);
 | |
| 
 | |
|         try {
 | |
|             $response = $adService->mutateAds($customerId, [$operation]);
 | |
|             return $response->getResults()[0]->getResourceName();
 | |
|         } catch (\Google\Ads\GoogleAds\Errors\GoogleAdsException $e) {
 | |
|             return $e->getMessage();
 | |
|         }
 | |
|     }
 | |
| }
 |