0

我需要在午夜过后的时间段内按小时报告指标。例如,从 20:00 开始,到 03:00 结束。为此,我设置了 StartDate'yesterday'和 EndDate 的日期范围'today'

$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate('yesterday');
$dateRange->setEndDate('today');

为了每小时检索数据,我利用了两个部分。一个是午夜之前的数据(例如,20:00 到 23:59),另一个是午夜之后的数据(例如,00:00 到 03:00)。在 JSON 请求中,段看起来像这样sessions::condition::ga:hour<>20_23,ga:hour<>00_03::

https://support.google.com/analytics/answer/7331978?hl=en

下面是我的请求代码,带有一些维度和指标过滤器(也就是说,我请求用户在页面上超过 8 秒的页面浏览量,其标题包含单词:'portfolio'):

// How to create $client you can read in Google Analytics documentation
$analytics = new Google_Service_AnalyticsReporting($client);

// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate('yesterday');
$dateRange->setEndDate('today');

// Create the Metrics object.
$metrics = new Google_Service_AnalyticsReporting_Metric();
$metrics->setExpression('ga:pageviews');

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId(VIEW_ID);
$request->setDateRanges([$dateRange]);
$request->setMetrics(array($metrics));

// Create the DimensionFilter.
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName('ga:pageTitle');
$dimensionFilter->setOperator('PARTIAL');
$dimensionFilter->setExpressions(['portfolio']);

// Create the MetricFilter.
$metricFilter = new Google_Service_AnalyticsReporting_MetricFilter();
$metricFilter->setMetricName('ga:timeOnPage');
$metricFilter->setOperator('GREATER_THAN');
$metricFilter->setComparisonValue('8');

// Create the DimensionFilterClauses
$dimensionFilterClause = new 
Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters($dimensionFilter);
$request->setDimensionFilterClauses(array($dimensionFilterClause));

// Create the MetricFilterClauses
$metricFilterClause = new 
Google_Service_AnalyticsReporting_MetricFilterClause();
$metricFilterClause->setFilters($metricFilter);
$request->setMetricFilterClauses([$metricFilterClause]);

// Creating multiple segments with function createSegment()
$segments = [];
$segments[] = createSegment('15', '23', 'Before Midnight');
$segments[] = createSegment('00', '10', 'After Midnight');

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

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

// Set the Segment to the ReportRequest object.
$request->setDimensions(array($hour, $segmentDimensions));
$request->setSegments($segments);

// Get report request
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($request));
$report = $analytics->reports->batchGet($body);
$reports = $report->getReports();

function createSegment($min, $max, $name) {

    // Set a segment for hourly time range. For daily time range there is no neen id a segment.
    $dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
    $dimensionFilter->setDimensionName('ga:hour');
    $dimensionFilter->setOperator('NUMERIC_BETWEEN');

    // $dimensionFilter->setExpressions($alert->get_expressions());
    $dimensionFilter->setMinComparisonValue($min);
    $dimensionFilter->setMaxComparisonValue($max);

    // Create Segment Filter Clause.
    $segmentFilterClause = new Google_Service_AnalyticsReporting_SegmentFilterClause();
    $segmentFilterClause->setDimensionFilter($dimensionFilter);

    // Create the Or Filters for Segment.
    $orFiltersForSegment = new Google_Service_AnalyticsReporting_OrFiltersForSegment();
    $orFiltersForSegment->setSegmentFilterClauses(array($segmentFilterClause));

    // Create the Simple Segment.
    $simpleSegment = new Google_Service_AnalyticsReporting_SimpleSegment();
    $simpleSegment->setOrFiltersForSegment(array($orFiltersForSegment));

    // Create the Segment Filters.
    $segmentFilter = new Google_Service_AnalyticsReporting_SegmentFilter();
    $segmentFilter->setSimpleSegment($simpleSegment);

    // Create the Segment Definition.
    $segmentDefinition = new Google_Service_AnalyticsReporting_SegmentDefinition();
    $segmentDefinition->setSegmentFilters(array($segmentFilter));

    // Create the Dynamic Segment.
    $dynamicSegment = new Google_Service_AnalyticsReporting_DynamicSegment();
    $dynamicSegment->setSessionSegment($segmentDefinition);
    $dynamicSegment->setName($name);

    // Create the Segments object.
    $segment = new Google_Service_AnalyticsReporting_Segment();
    $segment->setDynamicSegment($dynamicSegment);

    return $segment;
}

您可以从以下请求中看到响应:

Google_Service_AnalyticsReporting_GetReportsResponse Object
(
[collection_key:protected] => reports
[queryCost] => 
[reportsType:protected] => Google_Service_AnalyticsReporting_Report
[reportsDataType:protected] => array
[resourceQuotasRemainingType:protected] => Google_Service_AnalyticsReporting_ResourceQuotasRemaining
[resourceQuotasRemainingDataType:protected] => 
[internal_gapi_mappings:protected] => Array
    (
    )

[modelData:protected] => Array
    (
        [reports] => Array
            (
                [0] => Google_Service_AnalyticsReporting_Report Object
                    (
                        [columnHeaderType:protected] => Google_Service_AnalyticsReporting_ColumnHeader
                        [columnHeaderDataType:protected] => 
                        [dataType:protected] => Google_Service_AnalyticsReporting_ReportData
                        [dataDataType:protected] => 
                        [nextPageToken] => 
                        [internal_gapi_mappings:protected] => Array
                            (
                            )

                        [modelData:protected] => Array
                            (
                                [columnHeader] => Array
                                    (
                                        [dimensions] => Array
                                            (
                                                [0] => ga:hour
                                                [1] => ga:segment
                                            )

                                        [metricHeader] => Array
                                            (
                                                [metricHeaderEntries] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [name] => ga:pageviews
                                                                [type] => INTEGER
                                                            )

                                                    )

                                            )

                                    )

                                [data] => Google_Service_AnalyticsReporting_ReportData Object
                                    (
                                        [collection_key:protected] => totals
                                        [dataLastRefreshed] => 
                                        [isDataGolden] => 
                                        [maximumsType:protected] => Google_Service_AnalyticsReporting_DateRangeValues
                                        [maximumsDataType:protected] => array
                                        [minimumsType:protected] => Google_Service_AnalyticsReporting_DateRangeValues
                                        [minimumsDataType:protected] => array
                                        [rowCount] => 6
                                        [rowsType:protected] => Google_Service_AnalyticsReporting_ReportRow
                                        [rowsDataType:protected] => array
                                        [samplesReadCounts] => 
                                        [samplingSpaceSizes] => 
                                        [totalsType:protected] => Google_Service_AnalyticsReporting_DateRangeValues
                                        [totalsDataType:protected] => array
                                        [internal_gapi_mappings:protected] => Array
                                            (
                                            )

                                        [modelData:protected] => Array
                                            (
                                                [rows] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [dimensions] => Array
                                                                    (
                                                                        [0] => 00
                                                                        [1] => After Midnight
                                                                    )

                                                                [metrics] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [values] => Array
                                                                                    (
                                                                                        [0] => 15
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [1] => Array
                                                            (
                                                                [dimensions] => Array
                                                                    (
                                                                        [0] => 01
                                                                        [1] => After Midnight
                                                                    )

                                                                [metrics] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [values] => Array
                                                                                    (
                                                                                        [0] => 3
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [2] => Array
                                                            (
                                                                [dimensions] => Array
                                                                    (
                                                                        [0] => 15
                                                                        [1] => Before Midnight
                                                                    )

                                                                [metrics] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [values] => Array
                                                                                    (
                                                                                        [0] => 3
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [3] => Array
                                                            (
                                                                [dimensions] => Array
                                                                    (
                                                                        [0] => 16
                                                                        [1] => Before Midnight
                                                                    )

                                                                [metrics] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [values] => Array
                                                                                    (
                                                                                        [0] => 7
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [4] => Array
                                                            (
                                                                [dimensions] => Array
                                                                    (
                                                                        [0] => 19
                                                                        [1] => Before Midnight
                                                                    )

                                                                [metrics] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [values] => Array
                                                                                    (
                                                                                        [0] => 5
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [5] => Array
                                                            (
                                                                [dimensions] => Array
                                                                    (
                                                                        [0] => 22
                                                                        [1] => Before Midnight
                                                                    )

                                                                [metrics] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [values] => Array
                                                                                    (
                                                                                        [0] => 4
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                    )

                                                [totals] => Array
                                                    (
                                                        [0] => Google_Service_AnalyticsReporting_DateRangeValues Object
                                                            (
                                                                [collection_key:protected] => values
                                                                [pivotValueRegionsType:protected] => Google_Service_AnalyticsReporting_PivotValueRegion
                                                                [pivotValueRegionsDataType:protected] => array
                                                                [values] => Array
                                                                    (
                                                                        [0] => 37
                                                                    )

                                                                [internal_gapi_mappings:protected] => Array
                                                                    (
                                                                    )

                                                                [modelData:protected] => Array
                                                                    (
                                                                    )

                                                                [processed:protected] => Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                                [minimums] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [values] => Array
                                                                    (
                                                                        [0] => 3
                                                                    )

                                                            )

                                                    )

                                                [maximums] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [values] => Array
                                                                    (
                                                                        [0] => 15
                                                                    )

                                                            )

                                                    )

                                            )

                                        [processed:protected] => Array
                                            (
                                                [totals] => 1
                                            )

                                    )

                            )

                        [processed:protected] => Array
                            (
                                [data] => 1
                            )

                    )

            )

    )

[processed:protected] => Array
    (
        [reports] => 1
    )

)

我感兴趣的数据位于:[modelData:protected] => [reports] => [0] => [modelData:protected] => [data] => [modelData:protected] => [rows] => [].

问题是,因为这些段适用于每一天,所以我两天都收到“午夜之前”和“午夜之后”的数据。(我没有提到数据也是按小时采样的)。并且没有迹象表明这些值与哪一天相关。

所以,问题是: 是否有可能找到数据与哪一天相关?

4

1 回答 1

0

我终于解决了这个问题。我决定分析不同请求的实时数据。

所以事实证明,只有一个日期范围,就像这样:

$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate('yesterday');
$dateRange->setEndDate('today');

度量值相加。也就是说,这个指标值(您可以在下面用 key 看到)由hour和[values]的值组成:'today''yesterday''19'

[4] => Array
  (
    [dimensions] => Array
      (
        [0] => 19
        [1] => Before Midnight
      )
    [metrics] => Array
      (
        [0] => Array
          (
            [values] => Array
              (
                [0] => 5
              )
           )
      )
   )

但是如果我们设置两个日期范围:

$dateRange[0] = new Google_Service_AnalyticsReporting_DateRange();
$dateRange[0]->setStartDate('yesterday');
$dateRange[0]->setEndDate('yesterday');

$dateRange[1] = new Google_Service_AnalyticsReporting_DateRange();
$dateRange[1]->setStartDate('today');
$dateRange[1]->setEndDate('today');

$request->setDateRanges($dateRange);

一个用于'yesterday'和另一个用于'today'带有键的度量值[values]将具有数组的两个元素,其中第一个带有[0]索引的元素将与设置第一个的日期范围相关,而带有[1]索引的第二个将与设置第二个的日期范围相关:

[4] => Array
  (
    [dimensions] => Array
      (
        [0] => 19
        [1] => Before Midnight
      )
    [metrics] => Array
      (
        [0] => Array
          (
            [values] => Array
              (
                [0] => 2
                [1] => 3
              )
           )
      )
   )

因此,我们可以选择索引为[0]where is 的所有值,以及索引为 where is的[dimensions]所有'Before midnight'值,并将它们相加。总和将是从 20:00 开始到 03:00 结束的时间范围内的所有页面视图。瞧!希望我的解释能帮助寻找解决方案的人。[1][dimensions]'After midnight'

于 2019-01-17T15:13:06.843 回答