2017-04-24 97 views
0

我運行一個小博客網站並在帖子列表中顯示GA瀏覽量。 問題是取回過程大約需要20s或更多。 爲了讓它更快,我發現了Guzzle6 API,但我不知道如何將這些API結合在一起工作。 我想要做的就是運行GA api同時爲帖子獲取綜合瀏覽量。Guzzle6 with Google Analytics API

或者如果你們知道任何其他方式更快地獲得每篇文章的瀏覽量,我將非常感激!

謝謝。

僅供參考,以下是我目前使用的GA api代碼。 (這適用於多個職位不錯,但速度太慢得到的瀏覽量。)

function views() { 

require_once 'gapi.php'; 
$jsonPath = __DIR__ . '/THE-PATH'; 
$serviceClientId = 'THE-SC-ID'; 
$pID = 'P-ID'; 
$slug = get_the_slug_for_each_post(); 

$ga=new gapi($serviceClientId,$jsonPath); 

date_default_timezone_set('Asia/Tokyo'); 
$dimensions = 'pagePath'; 
$metrics = 'Pageviews'; 
$sortMetric = null; 
$filter = 'ga:[email protected]/'.$slug; 
$startDate = '2015-01-01'; 
$endDate = date('Y-m-d'); 
$startIndex = 1; 
$maxResults = 10000; 

$ga->requestReportData($pID, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults); 

foreach($ga->getResults()as$result) { 
    $views = $result->getPageviews(); 
} 
    return $views; 
} 

回答

0

如果你看看​​你會看到,它已經使用狂飲。所以,不需要採取行動。

"name": "google/apiclient", 
    "type": "library", 
    "description": "Client library for Google APIs", 
    "keywords": ["google"], 
    "homepage": "http://developers.google.com/api-client-library/php", 
    "license": "Apache-2.0", 
    "require": { 
     "php": ">=5.4", 
     "google/auth": "^0.11", 
     "google/apiclient-services": "^0.11", 
     "firebase/php-jwt": "~2.0|~3.0|~4.0", 
     "monolog/monolog": "^1.17", 
     "phpseclib/phpseclib": "~0.3.10|~2.0", 
     "guzzlehttp/guzzle": "~5.2|~6.0", 
     "guzzlehttp/psr7": "^1.2" 
    } 

解決你的問題,我建議你下載數據提前(每天一次或左右)到數據庫表,您的服務器上,然後從本地數據庫中查詢每頁的數據。不僅如此,它還可以幫助您避免在您的網站發展時達到API的查詢限制。