![](/img/trans.png)
[英]how to show Google analytics data impressions/clicks/ctr to my site?
[英]How to query data from Google Analytics of a site?
首先,我以前從未使用過Google Analytics(分析),現在,當我需要一點點混亂來掌握流程時,我從來沒有使用過。
我在網上做了很多研究。 我遇到的是,您需要具有在開發人員控制台上創建的密鑰以進行身份驗證。 如果我有此密鑰,則可以按照發現的標准示例檢索站點所需的任何數據。
但是我有一些疑問:
https://console.developers.google.com
創建密鑰並使用該密鑰讀取客戶端數據一樣嗎? 只要他們在帳戶中添加了我,此密鑰是否可以充當一站式中心來驗證自己從任何站點訪問任何api的身份? https://analytics.google.com/analytics/web
: https://analytics.google.com/analytics/web
請向我解釋如何通過PHP讀取他人站點數據的正確流程。我只需要總體思路即可。
先感謝您。
我嘗試一個例子首先,谷歌客戶端
作曲家需要“ google / apiclient”
在console.developers.google.com中:
2)憑據文件
在以下位置創建服務帳戶:
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);
}
您可以使用兩種方法將站點搜索用於基於POST的搜索引擎:
選項1:配置您的Web應用程序以將查詢關鍵字附加到URL的末尾(例如, http : //www.example.com/search_results.php?q=keyword ),然后如前所述設置站點搜索部分。
選項2:自定義結果頁上的跟蹤代碼,以動態指定包含查詢關鍵字的虛擬頁路徑。 結果頁面上的跟蹤代碼如下所示:
analytics.js:ga('send','pageview','/search_results.php?q=keyword');
參考: https : //support.google.com/analytics/answer/1012264?hl=zh_CN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.