Skip to content

Downloading Reports

Anash P. Oommen edited this page May 17, 2019 · 17 revisions

AdWords API: Downloading reports

You can download AdWords API report using the ReportUtilities class. The typical usage is shown below:

Downloading report to memory

ReportUtilities utilities = new ReportUtilities(user,
    "v201809", definition);
using (ReportResponse reportResponse = utilities.GetResponse()) {
  byte[] reportBytes = reportResponse.Download();
  string reportText = Encoding.UTF8.GetString(reportBytes);
}

If you were downloading in gzip format, this would look like:

ReportUtilities utilities = new ReportUtilities(user,
    "v201809", definition);
using (ReportResponse reportResponse = utilities.GetResponse()) {
  byte[] reportBytes = reportResponse.Download();
  string deflatedReportText = Encoding.UTF8.GetString(
      MediaUtilities.DeflateGZipData(reportResponse.Contents));
}

Downloading report to disk

ReportUtilities utilities = new ReportUtilities(user,
    "v201809", definition);
using (ReportResponse reportResponse = utilities.GetResponse()) {
  reportResponse.Save(filePath);
}

Streaming a report

ReportUtilities utilities = new ReportUtilities(user,
    "v201809", definition);
using (ReportResponse response = utilities.GetResponse()) {
  using (StreamReader reader = new StreamReader(response.Stream)) {
    // Process your report here.
  }
}

Downloading report asynchronously

ReportUtilities utilities = new ReportUtilities(user,
    "v201809", definition);
ReportResponse reportResponse = utilities.GetResponse();
reportResponse.OnSuccess += delegate() {
  // Process the file.
};
reportResponse.SaveAsync(filePath);

Google Ad Manager API: Downloading reports

You can download Google Ad Manager API report using the ReportUtilities class. The typical usage is shown below:

Run the report and set the download options

reportJob = reportService.runReportJob(reportJob);

ReportUtilities reportUtilities = new ReportUtilities(reportService, reportJob.id);
// Set download options.
ReportDownloadOptions options = new ReportDownloadOptions();
options.exportFormat = ExportFormat.CSV_DUMP;
options.useGzipCompression = true;
reportUtilities.reportDownloadOptions = options;

Downloading report to disk

using (ReportResponse reportResponse = reportUtilities.GetResponse()) {
  reportResponse.Save(filePath);
}

Streaming a report

using (ReportResponse response = reportUtilities.GetResponse()) {
  using (StreamReader reader = new StreamReader(response.Stream)) {
    // Process your report here.
  }
}

Downloading report asynchronously

ReportResponse reportResponse = reportUtilities.GetResponse();
reportResponse.OnSuccess += delegate() {
  // Process the file.
};
reportResponse.SaveAsync(filePath);

Working With Reports as Strongly-Typed Objects

For each row of a report, you can obtain strongly-typed objects as an XML stream using the AwReport and AwXmlTextReader classes.

Note: You need to set the UseRawEnumValues header when using this option. See an example.

Using Your Own Schema Classes

You can create classes to define custom report schemas. The properties in the class determine the columns selected from your report data

Define Schema

Each row of the report’s data will be held in an instance of a class you define. For example, the following is a class that holds three columns from your report data:

using Google.Api.Ads.AdWords.Util.Reports;
 
public class CriteriaReportRow {
 
  [ReportColumn("keywordID")]
  public long KeywordID { get; set; }
 
  [ReportColumn("impressions")]
  public long Impressions { get; set; }
 
  [ReportColumn("network")]
  public string NetworkType { get; set; }
 
  override public string ToString() {
    return "Id: " + KeywordID + " Impressions: "
      + Impressions + " NetworkType: " + NetworkType;
  }
}

Properties with public get and set are required to be annotated with ReportColumn. The parser uses this to match properties in your class with columns specified in the XML report data. The string provided to the ReportColumn constructor is the name of the column in the XML. If no name is provided, then the name of the property is used.

Download Report Data

Report data must be downloaded as XML and available as a Stream. You can use FileStreams created from XML report data files on disk, or you can download report data directly as a stream.

Create AwReport

With an XML stream of the report data and a report row type, you can create an AwReport instance.

var report = new AwReport<CriteriaReportRow>(new AwXmlTextReader(myStream), "ExampleReport");

Use the AwReport

The AwReport type implements the IEnumerator interface, but does not support the Reset() function. You can iterate through the report one row at a time, which allows you to consume report rows as they become available in a long stream using the IEnumerator functions.

while (report.MoveNext()) {
  Console.WriteLine(report.Current);
}

You can also obtain an IEnumerable of all rows using the GetRows() function or the Rows property. Note that the AwReport will be iterated to the end and cannot be reset if GetRows() or Rows is used, but GetRows() and Rows can be called repeatedly.

foreach (var record in report.Rows) {
  Console.WriteLine(record);
}

Using Predefined Report Row Classes

You can also use pre-defined report row types available for all Google Ads reports. If there are columns in the report row type that do not appear in your actual report data, those properties of the report row objects will hold the default values of their types.

For example, you can use the provided KeywordsPerformanceReportRow class to hold report data from the Keywords Performance Report:

var report = new AwReport<KeywordsPerformanceReportReportRow>(
    new AwXmlTextReader(myStream), "ExampleReport");

Code examples

You can find more reporting code examples below: