gpt4 book ai didi

php - 如何使用 Google Analytics API 在 php 中提取 channel 信息(在获取下)?

转载 作者:行者123 更新时间:2023-12-01 22:30:37 25 4
gpt4 key购买 nike

我正在尝试使用 Google Analytics API (PHP) 将有关 channel (主要是 Facebook)的信息提取到我的系统中。我想提取的一些信息是每个 channel 的 session 数、顶部登陆页面、跳出率等,但是尽管在查询浏览器上尝试了 ga:acquisitionTrafficChannel 和 ga:channelGrouping 似乎没有任何效果。

我查看了 Google API 文档的其他位置,并浏览了各个网站,但我要么被告知目前不存在用于此目的的 API,要么使用上述似乎不起作用的 API。

上面的两个维度/指标是否应该起作用并执行我想要做的事情,如果是的话,如何或目前没有可用的 API 可以让我执行我需要做的事情?

谢谢

最佳答案

维度ga:acquisitionTrafficChannelV4 Cohort & lifetime Value dimension ,仅适用于 Analytics Reporting API V4蜜蜂。查询浏览器是 builtCore Reporting API V3 .

下面我给出了 JSON 和 PHP 中的相同示例请求。

JSON 队列请求

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "XXXX",
"dimensions":
[
{
"name": "ga:cohort"
},
{
"name": "ga:acquisitionTrafficChannel"
}
],
"metrics":
[
{
"expression": "ga:cohortActiveUsers"
}
],
"cohortGroup":
{
"cohorts":
[
{
"name": "cohort 1",
"type": "FIRST_VISIT_DATE",
"dateRange":
{
"startDate": "2015-08-01",
"endDate": "2015-09-01"
}
},
{
"name": "cohort 2",
"type": "FIRST_VISIT_DATE",
"dateRange":
{
"startDate": "2015-07-01",
"endDate": "2015-08-01"
}
}
]
}
}
]
}

PHP 队列请求

function cohortRequest(&$analyticsreporting) {
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("XXXX");

$cohortDimension = new Google_Service_AnalyticsReporting_Dimension();
$cohortDimension->setName("ga:cohort");

$acquisitionTrafficChannel = new Google_Service_AnalyticsReporting_Dimension();
$acquisitionTrafficChannel->setName("ga:acquisitionTrafficChannel");

// Set the cohort dimensions
$request->setDimensions(array($cohortDimension, $acquisitionTrafficChannel));

$cohortActiveUsers = new Google_Service_AnalyticsReporting_Metric();
$cohortActiveUsers->setExpression("ga:cohortActiveUsers");

// Set the cohort metrics
$request->setMetrics(array($cohortActiveUsers));

$dateRange1 = new Google_Service_AnalyticsReporting_DateRange();
$dateRange1->setStartDate("2015-08-01");
$dateRange1->setEndDate("2015-09-01");

// Create the first cohort
$cohort1 = new Google_Service_AnalyticsReporting_Cohort();
$cohort1->setName("cohort_1");
$cohort1->setType("FIRST_VISIT_DATE");
$cohort1->setDateRange($dateRange1);

$dateRange2 = new Google_Service_AnalyticsReporting_DateRange();
$dateRange2->setStartDate("2015-07-01");
$dateRange2->setEndDate("2015-08-01");

// Create the second cohort which only differs from the first one by the date range
$cohort2 = new Google_Service_AnalyticsReporting_Cohort();
$cohort2->setName("cohort_2");
$cohort2->setType("FIRST_VISIT_DATE");
$cohort2->setDateRange($dateRange2);

// Create the cohort group
$cohortGroup = new Google_Service_AnalyticsReporting_CohortGroup();
$cohortGroup->setCohorts(array($cohort1, $cohort2));

$request->setCohortGroup($cohortGroup);

// 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());
}

查询浏览器

正如评论中所指出的,ga:channelGrouping 是一个标准的 V3 API 维度,它应该在 Query Explorer 中正常工作。 。正如您所看到的Query Explorer is designed to only list V3 Dimensions and Metrics

关于php - 如何使用 Google Analytics API 在 php 中提取 channel 信息(在获取下)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38434888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com