使用Adwords API在AWQL csv报告中将micro转换为货币

时间:2014-09-13 03:44:17

标签: php csv google-adwords adwords-budgetservice

我在AWQL中有这个查询,我使用ReportUtils::DownloadReportWithAwql

以CSV格式获得响应
select Date, Clicks, Cost from ACCOUNT_PERFORMANCE_REPORT during LAST_30_DAYS

我需要将帐户中的费用数据从微观转换为货币(费用/ 1000000)。

此外,我需要能够使用任何 AWQL查询转换响应中的任何成本数据,例如解决方案也必须适用于此查询:< / p>

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中的第一个问题。

1 个答案:

答案 0 :(得分: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