gpt4 book ai didi

php - 谷歌分析 API 关键字

转载 作者:行者123 更新时间:2023-12-03 16:15:12 24 4
gpt4 key购买 nike

我使用 google analytics example我想得到我的关键字列表。抱歉,我对分析 API 很陌生,找不到它的引用。这是从那里复制的代码:

<?php
require_once __DIR__ . '/vendor/autoload.php';
$analytics = initializeAnalytics();
$response = getReport($analytics);
printResults($response);

// Initializes an Analytics Reporting API service object.
function initializeAnalytics()
{
}
// Queries the Analytics Reporting API V4.
function getReport($analytics) {
$VIEW_ID = "xxxx";
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");

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

// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
}
// Parses and prints the Analytics Reporting API V4 response.
function printResults($reports) {
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();

for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
}

for ($j = 0; $j < count($metrics); $j++) {
$values = $metrics[$j]->getValues();
for ($k = 0; $k < count($values); $k++) {
$entry = $metricHeaders[$k];
print($entry->getName() . ": " . $values[$k] . "\n");
}
}
}
}
}

这会输出给定 View 过去 7 天的 session 数,但我想获取关键字。

最佳答案

您需要添加dimensions API 请求的参数。就像是

//Create the Dimensions object.
$keyword = new Google_Service_AnalyticsReporting_Dimension();
$keyword->setName("ga:keyword");
///...
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
// Here is an addition
$request->setDimensions(array($keyword));

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();

您可以在 Analytics 开发站点找到更多示例: https://developers.google.com/analytics/devguides/reporting/core/v4/samples

请记住,通过这种方式,您只能获取付费关键字的数据,因为在大多数情况下,Google Analytics(分析)不会跟踪自然关键字。

关于php - 谷歌分析 API 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48793870/

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