SOAP .NET Example 4: Retrieving data for specified dimensions and metrics
(Return to main article: Samples for .NET (SOAP) v1)
1. Include namespace to client application to use the types from generated class for ReportService API without specifying the namespace:
using SampleCode.com.google.DataAPI.ReportService;
2. Set up date format
private const string DATE_FORMAT = "yyyy-MM-dd";
3. Provide login and profile information:
private const string login = "YOUR_LOGIN"; private const string password = "YOUR_PASSWORD"; private const int profileId = 16; private const string startDate = "2000-01-01"; private const string endDate = "2009-01-01"; private const string dimensions = "u:visitor_id"; private const string metrics = "u:pages,u:visits"; private const int maxResults = 5;
4. Define a service connection for ReportService API:
reportservice reportService = new reportservice();
5. Retrieve and display data
- (implicit table id):
Data[] data = reportService.getData( login, password, profileId.ToString(), 0, false, 0, false, DateTime.ParseExact(startDate, DATE_FORMAT, null), DateTime.ParseExact(endDate, DATE_FORMAT, null), dimensions, metrics, null, null, 0, false); Console.WriteLine("Data retrieved with implicit table id for specific date range:"); foreach (Data item in data) { Console.WriteLine("Record id is \"" + item.recordId + "\", "); // Display data dimensions. Console.WriteLine("dimensions are:"); foreach (DataDimension dataDimension in item.dimensions) Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value); // Display data metrics (Hits, Bytes). Console.WriteLine("metrics are:"); Console.WriteLine("\tpages: " + item.metrics.pages + ", visits: " + item.metrics.visits); }
- (explicit table id)
Data[] data = reportService.getData( login, password, profileId.ToString(), 0, false, 0, false, DateTime.ParseExact(startDate, DATE_FORMAT, null), DateTime.ParseExact(endDate, DATE_FORMAT, null), dimensions, null, null, null, 258, true); Console.WriteLine("Data retrieved with explicit table id for specific date range w/o metrics:"); foreach (Data item in data) { Console.WriteLine("Record id is \"" + item.recordId + "\", "); // Display data dimensions. Console.WriteLine("dimensions are:"); foreach (DataDimension dataDimension in item.dimensions) Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value); }
- (limited list)
Data[] data = reportService.getData( login, password, profileId.ToString(), 0, false, maxResults, true, DateTime.ParseExact(startDate, DATE_FORMAT, null), DateTime.ParseExact(endDate, DATE_FORMAT, null), dimensions, null, null, null, 0, false); Console.WriteLine("Visitor list limited with max-records (" + maxResults + "):"); foreach (Data item in data) { Console.WriteLine("Record id is \"" + item.recordId + "\", "); // Display data dimensions. Console.WriteLine("dimensions are:"); foreach (DataDimension dataDimension in item.dimensions) Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value); }
- (sorted)
Data[] data = reportService.getData( login, password, profileId.ToString(), 0, false, 0, false, DateTime.ParseExact(startDate, DATE_FORMAT, null), DateTime.ParseExact(endDate, DATE_FORMAT, null), "u:transaction_id", "u:transactions,u:revenue", "-u:transactions", null, 258, true); Console.WriteLine("Transaction list sorted by Id desc:"); foreach (Data item in data) { Console.WriteLine("Record id is \"" + item.recordId + "\", "); // Display data dimensions. Console.WriteLine("dimensions are:"); foreach (DataDimension dataDimension in item.dimensions) Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value); // Display data metrics (Ttransactions, Revenue). Console.WriteLine("metrics are:"); Console.WriteLine("\ttransactions: " + item.metrics.transactions + ", revenue: " + item.metrics.revenue); }
- (Aggregated)
Data[] data = reportService.getData( login, password, profileId.ToString(), 0, false, maxResults, true, DateTime.ParseExact(startDate, DATE_FORMAT, null), DateTime.ParseExact(endDate, DATE_FORMAT, null), "u:month", "u:hits,u:bytes", "u:hits", null, 258, true); Console.WriteLine("Totals aggregated by month for specific date range sorted by one metric, limited with max-records (" + maxResults + "):"); foreach (Data item in data) { Console.WriteLine("Record id is \"" + item.recordId + "\", "); // Display data dimensions. Console.WriteLine("dimensions are:"); foreach (DataDimension dataDimension in item.dimensions) Console.WriteLine(" \tname: \"" + dataDimension.name + "\", string: " + dataDimension.Value); // Display data metrics (Hits, Bytes). Console.WriteLine("metrics are:"); Console.WriteLine("\thits: " + item.metrics.hits + ", bytes: " + item.metrics.bytes); }
Complete sample code for this example is available in the ReportServiceGetDataSOAP.cs file.
(Return to main article: Samples for .NET (SOAP) v1)