根据给定的前缀搜索 Google Ads 字段,检索到关于这些字段的元数据(metadata)

This commit is contained in:
hgc 2024-12-17 16:22:16 +08:00
parent 96837526eb
commit bd0b06ee30
3 changed files with 146 additions and 1 deletions

View File

@ -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

View File

@ -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
}
}

View File

@ -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 () {