如何按小时获取两天的数据? (Google 分析报告 API v4,PHP 库)
How to get data on hourly basis for two days? (Google Analytics Reporting API v4, PHP library)
我需要在午夜过后的时间段内按小时报告指标。例如,从 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
:
下面是我的请求代码,带有一些维度和指标过滤器(也就是说,我请求用户在页面上停留超过 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] => []
.
问题是,因为细分应用到每一天,所以我收到了这两天的数据 'before midnight' 和 'after midnight'。 (我没有提到数据也是按小时采样的)。并且没有迹象表明这些值与哪一天有关。
所以,问题是:
是否有可能找到数据与哪一天相关?
我终于解决了这个问题。我决定分析不同请求的实时数据。
事实证明,在一个日期范围内,就像这样:
$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
)
)
)
)
所以我们可以选择所有带有 [0]
索引的值,其中 [dimensions]
是 'Before midnight'
,以及所有带有 [1]
索引的值,其中 [dimensions]
是 'After midnight'
,并将它们相加。总和将是从 20:00 开始到 03:00 结束的时间范围内的所有页面视图。瞧!
希望我的解释能帮助寻找解决方案的人。
我需要在午夜过后的时间段内按小时报告指标。例如,从 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
:
下面是我的请求代码,带有一些维度和指标过滤器(也就是说,我请求用户在页面上停留超过 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] => []
.
问题是,因为细分应用到每一天,所以我收到了这两天的数据 'before midnight' 和 'after midnight'。 (我没有提到数据也是按小时采样的)。并且没有迹象表明这些值与哪一天有关。
所以,问题是: 是否有可能找到数据与哪一天相关?
我终于解决了这个问题。我决定分析不同请求的实时数据。
事实证明,在一个日期范围内,就像这样:
$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
)
)
)
)
所以我们可以选择所有带有 [0]
索引的值,其中 [dimensions]
是 'Before midnight'
,以及所有带有 [1]
索引的值,其中 [dimensions]
是 'After midnight'
,并将它们相加。总和将是从 20:00 开始到 03:00 结束的时间范围内的所有页面视图。瞧!
希望我的解释能帮助寻找解决方案的人。