How to add as parameter competitor and avgsearchmonthly before i generate results

102 views
Skip to first unread message

stela zh

unread,
May 13, 2020, 4:44:10 PM5/13/20
to AdWords API and Google Ads API Forum
here is my code : 

if (empty($keywords)) {
// Only page URL was specified, so use a UrlSeed.
$requestOptionalArgs['urlSeed'] =
new UrlSeed(['url' => new StringValue(['value' => $link_platform_product_page])]);
} elseif (is_null($link_platform_product_page)) {
// Only keywords were specified, so use a KeywordSeed.
$requestOptionalArgs['keywordSeed'] = new KeywordSeed([
'keywords' => array_map(function ($keyword) {
return new StringValue(['value' => $keyword]);
}, $keywords)
]);
} else {
// Both page URL and keywords were specified, so use a KeywordAndUrlSeed.
$requestOptionalArgs['keywordAndUrlSeed'] = new KeywordAndUrlSeed([
'url' => new StringValue(['value' => $link_platform_product_page]),
'keywords' => array_map(function ($keyword) {
return new StringValue(['value' => $keyword]);
}, $keywords)
]);
}

// Create a list of geo target constants based on the resource name of specified location
// IDs.
$geoTargetConstants = array_map(function ($locationId) {
return new StringValue(
['value' => ResourceNames::forGeoTargetConstant($locationId)]
);
}, $locationIds);

// Generate keyword ideas based on the specified parameters.
$response = $keywordPlanIdeaServiceClient->generateKeywordIdeas(
$customerId,
// Set the language resource using the provided language ID.
new StringValue(['value' => ResourceNames::forLanguageConstant($language)]),
// Add the resource name of each location ID to the request.
$geoTargetConstants,
// Set the network. To restrict to only Google Search, change the parameter below to
// KeywordPlanNetwork::GOOGLE_SEARCH.
KeywordPlanNetwork::GOOGLE_SEARCH_AND_PARTNERS,
$requestOptionalArgs
);

$keywords = "";
$avgmonthly = "";
$i = 0;

// Iterate over the results and print its detail.
foreach ($response->getResults() as $result) {
if ($result->getKeywordIdeaMetrics()) {

$competitor = $result->getKeywordIdeaMetrics()->getCompetition();
$avgmonthly = $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue();


if ($competitor == 4 && $avgmonthly > 100
) {
// echo str_word_count($words);
$keywords .= $result->getText()->getValue() . ",";

if ($i++ == $limit) break;

printf(
"Keyword idea text '%s' has %d average monthly searches and competition as %d.%s",
$result->getText()->getValue(),
is_null($result->getKeywordIdeaMetrics()) ?
0 : $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue(),
is_null($result->getKeywordIdeaMetrics()) ?
0 : $result->getKeywordIdeaMetrics()->getCompetition(),
PHP_EOL
);
}
 }
}
return $keywords;


how to add    $competitor = $result->getKeywordIdeaMetrics()->getCompetition();
        $avgmonthly = $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue();  before foreach as paramether so to not generate all the keywords and to filter them but to generated only keywords i need.

Google Ads API Forum Advisor Prod

unread,
May 14, 2020, 10:11:06 AM5/14/20
to stel...@gmail.com, adwor...@googlegroups.com
Hi Stela,

Thank you for reaching out. Unfortunately, it will not be possible to filter by the metrics before you grab the responses. You will have to filter the responses as you are doing.

Regards,
Mitchell
Google Ads API Team

ref:_00D1U1174p._5004Q1zLr2S:ref
Reply all
Reply to author
Forward
0 new messages