-
Notifications
You must be signed in to change notification settings - Fork 199
Downloading Reports
You can download AdWords API report using the ReportUtilities class. The typical usage is shown below:
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));
}
ReportUtilities utilities = new ReportUtilities(user,
"v201809", definition);
using (ReportResponse reportResponse = utilities.GetResponse()) {
reportResponse.Save(filePath);
}
ReportUtilities utilities = new ReportUtilities(user,
"v201809", definition);
using (ReportResponse response = utilities.GetResponse()) {
using (StreamReader reader = new StreamReader(response.Stream)) {
// Process your report here.
}
}
ReportUtilities utilities = new ReportUtilities(user,
"v201809", definition);
ReportResponse reportResponse = utilities.GetResponse();
reportResponse.OnSuccess += delegate() {
// Process the file.
};
reportResponse.SaveAsync(filePath);
You can download Google Ad Manager API report using the ReportUtilities class. The typical usage is shown below:
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;
using (ReportResponse reportResponse = reportUtilities.GetResponse()) {
reportResponse.Save(filePath);
}
using (ReportResponse response = reportUtilities.GetResponse()) {
using (StreamReader reader = new StreamReader(response.Stream)) {
// Process your report here.
}
}
ReportResponse reportResponse = reportUtilities.GetResponse();
reportResponse.OnSuccess += delegate() {
// Process the file.
};
reportResponse.SaveAsync(filePath);
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.
You can create classes to define custom report schemas. The properties in the class determine the columns selected from your report data
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.
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.
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");
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);
}
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");
You can find more reporting code examples below: