REST .NET Example 4: Retrieving data for specified dimensions and metrics
(Return to main article: Samples for .NET (REST) v1)
1. Specify Service URL:
private const string serviceURL = "http://SERVER_NAME:URCHIN_PORT/services/v1/reportservice/data";
2. Specify login, profile, date, metrics and dimensions:
// Set up date format. private const string DATE_FORMAT = "yyyy-MM-dd"; // Provide login 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 table = "258"; private const string dimensions = "u:visitor_id"; private const string metrics = "u:pages,u:visits"; private const int maxResults = 5;
3. Compose REST URL:
- Retrieve data with implicit table id for specific date range.
string url = serviceURL + "?login=" + login + "&password=" + password + "&ids=" + profileId; url += "&start-date=" + startDate + "&end-date=" + endDate; url += "&dimensions=" + dimensions + "&metrics=" + metrics;
- Retrieve data with explicit table id for specific date range w/o metrics.
string url = serviceURL + "?login=" + login + "&password=" + password + "&ids=" + profileId; url += "&start-date=" + startDate + "&end-date=" + endDate; url += "&dimensions=" + dimensions + "&table=" + table;
- Retrieve visitor list limited with max-records.
string url = serviceURL + "?login=" + login + "&password=" + password + "&ids=" + profileId; url += "&start-date=" + startDate + "&end-date=" + endDate; url += "&dimensions=" + dimensions + "&max-results=" + maxResults;
- Retrieve transaction list sorted by Id (descending order).
string url = serviceURL + "?login=" + login + "&password=" + password + "&ids=" + profileId; url += "&start-date=" + startDate + "&end-date=" + endDate; url += "&dimensions=u:transaction_id" + "&table=258"; url += "&metrics=u:transactions,u:revenue"; url += "&sort=-u:transactions";
- Retrieve totals aggregated by month for specific date range sorted by one metric, limited with max-records.
string url = serviceURL + "?login=" + login + "&password=" + password + "&ids=" + profileId; url += "&start-date=" + startDate + "&end-date=" + endDate; url += "&dimensions=u:month" + "&table=total"; url += "&metrics=u:hits,u:bytes"; url += "&sort=u:hits" + "&max-results=" + maxResults;
4. Retrieve data:
XPathDocument xPathDocument = new XPathDocument(url);
5. Parse response:
XPathNavigator xPathNavigator = xPathDocument.CreateNavigator(); XmlNameTable xmlNameTable = new NameTable(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlNameTable); xmlNamespaceManager.AddNamespace("tns", "https://urchin.com/api/urchin/v1/"); // Get all data. XPathNodeIterator recordXPathNodeIterator = xPathNavigator.Select("/tns:getDataResponse/record", xmlNamespaceManager);
6. Display the information:
// Display profile data. DisplayDataRecordList(recordXPathNodeIterator); .... // Function to display retrieved profile data. private static void DisplayDataRecordList(XPathNodeIterator recordXPathNodeIterator) { while (recordXPathNodeIterator.MoveNext()) { Console.WriteLine("Record id is \"" + recordXPathNodeIterator.Current.SelectSingleNode("recordId/text()") + "\", "); // Display the table dimensions. Console.WriteLine("dimensions are:"); XPathNodeIterator dimensionsXPathNodeIterator = recordXPathNodeIterator.Current.Select("dimensions/dimension"); while (dimensionsXPathNodeIterator.MoveNext()) Console.WriteLine(" \tname: \"" + dimensionsXPathNodeIterator.Current.SelectSingleNode("@name") + "\", value: " + dimensionsXPathNodeIterator.Current.SelectSingleNode("text()")); // Display the table metrics. Console.WriteLine("metrics are:"); XPathNodeIterator metricsXPathNodeIterator = recordXPathNodeIterator.Current.Select("metrics/*"); while (metricsXPathNodeIterator.MoveNext()) Console.WriteLine(" \tname: \"" + metricsXPathNodeIterator.Current.LocalName + "\", value: " + metricsXPathNodeIterator.Current.SelectSingleNode("text()")); } }
The full sample code for this example is available in ReportServiceGetDataREST.cs file.