// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v201306;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.lib.utils.v201306.ReportDownloadResponse;
import com.google.api.adwords.lib.utils.v201306.ReportUtils;
import com.google.api.adwords.v201306.jaxb.cm.DownloadFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
/**
* This example downloads a keyword performance report with AWQL.
*
* @category adx-exclude
* @author api.thagikura@gmail.com (Takeshi Hagikura)
*/
public class DownloadAdHocReportWithAwql {
public static void main(String[] args) {
try {
// Log SOAP XML request and response.
AdWordsServiceLogger.log();
// Get AdWordsUser from "~/adwords.properties".
AdWordsUser user = new AdWordsUser();
// Location to download report to.
String reportFile = "/tmp/report.csv";
String query =
"SELECT AdGroupId, Id, KeywordText, KeywordMatchType, Impressions, Clicks, Cost "
+ "FROM KEYWORDS_PERFORMANCE_REPORT WHERE Impressions > 0 DURING YESTERDAY";
FileOutputStream fos = new FileOutputStream(new File(reportFile));
ReportDownloadResponse response =
ReportUtils.downloadReport(user, query, DownloadFormat.CSV, fos);
if (response.getHttpStatus() == HttpURLConnection.HTTP_OK) {
System.out.println("Report successfully downloaded: " + reportFile);
} else {
System.out.println("Report was not downloaded. " + response.getHttpStatus() + ": "
+ response.getHttpResponseMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}