REST Example 4 (v1): Retrieving data for specified dimensions and metrics

(Return to main article: Samples for Java (REST) v1)

1. Specify Service URL:

 

private static final String serviceURL =  "http://server[:port]/services/v1/reportservice/tables/";

2. Provide login and account information:

 

private static final String login = "INSERT_LOGIN_HERE";
private static final String password = "INSERT_PASSWORD_HERE";
private static final int profileId = INSERT_PROFILE_ID_HERE;

3. Compose REST URL:

  • (implicit table)

 

      String url = serviceURL + "data" +
                   "?login=" + login +
                   "&password=" + password +
                   "&ids=" + profileId +
                   "&start-date=" + startDate +
                   "&end-date=" + endDate +
                   "&dimensions=u:visitor_id" +
                   "&metrics=u:pages,u:visits" +
                   "&max-results=5";
      URL service = new URL(url);
  • (explicit table)

 

      String url = serviceURL + "data" +
                   "?login=" + login +
                   "&password=" + password +
                   "&ids=" + profileId +
                   "&start-date=" + startDate +
                   "&end-date=" + endDate +
                   "&dimensions=u:utm_source,u:utm_medium,u:utm_campaign" +
                   "&metrics=u:pages,u:visits,u:transactions" +
                   "&filters=u:utm_source%3D~direct,u:pages%3E0" +
                   "&max-results=5";
      URL service = new URL(url);
  • (retrieve data from transactions table and apply sort)

 

      String url = serviceURL + "data" +
                   "?login=" + login +
                   "&password=" + password +
                   "&ids=" + profileId +
                   "&start-date=" + startDate +
                   "&end-date=" + endDate +
                   "&dimensions=u:transaction_id" +
                   "&metrics=u:transactions,u:revenue" +
                   "&sort=-u:transactions" +
                   "&max-results=5";
      URL service = new URL(url);
  • (Retrieve data from transactions table and apply sort)

 

  public static void GetDataAggregated() throws Exception {
    try {
      // compose REST URL
      String url = serviceURL + "data" +
                   "?login=" + login +
                   "&password=" + password +
                   "&ids=" + profileId +
                   "&start-date=" + startDate +
                   "&end-date=" + endDate +
                   "&dimensions=u:month" +
                   "&metrics=u:hits,u:bytes" +
                   "&sort=u:hits" +
                   "&max-results=5";
      URL service = new URL(url);

4. Create the buffering character-input stream for retrieving account list:

 

BufferedReader in = new BufferedReader(new InputStreamReader(service.openStream()));

5. Define namespace context implementation for data parsing:

 

class UrchinNamespaceContext implements NamespaceContext {
  public String getNamespaceURI(String prefix) {
    if (prefix == null) throw new NullPointerException("Null prefix");
    if (prefix.equals("tns")) return "https://urchin.com/api/urchin/v1/";
    else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
    return XMLConstants.NULL_NS_URI;
  }
  public String getPrefix(String namespaceURI) {
    throw new UnsupportedOperationException();
  }
  public Iterator getPrefixes(String namespaceURI) {
    throw new UnsupportedOperationException();
  }
}

6. Execute request:

 

      XPathFactory fact = XPathFactory.newInstance();
      XPath xp = fact.newXPath();
      InputSource is = new InputSource(in);

      // set namespace context
      xp.setNamespaceContext(new UrchinNamespaceContext());
      // retrieve list of profiles
      NodeList records = (NodeList)xp.evaluate("/tns:getDataResponse/record", is, XPathConstants.NODESET);

7. Parse and display response:

 

      // dump each record
      for(int i = 0; i < records.getLength(); i++){
        System.out.println("record: ");

        //retrieve record id
        System.out.println("\trecord id: " + xp.evaluate("recordId/text()", records.item(i)));

        // retrieve dimensions
        NodeList dimensions = (NodeList)xp.evaluate("dimensions/dimension", records.item(i), XPathConstants.NODESET);
        for(int j = 0; j < dimensions.getLength(); j++){
          System.out.println("\tdimension: name: " + xp.evaluate("@name", dimensions.item(j)) + " value: " + xp.evaluate("text()", dimensions.item(j)));
        }

        // retrieve metrics
        NodeList metrics = (NodeList)xp.evaluate("metrics/*", records.item(i), XPathConstants.NODESET);
        for(int j = 0; j < metrics.getLength(); j++){
          System.out.println("\tmetric: name: " + metrics.item(j).getLocalName() + " value: " + xp.evaluate("text()", metrics.item(j)));
        }
      }
      in.close();

Complete sample code for this example can be found in the ReportService.java file.

(Return to main article: Samples for Java (REST) v1)