Codeigniter google analytics + google chart

I created a codeigniter app integrated with Google Analytics library using the following suggestion: Google Analytis API on Codeigniter website

For all the sizes and measures you can use, use the link. An example of how to send it to the view:

$results = $analytics->data_ga->get($profileId, $startDate, $endDate, $metrics, $optParams);
$this->view->load('your_view', $data);

      

and i used to display data

print_r ($report)

      

and the result is:

Array ( [0] => Array ( [0] => 20150330 [1] => 0 ) [1] => Array ( [0] => 20150331 [1] => 0 ) [2] => Array ( [0] => 20150401 [1] => 0 ) [3] => Array ( [0] => 20150402 [1] => 0 ) [4] => Array ( [0] => 20150403 [1] => 0 ) [5] => Array ( [0] => 20150404 [1] => 0 ) [6] => Array ( [0] => 20150405 [1] => 0 ) [7] => Array ( [0] => 20150406 [1] => 0 ) [8] => Array ( [0] => 20150407 [1] => 0 ) [9] => Array ( [0] => 20150408 [1] => 0 ) [10] => Array ( [0] => 20150409 [1] => 0 ) [11] => Array ( [0] => 20150410 [1] => 1 ) [12] => Array ( [0] => 20150411 [1] => 0 ) [13] => Array ( [0] => 20150412 [1] => 0 ) [14] => Array ( [0] => 20150413 [1] => 0 ) [15] => Array ( [0] => 20150414 [1] => 1 ) [16] => Array ( [0] => 20150415 [1] => 1 ) [17] => Array ( [0] => 20150416 [1] => 0 ) [18] => Array ( [0] => 20150417 [1] => 0 ) [19] => Array ( [0] => 20150418 [1] => 0 ) [20] => Array ( [0] => 20150419 [1] => 0 ) [21] => Array ( [0] => 20150420 [1] => 1 ) [22] => Array ( [0] => 20150421 [1] => 0 ) [23] => Array ( [0] => 20150422 [1] => 0 ) [24] => Array ( [0] => 20150423 [1] => 1 ) [25] => Array ( [0] => 20150424 [1] => 0 ) [26] => Array ( [0] => 20150425 [1] => 0 ) [27] => Array ( [0] => 20150426 [1] => 0 ) [28] => Array ( [0] => 20150427 [1] => 1 ) [29] => Array ( [0] => 20150428 [1] => 0 ) [30] => Array ( [0] => 20150429 [1] => 1 ) [31] => Array ( [0] => 20150430 [1] => 0 ) )

      

I need to process this result and integrate with google graph. Can anyone give me a hand?

After I complete the project ... I will post a complete tutorial on how to create google google analytics and google charts

+3


source to share


1 answer


You can use some ajax to fill the chart:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawGAnalytics);

function drawGAnalytics() {
    var jsonData = $.ajax({ url: "/ajax/google_analytics",
                            type: "POST",
                            dataType: "json",
                            data: { report_type: "google_analytics" },
                            async: false,
                            cache: false
                        }).responseText;

    var data = new google.visualization.DataTable(jsonData);

    var options = { title: 'Page Views' };

    var chart = new google.visualization.LineChart(document.getElementById('ga-chart'));
        chart.draw(data, options);
}
</script>
<style type="text/css">
    #ga_chart_container {
        width: 100%; 
        text-align: center; 
        height: auto; 
        float:left; 
        margin:0 auto; 
        margin-bottom:20px
    }

        #ga_chart_container #ga-chart {
            width: 900px; 
            height: 500px; 
            margin:0 auto;
        }
</style>

<div id="ga_chart_container">
    <div id="ga-chart"></div>
</div>

      

On your ajax controller, you should have a "google_analytics" method that gets the $ report_type variable and returns the json from your google analytics library. For example:

$ga_analytics = $analytics->data_ga->....;

      



You should probably format the data received from Google Analytics to support charts.

$this->output->set_content_type('application/json')->set_output(json_encode($ga_analytics));

      

Google devs also provide an example: https://developers.google.com/chart/interactive/docs/php_example

+1


source







All Articles