我需要在午夜过后的时间段内按小时报告指标。例如,从 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] => []
.
问题是,因为这些段适用于每一天,所以我两天都收到“午夜之前”和“午夜之后”的数据。(我没有提到数据也是按小时采样的)。并且没有迹象表明这些值与哪一天相关。
所以,问题是: 是否有可能找到数据与哪一天相关?