首先,我以前和现在从未使用过谷歌分析技术,现在我需要用它来理解流程。
我在网上做了很多研究。我看到的是,您需要在开发人员控制台中创建的密钥来进行身份验证。如果我有这个密钥,我可以按照找到的标准示例来检索我想要的站点的任何数据。
然而,我有几点疑问:
https://console.developers.google.com中创建了一个键,并使用这个键读取客户端数据?这个密钥是否像一个一站式中心,在访问任何网站的api时验证自己的身份,只要他们在他们的帐户中添加了我?https://analytics.google.com/analytics/web请向我解释正确的流程如何读取别人的网站数据通过PHP。我只是需要一个整体的想法。
提前谢谢你。
发布于 2018-10-23 08:45:03
我试着举一个例子,首先是google客户机
作曲家要求"google/apiclient“
在console.developers.google.com中:
2) credentials_file
在以下位置创建服务帐户:
https://console.developers.google.com/iam-admin/serviceaccounts?project=project-id

您将在"path/to/the/service-account-credentials.json“上创建凭证文件
{
"type": "service_account",
"project_id": "project-id",
"private_key_id": "1234567890abcderf1234567890abcderf1234567890abcderf",
"private_key": "-----BEGIN PRIVATE KEY-----\nBASE64KEY=\n-----END PRIVATE KEY-----\n",
"client_email": "service-user@some.domain.gserviceaccount.com",
"client_id": "000000000000000000000000000000",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/cront-reriever-search-stats%40redooc-dot-com.iam.gserviceaccount.com"
}3)定义您想要的内容($infos),对于女巫视图($viewId)和凭据文件($credentials_file)和日期范围,您将查询API并在$response中获得结果。
$infos= [
'users' => 'ga:users',
'pageviews' => 'ga:pageviews',
'pageviewsPerSession' => 'ga:pageviewsPerSession',
'unique page view' => 'ga:uniquePageviews',
'organicSearches' => 'ga:organicSearches',
'avgSessionDuration' => 'ga:avgSessionDuration',
'avgTimeOnPage' => 'ga:avgTimeOnPage',
];
$credentials_file='path/to/the/service-account-credentials.json';
$viewId='1600000'; // the view ID see imgae
$client = new \Google_Client();
$credentials_file = $this->checkServiceAccountCredentialsFile()) {
$client->setAuthConfig($credentials_file);
$client->addScope("https://www.googleapis.com/auth/analytics.readonly");
$analytics = new \Google_Service_AnalyticsReporting($client);
$response = getReport($viewId, $analytics, $infos, $DateStart, $DateEnd);

添加 getReport函数
function getReport($viewId, $analytics, $dataAnalytics, $startDate, $endDate)
{
$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($startDate);
$dateRange->setEndDate($endDate);
// Create the ReportRequest object.
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($viewId);
$request->setDateRanges($dateRange);
// Create the Metrics object.
$_metrics = [];
foreach ($dataAnalytics as $gaLabel => $gaValue) {
$metric = new \Google_Service_AnalyticsReporting_Metric();
$metric->setExpression($gaValue);
// $metric->setAlias($gaLabel);
$_metrics[] = $metric;
}
$request->setMetrics($_metrics);
$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($request));
return $analytics->reports->batchGet($body);
}发布于 2018-10-23 08:05:01
您有两个选项可以使用基于帖子的搜索引擎的站点搜索:
选项1:配置web应用程序,将查询关键字附加到URL的末尾(例如,results.php?q=keyword),然后设置上一节中描述的站点搜索。
选项2:自定义结果页上的跟踪代码,以动态指定包含查询关键字的虚拟页面路径。结果页面上的跟踪代码看起来类似于:
analytics.js: ga('send','pageview',‘/search_Resul.php?q=关键字’);
参考资料:https://support.google.com/analytics/answer/1012264?hl=en
https://stackoverflow.com/questions/52943778
复制相似问题