javascript - Google Analytics 数据显示在我的网站上/使用 Javascript

标签 javascript c# google-analytics analytics google-analytics-api

我需要在我的网站上显示 Analytics 仪表板。就像将其从谷歌的网站导出到我的网站一样。 我已经使用了 Reporting API V4 但我只得到登录按钮。 as you can see, the dashboard from analytics are not displayed.

我需要使用 javascript 或 C# 来完成此操作。

我的代码(基于this link):

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Analytics Reporting </title>
    <meta name="google-signin-client_id" content="472734728686-21tqq751d9mdpkopnjmc1dnctf9644qc.apps.googleusercontent.com">
    <meta name="google-signin-scope" content="https://www.googleapis.com/auth/analytics.readonly">
</head>
<body>

    <h1> Analytics Reporting</h1>

    <p>

        <div class="g-signin2" data-onsuccess="queryReports"></div>
    </p>


    <textarea cols="80" rows="20" id="query-output"></textarea>

    <script>

        var VIEW_ID = '130082935';


  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>




    <script src="https://apis.google.com/js/client:platform.js"></script>

</body>
</html>

最佳答案

我建议您查看Google Analytics Embed API 。从我的经验来看,这要容易得多。

关于javascript - Google Analytics 数据显示在我的网站上/使用 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39637428/

相关文章:

javascript - 在 Firebug 控制台中运行 jQuery 调用

c# - 在 C# 中将视频的所有帧提取为图像的最快方法是什么?

c# - 从 javascript 拖放事件调用 C# 方法

javascript - 如何在 WordPress 中翻译 Vue 输出?

javascript - 如何在滚动时从屏幕顶部添加一个 div 偏移量?

c# - 运行 MySql 存储过程的最低访问级别

ionic-framework - ionic 1 在特定屏幕中集成特定的谷歌分析

google-analytics - 在Android应用程序中支持多个Install接收器

google-analytics - React + 路由器 + Google 标签管理器

javascript - 如何在<选项值="url here & js here">中显示JS值