google analytics api v4 多指标查询
google analytics api v4 multiple metrics query
我一直在尝试获取其中包含特定 ID 的所有网址的指标。从这个问题: 我看到了查询方法并尝试了它而不是面向对象的版本。在查询方法中,代码工作得很好,除了它只会 return 最后发送的指标(在这个例子中它只有 return 的唯一页面浏览量,因为它是最后列出的指标)。我需要所有三个 returned 的值,而不必分别点击 api 三次。这是我的代码:
$query = [
"viewId" => $profileId,
"dateRanges" => [
"startDate" => "2018-01-25",
"endDate" => "2018-01-25"
],
"metrics" => [
"expression" => "ga:pageviews",
"expression" => "ga:avgTimeOnPage",
"expression" => "ga:uniquePageviews"
],
"dimensions" => [
"name" => "ga:pagepath"
],
"dimensionFilterClauses" => [
'filters' => [
"dimension_name" => "ga:pagepath",
"operator" => "PARTIAL",
"expressions" => $theId
]
]
];
// build the request and response
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($query));
$report = $analytics->reports->batchGet($body);
关于如何正确执行此操作的任何想法?
尝试以下功能,只需将 ENTER_ID 替换为您的 ID。这似乎也可以获取所有页面的流量
function segmentRequest(&$analyticsreporting) {
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2018-02-01");
$dateRange->setEndDate("2018-02-15");
// Create the Metrics object.
$pageviews = new Google_Service_AnalyticsReporting_Metric();
$pageviews->setExpression("ga:pageviews");
$pageviews->setAlias("pageviews");
//Create the browser dimension.
$path = new Google_Service_AnalyticsReporting_Dimension();
$path->setName("ga:pagePath");
// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");
// Create Dimension Filter.
$dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
$dimensionFilter->setDimensionName("ga:pagePath");
$dimensionFilter->setOperator("PARTIAL");
$dimensionFilter->setExpressions(array("ENTER_ID"));
// 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("Visits to /en/listings.php");
// Create the Segments object.
$segment = new Google_Service_AnalyticsReporting_Segment();
$segment->setDynamicSegment($dynamicSegment);
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("XXXXX");
$request->setDateRanges(array($dateRange));
$request->setDimensions(array($path, $segmentDimensions));
$request->setSegments(array($segment));
$request->setMetrics(array($pageviews));
// Create the GetReportsRequest object.
$getReport = new Google_Service_AnalyticsReporting_GetReportsRequest();
$getReport->setReportRequests(array($request));
// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$response = $analyticsreporting->reports->batchGet( $body );
printResults($response->getReports());
}
对于那些想继续使用数组构建查询的人来说,这应该可行:
$request = [
"viewId" => "123456789",
"dateRanges" => [
"startDate" => "2018-01-01",
"endDate" => "today"
],
"metrics" => [
"expression" => "ga:pageviews"
],
"dimensions" => [
["name" => "ga:browser"],
["name" => "ga:sessionDurationBucket"]
],
"dimensionFilterClauses" => [
'filters' => [
"dimension_name" => "ga:pagepath",
"operator" => "EXACT",
"expressions" => $url
]
]
];
您可以通过为每个表达式使用一个数组来以数组形式定义多个指标 (["expression" => "ga:uniquePageviews"]
)。
$query = [
"viewId" => profileId,
"dateRanges" => [
"startDate" => "2018-01-25",
"endDate" => "2018-01-25"
],
"metrics" => [
["expression" => "ga:pageviews"],
["expression" => "ga:avgTimeOnPage"],
["expression" => "ga:uniquePageviews"]
],
"dimensions" => [
"name" => "ga:pagepath"
],
];
我一直在尝试获取其中包含特定 ID 的所有网址的指标。从这个问题:
$query = [
"viewId" => $profileId,
"dateRanges" => [
"startDate" => "2018-01-25",
"endDate" => "2018-01-25"
],
"metrics" => [
"expression" => "ga:pageviews",
"expression" => "ga:avgTimeOnPage",
"expression" => "ga:uniquePageviews"
],
"dimensions" => [
"name" => "ga:pagepath"
],
"dimensionFilterClauses" => [
'filters' => [
"dimension_name" => "ga:pagepath",
"operator" => "PARTIAL",
"expressions" => $theId
]
]
];
// build the request and response
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($query));
$report = $analytics->reports->batchGet($body);
关于如何正确执行此操作的任何想法?
尝试以下功能,只需将 ENTER_ID 替换为您的 ID。这似乎也可以获取所有页面的流量
function segmentRequest(&$analyticsreporting) {
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("2018-02-01");
$dateRange->setEndDate("2018-02-15");
// Create the Metrics object.
$pageviews = new Google_Service_AnalyticsReporting_Metric();
$pageviews->setExpression("ga:pageviews");
$pageviews->setAlias("pageviews");
//Create the browser dimension.
$path = new Google_Service_AnalyticsReporting_Dimension();
$path->setName("ga:pagePath");
// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");
// Create Dimension Filter.
$dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
$dimensionFilter->setDimensionName("ga:pagePath");
$dimensionFilter->setOperator("PARTIAL");
$dimensionFilter->setExpressions(array("ENTER_ID"));
// 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("Visits to /en/listings.php");
// Create the Segments object.
$segment = new Google_Service_AnalyticsReporting_Segment();
$segment->setDynamicSegment($dynamicSegment);
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("XXXXX");
$request->setDateRanges(array($dateRange));
$request->setDimensions(array($path, $segmentDimensions));
$request->setSegments(array($segment));
$request->setMetrics(array($pageviews));
// Create the GetReportsRequest object.
$getReport = new Google_Service_AnalyticsReporting_GetReportsRequest();
$getReport->setReportRequests(array($request));
// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
$response = $analyticsreporting->reports->batchGet( $body );
printResults($response->getReports());
}
对于那些想继续使用数组构建查询的人来说,这应该可行:
$request = [
"viewId" => "123456789",
"dateRanges" => [
"startDate" => "2018-01-01",
"endDate" => "today"
],
"metrics" => [
"expression" => "ga:pageviews"
],
"dimensions" => [
["name" => "ga:browser"],
["name" => "ga:sessionDurationBucket"]
],
"dimensionFilterClauses" => [
'filters' => [
"dimension_name" => "ga:pagepath",
"operator" => "EXACT",
"expressions" => $url
]
]
];
您可以通过为每个表达式使用一个数组来以数组形式定义多个指标 (["expression" => "ga:uniquePageviews"]
)。
$query = [
"viewId" => profileId,
"dateRanges" => [
"startDate" => "2018-01-25",
"endDate" => "2018-01-25"
],
"metrics" => [
["expression" => "ga:pageviews"],
["expression" => "ga:avgTimeOnPage"],
["expression" => "ga:uniquePageviews"]
],
"dimensions" => [
"name" => "ga:pagepath"
],
];