如何在Google Analytics Reporting API v4上设置细分过滤器

时间:2016-09-13 16:48:22

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

我在从Google Analytics Core Reporting API返回数据时尝试添加细分过滤器,并提供以下代码:

<?php

function getReport($analytics)
{
    $VIEW_ID = "XXX";

    // Create the DateRange object.
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate("30daysAgo");
    $dateRange->setEndDate("yesterday");

    // Create the Metrics object.
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions->setExpression("ga:sessions");
    $sessions->setAlias("sessions");

    $goalCompletionAll = new Google_Service_AnalyticsReporting_Metric();
    $goalCompletionAll->setExpression("ga:goalCompletionsAll");
    $goalCompletionAll->setAlias("goalCompletionsAll");

    // Create first dimension object
    $dimensionChannel = new Google_Service_AnalyticsReporting_Dimension();
    $dimensionChannel->setName('ga:channelGrouping');

    // Create second dimension object
    $dimensionSource = new Google_Service_AnalyticsReporting_Dimension();
    $dimensionSource->setName('ga:source');

    // Create Segment object
    $segments = new Google_Service_AnalyticsReporting_Segment();
    $segments->setSegmentId('gaid::-4');

    // Create the ordering object
    $ordering = new Google_Service_AnalyticsReporting_OrderBy();
    $ordering->setFieldName("ga:sessions");
    $ordering->setOrderType("VALUE");
    $ordering->setSortOrder("DESCENDING");

    // Create the Report Request object
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request->setViewId($VIEW_ID);
    $request->setDateRanges($dateRange);

    // Set the metric
    $request->setMetrics([
            $sessions,
            $goalCompletionAll,
    ]);

    // Set the dimension
    $request->setDimensions([
        $dimensionChannel, 
        $dimensionSource
    ]);


    // Set the segments
    $request->setSegments($segments);

    // Set ordering
    $request->setOrderBys($ordering);

    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body->setReportRequests(array($request));

    return $analytics->reports->batchGet($body);
}

但是当我运行此代码时,我收到以下错误:

Requests with segments must have ga:segment dimension.

我按照我在Java中发现的this answer示例(段ID部分)。我有段ID的设置,所以我需要做什么来发送ga:segement维度的请求?

2 个答案:

答案 0 :(得分:2)

您必须创建细分维度:

        $dimensionSegment= new Google_Service_AnalyticsReporting_Dimension();
        $dimensionSegment->setName("ga:segment");

并将其添加到您的请求中:

$request->setDimensions([
    $dimensionChannel, 
    $dimensionSource, 
    $dimensionSegment
]);

答案 1 :(得分:2)

我正在使用PHP。我的答案与上面的答案几乎相同,但是语法segmentDimensions代替了dimensionSegment

这对我有用:

// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");

//create the Segment object.
$segments = new Google_Service_AnalyticsReporting_Segment();
$segments->setSegmentId("gaid::abcDE4fgHIJOOkLm1-nO1p");

然后在“报告请求”对象中:

$request->setDimensions(array($dimensions, $segmentDimensions));
$request->setSegments(array($segments));