【问题标题】:How to by pass authentication in google analytics script?如何绕过谷歌分析脚本中的身份验证?
【发布时间】:2015-08-15 17:14:23
【问题描述】:

我想在我的网络应用程序中显示 Google Analytics(分析)报告。到目前为止,我已经通过服务器端的 OAuth 成功验证了一个用户,并且我已经成功地将 AccessToken 和 UserProfileId 存储在我的数据库表中。

现在我想显示如下图表:

这是我从源代码中获取的用于显示上图的脚本:Embeded API demo

脚本:

gapi.analytics.auth.authorize({
    container: 'embed-api-auth-container',
    clientid: 'REPLACE WITH YOUR CLIENT ID', // i dont want to authenticate here as i have already done authentication.instead use access token to by pass this
                                               authentication
  });

那么是否可以不使用此脚本对用户进行身份验证并仍然为登录用户显示谷歌分析图表??

我在互联网上进行了搜索,发现下面的链接有些有用,其中 Philip Walton 的回答是说有可能:Google Analytics Embed API authentication

所以如果有人这样做了,那么请提供任何解决方案。

【问题讨论】:

  • 我看不出这不是您链接到的问题的重复 - Philip Walton 链接的文档实际上有一个完整的示例。

标签: google-api google-analytics-api


【解决方案1】:

使用 JavaScript sdk 试试这个

<!DOCTYPE html>
<html>
<head>
  <title>Embed API Demo</title>
</head>
<body>

<!-- Step 1: Create the containing elements. -->

<section id="auth-button"></section>
<section id="view-selector"></section>
<section id="timeline"></section>
<hr/>
<section id="chart-1-container"></section>




<!-- Step 2: Load the library. -->

<script>
(function(w,d,s,g,js,fjs){
  g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(cb){this.q.push(cb)}};
  js=d.createElement(s);fjs=d.getElementsByTagName(s)[0];
  js.src='https://apis.google.com/js/platform.js';
  fjs.parentNode.insertBefore(js,fjs);js.onload=function(){g.load('analytics')};
}(window,document,'script'));
</script>

<script>
gapi.analytics.ready(function() {

  // Step 3: Authorize the user.

  var CLIENT_ID = 'xxxxxxxxxxx-xxxx.apps.googleusercontent.com';

  //gapi.analytics.auth.authorize({
  //  container: 'auth-button',
  //  clientid: CLIENT_ID,
  //});

   gapi.analytics.auth.authorize({
    'serverAuth': {
      'access_token': 'xx.xxxxx-xxxxxxxxxx_xxxxxxx'
    }
  });

  // Step 4: Create the view selector.

  var viewSelector = new gapi.analytics.ViewSelector({
    container: 'view-selector'
  });

  // Step 5: Create the timeline chart.

  var timeline = new gapi.analytics.googleCharts.DataChart({
    reportType: 'ga',
    query: {
      'dimensions': 'ga:date',
      'metrics': 'ga:users',
      'start-date': '30daysAgo',
      'end-date': 'yesterday',
    },
    chart: {
      type: 'LINE',
      container: 'timeline'
    }
  });

  var dataChart1 = new gapi.analytics.googleCharts.DataChart({
    query: {
      'ids': 'ga:xxxxxxxx', // <-- Replace with the ids value for your view.
      'start-date': '30daysAgo',
      'end-date': 'yesterday',
      'metrics': 'ga:sessions,ga:users',
      'dimensions': 'ga:date'
    },
    chart: {
      'container': 'chart-1-container',
      'type': 'LINE',
      'options': {
        'width': '100%'
      }
    }
  });
  dataChart1.execute();

  // Step 6: Hook up the components to work together.

  gapi.analytics.auth.on('success', function(response) {
    viewSelector.execute();
  });

  viewSelector.on('change', function(ids) {
    var newIds = {
      query: {
        ids: ids
      }
    }
    timeline.set(newIds).execute();
  });
});
</script>
</body>
</html>

【讨论】:

    【解决方案2】:

    我从这个链接中找到了这个选项Embed API - Component Reference,它解决了我的问题:

    我只需要设置access_token:

    gapi.analytics.auth.authorize({
         serverAuth: {
            access_token: 'XXXXXX'
          }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      相关资源
      最近更新 更多