2014-09-13 50 views
1

我在AWQL此查询,我得到使用ReportUtils::DownloadReportWithAwql转换万分之一到货币AWQL CSV报告与Adwords API

select Date, Clicks, Cost from ACCOUNT_PERFORMANCE_REPORT during LAST_30_DAYS 

我需要将成本数据的CSV从转换成CSV格式的响应微分到账户中的货币(Cost/1000000)。

此外,我需要能够以任何成本数据转换使用任何 AWQL查询的响应,例如解决方案必须为这个查询还工作:

SELECT CampaignName, KeywordText, Cost, CostPerConversion, QualityScore FROM KEYWORDS_PERFORMANCE_REPORT DURING LAST_7_DAYS 

由于v201406的,returnMoneyInMicros标题不再有效,并且值总是以微格形式返回。 https://developers.google.com/adwords/api/docs/guides/reporting-concepts#money

这是我的第一个问题在stackoverflow。

+0

@drep我做2个查询和结果是[这里](https://gist.github.com/mayeco/1904e3d8243c62a129ea)现在想象你在$变量2,一个$变量1和cost2 COST1。如何使用相同的函数将变量中的成本和成本/转换列除以1000000? – mayeco 2014-09-13 04:44:01

回答

0

最后我做到了,对我很好。

//data is a string with data in micros in csv format 
    $data = $this->DownloadCriteriaReportWithAwql($awql); 

    //start micros conversion 
    $count = 0; 
    $costpos = array(); 
    $newarray = array(); 
    foreach(preg_split("/((\r?\n)|(\r\n?))/", $data) as $line){ 

     $linearray = str_getcsv($line); 

     if($count == 0) { 
      //adwords report title 
      $newarray[] = $linearray; 
      $count++; 
      continue; 
     } 

     if($count == 1) { 
      //columns report header 
      $postvalue = 0; 

      foreach($linearray as $value){ 
       if (strpos($value,'Cost') !== false) { 
        $costpos[] = $postvalue; 
       } 
       $postvalue++; 
      } 

      $newarray[] = $linearray; 
      $count++; 
      continue; 
     } 

     if(!empty($costpos)) { 

      foreach($costpos as $costpostval){ 

       if(isset($linearray[$costpostval])) { 
        $linearray[$costpostval] = $linearray[$costpostval]/1000000; 
       } 

      } 
     } 

     $newarray[] = $linearray; 
     $count++; 
    } 


    $tmpfname = tempnam(sys_get_temp_dir(), "FOO"); 
    $outstream = fopen($tmpfname, "r+"); 
    foreach($newarray as $newline){ 
     fputcsv($outstream, $newline); 
    } 
    fclose($outstream); 
    //end micros conversion - $tmpfname temp file with cost in currency csv formated