I am trying to limit the number of returned keyword ideas. The code works fine, but when I try to set limit to the keywordIdeas that I get using 'pageSize' I get the following error:

An unexpected error occurred: { "message": "Resource has been exhausted (e.g. check quota).", "code": 8, "status": "RESOURCE_EXHAUSTED", "details": [ { "@type": "type.googleapis.com\/google.ads.googleads.v14.errors.GoogleAdsFailure", "errors": [ { "errorCode": { "quotaError": "RESOURCE_EXHAUSTED" }, "message": "Too many requests. Retry in 4 seconds.", "details": { "quotaErrorDetails": { "rateScope": "ACCOUNT", "rateName": "Requests per service per method", "retryDelay": "4s" } } } ], "requestId": "eTMFNxlHYYTzZGDT7Cksfw" } ] }

Which is odd, because I am trying to limit the results and without pageSize this is error is not displayed.

Here is the code:

namespace Google\Ads\GoogleAds\Examples\Planning;

require __DIR__ . '/../../vendor/autoload.php';

use GetOpt\GetOpt;

use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V14\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V14\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V14\GoogleAdsException;
use Google\Ads\GoogleAds\Util\V14\ResourceNames;
use Google\Ads\GoogleAds\V14\Enums\KeywordPlanNetworkEnum\KeywordPlanNetwork;
use Google\Ads\GoogleAds\V14\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V14\Services\GenerateKeywordIdeaResult;
use Google\Ads\GoogleAds\V14\Services\KeywordAndUrlSeed;
use Google\Ads\GoogleAds\V14\Services\KeywordSeed;
use Google\Ads\GoogleAds\V14\Services\UrlSeed;
use Google\ApiCore\ApiException;

function generateKeywordIdeas($customerId, $url, $language, $locationId)
{
    try {
        // Google Ads API Configurations
       $config = [
            'developerToken' => '***',
            'clientId' => '***',
            'clientSecret' => '***',
            'refreshToken' => '***',
            'loginCustomerId' => '***', 
            'useSandbox' => true, // Set to true if you want to use the sandbox environment for testing.
            'useImplicitConversion' => false, // Set to true if you want to automatically convert currency and units.
        ];
       

        $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile($_SERVER['DOCUMENT_ROOT'] . '/google-ads-php/google_ads_php.ini')
        ->build();

        $googleAdsClient = (new GoogleAdsClientBuilder())
        ->fromFile($_SERVER['DOCUMENT_ROOT'] . '/google-ads-php/google_ads_php.ini')
        ->withOAuth2Credential($oAuth2Credential)
        ->build();
        
        $pageUrl = null;
        $keywords = ['marketing','seo','advertising'];

      
       
        $locationIds = [21167];
        $geoTargetConstants =  array_map(function ($locationId) {
            return ResourceNames::forGeoTargetConstant($locationId);
        }, $locationIds);
        $keywordPlanIdeaServiceClient = $googleAdsClient->getKeywordPlanIdeaServiceClient();
     
        $requestOptionalArgs = [];
        if (empty($keywords)) {
            // Only page URL was specified, so use a UrlSeed.
            $requestOptionalArgs['urlSeed'] = new UrlSeed(['url' => $pageUrl]);
        } elseif (is_null($pageUrl)) {
            // Only keywords were specified, so use a KeywordSeed.
            $requestOptionalArgs['keywordSeed'] = new KeywordSeed(['keywords' => $keywords]);
        } else {
            // Both page URL and keywords were specified, so use a KeywordAndUrlSeed.
            $requestOptionalArgs['keywordAndUrlSeed'] =
                new KeywordAndUrlSeed(['url' => $pageUrl, 'keywords' => $keywords]);
        }
        $response = $keywordPlanIdeaServiceClient->generateKeywordIdeas(
            [
                // Set the language resource using the provided language ID.
                'language' => ResourceNames::forLanguageConstant(1000),
                'customerId' => ***,
                // Add the resource name of each location ID to the request.
                'geoTargetConstants' => $geoTargetConstants,
                'keywordPlanNetwork' => KeywordPlanNetwork::GOOGLE_SEARCH,
                'pageSize'=>50
      
       
            ]+$requestOptionalArgs
        );


        // Iterate over the results and print its detail.
        foreach ($response->iterateAllElements() as $result) {
        var_dump($result->getText());
           
    } catch (GoogleAdsException $e) {
        printf(
            "An error occurred while generating keyword ideas: %s\n",
            $e->getMessage()
        );
    } catch (\Throwable $e) {
        printf(
            "An unexpected error occurred: %s\n",
            $e->getMessage()
        );
    }
}

generateKeywordIdeas($customerId, $url, $language, $locationId);
?>

Tag:-

Add a new comment.