From bd0b06ee309d9e8b30c96c289dcea871fab005e7 Mon Sep 17 00:00:00 2001 From: hgc Date: Tue, 17 Dec 2024 16:22:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E7=BB=99=E5=AE=9A=E7=9A=84?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E6=90=9C=E7=B4=A2=20Google=20Ads=20=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E6=A3=80=E7=B4=A2=E5=88=B0=E5=85=B3=E4=BA=8E?= =?UTF-8?q?=E8=BF=99=E4=BA=9B=E5=AD=97=E6=AE=B5=E7=9A=84=E5=85=83=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=EF=BC=88metadata=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/GoogleAdsController.php | 17 ++++ app/service/GoogleAdsAdService.php | 127 ++++++++++++++++++++++++- config/route.php | 3 + 3 files changed, 146 insertions(+), 1 deletion(-) diff --git a/app/controller/GoogleAdsController.php b/app/controller/GoogleAdsController.php index 2e01fd4..deba0aa 100644 --- a/app/controller/GoogleAdsController.php +++ b/app/controller/GoogleAdsController.php @@ -126,6 +126,13 @@ class GoogleAdsController // 继续处理 Google Ads API 操作 return $this->getResponsiveSearchAds($options); } + public function listGoogleAdsFields(Request $request) + { + $options = $request->all(); + + // 继续处理 Google Ads API 操作 + return $this->searchForGoogleAdsFields($options); + } public function createLinkManagerToClient(Request $request) { @@ -275,6 +282,16 @@ class GoogleAdsController $resourceName = $this->googleAdsAdService->runGetResponsiveSearchAds($options); return $this->successResponse(['ads_list' => $resourceName]); } + + /** + * get campaigns + * @throws ApiException + */ + public function searchForGoogleAdsFields($options): Response + { + $googleAdsFieldData = $this->googleAdsAdService->runSearchForGoogleAdsFields($options); + return $this->successResponse(['ads_fields' => $googleAdsFieldData]); + } /** * get campaigns * @throws ApiException diff --git a/app/service/GoogleAdsAdService.php b/app/service/GoogleAdsAdService.php index 5edd57f..e4379e9 100644 --- a/app/service/GoogleAdsAdService.php +++ b/app/service/GoogleAdsAdService.php @@ -15,9 +15,12 @@ use Google\Ads\GoogleAds\Util\FieldMasks; use Google\Ads\GoogleAds\Util\V18\ResourceNames; use Google\Ads\GoogleAds\V18\Common\ResponsiveSearchAdInfo; use Google\Ads\GoogleAds\V18\Enums\AdGroupAdStatusEnum\AdGroupAdStatus; +use Google\Ads\GoogleAds\V18\Enums\GoogleAdsFieldCategoryEnum\GoogleAdsFieldCategory; +use Google\Ads\GoogleAds\V18\Enums\GoogleAdsFieldDataTypeEnum\GoogleAdsFieldDataType; use Google\Ads\GoogleAds\V18\Errors\GoogleAdsError; use Google\Ads\GoogleAds\V18\Resources\Ad; use Google\Ads\GoogleAds\V18\Resources\AdGroupAd; +use Google\Ads\GoogleAds\V18\Resources\GoogleAdsField; use Google\Ads\GoogleAds\V18\Services\AdGroupAdOperation; use Google\Ads\GoogleAds\V18\Services\AdOperation; use Google\Ads\GoogleAds\V18\Services\MutateAdGroupAdsRequest; @@ -26,6 +29,7 @@ use Google\Ads\GoogleAds\V18\Common\AdTextAsset; use Google\Ads\GoogleAds\V18\Enums\ServedAssetFieldTypeEnum\ServedAssetFieldType; use Google\Ads\GoogleAds\V18\Services\GoogleAdsRow; use Google\Ads\GoogleAds\V18\Services\MutateAdsRequest; +use Google\Ads\GoogleAds\V18\Services\SearchGoogleAdsFieldsRequest; use Google\Ads\GoogleAds\V18\Services\SearchGoogleAdsRequest; use Google\Protobuf\Internal\RepeatedField; @@ -275,7 +279,7 @@ class GoogleAdsAdService 'text' => 'Cruise to Pluto #' . Helper::getShortPrintableDatetime(), 'pinned_field' => ServedAssetFieldType::HEADLINE_1 ]), - new AdTextAsset(['text' => 'Tickets on sale now','pinned_field' => ServedAssetFieldType::HEADLINE_2]), + new AdTextAsset(['text' => 'Tickets on sale now', 'pinned_field' => ServedAssetFieldType::HEADLINE_2]), new AdTextAsset(['text' => 'Buy your ticket now']) ], 'descriptions' => [ @@ -313,4 +317,125 @@ class GoogleAdsAdService // [END update_responsive_search_ad] + /** + * 根据给定的前缀搜索 Google Ads 字段,检索到关于这些字段的元数据(metadata) + */ + /* @param int $customerId the customer ID + * @param $options + * @return mixed + * @throws ApiException + */ + public function runSearchForGoogleAdsFields($options): mixed + { + $googleAdsClient = $this->googleAdsClient; + // Creates a single shared budget to be used by the campaigns added below. + $googleAdsFieldData = self::searchForGoogleAdsFields($googleAdsClient, $options['name_prefix']); + + return $googleAdsFieldData; + } + + /** + * Runs the example SearchForGoogleAdsFields. + * + * @param GoogleAdsClient $googleAdsClient the Google Ads API client + * @param string $namePrefix the name prefix to use in the query + */ + public static function searchForGoogleAdsFields(GoogleAdsClient $googleAdsClient, string $namePrefix) + { + $googleAdsFieldServiceClient = $googleAdsClient->getGoogleAdsFieldServiceClient(); + // Searches for all fields whose name begins with the specified namePrefix. + // A single "%" is the wildcard token in the Google Ads Query language. + $query = "SELECT name, category, selectable, filterable, sortable, selectable_with, " + . "data_type, is_repeated WHERE name LIKE '$namePrefix%'"; + $response = $googleAdsFieldServiceClient->searchGoogleAdsFields( + SearchGoogleAdsFieldsRequest::build($query) + ); + + if (iterator_count($response->getIterator()) === 0) { + printf( + "No GoogleAdsFields found with a name that begins with %s.%s", + $namePrefix, + PHP_EOL + ); + return; + } + // Iterates over all rows and prints our the metadata of each matching GoogleAdsField. + foreach ($response->iterateAllElements() as $googleAdsField) { + /** @var GoogleAdsField $googleAdsField */ + $fieldInfo = [ + 'name' => $googleAdsField->getName(), + 'category' => GoogleAdsFieldCategory::name($googleAdsField->getCategory()), + 'data_type' => GoogleAdsFieldDataType::name($googleAdsField->getDataType()), + 'selectable' => $googleAdsField->getSelectable() ? 'true' : 'false', + 'filterable' => $googleAdsField->getFilterable() ? 'true' : 'false', + 'sortable' => $googleAdsField->getSortable() ? 'true' : 'false', + 'repeated' => $googleAdsField->getIsRepeated() ? 'true' : 'false', + 'selectable_with' => [] + ]; + // Check if there are fields that are selectable with the current field + if ($googleAdsField->getSelectableWith()->count() > 0) { + $selectableWithFields = iterator_to_array($googleAdsField->getSelectableWith()->getIterator()); + sort($selectableWithFields); // Sort the fields alphabetically + $fieldInfo['selectable_with'] = $selectableWithFields; + } + + // Add the field info to the result array + $googleAdsFieldData[] = $fieldInfo; +// +// printf("%s:%s", $googleAdsField->getName(), PHP_EOL); +// printf( +// " %-16s: %s%s", +// "category:", +// GoogleAdsFieldCategory::name($googleAdsField->getCategory()), +// PHP_EOL +// ); +// printf( +// " %-16s: %s%s", +// "data type:", +// GoogleAdsFieldDataType::name($googleAdsField->getDataType()), +// PHP_EOL +// ); +// printf( +// " %-16s: %s%s", +// "selectable:", +// $googleAdsField->getSelectable() ? 'true' : 'false', +// PHP_EOL +// ); +// printf( +// " %-16s: %s%s", +// "filterable:", +// $googleAdsField->getFilterable() ? 'true' : 'false', +// PHP_EOL +// ); +// printf( +// " %-16s: %s%s", +// "sortable:", +// $googleAdsField->getSortable() ? 'true' : 'false', +// PHP_EOL +// ); +// printf( +// " %-16s: %s%s", +// "repeated:", +// $googleAdsField->getIsRepeated() ? 'true' : 'false', +// PHP_EOL +// ); +// +// if ($googleAdsField->getSelectableWith()->count() > 0) { +// // Prints the list of fields that are selectable with the field. +// $selectableWithFields = +// iterator_to_array($googleAdsField->getSelectableWith()->getIterator()); +// // Sorts and then prints the list. +// sort($selectableWithFields); +// print ' selectable with:' . PHP_EOL; +// foreach ($selectableWithFields as $selectableWithField) { +// /** @var string $selectableWithField */ +// printf(" $selectableWithField%s", PHP_EOL); +// } +// } + } + + return $googleAdsFieldData; // Return the result array + } + + } diff --git a/config/route.php b/config/route.php index 330b711..3885bff 100644 --- a/config/route.php +++ b/config/route.php @@ -54,6 +54,9 @@ Route::group('/googleads', function () { Route::post('/list', [GoogleAdsController::class, 'listResponsiveSearchAds']); Route::post('/update', [GoogleAdsController::class, 'updateResponsiveSearchAd']); }); + Route::group('/fields', function () { + Route::post('/list', [GoogleAdsController::class, 'listGoogleAdsFields']); + }); }); Route::group('/account_link', function () {