将google analytics api添加到codeigniter网站获取空白页面

时间:2017-08-31 09:17:36

标签: php codeigniter google-analytics

我已经按照本教程在我的网站上实施了谷歌分析,以查看网站查看流量但获取空白页面。

Google Analytics API on Codeigniter website

我已下载了谷歌API代码并添加到我的application / third_party文件夹中。 创建密钥下载并上传到我的google文件夹。

控制器:

body {
    transform: rotate(-90deg);
}

查看:(api.php)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        require_once(BASEPATH 'application/third_party/Google/Client.php');
        require_once(BASEPATH  'application/third_party/Google/Service/Analytics.php');
        session_start();

        $client_id = '<YOUR_CLIENT_ID>'; //Client ID
        $service_account_name = '<YOUR_CLIENT_EMAIL>'; //Email Address 
        $key_file_location = BASEPATH  'application/third_party/Google/<YOUR KEY.p12>'; //key.p12

        $client = new Google_Client();
        $client->setApplicationName("ApplicationName");
        $service = new Google_Service_Analytics($client);

        if (isset($_SESSION['service_token'])) {
          $client->setAccessToken($_SESSION['service_token']);
        }

        $key = file_get_contents($key_file_location);
        $cred = new Google_Auth_AssertionCredentials(
            $service_account_name,
            array(
                'https://www.googleapis.com/auth/analytics',
            ),
            $key,
            'notasecret'
        );
        $client->setAssertionCredentials($cred);
        if($client->getAuth()->isAccessTokenExpired()) {
          $client->getAuth()->refreshTokenWithAssertion($cred);
        }
        $_SESSION['service_token'] = $client->getAccessToken();
        $analytics = new Google_Service_Analytics($client);

        $profileId = "ga:<YOUR_PROFILE_ID>";
        $startDate = date('Y-m-d', strtotime('-31 days')); // 31 days from now
        $endDate = date('Y-m-d'); // todays date

        $metrics = "ga:sessions";

        $optParams = array("dimensions" => "ga:date");
        $results = $analytics->data_ga->get($profileId, $startDate, $endDate, $metrics, $optParams);

        $data['report'] = $results->rows; //To send it to the view later
        $this->view->load('api', $data);

我的网址网址如下<html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Year', 'Sales', 'Expenses'], ['2004', 1000, 400], ['2005', 1170, 460], ['2006', 660, 1120], ['2007', 1030, 540] ]); var options = { title: 'Company Performance', curveType: 'function', legend: { position: 'bottom' } }; var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); chart.draw(data, options); } </script> </head> <body> <div id="curve_chart" style="width: 900px; height: 500px"></div> </body> </html>

0 个答案:

没有答案