Using adwords API how can I get a keywords competition, local search volume for a particular country

1,446 views
Skip to first unread message

Bikram The PHP Developer

unread,
Mar 31, 2011, 11:50:58 AM3/31/11
to AdWords API Forum, web...@gmail.com
Using adwords API how can I get a keywords competition, local search
volume for a particular country

A sample code in PHP is highly recommended. I have all examples of
082010 adwords api package, is there any example which has similar
codes?

My webpage will be like this :

Keyword Text Input Box : User will type his/her keyword ( One keyword
at a time)
A drop down list having all country list : User will select the
country
A language drop down list ( Default is English)

A submit button

Upon submit keyword's global and local search volume on that
particular country will be shown. Also the competition of the keywords
will be shown in % or in fraction

David Torres

unread,
Apr 4, 2011, 3:57:45 PM4/4/11
to adwor...@googlegroups.com, web...@gmail.com
Hi Bikram,

Perhaps an example in Perl works for you, it shouldn't be difficult to adapt this PHP code example from the Perl code.

Best,

-David Torres - AdWords API Team

-----------------------------------------------------

#!/usr/bin/perl -w
#
# Copyright 2011, 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
#
#
# 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.
#
# This example gets keywords related to a seed keyword.
#
# Tags: TargetingIdeaService.get
# Author: David Torres <api.dav...@gmail.com>

use strict;
use lib "../../lib";

use Google::Ads::AdWords::Client;
use Google::Ads::AdWords::Logging;
use Google::Ads::AdWords::v201101::CountryTarget;
use Google::Ads::AdWords::v201101::CountryTargetSearchParameter;
use Google::Ads::AdWords::v201101::Keyword;
use Google::Ads::AdWords::v201101::KeywordMatchTypeSearchParameter;
use Google::Ads::AdWords::v201101::Paging;
use Google::Ads::AdWords::v201101::RelatedToKeywordSearchParameter;
use Google::Ads::AdWords::v201101::TargetingIdeaSelector;
use Google::Ads::Common::MapUtils;

# Log SOAP XML request and response.
Google::Ads::AdWords::Logging::enable_soap_logging();

# Get AdWords Client, credentials will be read from ~/adwords.properties.
my $client = Google::Ads::AdWords::Client->new({version => "v201101"});

# By default examples are set to die on any server returned fault.
$client->set_die_on_faults(1);

# Create seed keyword.
my $seed_keyword = Google::Ads::AdWords::v201101::Keyword->new({
  text => "mars cruise",
  matchType => "BROAD"
});

# Create country target.
my $country = Google::Ads::AdWords::v201101::CountryTarget->new({
  countryCode => 'US'
});

# Create selector.
my $selector = Google::Ads::AdWords::v201101::TargetingIdeaSelector->new({
  requestType => "IDEAS",
  ideaType => "KEYWORD",
  requestedAttributeTypes => ["CRITERION",
                              "AVERAGE_TARGETED_MONTHLY_SEARCHES",
                              "COMPETITION",
                              "GLOBAL_MONTHLY_SEARCHES"],
});

# Set selector paging (required for targeting idea service).
my $paging = Google::Ads::AdWords::v201101::Paging->new({
  startIndex => 0,
  numberResults => 10
});
$selector->set_paging($paging);

# Create related to keyword search parameter.
my $keyword_search_parameter =
    Google::Ads::AdWords::v201101::RelatedToKeywordSearchParameter->new({
      keywords => [$seed_keyword]
    });

# Country target search parameter.
my $country_target_search_parameter =
    Google::Ads::AdWords::v201101::CountryTargetSearchParameter->new({
      countryTargets => [$country]
    });

# Create keyword match type search parameter to ensure unique results.
my $keyword_match_type =
    Google::Ads::AdWords::v201101::KeywordMatchTypeSearchParameter->new({
      keywordMatchTypes => ["BROAD"]
    });
$selector->set_searchParameters([$keyword_search_parameter,
                                 $keyword_match_type,
                                 $country_target_search_parameter
                                 ]);

# Get related keywords.
my $page = $client->TargetingIdeaService()->get({selector => $selector});

# Display related keywords.
if ($page->get_entries()) {
  foreach my $targeting_idea (@{$page->get_entries()}) {
    my $data =
        Google::Ads::Common::MapUtils::get_map($targeting_idea->get_data());
    my $keyword = $data->{"CRITERION"}->get_value();
    my $average_monthly_searches =
        $data->{"AVERAGE_TARGETED_MONTHLY_SEARCHES"}->get_value()?
        $data->{"AVERAGE_TARGETED_MONTHLY_SEARCHES"}->get_value():0;
    my $competition =
        $data->{"COMPETITION"}->get_value()?
        $data->{"COMPETITION"}->get_value():0;
    my $global_monthly_searches =
        $data->{"GLOBAL_MONTHLY_SEARCHES"}->get_value()?
        $data->{"GLOBAL_MONTHLY_SEARCHES"}->get_value():0;
    printf "Keyword with text \"%s\", match type \"%s\", and average monthly " .
           "search volume \"%s\", competition \"%s\" was found.\n", $keyword->get_text(),
           $keyword->get_matchType(), $average_monthly_searches, $competition;
  }
} else {
  print "No related keywords were found.\n";
}

Evgeniy Bogdanov

unread,
Apr 6, 2011, 3:28:33 PM4/6/11
to AdWords API Forum
Hi.

This should help you:

$user = new AdWordsUser();

// Log SOAP XML request and response.
$user->LogDefaults();

// Get the TargetingIdeaService.
$targetingIdeaService = $user->GetTargetingIdeaService('v201008');

$countryTarget = new CountryTarget('US', false);
$countryTargetSearchParameter = new
CountryTargetSearchParameter();
$countryTargetSearchParameter->countryTargets =
array($countryTarget);

// Create seed keyword.
$keyword = new Keyword();
$keyword->text = 'mars cruise';
$keyword->matchType = 'BROAD';

// Create selector.
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes =
array('KEYWORD', 'AVERAGE_TARGETED_MONTHLY_SEARCHES',
'GLOBAL_MONTHLY_SEARCHES', 'COMPETITION');

// Set selector paging (required for targeting idea service).
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;

// Create related to keyword search parameter.
$relatedToKeywordSearchParameter = new
RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);

// Create keyword match type search parameter to ensure unique
results.
$keywordMatchTypeSearchParameter = new
KeywordMatchTypeSearchParameter();
$keywordMatchTypeSearchParameter->keywordMatchTypes =
array('BROAD');

$selector->searchParameters =
array($relatedToKeywordSearchParameter,
$keywordMatchTypeSearchParameter, $countryTargetSearchParameter);

// Get related keywords.
$page = $targetingIdeaService->get($selector);

Bohdan Kit

unread,
Apr 24, 2014, 10:01:39 AM4/24/14
to adwor...@googlegroups.com

Ні, Evgeniy

Your code become old and some methods are missing. I've changed this code and got this:

    $user = new AdWordsUser();
        $user->LogDefaults();

        $targetingIdeaService = $user->GetService('TargetingIdeaService', 'v201402');
        $keyword = 'mars cruise';
        $selector = new TargetingIdeaSelector();
        $selector->requestType = 'STATS';
        $selector->ideaType = 'KEYWORD';
        $selector->requestedAttributeTypes = array('COMPETITION');

        // code below give this exception: Unmarshalling Error: cvc-type.2: The type definition cannot be abstract for element ns1:searchParameters.
        $relatedToQuerySearchParameter = new RelatedToQuerySearchParameter();
        $relatedToQuerySearchParameter->queries = array($keyword);
        $selector->searchParameters[] = $relatedToQuerySearchParameter;

        //if do not use code above I have this error: [AuthenticationError.OAUTH_TOKEN_INVALID @ ; trigger:'']
        //could it be because my developer token is not registred yet?

        // Set selector paging (required by this service).
        $selector->paging = new Paging(0, 10);

        do {
            // code below gives this warning: Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /googleads-php-lib-master/src/Google/Api/Ads/Common/Util/CurlUtils.php on line 107
            $page = $targetingIdeaService->get($selector);

            if (isset($page->entries)) {
                foreach ($page->entries as $targetingIdea) {
                    $data = MapUtils::GetMap($targetingIdea->data);

                    $competition =
                        isset($data['COMPETITION']->value)
                            ? $data['COMPETITION']->value : 0;
                    echo($competition);
                }
            } else {
                print "No keywords ideas were found.\n";
            }
            $selector->paging->startIndex += 10;
        } while ($page->totalNumEntries > $selector->paging->startIndex);

I have added some comments that discribes my errors and warnings.

Thanx for helping.

Regards, Bohdan Kit

Bohdan Kit

unread,
Apr 25, 2014, 7:08:11 AM4/25/14
to adwor...@googlegroups.com, web...@gmail.com
Hi, David.

Please, reply my previost reply in this theme. You or somebody else. I Added in that reply me Errors and Warnings in code.
There is developerToken, userAgent, clientCustomerId, email, password, [OAUTH2], client_id, client_secret, refresh_token in my auth.ini file.

Please help me.

Ray Tsang (AdWords API Team)

unread,
Apr 28, 2014, 4:59:54 PM4/28/14
to adwor...@googlegroups.com, web...@gmail.com
Bohdan,

Unfortuantely I won't be able to write the actual code.  What PHP version are you using?

Thanks,

Ray

Bohdan Kit

unread,
Apr 28, 2014, 6:00:28 PM4/28/14
to adwor...@googlegroups.com, web...@gmail.com
I'm using PHP 5.5

Ray Tsang (AdWords API Team)

unread,
Apr 29, 2014, 6:29:17 PM4/29/14
to adwor...@googlegroups.com, web...@gmail.com
Bohdan,

Could I trouble you to send me the full request/response XML by replying to author?  Please make sure sensitive data has been removed.

Thanks!

Ray

On Monday, April 28, 2014 6:00:28 PM UTC-4, Bohdan Kit wrote:
I'm using PHP 5.5

Ray Tsang (AdWords API Team)

unread,
Apr 30, 2014, 3:57:19 PM4/30/14
to adwor...@googlegroups.com, web...@gmail.com
Bohdan,

Thanks for sending me the fully request/response.  It appears that you are receiving OAUTH_TOKEN_INVALID error.  You'll need to make sure that you have OAuth 2.0 properly configured.  Please review our OAuth 2.0 guide as well as the specific guide for the PHP client library.

Thanks,

Ray
Reply all
Reply to author
Forward
0 new messages