php - PHP 上的 Google Analytics API

标签 php google-analytics google-analytics-api

但我仍然遇到这个问题:

Fatal error: Uncaught exception 'Google_Exception' with message '(get) missing required param: 'ids'' in /home/sample/www/test/google-api-php-client-master/src/Google/Service/Resource.php:163 Stack trace: #0 /home/sample/www/test/google-api-php-client-master/src/Google/Service/Analytics.php(2107): Google_Service_Resource->call('get', Array, 'Google_Service_...') #1 /home/sample/www/test/google-api-php-client-master/practicein.php(57): Google_Service_Analytics_DataRealtime_Resource->get('ga:********', 'ga:pageviews', '2015-03-07', '2015-04-04') #2 /home/sample/www/test/index.php(34): require_once('/home/lanforest...') #3 {main} thrown in /home/sample/www/test/google-api-php-client-master/src/Google/Service/Resource.php on line 163

我认为这里的代码是错误的。

$results = $analytics->data_realtime->get(
    $analytics_view_id,
    'ga:pageviews',//**I did correct this.**
    '2015-03-07',
    '2015-04-04'
);

//**I think these goes wrong**
$totals = $results->getTotalsForAllResults();
$array = array("activeUsers" => $totals["rt:activeUsers"]);

我只想查看页面浏览量。我可以编写什么代码?

我认为我获得了活跃用户值(value)。但是,它仍然发出警告。 这是什么错误?

Warning: Cannot modify header information - headers already sent by (output started at /home/sample/www/test/index.php:16) in /home/sample/www/test/google-api-php-client-master/practicein.php on line 76

Warning: Cannot modify header information - headers already sent by (output started at /home/sample/www/test/index.php:16) in /home/sample/www/test/google-api-php-client-master/practicein.php on line 77

{"activeUsers":"0"}//this is the result.

最佳答案

从你的starcktrace中我看到:

#1 .../practicein.php(66): Google_Service_Analytics_DataRealtime_Resource->get('ga:*******', '2015-03-07', '2015-04-04', 'ga:pageviews'

您使用错误的参数调用 get 方法:第一个是您的 ga id,第二个必须是指标,然后您可以传递 opt 参数。

检查 src/Google/Service/Analytics.php 中的 get 方法文档:

/**
 * Returns real time data for a view (profile). (realtime.get)
 *
 * @param string $ids Unique table ID for retrieving real time data. Table ID is
 * of the form ga:XXXX, where XXXX is the Analytics view (profile) ID.
 * @param string $metrics A comma-separated list of real time metrics. E.g.,
 * 'rt:activeUsers'. At least one metric must be specified.
 * @param array $optParams Optional parameters.
 *
 * @opt_param int max-results The maximum number of entries to include in this
 * feed.
 * @opt_param string sort A comma-separated list of dimensions or metrics that
 * determine the sort order for real time data.
 * @opt_param string dimensions A comma-separated list of real time dimensions.
 * E.g., 'rt:medium,rt:city'.
 * @opt_param string filters A comma-separated list of dimension or metric
 * filters to be applied to real time data.
 * @return Google_Service_Analytics_RealtimeData
 */
 public function get($ids, $metrics, $optParams = array())

关于php - PHP 上的 Google Analytics API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495572/

相关文章:

php - 设计日志系统

jquery - 维护和转义自定义维度中的单引号Google Analytics(分析)

ios5 - 为 iOS 实现 Google Analytics

json - nifi invokehttp post复杂的json

python - Pandas 将字典列表(GA 输出)转换为有意义的数据帧

php - CodeIgniter 500 错误

php - 将 phpmailer_v5.1 use_gmail 与 paypal ipn 响应器相结合

javascript - Google Analytics API - 跟踪子域

c# - 以编程方式指定 Google Analytics Reporting API v4 查询结果

php - 我应该使用 set_value() 来重新填充 CodeIgniter 中的表单吗