如何连续两天按小时获取数据? (Google Analytics Reporting API v4,PHP库)

时间:2019-01-12 14:24:59

标签: php google-analytics-api segment google-apis-explorer google-reporting-api

我需要在经过午夜的时段内每小时生成一次报告指标。例如,从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'):< / p>

// 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] => []

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

因此,问题是: 是否有可能找到与数据相关的日期?

1 个答案:

答案 0 :(得分:0)

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

事实证明,只有一个日期范围,例如:

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

度量值相加。也就是说,您可以在下面用[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
              )
           )
      )
   )

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