【问题标题】:Guzzle6 with Google Analytics apiGuzzle6 与 Google Analytics api
【发布时间】:2017-04-24 13:03:05
【问题描述】:

我运行一个小型博客网站并在帖子列表中显示 GA 网页浏览量。 问题是获取过程大约需要 20 秒或更长时间。 为了让它更快,我发现了 Guzzle6 api,但我不知道如何将这些 api 组合在一起工作。 我想要做的是运行 GA 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:pagePath=@/'.$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;
}

【问题讨论】:

    标签: php api google-analytics guzzle pageviews


    【解决方案1】:

    如果您查看composer file of the PHP API client,您会发现它已经使用了 Guzzle。所以,不需要采取行动。

    "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 的查询限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多