Get keywords related to a seed keyword

181 views
Skip to first unread message

neetu varshney

unread,
Apr 11, 2019, 9:00:58 AM4/11/19
to AdWords API and Google Ads API Forum
Hello Team,
As i am using Google Adword api for getting related keyword ideas, 
I am using "GetKeywordIdeas" apis for getting the keywords value.

 $targetingIdeaService = $adWordsServices->get($session, TargetingIdeaService::class);

        // Create selector.
        $selector = new TargetingIdeaSelector();
        $selector->setRequestType(RequestType::IDEAS);
        $selector->setIdeaType(IdeaType::KEYWORD);
        $selector->setRequestedAttributeTypes(
            [
                AttributeType::KEYWORD_TEXT,
                AttributeType::SEARCH_VOLUME,
                AttributeType::AVERAGE_CPC,
                AttributeType::COMPETITION,
                AttributeType::CATEGORY_PRODUCTS_AND_SERVICES
            ]
        );

<?php
/**
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


namespace Google\AdsApi\Examples\AdWords\v201809\Optimization;

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

use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201809\cm\Language;
use Google\AdsApi\AdWords\v201809\cm\NetworkSetting;
use Google\AdsApi\AdWords\v201809\cm\Paging;
use Google\AdsApi\AdWords\v201809\o\AttributeType;
use Google\AdsApi\AdWords\v201809\o\IdeaType;
use Google\AdsApi\AdWords\v201809\o\LanguageSearchParameter;
use Google\AdsApi\AdWords\v201809\o\NetworkSearchParameter;
use Google\AdsApi\AdWords\v201809\o\RelatedToQuerySearchParameter;
use Google\AdsApi\AdWords\v201809\o\RequestType;
use Google\AdsApi\AdWords\v201809\o\SeedAdGroupIdSearchParameter;
use Google\AdsApi\AdWords\v201809\o\TargetingIdeaSelector;
use Google\AdsApi\AdWords\v201809\o\TargetingIdeaService;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\Common\Util\MapEntries;

/**
 * This example gets keyword ideas related to a seed keyword.
 */

class GetKeywordIdeas
{

   
// If you do not want to use an existing ad group to seed your request, you
   
// can set this to null.
   
const AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE';
   
const PAGE_LIMIT = 500;

   
public static function runExample(
       
AdWordsServices $adWordsServices,
       
AdWordsSession $session,
        $adGroupId
   
) {
        $targetingIdeaService
= $adWordsServices->get($session, TargetingIdeaService::class);

       
// Create selector.
        $selector
= new TargetingIdeaSelector();
        $selector
->setRequestType(RequestType::IDEAS);
        $selector
->setIdeaType(IdeaType::KEYWORD);
        $selector
->setRequestedAttributeTypes(
           
[
               
AttributeType::KEYWORD_TEXT,
               
AttributeType::SEARCH_VOLUME,
               
AttributeType::AVERAGE_CPC,
               
AttributeType::COMPETITION,
               
AttributeType::CATEGORY_PRODUCTS_AND_SERVICES
           
]
       
);

        $paging
= new Paging();
        $paging
->setStartIndex(0);
        $paging
->setNumberResults(10);
        $selector
->setPaging($paging);

        $searchParameters
= [];
       
// Create related to query search parameter.
        $relatedToQuerySearchParameter
= new RelatedToQuerySearchParameter();
        $relatedToQuerySearchParameter
->setQueries(
           
[
               
'bakery',
               
'pastries',
               
'birthday cake'
           
]
       
);
        $searchParameters
[] = $relatedToQuerySearchParameter;

       
// Create language search parameter (optional).
       
// The ID can be found in the documentation:
       
// https://developers.google.com/adwords/api/docs/appendix/languagecodes
        $languageParameter
= new LanguageSearchParameter();
        $english
= new Language();
        $english
->setId(1000);
        $languageParameter
->setLanguages([$english]);
        $searchParameters
[] = $languageParameter;

       
// Create network search parameter (optional).
        $networkSetting
= new NetworkSetting();
        $networkSetting
->setTargetGoogleSearch(true);
        $networkSetting
->setTargetSearchNetwork(false);
        $networkSetting
->setTargetContentNetwork(false);
        $networkSetting
->setTargetPartnerSearchNetwork(false);

        $networkSearchParameter
= new NetworkSearchParameter();
        $networkSearchParameter
->setNetworkSetting($networkSetting);
        $searchParameters
[] = $networkSearchParameter;

       
// Optional: Use an existing ad group to generate ideas.
       
if (!empty($adGroupId)) {
            $seedAdGroupIdSearchParameter
= new SeedAdGroupIdSearchParameter();
            $seedAdGroupIdSearchParameter
->setAdGroupId($adGroupId);
            $searchParameters
[] = $seedAdGroupIdSearchParameter;
       
}
        $selector
->setSearchParameters($searchParameters);
        $selector
->setPaging(new Paging(0, self::PAGE_LIMIT));

       
// Get keyword ideas.
        $page
= $targetingIdeaService->get($selector);

       
// Print out some information for each targeting idea.
        $entries
= $page->getEntries();
       
if ($entries !== null) {
           
foreach ($entries as $targetingIdea) {
                $data
= MapEntries::toAssociativeArray($targetingIdea->getData());
                $keyword
= $data[AttributeType::KEYWORD_TEXT]->getValue();
                $searchVolume
= ($data[AttributeType::SEARCH_VOLUME]->getValue() !== null)
                   
? $data[AttributeType::SEARCH_VOLUME]->getValue() : 0;
                $averageCpc
= $data[AttributeType::AVERAGE_CPC]->getValue();
                $competition
= $data[AttributeType::COMPETITION]->getValue();
                $categoryIds
= ($data[AttributeType::CATEGORY_PRODUCTS_AND_SERVICES]->getValue() === null)
                   
? $categoryIds = ''
                   
: implode(
                       
', ',
                        $data
[AttributeType::CATEGORY_PRODUCTS_AND_SERVICES]->getValue()
                   
);
                printf
(
                   
"Keyword with text '%s', average monthly search volume %d, "
                   
. "average CPC %d, and competition %.2f was found with categories: %s\n",
                    $keyword
,
                    $searchVolume
,
                   
($averageCpc === null) ? 0 : $averageCpc->getMicroAmount(),
                    $competition
,
                    $categoryIds
               
);
           
}
       
}

       
if (empty($entries)) {
           
print "No related keywords were found.\n";
       
}
   
}

   
public static function main()
   
{
       
// Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential
= (new OAuth2TokenBuilder())->fromFile()->build();

       
// Construct an API session configured from a properties file and the
       
// OAuth2 credentials above.
        $session
= (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
       
self::runExample(new AdWordsServices(), $session, self::AD_GROUP_ID);
   
}
}

GetKeywordIdeas::main();
I am using RequestType is "$selector->setRequestType(RequestType::IDEAS)", can you please provide me the output of these code.

googleadsapi...@google.com

unread,
Apr 11, 2019, 3:55:18 PM4/11/19
to AdWords API and Google Ads API Forum
Hello Neetu,

The did a sample run using my test account and the part of the results copied below, 

Keyword with text 'birthday cake c7ea94a8', average monthly search volume 3960895, average CPC 4864816, and competition 0.66 was found with categories: 41, 10628, 21, 47
Keyword with text 'bakery 6b76bfff', average monthly search volume 5728336, average CPC 15754491, and competition 0.24 was found with categories: 48, 83, 68, 10628
Keyword with text 'bakery b8bf8bf9', average monthly search volume 5169192, average CPC 19104287, and competition 0.88 was found with categories: 16, 24, 9, 10628
Keyword with text 'bakery dee9a487', average monthly search volume 4985818, average CPC 1924203, and competition 0.05 was found with categories: 19, 10628, 30, 23
Keyword with text 'birthday cake bc36e2b', average monthly search volume 6602809, average CPC 6061040, and competition 0.95 was found with categories: 73, 10628, 47, 63
.
.

In the above example yo have to specify the AdGroupId and execute the code from command line to see the similar results at your end. You can follow the instructions mentioned here to download and install the php client library as well. Please let me know if you have nay further questions.

Regards,
Sai Teja, Google Ads API Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Was your question answered? Please rate your experience with us by taking a short survey.
If not -- reply to this email and tell us what else we can do to help.

Take Survey

Also find us on our blog and discussion group:
http://googleadsdeveloper.blogspot.com/search/label/adwords_api
https://developers.google.com/adwords/api/community/

neetu varshney

unread,
Apr 12, 2019, 2:49:29 AM4/12/19
to AdWords API and Google Ads API Forum

Hello Sai Teja,


The sample code using test account gives results - 
Keyword with text 'birthday cake c7ea94a8', average monthly search volume 3960895, average CPC 4864816, and competition 0.66 was found with categories: 41, 10628, 21, 47
Keyword with text 'bakery 6b76bfff', average monthly search volume 5728336, average CPC 15754491, and competition 0.24 was found with categories: 48, 83, 68, 10628
Keyword with text 'bakery b8bf8bf9', average monthly search volume 5169192, average CPC 19104287, and competition 0.88 was found with categories: 16, 24, 9, 10628 etc

I just want to confirm one thing - what is --
why the keyword are like unusual dummy strings "birthday cake c7ea94a8", "bakery 6b76bfff", "bakery 6b76bfff", "bakery b8bf8bf9", How they are related to bakery keywords,

If search a keyword "Paytm", than there would be related keywords like - paytm offers, paytm login, paytm code, paytm mall etc ,

But i got like "paytm c7ea94a8", "paytm 6b76bfff", "paytm 6b76bfff"  etc

Please suggest me -

Peter Bowen

unread,
Apr 12, 2019, 8:05:20 AM4/12/19
to AdWords API and Google Ads API Forum
The junk text you're getting happens when you're using a test account or if your token hasn't been approved yet.

neetu varshney

unread,
Apr 12, 2019, 8:44:36 AM4/12/19
to AdWords API and Google Ads API Forum
Okay so if i use a manager account , than my results be like -

Search a keyword "Paytm", than there would be related keywords like - paytm offers, paytm login, paytm code, paytm mall etc .

So it would be same as above , am i right Mr. Peter ?

googleadsapi...@google.com

unread,
Apr 15, 2019, 12:15:01 PM4/15/19
to AdWords API and Google Ads API Forum
Hello Neetu,

As Peter mentioned using the Test accounts to fetch keyword ideas might result the junk data. You can expect similar keywords as mentioned above when you use production account to fetch keyword ideas.

Regards,
Sai teja, Google Ads API Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

neetu varshney

unread,
Apr 16, 2019, 2:40:08 AM4/16/19
to AdWords API and Google Ads API Forum
Thanks Sai :)
Reply all
Reply to author
Forward
0 new messages