How to generate csv with awql report?

226 views
Skip to first unread message

Ken Dan Tinio

unread,
Jan 11, 2018, 3:24:33 PM1/11/18
to AdWords API Forum

Download a criteria performance report with AWQL

Reference: https://developers.google.com/adwords/api/docs/samples/php/reporting#download-a-criteria-performance-report-with-awql

This is my code:

    public static function runExample(AdWordsSession $session, $reportFormat) 
    {
        // Create report query to get the data for last 7 days.
        $reportQuery = 'SELECT CampaignId, AdGroupId, Id, Criteria, CriteriaType, '
            . 'Impressions, Clicks, Cost FROM CRITERIA_PERFORMANCE_REPORT '
            . 'WHERE Status IN [ENABLED, PAUSED] DURING LAST_7_DAYS';

        // Download report as a string.
        $reportDownloader = new ReportDownloader($session);
        // Optional: If you need to adjust report settings just for this one
        // request, you can create and supply the settings override here. Otherwise,
        // default values from the configuration file (adsapi_php.ini) are used.
        $reportSettingsOverride = (new ReportSettingsBuilder())
            ->includeZeroImpressions(false)
            ->build();
        $reportDownloadResult = $reportDownloader->downloadReportWithAwql($reportQuery, $reportFormat, $reportSettingsOverride);
        
        self::$report = $reportDownloadResult->getAsString();
        self::$status  = "success";
        self::$message = "";
    }

    public static function main() 
    {
        // Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->fromFile()
            ->build();

        // See: AdWordsSessionBuilder for setting a client customer ID that is
        // different from that specified in your adsapi_php.ini file.
        // Construct an API session configured from a properties file and the OAuth2
        // credentials above.
        $session = (new AdWordsSessionBuilder())
            ->fromFile()
            ->withOAuth2Credential($oAuth2Credential)
            ->build();

        try {
            self::runExample($session, DownloadFormat::CSV);
        } catch (\Exception $e) {
            return array(
                "status" => "fail",
                "message" => $e->getMessage()
            );
        }
        
        return array(
            "status" => self::$status,
            "report" => self::$report,
        );
    }


How do I generate the csv?


Peter Oliquino

unread,
Jan 12, 2018, 1:31:39 AM1/12/18
to AdWords API Forum
Hi Ken,

Running this PHP example should already generate a report in CSV format. You may specify the type of the report using the "DownloadFormat::CSV" part of the code. You may also refer to this guide for the supported download types.

This said, could you confirm if the generated report is indeed already in CSV?

Best regards,
Peter
AdWords API Team

Ken Dan Tinio

unread,
Jan 12, 2018, 1:34:50 AM1/12/18
to AdWords API Forum
No it doesn't generate any file. it just return text, not in array.

Peter Oliquino

unread,
Jan 12, 2018, 1:58:46 AM1/12/18
to AdWords API Forum
Hi Ken,

My apologies for not noticing earlier. You will need to remove the "getAsString()" code as this allows the user to download the report as a string object instead of an actual file. Let me know if this helps.

Thanks and regards,
Peter
AdWords API Team

Ken Dan Tinio

unread,
Jan 12, 2018, 5:24:47 AM1/12/18
to AdWords API Forum
Where can I set the path?

Ken Dan Tinio

unread,
Jan 12, 2018, 5:27:21 AM1/12/18
to AdWords API Forum
I got it. The correct answer is,

$reportDownloadResult->saveToFile('docs/test.csv')

Thank you!
Reply all
Reply to author
Forward
0 new messages