且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Codeigniter 网站上的 Google Analytics API

更新时间:2023-12-02 16:40:46

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

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

这就是我的做法:

我从 他们的 github 下载了最新的 Google Client API(用于 php).

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

我在 application/third_party 文件夹中添加了 Google 文件夹 (src).

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

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

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

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

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

为了将其写成图表,我在 JS 中使用了 Google 图表(在视图中)我只是循环了 $data['report'] 中的数据来绘制图表.

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.