欄:清單

列出某個報表類型的所有資料欄。 立即試用查看範例

要求

HTTP 要求

GET https://www.googleapis.com/analytics/v3/metadata/reportType/columns

參數

參數名稱 說明
必要參數
reportType string 報表類型。允許的值:ga。其中 ga 與 Core Reporting API 對應。

要求主體

請勿使用這個方法提供要求主體。

回應

如果成功的話,這個方法會傳回回應內文,其結構如下:

{
  "kind": "analytics#columns",
  "etag": etag,
  "totalResults": integer,
  "attributeNames": [
    string
  ],
  "items": [
    metadata.columns Resource
  ]
}
屬性名稱 說明 附註
kind string 系列作品類型。
etag etag ETag。您可以將這個 etag 與最後一個回應 Etag 進行比較,以查看回應是否有變化。
totalResults integer 回應中傳回的資料欄總數。
attributeNames[] list 由資料欄傳回的屬性名稱清單。
items[] list 報表類型的欄清單。

範例

注意:這個方法適用的程式語言眾多,我們只在此提供部分程式碼範例,完整的支援語言清單請參閱用戶端程式庫頁面

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()); } } }

PHP

使用 PHP 用戶端程式庫

/**
 * 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 {

  $results = $analytics->metadata_columns->listMetadataColumns('ga');
  // Success

} catch (apiServiceException $e) {
  // Handle API service exceptions.
  $error = $e->getMessage();
}


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

function printMetadataReport($results) {
  print '<h1>Metadata Report</h1>';
  printReportInfo($results);
  printAttributes($results);
  printColumns($results);
}


function printReportInfo(&$results) {
  $html = '<h2>Report Info</h2>';
  $html .= <<<HTML
<pre>
Kind                  = {$results->getKind()}
Etag                  = {$results->getEtag()}
Total Results         = {$results->getTotalResults()}
</pre>
HTML;
  print $html;
}


function printAttributes(&$results) {
  $html = '<h2>Attribute Names</h2><ul>';
  $attributes = $results->getAttributeNames();
  foreach ($attributes as $attribute) {
    $html .= '<li>'. $attribute . '</li>';
  }
  $html .= '</ul>';
  print $html;
}


function printColumns(&$results) {
  $columns = $results->getItems();
  if (count($columns) > 0) {
    $html = '<h2>Columns</h2>';
    foreach ($columns as $column) {
      $html .= '<h3>' . $column->getId() . '</h3>';
      $column_attributes = $column->getAttributes();
      foreach ($column_attributes as $name=>$value) {
        $html .= <<<HTML
<pre>
{$name}: {$value}
</pre>
HTML;
      }
    }
  } else {
    $html = '<p>No Results Found.</p>';
  }
  print $html;
}

Python

使用 Python 用戶端程式庫

# 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:
  results = service.metadata().columns().list(reportType='ga').execute()

except TypeError, error:
  # Handle errors in constructing a query.
  print ('There was an error in constructing your query : %s' % error)

except HttpError, error:
  # Handle API errors.
  print ('Arg, there was an API error : %s : %s' %
         (error.resp.status, error._get_reason()))

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

def print_metadata_report(results):
  print 'Metadata Response Report'
  print_report_info(results)
  print_attributes(results.get('attributeNames'))
  print_columns(results)


def print_report_info(columns):
  print "Metadata Report Info"
  if columns:
    print 'Kind           = %s' % columns.get('kind')
    print 'Etag           = %s' % columns.get('etag')
    print 'Total Results  = %s' % columns.get('totalResults')


def print_attributes(attributes):
  if attributes:
    print 'Attribute Names:'
    for attribute in attributes:
      print attribute

def print_columns(columns_data):
  if columns_data:
    print 'Columns:'

    columns = columns_data.get('items', [])

    for column in columns:
      print
      print '%15s = %35s' % ('Column ID', column.get('id'))
      print '%15s = %35s' % ('Kind', column.get('kind'))

      column_attributes = column.get('attributes', [])

      for name, value in column_attributes.iteritems():
        print '%15s = %35s' % (name, value)

JavaScript

使用 JavaScript 用戶端程式庫

/**
 * 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.
 */

function makeMetadataRequest() {
  var request = gapi.client.analytics.metadata.columns.list({
      'reportType': 'ga'
  });
  request.execute(renderMetadataReport);
}


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

function renderMetadataReport(results) {
  var reportHtml = [];
  reportHtml.push(
      getReportInfo(results),
      getAttributes(results),
      getColumns(results));

  // Renders the results to a DIV element
  document.getElementById('DIV_ID').innerHTML = reportHtml.join('');
}


function getReportInfo(results) {
  var html = [];
  if (results) {
    html.push('<h2>Report Info</h2>');
    html.push('<pre>Kind: ', results.kind, '</pre>');
    html.push('<pre>Etag: ', results.etag, '</pre>');
    html.push('<pre>Total Results: ', results.totalResults, '</pre>');
  }
  return html.join('');
}


function getAttributes(results) {
  var html = [];
  if (results) {
    html.push('<h2>Attribute Names</h2><ul>');
    var attributes = results.attributeNames;

    for (var i = 0, attribute; attribute = attributes[i]; i++) {
      html.push('<li>', attribute, '</li>');
    }
    html.push('</ul>');
  }
  return html.join('');
}

function  getColumns(results) {
  var html = [];
  if (results) {
    var columns = results.items;
    html.push('<h2>Columns</h2>');

    for (var i = 0, column; column = columns[i]; i++) {
      html.push('<h3>', column.id, '</h3>');
      var attributes = column.attributes;
      for (attribute in attributes) {
        html.push('<pre><strong>', attribute, '</strong> : ',
                  attributes[attribute], '</pre>');
      }
    }
  }
  return html.join('');
}

試試看!

您可以使用下方的 APIs Explorer,針對即時資料呼叫這個方法,然後查看回應。 或者,您也可以試試獨立的 Explorer