![](/img/trans.png)
[英]How creating a report with dimensionFilter and matchType in PHP ? (GA4)
[英]GA4 run report setting metrics & dimenssion
Using php laravel i am implementing GA4 data analytics api to get metrics data of my GA4 client website but i am unable to find method setName in Google_Service_AnalyticsReporting_Metric class when running script with out adding name i am getting error
Metric.name is required and must not be the empty string
这是我的代码
$analytics=new Google_Service_AnalyticsData($client);
// Create the ReportRequest object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$metric = new Google_Service_AnalyticsReporting_Metric();
$metric->setExpression("ga:city");
// Create the Entity object.
$entity=new Google_Service_AnalyticsData_Entity();
$entity->setPropertyId("11111111");
// Create the Request object.
$request = new Google_Service_AnalyticsData_RunReportRequest();
$request->setEntity($entity);
$request->setDateRanges($dateRange);
$request->setMetrics($metric);
$data=$analytics->v1alpha->runReport($request);
请将“Google_Service_AnalyticsReporting_Metric”替换为“Google_Service_AnalyticsData_Metric”。 替换该变量后,应该存在 setName 方法。
如果要检索 GA4 媒体资源的指标数据,则需要使用 AnalyticsData API。 如果要检索 Universal Analytics 视图的指标数据,可以使用 AnalyticsReporting API。 每个 API 都有单独的 PHP 类。
在此代码段中,两个 API 的代码都存在。 这两个独立 API 的类不能互换使用。
在 AnalyticsData API 中,指标 class 有一个名称字段: https://developers.google.com/analytics/devguides/reporting/Metricvdata/v
这是 AnalyticsReporting API: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php的参考。 所有对象都以“Google_Service_AnalyticsReporting_”开头。
此外,“城市”是一个维度,而不是一个指标。 在 GA4 中,维度和指标名称不以“ga:”为前缀; 维度名称只是“城市”而不是“ga:city”。 对于 AnalyticsData API,这些是 API 中可用的维度和指标: https://developers.google.com/analytics/devguides/reporting//
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.