Hello分析报告API v4;用于Web应用程序的JavaScript快速入门

r1zhe5dt  于 2023-01-16  发布在  Java
关注(0)|答案(3)|浏览(135)

我试图根据官方文档-https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js与GA API建立功能连接。我做了设置它所需的一切,但解决方案不起作用。奇怪的是,验证按钮被加载,直到我选择谷歌帐户登录。之后,加载出现,验证窗口关闭。控制台中没有错误,什么也没有发生。
我目前在localhost上使用它,但我也在服务器上尝试过,结果相同。选择帐户后,下一次尝试甚至不需要选择帐户,因此窗口只是打开,加载出现,然后再次关闭,没有任何事情发生。localhost
CredentialsGoogle_Analytics_View

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Hello Analytics Reporting API V4</title>
  <meta name="google-signin-client_id" content="1086039826600-2r3481ge270o57vau3kshic6l4vrg4gg.apps.googleusercontent.com">
  <meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly">
</head>
<body>

<h1>Hello Analytics Reporting API V4</h1>

<!-- The Sign-in button. This will run `queryReports()` on success. -->
<p class="g-signin2" data-onsuccess="queryReports"></p>

<!-- The API response will be printed here. -->
<textarea cols="80" rows="20" id="query-output"></textarea>

<script>
  // Replace with your view ID.
  var VIEW_ID = '271208943';

  // Query the API and print the results to the page.
  function queryReports() {
    gapi.client.request({
      path: '/v4/reports:batchGet',
      root: 'https://analyticsreporting.googleapis.com/',
      method: 'POST',
      body: {
        reportRequests: [
          {
            viewId: VIEW_ID,
            dateRanges: [
              {
                startDate: '7daysAgo',
                endDate: 'today'
              }
            ],
            metrics: [
              {
                expression: 'ga:sessions'
              }
            ]
          }
        ]
      }
    }).then(displayResults, console.error.bind(console));
  }

  function displayResults(response) {
    var formattedJson = JSON.stringify(response.result, null, 2);
    document.getElementById('query-output').value = formattedJson;
  }
</script>

<!-- Load the JavaScript API client and Sign-in library. -->
<script src="https://apis.google.com/js/client:platform.js"></script>

</body>
</html>
ckocjqey

ckocjqey1#

我怀疑您遇到的问题是该示例使用的是Google Sign-In for Websites
我们将停止使用Google Sign-In JavaScript Web平台库。该库在2023年3月31日弃用日期之后将无法下载。请改用新的Google Identity Services Web。默认情况下,新创建的客户端ID现在被阻止使用旧的平台库,现有客户端ID不受影响。在7月29日之前创建的新客户端ID,2022可以设置plugin_name以启用Google平台库。
这意味着它不再工作了。我会ping团队,但上次我听说他们还在更新所有的Javascript样本。
鉴于UA分析将在一月份停止,我怀疑他们是否会费心更新这个API的样本。
我一有消息就通知你

af7jpaap

af7jpaap2#

因此,在怀疑Google Sign-In不再使用之后,我按照迁移到Google身份服务的说明添加了以下行**** metaname=“google-signin-plugin_name”content=“quickstart”****但我仍然无法正确加载数据或登录x1c 0d1x我不确定插件名称是否为第二个屏幕截图中的“quickstart”

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Hello Analytics Reporting API V4</title>
  <meta name="google-signin-client_id" content="1086039826600-2r3481ge270o57vau3kshic6l4vrg4gg.apps.googleusercontent.com">
  <meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly">
  <meta name="google-signin-plugin_name" content="quickstart">
</head>
<body>

<h1>Hello Analytics Reporting API V4</h1>

<!-- The Sign-in button. This will run `queryReports()` on success. -->
<p class="g-signin2" data-onsuccess="queryReports"></p>

<!-- The API response will be printed here. -->
<textarea cols="80" rows="20" id="query-output"></textarea>

<script>
  // Replace with your view ID.
  var VIEW_ID = '271208943';

  // Query the API and print the results to the page.
  function queryReports() {
    gapi.client.request({
      path: '/v4/reports:batchGet',
      root: 'https://analyticsreporting.googleapis.com/',
      method: 'POST',
      
      body: {
        reportRequests: [
          {
            viewId: VIEW_ID,
            dateRanges: [
              {
                startDate: '7daysAgo',
                endDate: 'today'
              }
            ],
            metrics: [
              {
                expression: 'ga:sessions'
              }
            ]
          }
        ]
      }
    }).then(displayResults, console.error.bind(console));
  }

  function displayResults(response) {
    var formattedJson = JSON.stringify(response.result, null, 2);
    document.getElementById('query-output').value = formattedJson;
  }
</script>

<!-- Load the JavaScript API client and Sign-in library. -->
<script src="https://apis.google.com/js/client:platform.js"></script>

</body>
</html>
k3bvogb1

k3bvogb13#

此问题是否有更新?
我今天遵循了相同的安装指南,并收到了一条“即将被弃用”的错误消息,阻止了快速入门示例的最终完成。
(and显然我们试用了Google报告API v4)
https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js#2_setup_the_sample

相关问题