PHP: Get "exact monthly local traffic for keyword"-value from Adwords API

1,964 views
Skip to first unread message

jepster

unread,
Dec 23, 2011, 2:25:20 PM12/23/11
to AdWords API Forum
Hi,

there are lots of examples in ADWORDS-API-PHP-FOLDER/examples/v201109
- where is the example file, which gets the "exact monthly local
traffic value" for a keyword? I've tried the file
GetTrafficEstimates.php, but it isn't giving me the searched value.

Hope somebody can help me, I've spend a lot of time on that..

Evgeniy Bogdanov

unread,
Dec 24, 2011, 5:40:08 PM12/24/11
to AdWords API Forum
Hi.

This is part of my working code...

$returnKeywords = array();
// We need only keywords for United States
$countryTarget = new CountryTarget('US', false);
$countryTargetSearchParameter = new
CountryTargetSearchParameter();
$countryTargetSearchParameter->countryTargets =
array($countryTarget);

// We need only EXACT matches for keyword
$keywordMatch = new KeywordMatchTypeSearchParameter('EXACT');

// Describing selector
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes = array('CRITERION');

// We want to get only first 10 records, without keywords
which we already have
$paging = new Paging();
$paging->startIndex = (count($initialKeywords)-1);
$paging->numberResults = 10;
$selector->paging = $paging;

// Build array
$toCheckKeywords = array();
foreach ($initialKeywords as &$word) {
$toCheckKeywords[] = new Keyword($word, 'EXACT');
$returnKeywords[] = strtolower($word);
}

// Load related keywords only
$relatedToKeywordSearchParameter1 = new
RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter1->keywords =
$toCheckKeywords;
$selector->searchParameters =
array($relatedToKeywordSearchParameter1,
$countryTargetSearchParameter,
$keywordMatch);

// Get data from Google Adwords
$page = $this->targetingIdeaService->get($selector);

You need to check TARGETED_MONTHLY_SEARCHES parameter.

Regards,
Evgeniy.

jonasl

unread,
Dec 26, 2011, 9:22:57 AM12/26/11
to AdWords API Forum
Hello,

Thanks for sharing your code Evgeniy. I needed to change a few lines:

Remove the last line and add the following:

$user = new AdWordsUser();
// Log SOAP XML request and response.
$user->LogDefaults();
// Get the TargetingIdeaService.
$targetingIdeaService = $user->GetService('TargetingIdeaService',
'v201109');
// Get data from Google Adwords
$page = $targetingIdeaService->get($selector);

And change the line $selector->requestedAttributeTypes =
array('CRITERION'); to:
$selector->requestedAttributeTypes = array('CRITERION',
'AVERAGE_TARGETED_MONTHLY_SEARCHES');

Thanks
Jonas Lejon
http://triop.se
Message has been deleted

jepster

unread,
Dec 28, 2011, 5:14:36 PM12/28/11
to AdWords API Forum
Thank's for sharing your code.

I've tried your code and I get always the error.

Fatal error: Call to undefined function CountryTargetSearchParameter()

But the file TargetingIdeaService.php is required - there is it's
definition.



On 26 Dez., 15:22, jonasl <jonas+adwo...@triop.se> wrote:
> Hello,
>
> Thanks for sharing your code Evgeniy. I needed to change a few lines:
>
> Remove the last line and add the following:
>
>   $user = new AdWordsUser();
>   // Log SOAP XML request and response.
>   $user->LogDefaults();
>   // Get the TargetingIdeaService.
>   $targetingIdeaService = $user->GetService('TargetingIdeaService',
> 'v201109');
>   // Get data from Google Adwords
>   $page = $targetingIdeaService->get($selector);
>
> And change the line  $selector->requestedAttributeTypes =
> array('CRITERION'); to:
> $selector->requestedAttributeTypes = array('CRITERION',
> 'AVERAGE_TARGETED_MONTHLY_SEARCHES');
>
> Thanks
> Jonas Lejonhttp://triop.se

jepster

unread,
Dec 29, 2011, 3:35:33 AM12/29/11
to AdWords API Forum
I mean that the function CountryTargetSearchParameter() is defined in
the file TargetingIdeaService.php. So why this error?

Evgeniy Bogdanov

unread,
Dec 29, 2011, 1:29:38 PM12/29/11
to AdWords API Forum
My code were outdated. For not last version of API (which is currently
v201109).
I've researched this issue and this is new version of code that you
need, it's based on examples/v201109/GetRelatedKeywords.php, changes
are commented:

try {
// Get AdWordsUser from credentials in "../auth.ini"
// relative to the AdWordsUser.php file's directory.
$user = new AdWordsUser();

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

// Get the TargetingIdeaService.
$targetingIdeaService = $user->GetService('TargetingIdeaService',
'v201109');

// 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('CRITERION', 'AVERAGE_TARGETED_MONTHLY_SEARCHES');

// 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');

// Start changes
$locations = array();
$location = new Location();
$location->id = 2840; //Id can be found with examples/v201109/
GetLocationCriteria.php
// I've selected whole United States
$locations[] = $location;

$locationTargetParameter = new
LocationSearchParameter($locations); // Expects arra of locations

$selector->searchParameters =
array($relatedToKeywordSearchParameter,
$keywordMatchTypeSearchParameter, $locationTargetParameter);
// End changes

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

// Display related keywords.
if (isset($page->entries)) {
foreach ($page->entries as $targetingIdea) {
$data = MapUtils::GetMap($targetingIdea->data);
$keyword = $data['CRITERION']->value;
$averageMonthlySearches =
isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
printf("Keyword with text '%s', match type '%s', and average
monthly "
. "search volume '%s' was found.\n", $keyword->text,
$keyword->matchType, $averageMonthlySearches);
}
} else {
print "No related keywords were found.\n";
}
} catch (Exception $e) {
print $e->getMessage();
}

jepster

unread,
Dec 29, 2011, 7:25:11 PM12/29/11
to AdWords API Forum
Thank's for your answer, but I still cannot solve it.

The following is my whole code, which I get the error message
"Unmarshalling Error: cvc-type.2: The type definition cannot be
abstract for element ns1:searchParameters."


<?php
// require_once 'aw-api/src/Google/Api/Ads/AdWords/Lib/
AdWordsUser.php';

/**
* This example gets keywords related to a seed keyword.
*
* Tags: TargetingIdeaService.get
* Restriction: adwords-only
*
* PHP version 5
*
* 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
*
* 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.
*
* @package GoogleApiAdsAdWords
* @subpackage v201109
* @category WebServices
* @copyright 2011, Google Inc. All Rights Reserved.
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
License,
* Version 2.0
* @author Eric Koleda <api.e...@gmail.com>
*/

error_reporting(E_ALL);

// You can set the include path to src directory or reference
// AdWordsUser.php directly via require_once.
// $path = '/path/to/aw_api_php_lib/src';
$path = dirname(__FILE__) . '/../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once 'aw-api/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'aw-api/src/Google/Api/Ads/Common/Util/MapUtils.php';

try {
// Get AdWordsUser from credentials in "../auth.ini"
// relative to the AdWordsUser.php file's directory.
$user = new AdWordsUser();

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

// Get the TargetingIdeaService.
$targetingIdeaService = $user->GetService('TargetingIdeaService',
'v201109');

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

// START CHANGES

$locations = array();
$location = new Location();
$location->id = 2840; //Id can be found with examples/v201109/
GetLocationCriteria.php

$locations[] = $location;
$locationTargetParameter = new
LocationSearchParameter($locations); // Expects arra of locations
$selector->searchParameters =
array($relatedToKeywordSearchParameter,
$keywordMatchTypeSearchParameter, $locationTargetParameter);

// END CHANGES

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

// Display related keywords.
if (isset($page->entries)) {
foreach ($page->entries as $targetingIdea) {
$data = MapUtils::GetMap($targetingIdea->data);
$keyword = $data['CRITERION']->value;
$averageMonthlySearches =
isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
printf("Keyword with text '%s', match type '%s', and average
monthly "
. "search volume '%s' was found.\n", $keyword->text,
$keyword->matchType, $averageMonthlySearches);
}
} else {
print "No related keywords were found.\n";
}
} catch (Exception $e) {
print $e->getMessage();
}
?>

Evgeniy Bogdanov

unread,
Dec 30, 2011, 2:47:54 AM12/30/11
to AdWords API Forum
As I see you've deleted some important parts of code:

// Create selector. Important! What data you want to load?
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes =
array('CRITERION', 'AVERAGE_TARGETED_MONTHLY_SEARCHES');
// Set selector paging (required for targeting idea service). //
Important (how many keywords you want to load)
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;
// Create related to keyword search parameter. (Important - you
need to specify, what type of data you want to get - about keyword or
about URL)
$relatedToKeywordSearchParameter = new
RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);

Insert this code before // START CHANGES and it should work.

If you want to download data about 1 keyword, and do not want get
related keywords for your seed keyword - you need to change:
$selector->requestType = 'IDEAS';
to
$selector->requestType = 'STATS';

But loading data for 1 keyword is not economically effective as you
will be charged 6 API units for this action. (5 per call
TargetingIdeaService + 0.1 rounded to 1).
For example loading data for 10 keywords, you will be charged for 6
API units too.

In that case you better to review your requirements and create more
effective code.

Regards,
Evgeniy.
>  * @author     Eric Koleda <api.ekol...@gmail.com>

jepster

unread,
Dec 31, 2011, 4:01:20 PM12/31/11
to AdWords API Forum
Thank's for your answer.. but I'm sorry - it isn't still working.

I've added your code after

try {

before

// START CHANGES

like you wrote.

<?php
require_once 'aw-api/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'aw-api/src/Google/Api/Ads/Common/Util/MapUtils.php';
require_once 'aw-api/src/Google/Api/Ads/AdWords/v201109/
TargetingIdeaService.php';


try {
// Create selector. Important! What data you want to load?
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes =
array('CRITERION', 'AVERAGE_TARGETED_MONTHLY_SEARCHES');
/* Set selector paging (required for targeting idea service).
Important (how many keywords you want to load) */
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;
/* Create related to keyword search parameter. (Important - you
need to specify, what type of data you want to get - about keyword
or
about URL)
*/
$relatedToKeywordSearchParameter = new
RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);

Evgeniy Bogdanov

unread,
Jan 1, 2012, 4:27:19 AM1/1/12
to AdWords API Forum
You don't understand me.

Just copy and paster this code to your file and run it. Without any
chagnes, edits.
After you will get it to work - you can do it what ever you want.

<?php
require_once 'aw-api/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'aw-api/src/Google/Api/Ads/Common/Util/MapUtils.php';
require_once 'aw-api/src/Google/Api/Ads/AdWords/v201109/
TargetingIdeaService.php';

try {
$location->id = 2840; // Id can be found with examples/v201109/
?>
> ...
>
> продолжение »

jepster

unread,
Jan 1, 2012, 10:09:52 AM1/1/12
to AdWords API Forum
OK, it works now. Thank you!
> ...
>
> Erfahren Sie mehr »
Reply all
Reply to author
Forward
0 new messages