【问题标题】:PHP Google analytics API v4 Metrics setAlias function not returning correct Metrics titlePHP Google Analytics API v4 Metrics setAlias 函数未返回正确的 Metrics 标题
【发布时间】:2016-09-07 01:41:05
【问题描述】:

我正在使用 Google API V4 https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php

我遇到了 setAlias 函数的问题。 When there are multiple metrics selected, all the returned headers are same.

代码:

   <?php
    $metrices = {FORM ARRY WITH MULTIPLE METRICES} 
       $nmetrices = array();
        if(is_array($metrices) && count($metrices) > 0){
          $i=0;
          foreach($metrices as $metric){
            $nmetrices[$i] = new     Google_Service_AnalyticsReporting_Metric();
            $nmetrices[$i]->setExpression($metric);
            $nmetrices[$i]->setAlias($metric);
            $i+=1;
          }
        }?>

例子:

我选择了“ga:users”和“ga:percentNewSessions”,但返回的结果的标题为:“ga:users”这两个指标。

ga:medium: (none)
ga:date: 20150810
Metric type: INTEGER
**ga:users: 764
ga:users: 97.38219895287958**
ga:medium: (none)
ga:date: 20150811
Metric type: INTEGER
**ga:users: 2495
ga:users: 85.50284629981024**

【问题讨论】:

    标签: php google-analytics-api google-analytics-firebase


    【解决方案1】:

    我刚开始使用 Analytics API v4 并遇到了这个问题。提供的 PHP 代码不允许示例中的 printResults() 函数使用多个指标,但是来自 Google 的数据包含正确的别名。

    来自 google 的指标对象是单个根,但指标标头对象是每个请求的指标类型的单独条目。

    这是一个更新的 printResults() 函数,它将满足多个指标。

    function printResults($reports) {
        for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
            $report = $reports[ $reportIndex ];
            $header = $report->getColumnHeader();
            $dimensionHeaders = $header->getDimensions();
            $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
            $rows = $report->getData()->getRows();
    
            for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
                $row = $rows[ $rowIndex ];
                $dimensions = $row->getDimensions();
                $metrics = $row->getMetrics();
                for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
                    print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
                }
    
                for ($j = 0; $j < count( $metricHeaders ); $j++) {
                    $entry = $metricHeaders[$j];
                    $values = $metrics[$rowIndex];
                    $value = $values->getValues()[$j];
                    print($entry->getName() . ": " . $value . "\n");
                }
            }
        }
    }
    

    【讨论】:

    • 不错的收获,快把我逼疯了
    猜你喜欢
    • 2016-01-12
    • 2017-10-21
    • 2022-12-20
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多