サーチ…


構文

パラメーター

(必須)パラメータ名説明
ids アナリティクスデータを取得するためのユニークなテーブルID。表IDの形式はga:XXXXです。XXXXはアナリティクスのビュー(旧プロファイル)IDです。
メトリックカンマ区切りのアナリティクス指標のリスト。例: 'rt:activeUsers'少なくとも1つのメトリックを指定する必要があります。
(オプション)パラメータ名 説明
ディメンションコンマ区切りのリアルタイムディメンションのリスト。例: 'rt:medium、rt:city'
フィルターリアルタイムデータに適用されるディメンションフィルタまたはメトリックフィルタのカンマ区切りリストです。
最大結果このフィードに含めるエントリの最大数。
ソートリアルタイムデータのソート順を決定するディメンションまたはメトリックのカンマ区切りリストです。
(標準)パラメータ名 説明
折り返し電話応答を処理するJavaScriptコールバック関数の名前。 JavaScript JSON-Pリクエストで使用されます。
prettyPrint trueの場合、人間が読める形式で応答を返します。デフォルト値:trueこれが偽の場合、レスポンスペイロードサイズが小さくなり、一部の環境でパフォーマンスが向上する可能性があります。
クォータユーザーユーザーのIPアドレスが不明な場合でも、サーバー側アプリケーションからユーザーごとのクォータを適用できます。これは、たとえば、ユーザーのためにApp Engineでcronジョブを実行するアプリケーションで発生します。ユーザーを一意に識別する任意の文字列を選択できますが、40文字に制限されています。両方が提供されている場合は、userIpをオーバーライドします。キャッピングの使用の詳細をご覧ください。
userIp サーバーサイドアプリケーションからAPIを呼び出すときにユーザーごとのクォータを適用できます。キャッピングの使用の詳細をご覧ください。

備考

制限付きベータ版のリアルタイムレポートAPIは、 開発者 向けの プレビューのみで 利用できます 登録してAPIにアクセスします。

Real Time Reporting APIを使用すると、認証されたユーザーに対して、リアルタイムのデータ(プロパティのリアルタイムアクティビティなど)をリクエストできます。

Real Time Reporting APIを使用すると、次のことができます。

  • 有限の在庫を持つアイテムを見ているユーザーにとって、ページのアクティブな視聴者を表示し、緊急性を感じる。
  • 上位10個のアクティブなページなどの最も人気のあるコンテンツを表示します。
  • リアルタイムダッシュボードを作成して表示します。

承認

GoogleアナリティクスのリアルタイムAPIを呼び出すには、次のいずれかの有効範囲の認証が必要です(認証と承認について詳しくは、こちらをご覧ください)。

範囲アクセス許可
https://www.googleapis.com/auth/analytics Googleアナリティクスアカウントへのフルアクセスは、認証されたユーザーのアクセスレベルまで可能です。
https://www.googleapis.com/auth/analytics.readonly 認証されたユーザーのGoogleアナリティクスアカウントへの読み取り専用アクセス

PHPの例

PHPクライアントライブラリを使用する

/**
 * 1.Create and Execute a Real Time Report
 * An application can request real-time data by calling the get method on the Analytics service object.
 * The method requires an ids parameter which specifies from which view (profile) to retrieve data.
 * For example, the following code requests real-time data for view (profile) ID 56789.
 */
$optParams = array(
    'dimensions' => 'rt:medium');

try {
  $results = $analytics->data_realtime->get(
      'ga:56789',
      'rt:activeUsers',
      $optParams);
  // Success. 
} catch (apiServiceException $e) {
  // Handle API service exceptions.
  $error = $e->getMessage();
}


/**
 * 2. Print out the Real-Time Data
 * The components of the report can be printed out as follows:
 */

function printRealtimeReport($results) {
  printReportInfo($results);
  printQueryInfo($results);
  printProfileInfo($results);
  printColumnHeaders($results);
  printDataTable($results);
  printTotalsForAllResults($results);
}

function printDataTable(&$results) {
  if (count($results->getRows()) > 0) {
    $table .= '<table>';

    // Print headers.
    $table .= '<tr>';

    foreach ($results->getColumnHeaders() as $header) {
      $table .= '<th>' . $header->name . '</th>';
    }
    $table .= '</tr>';

    // Print table rows.
    foreach ($results->getRows() as $row) {
      $table .= '<tr>';
        foreach ($row as $cell) {
          $table .= '<td>'
                 . htmlspecialchars($cell, ENT_NOQUOTES)
                 . '</td>';
        }
      $table .= '</tr>';
    }
    $table .= '</table>';

  } else {
    $table .= '<p>No Results Found.</p>';
  }
  print $table;
}

function printColumnHeaders(&$results) {
  $html = '';
  $headers = $results->getColumnHeaders();

  foreach ($headers as $header) {
    $html .= <<<HTML
<pre>
Column Name       = {$header->getName()}
Column Type       = {$header->getColumnType()}
Column Data Type  = {$header->getDataType()}
</pre>
HTML;
  }
  print $html;
}

function printQueryInfo(&$results) {
  $query = $results->getQuery();
  $html = <<<HTML
<pre>
Ids         = {$query->getIds()}
Metrics     = {$query->getMetrics()}
Dimensions  = {$query->getDimensions()}
Sort        = {$query->getSort()}
Filters     = {$query->getFilters()}
Max Results = {$query->getMax_results()}
</pre>
HTML;

  print $html;
}

function printProfileInfo(&$results) {
  $profileInfo = $results->getProfileInfo();

  $html = <<<HTML
<pre>
Account ID               = {$profileInfo->getAccountId()}
Web Property ID          = {$profileInfo->getWebPropertyId()}
Internal Web Property ID = {$profileInfo->getInternalWebPropertyId()}
Profile ID               = {$profileInfo->getProfileId()}
Profile Name             = {$profileInfo->getProfileName()}
Table ID                 = {$profileInfo->getTableId()}
</pre>
HTML;

  print $html;
}

function printReportInfo(&$results) {
  $html = <<<HTML
  <pre>
Kind                  = {$results->getKind()}
ID                    = {$results->getId()}
Self Link             = {$results->getSelfLink()}
Total Results         = {$results->getTotalResults()}
</pre>
HTML;

  print $html;
}

function printTotalsForAllResults(&$results) {
  $totals = $results->getTotalsForAllResults();

  foreach ($totals as $metricName => $metricTotal) {
    $html .= "Metric Name  = $metricName\n";
    $html .= "Metric Total = $metricTotal";
  }

  print $html;
}

公式ドキュメントから元のバージョンに対応



Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow