1

i have followed this tutorial and implemented google analytics on my website to see the website views traffic but getting a blank page for this.

Google Analytics API on Codeigniter website

I have downloaded the google API code and added in my application/third_party folder. Created key downloaded and uploaded into my google folder.

Controller:

<?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);

View:(api.php)

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

My website url looks like this staging.website.com/admin

Pouya Samie
  • 3,718
  • 1
  • 21
  • 34
user8001297
  • 173
  • 1
  • 4
  • 18
  • 1
    Please have a look here https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – BenRoob Aug 31 '17 at 09:38
  • Any solutions for this please help me out this problem – user8001297 Aug 31 '17 at 10:31
  • One possible error, which you could have detected, with a look at error reporting: in require_once you are missing the point operator "BASEPATH . 'application...'". But it is hard to figure out without an error message. – BenRoob Aug 31 '17 at 10:34
  • Not getting any error message displaying only blank page – user8001297 Aug 31 '17 at 10:35
  • A blank page does not mean, that no error is logged/tracked. You can easily turn on error reporting to display error messages. Otherwise, the error log will give you hints, that is why I linked to the debugging SO. – BenRoob Aug 31 '17 at 10:37

0 Answers0