php - 如何在 Google Analytics Reporting API v4 上设置分段过滤器

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

我在从 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.

我按照我发现的例子是this answer在 Java 中(段 ID 部分)。我已经设置了段 ID,那么我需要做什么才能发送 ga:segement 维度的请求?

最佳答案

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

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

并将其添加到您的请求中:
$request->setDimensions([
    $dimensionChannel, 
    $dimensionSource, 
    $dimensionSegment
]);

关于php - 如何在 Google Analytics Reporting API v4 上设置分段过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39475185/

相关文章:

php - 简单查询错误

google-analytics - 在本地主机上运行时,Google Optimize不触发

r - 无法通过 googleAnalyticsR 导出超过 1000 行

google-analytics - Analytics API-多个帐户

PHP 获取未括起来的逗号分隔值

php - 如何从 Amazon SES 检索响应?

javascript - dataLayer.push 在 GTM 脚本后不起作用

google-analytics - 谷歌分析 API : Include profile domain in Profile Feed

php - 读取斜线输入崩溃

google-analytics - Google Universal Analytics 不会跟踪所有访问