1

I am using the gapi class in a CodeIgniter website. I am using this implementation:

http://jjc.net/2011/google-analytics-api-for-codeigniter/

This returns an array which works perfectly. I want to pass it to my js and I am doing this like so:

$gapi_arr = array();
$gapi_arr = $this->gapi->requestReportData($report_id, array('date'), array('pageviews', 'visits', 'newVisits'), 'date', '', $start_date, $end_date, 1, 366);

echo json_encode($gapi_arr);

However this returns:

[{},{},{},{},{}...

The original array that the gapi class returns looks like this:

Array
(
[0] => gapiReportEntry Object
    (
        [metrics:gapiReportEntry:private] => Array
            (
                [pageviews] => 3
                [visits] => 3
                [newVisits] => 0
            )

        [dimensions:gapiReportEntry:private] => Array
            (
                [date] => 20110101
            )

    )

[1] => ...

I just want to pass this array to my js without writing inline js code in my views. Is there a fix or another way to achieve this?

4 Answers 4

2

json_encode() can process object just fine, however, all the properties in the gapiReportEntry object are private and json_encode() only shows the public properties.

This explains all the empty objects {} in the json-array.

1

Change the PRIVATE by PUBLIC in gapi.php

class gapiReportEntry
    {
      public $metrics = array();
      public $dimensions = array();
      ....

DOne!

0

json_encode knows how to handle primatives, numeric index arrays and associative arrays. This is none of the above.

-1

The given array is not an array at all, look at the "gapiReportEntry", this is an Object. You should first iterate through all and convert to primitives.

1
  • Ok, didn't think that mattered since it is an array of objects. Thanks! Commented Jun 17, 2011 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.