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

How to set a segment filter on Google Analytics Reporting API 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 中的 (段 ID 部分)进行了操作。我已经设置了分段 ID,所以我需要做什么才能发送 ga:segement 维度的请求?

您必须创建一个细分维度:

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

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

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

我正在使用 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");

然后在 Report Request 对象中:

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