Sorry - clicked the post button prematurely there. The problem is that if I
var_dump($conversionreport->table); The actual data isn't included, just
the column headers:
object(SimpleXMLElement)#627 (1) { ["columns"]=>
object(SimpleXMLElement)#625 (1) { ["column"]=>
object(SimpleXMLElement)#687 (1) { ["@attributes"]=> array(2) { ["name"]=>
string(20) "conversionActionName" ["display"]=> string(22) "Conversion
action name" } } } }
How do I actually get the data?
Thanks very much everyone,
Ryan
On Friday, 14 September 2012 15:25:30 UTC+1, SpaceAndTime wrote:
>
> Hi everyone,
>
> I'm building a report for one of our huge clients to pull in data from our
> AdWords MCC and display it in their dashboard. I am using the PHP Client
> Library, with a mix of version 201109 for gathering the individual ad data
> and 201206 for (attempting to) gather the data on Conversion Types. What I
> an trying to achieve is to get the same data as is available when you go to
> My Client Centre > select the client > Select the campaign > Select the Ad
> Group > Manage Columns and add the Conversions > Conv. (many per-click)
> column to the display. We have this set up to track conversions such as
> 'Appointment Bookings', 'Request for Information' etc. My current code in
> it's entirety is below:
>
> Ad.php:
>
> <?php
> error_reporting(E_ALL);
> ini_set('max_execution_time', 2000); //300 seconds = 5 minutes
> ini_set('memory_limit', '4096M');
> set_time_limit(15000);
>
> $path = dirname(__FILE__) . '/src';
> set_include_path(get_include_path() . PATH_SEPARATOR . $path);
>
> require_once 'kint/Kint.class.php';
> require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
> require_once 'Google/Api/Ads/AdWords/Util/ReportUtils.php';
> require_once $_SERVER['DOCUMENT_ROOT'] . '/report.php';
>
> function getXML($user,$clientId='',$startDate='',$endDate='') {
> $start_time = microtime();
> $start_array = explode(" ",$start_time);
> $start_time = $start_array[1] + $start_array[0];
>
> if (!empty($startDate) && !empty($endDate)) {
> $date = explode('-',$startDate);
> $date2 = explode('-',$endDate);
>
> $sd = mktime(0, 0, 0, $date[1], $date[0], $date[2]);
> $ed = mktime(0, 0, 0, $date2[1], $date2[0], $date2[2]);
>
> if ($sd > $ed) {
> $tmp = $startDate;
> $startDate = $endDate;
> $endDate = $tmp;
> }
> }
>
> if (!empty($startDate)) {
> $date = explode('-',$startDate);
> if (is_array($date)) {
> $startDate = date("Ymd",mktime(0, 0, 0, $date[1], $date[0],
> $date[2]));
> }
> }
> else
> {
> $startDate = '19700101';
> }
>
> if (!empty($endDate)) {
> $date = explode('-',$endDate);
> if (is_array($date)) {
> $endDate = date("Ymd",mktime(0, 0, 0, $date[1], $date[0],
> $date[2]));
> }
> }
> else
> {
> $endDate = '20380101';
> }
>
> /*$conversionreport = DownloadDefinedReportExample($user,
> 'ACCOUNT_PERFORMANCE_REPORT', NULL);
> $conversionreport = simplexml_load_string($conversionreport);*/
>
> /*echo '<br /><br /><br />';*/
>
> $filename =
> "ryanad_".date('d_m_Y')."_".$startDate."_".$endDate."_".$clientId.".xml";
>
> if (is_file(dirname(__FILE__) . '/xmls/'.$filename) && 1==2) {
> $fh = fopen(dirname(__FILE__) . '/xmls/'.$filename, 'r');
> $res = fread($fh, filesize(dirname(__FILE__) . '/xmls/'.$filename));
> fclose($fh);
> }
> else {
>
> $campaignService = $user->GetService('CampaignService', 'v201109');
> $campaignService = $user->GetService('ServicedAccountService',
> 'v201109');
>
> $selector = new Selector();
> $accounts = $campaignService->get($selector);
>
> $counter = 0;
>
> $res="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
> $res.="<clients>\n";
> foreach ($accounts->accounts as $key=>$account) {
> if (empty($clientId) || (!empty($clientId) &&
> $account->customerId == $clientId)) {
> $res2 ="\t<client id=\"".$account->customerId."\"
> name=\"".str_replace('&','&',$account->companyName)."\">\n".
> "\t\t<campaigns>\n";
> $issetCompany = 0;
> $user->SetClientId($account->customerId);
> $campaignService2 = $user->GetService('CampaignService',
> 'v201109');
> $selector = new Selector();
> $selector->fields = array('Id', 'Name', 'Period','Status');
> //,'Cost'
> $selector->ordering[] = new OrderBy('Name', 'ASCENDING');
> $selector->dateRange = new DateRange($startDate, $endDate);
>
> $page = $campaignService2->get($selector);
>
> $number = 0;
>
> if(isset($page->entries)){
> foreach ($page->entries as $campaign) {
> if(isset($campaign->campaignStats) &&
> $campaign->status == 'ACTIVE' && $campaign->budget->period == 'DAILY') {
> $res2 .="\t\t\t<campaign>\n
> \t\t\t<id>".$campaign->id."</id>\n
>
> \t\t\t\t<title>".str_replace('&','&',$campaign->name)."</title>\n\t\t\t\t<ads>\n";
>
> $adGroupAdService =
> $user->GetService('AdGroupAdService', 'v201109');
>
> $selector = new Selector();
> $selector->fields = array('Headline',
> 'Id','Clicks','Ctr','Conversions');
> $selector->ordering[] = new
> OrderBy('Headline', 'ASCENDING');
> $selector->predicates[] = new
> Predicate('CampaignId', 'EQUALS', array($campaign->id));
> $selector->predicates[] = new
> Predicate('AdType', 'EQUALS', array('TEXT_AD'));
> $selector->predicates[] = new
> Predicate('Status', 'IN', array('ENABLED', 'PAUSED')); //'DISABLED'
> $selector->dateRange = new
> DateRange($startDate, $endDate);
>
> $page1 = $adGroupAdService->get($selector);
> if(isset($page1->entries)){
>
> foreach ($page1->entries as $ad) {
>
> $adId = $ad->ad->id;
> $adText = $ad->ad->headline;
> $adDesc1 = $ad->ad->description1;
> $adDesc2 = $ad->ad->description2;
> $adClicks = $ad->stats->clicks;
> $adCtr = $ad->stats->ctr;
> $adConv = $ad->stats->conversions;
>
> echo $counter . ': ' .
> $account->customerId . ' - ' . $campaign->id . ' - ' . $campaign->name . '
> - ' . $ad->ad->id . ' - ' . $ad->ad->headline . ' - <b>' . $adConv .
> '</b><br /><script type="text/javascript">window.scrollTo(0,
> document.body.scrollHeight);</script>';
>
> if($adConv > 0){
>
> $conversionreport =
> DownloadCriteriaReportExample($user, NULL, $campaign->id, $ad->ad->id,
> $startDate, $endDate);
> $conversionreport =
> simplexml_load_string($conversionreport);
>
> //var_dump($ad);
> var_dump($conversionreport->table);
>
> $counter++;
>
> flush();
>
> }
>
> if($counter > 20){ die(); }
>
> $res2 .="\t\t\t\t\t<ad>\n
>
> \t\t\t\t\t\t<id>".$adId."</id>\n
>
> \t\t\t\t\t\t<text>".str_replace('&','&',$adText)."</text>\n
>
> \t\t\t\t\t\t<desc1>".str_replace('&','&',$adDesc1)."</desc1>\n
>
> \t\t\t\t\t\t<desc2>".str_replace('&','&',$adDesc2)."</desc2>\n
>
> \t\t\t\t\t\t<clicks>".$adClicks."</clicks>\n
>
> \t\t\t\t\t\t<ctr>".$adCtr."</ctr>\n
>
> \t\t\t\t\t\t<convPerClick>".$adConv."</convPerClick>\n
> \t\t\t\t\t</ad>\n";
> }
> }
>
> $res2 .="\t\t\t\t</ads>\n\t\t\t</campaign>\n";
> $issetCompany = 1;
> }
> }
> }
> /*if ($key == 10) {
> $end_time = microtime();
> $end_array = explode(" ",$end_time);
> $end_time = $end_array[1] + $end_array[0];
> echo $time = $end_time - $start_time;
> exit;
> } */
> $res2.="\t\t</campaigns>\n
> \t</client>\n";
> if (!empty($issetCompany)) {
> $res.= $res2;
> }
> }
> }
> $res.="</clients>\n";
>
>
> /*$handle = fopen(dirname(__FILE__) . '/xmls/'.$filename, "w+");
> fwrite($handle,$res);
> fclose($handle);*/
> }
> /*Header('Content-Type: text/xml; charset="UTF-8');
> Header('Content-Length: '.strval(strlen($res)));
> echo $res;*/
> exit;
> }
>
> try {
>
> $user = new AdWordsUser();
>
> $user->LogAll();
> $user->SetClientId('xxx-xxx-xxxx');
>
> $clientId = !empty($_GET['client']) ?
> str_replace('-','',$_GET['client']) : '';
> $from = !empty($_GET['from']) ? $_GET['from'] : '';
> $to = !empty($_GET['to']) ? $_GET['to'] : '';
>
> getXML($user,$clientId,$from,$to);
>
> } catch (Exception $e) {
> printf("An error has occurred: %s\n", $e->getMessage());
> }
>
> Report.php:
>
> <?php
>
> /**
> * Runs the example.
> * @param AdWordsUser $user the user to run the example with
> * @param string $filePath the path of the file to download the report to
> */
> function DownloadCriteriaReportExample(AdWordsUser $user, $filePath,
> $campaignid, $adgroupid, $startDate, $endDate) {
> // Load the service, so that the required classes are available.
> $user->LoadService('ReportDefinitionService', 'v201206');
>
> // Create selector.
> $selector = new Selector();
> $selector->fields = array('ConversionTypeName');
> $selector->predicates[] = new Predicate('CampaignId', 'EQUALS',
> array($campaignid));
> $selector->predicates[] = new Predicate('Id', 'EQUALS',
> array($adgroupid));
> $selector->dateRange = new DateRange($startDate, $endDate);
>
> // Create report definition.
> $reportDefinition = new ReportDefinition();
> $reportDefinition->selector = $selector;
> $reportDefinition->reportName = 'Ad Performance Report' . $rand;
> $reportDefinition->dateRangeType = 'CUSTOM_DATE';
> $reportDefinition->reportType = 'AD_PERFORMANCE_REPORT';
> $reportDefinition->downloadFormat = 'XML';
>
> // Exclude criteria that haven't recieved any impressions over the date
> range.
> $reportDefinition->includeZeroImpressions = TRUE;
>
> // Set additional options.
> $options = array('version' => 'v201206', 'returnMoneyInMicros' => TRUE);
>
> // Download report.
> $report = ReportUtils::DownloadReport($reportDefinition, $filePath,
> $user, $options);
>
> return $report;
>
> }
>
>
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
http://adwordsapi.blogspot.com
http://groups.google.com/group/adwords-api
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en