簡體   English   中英

使用Adwords API在AWQL CSV報告中將微米轉換為貨幣

[英]Convert micros to currency in AWQL csv report with Adwords API

我在AWQL中有此查詢,並且使用ReportUtils :: DownloadReportWithAwql以CSV格式獲取響應

select Date, Clicks, Cost from ACCOUNT_PERFORMANCE_REPORT during LAST_30_DAYS

我需要將CSV中的“ 費用”數據從微米轉換為帳戶中的貨幣 (“費用/ 1000000”)。

另外,我還需要能夠使用任何 AWQL查詢在響應中轉換任何Cost數據,例如,該解決方案也必須適用於該查詢:

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

從v201406開始,returnMoneyInMicros標頭不再有效,並且值始終以micros形式返回。 https://developers.google.com/adwords/api/docs/guides/reporting-concepts#money

這是我在stackoverflow中的第一個問題。

最終我做到了,對我來說很棒。

    //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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM