codeigniter谷歌分析+谷歌图表

时间:2015-04-30 03:43:06

标签: php codeigniter charts google-analytics-api

我使用以下建议创建了与Google Analytics(分析)库集成的codeigniter应用程序: Google Analytis API on Codeigniter website

要获取可以使用的所有维度和指标,请使用此link。有关如何将其发送到视图的示例:

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

在视图中我曾经用来显示数据

print_r ($report)

结果是:

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

我需要处理此结果并与Google图表集成。有人可以帮我一把吗?

完成项目后...我将发布关于如何构建codeigniter谷歌分析和谷歌图表的完整教程

1 个答案:

答案 0 :(得分:1)

您可以使用一些ajax来填充图表:

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

在ajax控制器上,您应该有一个名为&#34; google_analytics&#34;的方法。接收$ report_type变量并从您的Google Analytics库返回json。例如:

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

您应该格式化从Google Analytics收到的数据以支持图表。

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

Google Developers也提供了一个示例:https://developers.google.com/chart/interactive/docs/php_example