我甚至可以将客户端JavaScript连接到Google-Analytics API吗?

发布于 2025-01-29 08:10:07 字数 2138 浏览 6 评论 0原文

在过去的一天中,我一直在研究Google Analytics(GA4)。而且我希望能够使用GA4 API函数,例如 Runreport 在客户端上。我希望每个网络客户都可以看到一块分析。

这是我从GAPI文档中提取的代码。

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for analyticsdata.properties.runReport
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/code-samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/analytics.readonly"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    gapi.client.setApiKey("FILLED_THIS_WITH_MY_API_KEY");
    return gapi.client.load("https://analyticsdata.googleapis.com/$discovery/rest?version=v1beta")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.analyticsdata.properties.runReport({
      "resource": {}
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "FILLED_THIS_WITH_MY_CLIENT_ID"});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

这给了我这个错误:

gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy\

I have been researching the google analytics api (GA4) for the past day. And I want to be able to use GA4 api functions like runReport on the client-side. There is a piece of analytics that I want each of my web-clients to see.

Here is the code that I pulled out from the gapi documentation.

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for analyticsdata.properties.runReport
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/code-samples#javascript
   */

  function authenticate() {
    return gapi.auth2.getAuthInstance()
        .signIn({scope: "https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/analytics.readonly"})
        .then(function() { console.log("Sign-in successful"); },
              function(err) { console.error("Error signing in", err); });
  }
  function loadClient() {
    gapi.client.setApiKey("FILLED_THIS_WITH_MY_API_KEY");
    return gapi.client.load("https://analyticsdata.googleapis.com/$discovery/rest?version=v1beta")
        .then(function() { console.log("GAPI client loaded for API"); },
              function(err) { console.error("Error loading GAPI client for API", err); });
  }
  // Make sure the client is loaded and sign-in is complete before calling this method.
  function execute() {
    return gapi.client.analyticsdata.properties.runReport({
      "resource": {}
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "FILLED_THIS_WITH_MY_CLIENT_ID"});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

And it gives me this error:

gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文