Reputation: 91
I created a codeigniter application integrated with google analytics library using the following suggestion: Google Analytis API on Codeigniter website
To get all the dimensions and metrics that you can use, use this link. Example on how to send it to the view:
$results = $analytics->data_ga->get($profileId, $startDate, $endDate, $metrics, $optParams);
$this->view->load('your_view', $data);
and in view I used to display the 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 treat this result and integrate with google chart. Could someone give me a hand?
After I finish the project ... I'll post the full tutorial on how to build the codeigniter google analytics and google charts
Upvotes: 3
Views: 3345
Reputation: 36
You could use some ajax to populate your 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 ajax controller you should have a method called "google_analytics" that receives a $report_type variable and returns the json from your Google Analytics library. Eg:
$ga_analytics = $analytics->data_ga->....;
You should probably format data received from Google Analytics to support Charts.
$this->output->set_content_type('application/json')->set_output(json_encode($ga_analytics));
Google Developers provides an example as well: https://developers.google.com/chart/interactive/docs/php_example
Upvotes: 1