$ yii kw/stats
Warning: [CollectionSizeError.TOO_MANY @ selector.searchParameters[1].queries]
Error: Giving up.
function actionStats() {
// 800 limit: https://developers.google.com/adwords/api/docs/appendix/limits#targetingidea
$keywords = Keyword::find()->orderBy('modified_on')->limit(800)->all();
$this->targetingIdeaService('STATS', $keywords);
}
function targetingIdeaService($requestType, $keywords) {
$gaw = new GoogleAdWords();
$user = $gaw->getUser();
// Get the service, which loads the required classes.
$targetingIdeaService = $user->GetService('TargetingIdeaService');
// Create selector.
$selector = new TargetingIdeaSelector();
$selector->requestType = $requestType;
$selector->ideaType = 'KEYWORD';
// 'TARGETED_MONTHLY_SEARCHES' returns monthly historical data
$selector->requestedAttributeTypes = array('KEYWORD_TEXT', 'SEARCH_VOLUME', 'CATEGORY_PRODUCTS_AND_SERVICES', 'AVERAGE_CPC', 'COMPETITION');
// Create language search parameter (optional).
// The ID can be found in the documentation:
// https://developers.google.com/adwords/api/docs/appendix/languagecodes
// Note: As of v201302, only a single language parameter is allowed.
$languageParameter = new LanguageSearchParameter();
$english = new Language();
$english->id = 1000;
$languageParameter->languages = array($english);
// Create related to query search parameter. Keyword goes in here.
$relatedToQuerySearchParameter = new RelatedToQuerySearchParameter();
// Set the network.
$networkSearchParameter = new NetworkSearchParameter();
$networkSetting = new NetworkSetting();
$networkSetting->targetGoogleSearch = true;
$networkSetting->targetSearchNetwork = false;
$networkSetting->targetContentNetwork = false;
$networkSetting->targetPartnerNetwork = false;
$networkSearchParameter->networkSetting = $networkSetting;
// Set the location parameter
$location = new Location(); // USA // https://developers.google.com/adwords/api/docs/appendix/geotargeting
$location->id = 2840;
$locationParameter = new LocationSearchParameter();
$locationParameter->locations = array($location);
// $categoryProductsAndServicesSearchParameter = new CategoryProductsAndServicesSearchParameter();
// Set all the search parameters to the Selector
$selector->searchParameters[] = $relatedToQuerySearchParameter;
$selector->searchParameters[] = $languageParameter;
$selector->searchParameters[] = $networkSearchParameter;
$selector->searchParameters[] = $locationParameter;
$relatedToQuerySearchParameter->queries = array();
$index = 0;
foreach ($keywords as $keyword) {
$relatedToQuerySearchParameter->queries[] = $keyword->keyword;
// echo "$index: $keyword->keyword\n"; $index++;
}
// Set selector paging (required by this service).
$selector->paging = new Paging(0, max(AdWordsConstants::RECOMMENDED_PAGE_SIZE, 800));
do {
// Make the get request.
try {
$retry = false;
$page = $targetingIdeaService->get($selector);
} catch (\SoapFault $e) {
Yii::error($e->getMessage());
echo ("Warning: ".$e->getMessage().PHP_EOL);
[Aug 25 2016 18:33:08.000000 - ERROR] effectiveUser=999999999 service=TargetingIdeaService method=get operators={} responseTime=163 requestId=00053aecfb454cc80ac15e28de0d2694 operations=1 units= server=adwords.google.com isFault=true faultMessage=[CollectionSizeError.TOO_MANY @ selector.searchParameters[1].queries]