Java 示例

使用 Java 客戶端庫

/**
 * 1. Execute a Metadata Request
 * An application can request columns data by calling the list method on the Analytics service object.
 * The method requires an reportType parameter that specifies the column data to retrieve.
 * For example, the following code requests columns for the ga report type.
 */

try {
  Columns results = getMetadata(analytics);
  // Success

} catch (GoogleJsonResponseException e) {
  // Catch API specific errors.
  handleApiError(e);
} catch (IOException e) {
  // Catch general parsing network errors.
  e.printStackTrace();
}

/**
 * 2. Print out the Columns data
 * The components of the result can be printed out as follows:
 */

private static Columns getMetadata(Analytics analytics) throws IOException {
  String reportType = "ga";
  return analytics.metadata()
      .columns()
      .list(reportType)
      .execute();
}

private static void printMetadataReport(Columns results) {
  System.out.println("Metadata Response Report");
  printReportInfo(results);
  printAttributes(results.getAttributeNames());
  printColumns(results.getItems());
}

private static void printReportInfo(Columns results) {
  System.out.println("## Metadata Report Info ##");
  System.out.println("Kind: " + results.getKind());
  System.out.println("Etag: " + results.getEtag());
  System.out.println("Total Results: " + results.getTotalResults());
  System.out.println();
}

private static void printAttributes(List<String> attributeNames) {
  System.out.println("## Attribute Names ##");
  for (String attribute : attributeNames) {
    System.out.println(attribute);
  }
}

private static void printColumns(List<Column> columns) {
  System.out.println("## Columns ##");

  for (Column column : columns) {
    System.out.println();
    System.out.println("Column ID: " + column.getId());
    System.out.println("Kind: " + column.getKind());

    Map<String, String> columnAttributes = column.getAttributes();

    for (Map.Entry<String, String> attribute: columnAttributes.entrySet()) {
      System.out.println(attribute.getKey() + ": " + attribute.getValue());
    }
  }
}

注意:從 Metadata.list 複製的第一個版本