PHP Working Example of TrafficEstimatorService v201109

1,044 views
Skip to first unread message

Yakito

unread,
Jan 31, 2012, 7:17:34 PM1/31/12
to AdWords API Forum
Hello,
I am new to the AdWords API and I am trying to understand how to use
the different services. I am having problems with the
TrafficEstimatorService as there is no example in the current version
of the PHP API. When I try to use the previous version API I get some
error with the class "CountryTarget()" that does not exists in the
current version.

This is the code I am testing:

$path = dirname(__FILE__) . '/../../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'Google/Api/Ads/Common/Util/MapUtils.php';

// Constants used in the example.
define('PAGE_SIZE', 500);
/**
* Runs the example.
* @param AdWordsUser $user the user to run the example with
*/
function GetKeywordIdeasExample(AdWordsUser $user) {
// Get the service, which loads the required classes.
$targetingIdeaService = $user->GetService('TrafficEstimatorService',
'v201109');
$keywords = array();
$keywords[] = new Keyword('mars cruise', 'BROAD');
$keywords[] = new Keyword('cheap cruise', 'PHRASE');
$keywords[] = new Keyword('cruise', 'EXACT');

// Create a keyword estimate request for each keyword.
$keywordEstimateRequests = array();
foreach ($keywords as $keyword) {
$keywordEstimateRequest = new KeywordEstimateRequest();
$keywordEstimateRequest->keyword = $keyword;
$keywordEstimateRequests[] = $keywordEstimateRequest;
}

// Create ad group estimate requests.
$adGroupEstimateRequest = new AdGroupEstimateRequest();
$adGroupEstimateRequest->keywordEstimateRequests =
$keywordEstimateRequests;
$adGroupEstimateRequest->maxCpc = new Money(1000000);
$adGroupEstimateRequests = array($adGroupEstimateRequest);

// Create campaign estimate requests.
$campaignEstimateRequest = new CampaignEstimateRequest();
$campaignEstimateRequest->adGroupEstimateRequests =
$adGroupEstimateRequests;
$campaignEstimateRequest->targets = array(new CountryTarget('US'),
new LanguageTarget('en'));
$campaignEstimateRequests = array($campaignEstimateRequest);

// Create selector.
$selector = new TrafficEstimatorSelector();
$selector->campaignEstimateRequests = $campaignEstimateRequests;

// Get traffic estimates.
$result = $trafficEstimatorService->get($selector);

// Display traffic estimates.
if (isset($result)) {
$keywordEstimates =
$result->campaignEstimates[0]->adGroupEstimates[0]-
>keywordEstimates;
for ($i = 0; $i < sizeof($keywordEstimates); $i++) {
$keyword = $keywordEstimateRequests[$i]->keyword;
$keywordEstimate = $keywordEstimates[$i];

// Find the mean of the min and max values.
$meanAverageCpc = ($keywordEstimate->min->averageCpc-
>microAmount
+ $keywordEstimate->max->averageCpc->microAmount) / 2;
$meanAveragePosition = ($keywordEstimate->min->averagePosition
+ $keywordEstimate->max->averagePosition) / 2;
$meanClicks = ($keywordEstimate->min->clicks
+ $keywordEstimate->max->clicks) / 2;
$meanTotalCost = ($keywordEstimate->min->totalCost->microAmount
+ $keywordEstimate->max->totalCost->microAmount) / 2;

printf("Results for the keyword with text '%s' and match type
'%s':\n",
$keyword->text, $keyword->matchType);
printf(" Estimated average CPC: %.0f\n", $meanAverageCpc);
printf(" Estimated ad position: %.2f \n",
$meanAveragePosition);
printf(" Estimated daily clicks: %d\n", $meanClicks);
printf(" Estimated daily cost: %.0f\n\n", $meanTotalCost);
}
} else {
print "No traffic estimates were returned.\n";
}
}



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

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

// Run the example.
GetKeywordIdeasExample($user);
} catch (Exception $e) {
printf("An error has occurred: %s\n", $e->getMessage());
}

The error is on line 72, but remember thats an example from a previous
version of the API. Any tip in the right direction will be much
appreciated!

Thanks!

Evgeniy Bogdanov

unread,
Feb 1, 2012, 6:26:50 AM2/1/12
to AdWords API Forum
Hi.

You can find this example in older releases of Adwords API PHP
Library.
Main idea why old version is not works, in v201109 were changed idea
of setting targeting.
Now you need to specify not country, language or other string codes,
but integer IDs of them.

Here is example which I've:

<?php
/**
* This example gets keyword traffic estimates.
*
* Tags: TrafficEstimatorService.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_STRICT | 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 'Google/Api/Ads/AdWords/Lib/AdWordsUser.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 TrafficEstimatorService.
$trafficEstimatorService =
$user->GetService('TrafficEstimatorService', 'v201109');

// Create keywords. Up to 2000 keywords can be passed in a single
request.
$keywords = array();
$keywords[] = new Keyword('mars cruise', 'BROAD');
$keywords[] = new Keyword('cheap cruise', 'PHRASE');
$keywords[] = new Keyword('cruise', 'EXACT');

// Negative keywords don't return estimates, but adjust the
estimates of the
// other keywords in the hypothetical ad group.
$negativeKeywords = array();
$negativeKeywords[] = new Keyword('moon walk', 'BROAD');

// Create a keyword estimate request for each keyword.
$keywordEstimateRequests = array();
foreach ($keywords as $keyword) {
$keywordEstimateRequest = new KeywordEstimateRequest();
$keywordEstimateRequest->keyword = $keyword;
$keywordEstimateRequests[] = $keywordEstimateRequest;
}

// Create a keyword estimate request for each negative keyword.
foreach ($negativeKeywords as $negativeKeyword) {
$keywordEstimateRequest = new KeywordEstimateRequest();
$keywordEstimateRequest->keyword = $negativeKeyword;
$keywordEstimateRequest->isNegative = TRUE;
$keywordEstimateRequests[] = $keywordEstimateRequest;
}

// Create ad group estimate requests.
$adGroupEstimateRequest = new AdGroupEstimateRequest();
$adGroupEstimateRequest->keywordEstimateRequests =
$keywordEstimateRequests;
$adGroupEstimateRequest->maxCpc = new Money(1000000);

// Create campaign estimate requests.
$campaignEstimateRequest = new CampaignEstimateRequest();
$campaignEstimateRequest->adGroupEstimateRequests[] =
$adGroupEstimateRequest;

// Set targeting criteria. Only locations and languages are
supported.
$unitedStates = new Location();
$unitedStates->id = 2840;
$campaignEstimateRequest->criteria[] = $unitedStates;

$english = new Language();
$english->id = 1000;
$campaignEstimateRequest->criteria[] = $english;

// Create selector.
$selector = new TrafficEstimatorSelector();
$selector->campaignEstimateRequests[] = $campaignEstimateRequest;

// Get traffic estimates.
$result = $trafficEstimatorService->get($selector);

// Display traffic estimates.
if (isset($result)) {
$keywordEstimates =
$result->campaignEstimates[0]->adGroupEstimates[0]-
>keywordEstimates;
for ($i = 0; $i < sizeof($keywordEstimates); $i++) {
$keywordEstimateRequest = $keywordEstimateRequests[$i];
// Skip negative keywords, since they don't return estimates.
if (!$keywordEstimateRequest->isNegative) {
$keyword = $keywordEstimateRequest->keyword;
$keywordEstimate = $keywordEstimates[$i];

// Find the mean of the min and max values.
$meanAverageCpc = ($keywordEstimate->min->averageCpc-
>microAmount
+ $keywordEstimate->max->averageCpc->microAmount) / 2;
$meanAveragePosition = ($keywordEstimate->min->averagePosition
+ $keywordEstimate->max->averagePosition) / 2;
$meanClicks = ($keywordEstimate->min->clicksPerDay
+ $keywordEstimate->max->clicksPerDay) / 2;
$meanTotalCost = ($keywordEstimate->min->totalCost-
>microAmount
+ $keywordEstimate->max->totalCost->microAmount) / 2;

printf("Results for the keyword with text '%s' and match type
'%s':\n",
$keyword->text, $keyword->matchType);
printf(" Estimated average CPC: %.0f\n", $meanAverageCpc);
printf(" Estimated ad position: %.2f \n",
$meanAveragePosition);
printf(" Estimated daily clicks: %d\n", $meanClicks);
printf(" Estimated daily cost: %.0f\n\n", $meanTotalCost);
}
}
} else {
print "No traffic estimates were returned.\n";
}
} catch (Exception $e) {
print $e->getMessage();
}
?>

Regards,
Evgeniy.

Yakito

unread,
Feb 1, 2012, 11:42:23 AM2/1/12
to adwor...@googlegroups.com
Thanks Evgeniy! I did not knew about those changes! Thanks for your help =)

ClarityWebworks

unread,
Feb 5, 2016, 6:42:45 PM2/5/16
to AdWords API Forum
Will this code work now with v201601 of the TrafficEstimatorService?

Anthony Madrigal

unread,
Feb 8, 2016, 9:32:15 AM2/8/16
to AdWords API Forum
Hello,

I cannot say for sure whether or not this code will still work since it is a very old version. However, our v201601 client examples provide an example of how to use the service.

Cheers,
Anthony
AdWords API Team

vino...@skills-agency.com

unread,
Jan 19, 2022, 5:45:04 AM1/19/22
to Google Ads API and AdWords API Forum
Hi, 

Im using TrafficEstimatorService to find keywords Average CPC based on the location. In my case i want to pass multiple locations (Mostly postal codes). Can someone tell me how can i pass multiple location datas.

Google Ads API Forum Advisor

unread,
Jan 20, 2022, 1:20:03 AM1/20/22
to vino...@skills-agency.com, adwor...@googlegroups.com
Hi,

Thank you for reaching out to our API support team.

You can refer to the examples here on how you can include different or multiple criteria in your request. Also, if you are indeed using the PHP client library and if you are looking for a more location criteria specific example, I would recommend that you reach out instead to the PHP client library owners via this link, as they should be more equipped to provide guidance regarding this matter.

In addition, please note that the AdWords API is deprecated and will be sunset on April 27, 2022. We strongly recommend that you migrate to the Google Ads API as soon as possible.

Best regards,

Google Logo
Peter Laurence Napa Oliquino
Google Ads API Team
 


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