get($session, TrafficEstimatorService::class); // Create keywords. Up to 2000 keywords can be passed in a single request. $keywords=getKeywords() ; // Create a keyword estimate request for each keyword. $keywordEstimateRequests = []; foreach ($keywords as $keyword) { $keywordEstimateRequest = new KeywordEstimateRequest(); $keywordEstimateRequest->setKeyword($keyword); $keywordEstimateRequests[] = $keywordEstimateRequest; } // Negative keywords don't return estimates, but adjust the estimates of the // other keywords in the hypothetical ad group. /* $negativeKeywords = []; $keyword = new Keyword(); $keyword->setText('moon walk'); $keyword->setMatchType(KeywordMatchType::BROAD); $negativeKeywords[] = $keyword; // Create a keyword estimate request for each negative keyword. foreach ($negativeKeywords as $negativeKeyword) { $keywordEstimateRequest = new KeywordEstimateRequest(); $keywordEstimateRequest->setKeyword($negativeKeyword); $keywordEstimateRequest->setIsNegative(true); $keywordEstimateRequests[] = $keywordEstimateRequest; } */ // Create ad group estimate requests. $adGroupEstimateRequest = new AdGroupEstimateRequest(); $adGroupEstimateRequest->setKeywordEstimateRequests( $keywordEstimateRequests); $money = new Money(); $money->setMicroAmount(1000000); $adGroupEstimateRequest->setMaxCpc($money); // Create campaign estimate requests. $campaignEstimateRequest = new CampaignEstimateRequest(); $campaignEstimateRequest->setAdGroupEstimateRequests( [$adGroupEstimateRequest]); // Optional: Set additional criteria for filtering estimates. // See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html // for a detailed list of country codes. // Set targeting criteria. Only locations and languages are supported. $usa = new Location(); $usa->setId(2840); $canada = new Location(); $canada->setId(2124); $campaignEstimateRequest->setCriteria([$usa, $canada]); // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html // for a detailed list of language codes. $english = new Language(); $english->setId(1000); $campaignEstimateRequest->setCriteria([$english]); // Create selector. $selector = new TrafficEstimatorSelector(); $selector->setCampaignEstimateRequests([$campaignEstimateRequest]); // Optional: Request a list of campaign level estimates segmented by // platform. $selector->setPlatformEstimateRequested(true); $result = $trafficEstimatorService->get($selector); /* $platformEstimates = $result->getCampaignEstimates()[0]->getPlatformEstimates(); if ($platformEstimates !== null) { foreach ($platformEstimates as $platformEstimate) { if ($platformEstimate->getMinEstimate() !== null && $platformEstimate->getMaxEstimate() !== null) { printf( "Results for the platform with ID %d and name '%s':\n", $platformEstimate->getPlatform()->getId(), $platformEstimate->getPlatform()->getPlatformName() ); self::printMeanEstimate($platformEstimate->getMinEstimate(), $platformEstimate->getMaxEstimate()); } } } */ printf("") ; $keywordEstimates = $result ->getCampaignEstimates()[0] ->getAdGroupEstimates()[0] ->getKeywordEstimates(); $estimatesCount = count($keywordEstimates); for ($i = 0; $i < $estimatesCount; $i++) { $keywordEstimateRequest = $keywordEstimateRequests[$i]; // Skip negative keywords, since they don't return estimates. if ($keywordEstimateRequest->getIsNegative() !== true) { $keyword = $keywordEstimateRequest->getKeyword(); $keywordEstimate = $keywordEstimates[$i]; if ($keywordEstimate->getMin() !== null && $keywordEstimate->getMax() !== null) { // Print the mean of the min and max values. printf( "", $keyword->getText(), strtolower($keyword->getMatchType()) ); self::printMeanEstimate($keywordEstimate->getMin(), $keywordEstimate->getMax()); printf("") ; } } } printf("
KeywordMatch TypeClicks Per DayAverage Cpc
%s%s
") ; } /** * Prints estimated average CPC, ad position, daily clicks, and daily costs * between the provided lower bound and upper bound of estimated stats. * * @param StatsEstimate $minEstimate the lower bound on the estimated stats * @param StatsEstimate $maxEstimate the upper bound on the estimated stats */ private static function printMeanEstimate(StatsEstimate $minEstimate, StatsEstimate $maxEstimate) { $meanAverageCpc = self::calculateMeanMicroAmount($minEstimate->getAverageCpc(), $maxEstimate->getAverageCpc()); $meanAveragePosition = self::calculateMean($minEstimate->getAveragePosition(), $maxEstimate->getAveragePosition()); $meanClicks = self::calculateMean($minEstimate->getClicksPerDay(), $maxEstimate->getClicksPerDay()); $meanTotalCost = self::calculateMeanMicroAmount($minEstimate->getTotalCost(), $maxEstimate->getTotalCost()); /* printf(" Estimated average CPC: %s\n", self::formatMean($meanAverageCpc)); printf(" Estimated ad position: %s\n", self::formatMean($meanAveragePosition)); printf(" Estimated daily clicks: %s\n", self::formatMean($meanClicks)); printf(" Estimated daily cost: %s\n\n", self::formatMean($meanTotalCost)); */ printf("%s%s", self::formatMean($meanClicks), self::formatMean($meanAverageCpc/1000000)) ; } /** * Returns the mean of the two numbers if neither is null, else returns null. */ private static function calculateMean($min, $max) { if ($min === null || $max === null) { return null; } return ($min + $max) / 2; } /** * Returns the mean of the two object's microAmounts if neither is null, else * returns null. */ private static function calculateMeanMicroAmount($min, $max) { if ($min === null || $max === null) { return null; } if ($min->getMicroAmount() === null || $max->getMicroAmount() === null) { return null; } return ($min->getMicroAmount() + $max->getMicroAmount()) / 2; } /** * Returns a formatted version of the mean value, handling nulls. */ private static function formatMean($mean) { if ($mean === null) { return 'null'; } return sprintf("%.2f", $mean); } 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); } } class seedKeyword { var $keyword="" ; var $matchType="" ; } class OutputKeyword { var $keywordName=""; var $avgCpc=0.0 ; var $avgPos=0.0 ; var $clicks=0 ; var $cost=0.0 ; } function getKeywords() { $seedKeywords=array( array("quin hotel ny","Exact"), array("nyc quin hotel","Exact"), array("nyc the quin hotel","Phrase"), array("ny the quin hotel","Phrase"), array("quin hotel nyc","Exact"), array("quin hotel","Exact"), array("the quin hotel ny","Exact"), array("ny quin hotel","Exact"), array("the quin hotel nyc","Phrase"), array("the quin","Exact"), array("nyc the quin hotel","Exact"), array("the quin hotel new york","Phrase"), array("quin hotel ny","Phrase"), array("quin hotel","Phrase"), array("quin hotel nyc","Phrase"), array("the quin","Phrase"), array("quin hotel new york","Phrase"), array("the quin hotel","Exact"), array("nyc quin hotel","Phrase"), array("ny quin hotel","Phrase"), array("ny the quin hotel","Exact"), array("the quin hotel","Phrase"), array("the quin hotel nyc","Exact"), array("the quin hotel ny","Phrase") ) ; $keywods=[] ; foreach ($seedKeywords as $seed) { $keyword = new Keyword(); $keyword->setText($seed[0]); $keyword->setMatchType(getMatchType($seed[1])) ; $keywords[] = $keyword; } return ($keywords) ; } function getMatchType($type) { if (strtolower($type)=="phrase") return (KeywordMatchType::PHRASE) ; if (strtolower($type)=="broad") return (KeywordMatchType::BROAD) ; if (strtolower($type)=="exact") return (KeywordMatchType::EXACT) ; } EstimateKeywordTraffic::main(); ?>