Codeigniter网站上的Google Analytics API [英] Google Analytics API on Codeigniter website

查看:199
本文介绍了Codeigniter网站上的Google Analytics API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让Google API在我的网站上工作好几天,但没有得到它的工作。在我搜索的任何地方,我都会发现过时的例子...我需要它来处理codeigniter,我想要做的就是从Google Analytics获取数据,以便在我的codeigniter网站的管理控制台中显示。



我明白我需要一个服务帐户,如果我不想每次查看仪表板(?)都要进行身份验证(?)

任何人都可以请帮我解决这个问题?提前感谢你!

我想要做的是:从Google Analytics获取数据,将数据返回为json,然后我可以制作一张图表(可能有一些jQuery插件已经存在,或者我可以使用Google自己的图表抽屉?)并将其显示给管理员。我只是想要非常简单的数据,例如上个月有多少用户......

解决方案

大约一周后,当然,在我发布这个问题的同一天,我终于设法自己解决这个问题。



这是我如何去做的:



我从下载了最新的Google Client API(for php) github



我在我的应用程序/ third_party文件夹中添加了Google文件夹(src)。

在我的控制器内部,我通过执行以下操作包含了所需的文件:

  require_once(BASEPATH。'../application/third_party/Google/ Client.php'); 
require_once(BASEPATH。'/application/third_party/Google/Service/Analytics.php');

然后我在下面的代码中添加了以下用于授权服务帐户的代码(您从 Google控制台并选择您的项目> APIs& auth> Credentials>然后创建一个新的客户端ID,选择服务帐户,完成后按生成新的p12键并将该键添加到您的third_party / Google文件夹中:

  session_start(); 

$ client_id ='< YOUR_CLIENT_ID>'; //客户端ID
$ service_account_name ='< YOUR_CLIENT_EMAIL>'; //电子邮件地址
$ key_file_location = BASEPATH。' ../application/third_party/Google/<YOUR KEY.p12>'; //key.p12

$ client = new Google_Client();
$ client-> setApplicationName( ($ _SESSION ['service_token'])){
$ client->
$ service = setAccessToken($ _ SESSION ['service_token ]);
}

$键的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();

以下代码用于获取最近31天的会话(页面访问)

  $ analytics = new Google_Service_Analytics($ client); 

$ profileId =ga:< YOUR_PROFILE_ID>;
$ startDate = date('Y-m-d',strtotime(' - 31 days')); //从现在开始的31天
$ endDate = date('Y-m-d'); //今天的日期

$ metrics =ga:sessions;

$ optParams = array(dimensions=>ga:date);
$ results = $ analytics-> data_ga-> get($ profileId,$ startDate,$ endDate,$ metrics,$ optParams);

$ data ['report'] = $ results->行;稍后将其发送到视图

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

$ $ p $ $ this-> view-> load(' your_view',$ data);

要将它写成图表,我只用

p>

希望这能帮助未来有这个问题的人。


I've been trying to get Google API to work on my website for days now, but not getting it to work. Everywhere I search I find outdated examples... I need it to work with codeigniter and all I want to do is to fetch data from Google Analytics to show in the admin dashboard on my codeigniter website.

I understand that I need a Service Account, if I don't want to be authenticating everytime I look at the dashboard(?)

Can anyone please help me with getting this to work? Thank you so much in advance!

What I want done is: Fetch data from google analytics, return that data as maybe json, then I guess I can make a chart out of it with a plugin (perhaps some jQuery plugin already exists, or can I use googles own chart drawers?) and show this to administrators. I just want really simple data, as for example, how many users the last month...

解决方案

After about a week - and of course, on the same day I post this question, I finally manage to fix this myself.

This is how I go about:

I downloaded the latest Google Client API (for php) from their github.

I added the Google folder (src) in my application/third_party folder.

Inside my controller I included the required files by doing:

require_once(BASEPATH . '../application/third_party/Google/Client.php');
require_once(BASEPATH . '../application/third_party/Google/Service/Analytics.php');

Then I added the following code below that for authorization with Service Account (which you get from the Google Console and selecting your project > APIs & auth > Credentials > Then create a new Client ID, select Service Account, when done, press "Generate new p12 key" and add that key to your third_party/Google folder:

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

The code below is for getting sessions (page visits) from the last 31 days

$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

To get all the dimensions and metrics that you can use, use this link. Example on how to send it to the view:

$this->view->load('your_view', $data);

To write it out as a chart I just used Google charts, in the JS (in the view) I just looped the data from $data['report'] to draw a chart.

Hope this will help people with this problem in the future.

这篇关于Codeigniter网站上的Google Analytics API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆