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

时间:2016-09-22 11:11:24

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

我需要在我的网站上显示Google 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>

1 个答案:

答案 0 :(得分:1)

我建议你看看Google Analytics Embed API。从我的经验来看,这更容易。