PHP:如何从 Google 元数据 API 中检索所有指标和维度?
PHP: how to retrieve all the metrics and dimensions from the Google Metadata API?
Metadata API states that it can be used to return the available Google metrics and dimensions. There is a working javascript 示例页面对此进行了演示。
现在我正在寻找如何在 PHP 中实现此目的的片段或文档页面。我已经搜索了两个都没有成功。
所以我的问题是:如何使用 Google PHP 库来检索所有可用的指标和维度?
直接从 metadata API 上的文档,您可以使用 API 密钥执行此操作,无需使用 oauth2
/**
* 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;
}
Metadata API states that it can be used to return the available Google metrics and dimensions. There is a working javascript 示例页面对此进行了演示。
现在我正在寻找如何在 PHP 中实现此目的的片段或文档页面。我已经搜索了两个都没有成功。
所以我的问题是:如何使用 Google PHP 库来检索所有可用的指标和维度?
直接从 metadata API 上的文档,您可以使用 API 密钥执行此操作,无需使用 oauth2
/**
* 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;
}