Google Analytics(分析)如何通过php

时间:2016-09-29 12:15:36

标签: oauth google-analytics

我正在尝试为Google Analytics数据创建信息中心,但我不知道如何合并这些脚本。

我必须使用像https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-php这样的服务器到服务器身份验证,但我无法为我的Javascript身份验证获取访问令牌

gapi.analytics.auth.authorize({
   serverAuth: {
      access_token: >>How to get an Access token from PHP<<
   }
});

如果我只是做这样的事情

$KEY_FILE_LOCATION = __DIR__ . '/Service.json';
$client = new Google_Client();
$client->setApplicationName("Hello Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
var_dump($client->getAccessToken())

我的getAccessToken总是NULL是否有任何其他方法来接收服务器到服务器连接的访问​​令牌?我必须找到不需要通过Javascript登录的方法,因为内容管理系统的所有后端用户都应该能够看到数据。

非常感谢你。我是新手,如果你能解释它是如何工作的,那将是非常友好的,而不仅仅是发送一个链接

编辑:换句话说这就是我的整个代码......为什么它不起作用,出了什么问题?请帮帮我

    <?php

// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';

$accessToken = initializeAnalytics();

function initializeAnalytics()
{
  $KEY_FILE_LOCATION = __DIR__ . '/Service.json';

  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
  $accessToken = $client->getAccessToken();
  return $accessToken;
}

?>

<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>


<div id="chart-1-container"></div>

<script>
    gapi.analytics.ready(function() {
        gapi.analytics.auth.authorize({
          serverAuth: {
            access_token: '<?php echo $accessToken ?>'
          }
        });

        var dataChart = new gapi.analytics.googleCharts.DataChart({
            query: {
                ids: "ga:128053216",
                metrics: 'ga:sessions',
                dimensions: 'ga:date',
                'start-date': '30daysAgo',
                'end-date': 'yesterday',
            },
            chart: {
                container: 'chart-1-container',
                type: 'LINE',
            }
        });
        dataChart.execute();
    });
</script>

1 个答案:

答案 0 :(得分:3)

经过几天的尝试后终于找到了解决方案.... 它现在可以使用这个脚本

    <?php

// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';

$accessToken = initializeAnalytics();

function initializeAnalytics()
{
  $KEY_FILE_LOCATION = __DIR__ . '/Service.json';

  $client = new Google_Client();
  $client->setApplicationName("Hello Analytics Reporting");
  $client->setAuthConfig($KEY_FILE_LOCATION);
  $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);

  $client->refreshTokenWithAssertion();
  $token = $client->getAccessToken();
  $accessToken = $token['access_token'];

  return $accessToken;
}

?>

<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>


<div id="chart-1-container"></div>

<script>
    gapi.analytics.ready(function() {
        gapi.analytics.auth.authorize({
          serverAuth: {
            access_token: '<?php echo $accessToken ?>'
          }
        });

        var dataChart = new gapi.analytics.googleCharts.DataChart({
            query: {
                ids: ">your analytics ID<",
                metrics: 'ga:sessions',
                dimensions: 'ga:date',
                'start-date': '30daysAgo',
                'end-date': 'yesterday',
            },
            chart: {
                container: 'chart-1-container',
                type: 'LINE',
            }
        });
        dataChart.execute();
    });
</script>