gpt4 book ai didi

php - 如何从网站的谷歌分析中查询数据?

转载 作者:行者123 更新时间:2023-12-03 17:27:30 26 4
gpt4 key购买 nike

首先,我以前从未使用过谷歌分析,现在当我需要它有点困惑来掌握流程时。

我在网上做了很多研究。我遇到的是,您需要拥有在开发人员控制台创建的 key 来进行身份验证。如果我有这个 key ,我可以按照找到的标准示例来检索我想要的站点数据。

不过我有几个疑问:

  • 我在自由职业者的基础上工作。所以我的客户允许我访问他们网站的谷歌分析。那么如何读取访问者数量等分析数据呢?由于我的电子邮件已经被允许访问数据,我可以查询还是我仍然需要应该是 json 格式的身份验证 key ?
  • 如果我需要 json key ,它是如何工作的?是否就像我在开发者控制台中创建了一个 key https://console.developers.google.com并使用此 key 读取客户端数据?只要他们将我添加到他们的帐户中,这个 key 是否就像一个一站式中心来验证我从任何站点访问任何 api 的身份?
  • 我在这里访问我客户的谷歌分析数据:https://analytics.google.com/analytics/web

  • 请向我解释有关如何通过 PHP 读取他人网站数据的正确流程。我只需要总体思路。

    先感谢您。

    最佳答案

    我尝试举个例子
    首先是谷歌客户端

    composer require "google/apiclient"



    在 console.developers.google.com 中:
  • 启用分析 API
  • 定义一个项目(例如:project-id)

  • 2) 凭证文件

    在以下位置创建服务帐户:

    https://console.developers.google.com/iam-admin/serviceaccounts?project=project-id



    enter image description here

    至此,您将在“ 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),对于您想要的女巫 View ($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);

    enter image description here

    添加 获取报告功能
    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);
    }

    关于php - 如何从网站的谷歌分析中查询数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52943778/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com