我可以在同一查询中检查多个ga:pagePath吗?

时间:2019-01-19 15:21:29

标签: php analytics google-analytics-v4

我有一个包含100条帖子的数组,我希望查看每条帖子的Google Analytics(分析)统计信息。现在,我已经完成了一个遍历每个帖子并打印每个帖子的结果的函数-但是它花费的时间太长了,我想知道是否可以仅使用DimensionFilterClauses中的一个查询并通过帖子数组添加新的筛选器来完成此工作。

我已经尝试了几次,但是我没有使它起作用,它只返回该帖子的统计信息之一。

array('/post/hey-everyone', '/post/hello-world/', '/post/test-this/');

function getReports($page_path, $start_date){
    global $VIEW_ID, $end_date, $analytics;

    $query = [
        "viewId" => $VIEW_ID,
        "dateRanges" => [
            "startDate" => $start_date,
            "endDate" => $end_date
        ],
        "metrics" => [
            "expression" => "ga:pageviews"
        ],
        "dimensions" => [
            "name" => "ga:pagepath"
        ],
        "dimensionFilterClauses" => [
            'filters' => [
                "dimension_name" => "ga:pagepath",
                "operator" => "EXACT", // valid operators can be found here: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#FilterLogicalOperator
                "expressions" => $page_path
            ]
        ]
    ];


    // build the request and response
    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests(array($query));
    // now batchGet the results https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet
    $report = $analytics->reports->batchGet($body);
    $rows = $report[0]->getData()->getRows();
    if ($rows){
        $metrics = $rows[0]->getMetrics()[0]->values[0];
        if ($metrics){
            return $metrics;
        }
    }
    return false;
}

0 个答案:

没有答案