2017-04-24 98 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的查询限制。