如何使用PHP在Google Analytics(分析)API中实现!=?

时间:2018-11-06 13:20:31

标签: php google-analytics-api

我正在使用Google Analytics(分析)API来生成Google Analytics(分析)报告。

我正在实现过滤器,在过滤器中,我想确保这一点,

ga:contentGroup2!=Answered;ga:pageviews!=0

这是我的代码,

$processing[1] = str_replace('filters=', '' , $e1_param_url[$i]); //, here $processing[1] will be ga:contentGroup2!=Answered;ga:pageviews!=0
                $e1_filters = explode(';' , $processing[1]);
                for($k_filters =0; $k_filters < count($e1_filters); $k_filters++){
                    $operator = '';
                    for($c_ops =0; $c_ops < count($operators); $c_ops++){

                        $operator = strpos($e1_filters[$k_filters],$operators[$c_ops]) != false ? $operators[$c_ops] : '';
                        if($operator != '') break;
                    }
                    $the_filter = explode($operator , $e1_filters[$k_filters]);

                    if(in_array($the_filter[0],$metrics)){
                        $metricFilter = new Google_Service_AnalyticsReporting_MetricFilter();
                        $metricFilter->setMetricName($the_filter[0]);
                        if(strpos('!',$operator)!= false) 
                            $metricFilter->setNot(true);
                        $metricFilter->setOperator($this->getOperator($operator));
                        $metricFilter->setComparisonValue($the_filter[1]);
                        array_push($metricFilters , $metricFilter);
                    }else{
                        $dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
                        $dimensionFilter->setDimensionName($the_filter[0]);
                        if(strpos('!',$operator)!= false) 
                            $dimensionFilter->setNot(true);
                        $dimensionFilter->setOperator($this->getOperator($operator));
                        $dimensionFilter->setExpressions(array($the_filter[1]));
                        array_push($dimensionsFilters , $dimensionFilter);
                    }
                }

这是我的$ this-> getOperator函数

    // get this list from: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#Operator
function getOperator($operator){
    switch($operator){
        case '==':
            return 'EXACT';
        case '!=':
            return 'EXACT';
        case '>=':
            return 'NUMERIC_GREATER_THAN';
        case '<=':
            return 'NUMERIC_LESS_THAN';
        default:
            return null;
    }
}

那么,对于!=,我应该返回什么?

2 个答案:

答案 0 :(得分:1)

根据以下Google documentation for Metric Filters,您的指标筛选器运算符似乎不正确。您的getOperator()应该看起来像这样:

function getOperator($operator){
    switch($operator){
        case '==':
            return 'EQUAL';
        case '!=':
            return 'EQUAL';
        case '>=':
            return 'GREATER_THAN';
        case '<=':
            return 'LESS_THAN';
        default:
            return null;
    }
}

对于维度过滤器,由于您的示例期望进行字符串比较,因此似乎只需要==!=,并且可以省略NUMERIC_GREATER_THAN和{{1} }。因此,您将始终以Dimension Filter Operator的形式提供NUMERIC_LESS_THAN,并以与当前相同的方式处理EXACT值。

因此,请更改以下代码行:

setNot()

对此:

$dimensionFilter->setOperator($this->getOperator($operator));

答案 1 :(得分:0)

如果使用 Google Analytics(分析)ReportingV4 您可以直接设置表达式 由

$filter = "ga:contentGroup2!=Answered;ga:pageviews!=0";
$ga_report_request->setFiltersExpression($filter);

检查报告请求对象 here