ozankurt/google-analytics

Laravel Google Analytics

v2.0.1 2017-01-27 05:36 UTC

This package is auto-updated.

Last update: 2024-09-25 01:02:00 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

连接到您的分析账户并获取所需数据从未如此简单。 😎

安装

步骤 1

ozankurt/google-analytics 添加到您的 composer 需求中。

composer require ozankurt/google-analytics

步骤 2

通过遵循其 README 配置 ozankurt/google-core 包。

步骤 3

选择分析 API

Select the Analytics API

启用它

Enable It

步骤 4

将分析范围添加到 ozankurt/google-core 包中的配置的 scopes 数组中。

'scopes' => [
	'https://www.googleapis.com/auth/analytics.readonly',
],

使用(Laravel)

步骤 1

analytics.viewId 添加到您的 config/google.php

    /**
     * View ID can be found in `http://google.com/analytics` under the `Admin` tab on navigation.
     *
     * Select `Account`, `Property` and `View`. You will see a `View Settings` link.
     */
    'analytics' => [
        'viewId' => 'ga:12345678',
    ],

控制器示例

use Kurt\Google\Analytics\Analytics as GoogleAnalytics;

class GoogleController extends Controller
{
    private $ga;

    function __construct(GoogleAnalytics $ga) {
        $this->ga = $ga;
    }

    public function index()
    {
        $results = $this->ga->getUsersAndPageviewsOverTime();

        var_dump($results);
    }
}

使用(纯 PHP)

示例

<?php

require 'vendor/autoload.php';

use Kurt\Google\Core\Core;
use Kurt\Google\Analytics\Analytics;

$googleCore = new Core([
    'applicationName'       => 'Google API Wrapper Demo',
    'jsonFilePath'          => 'Google API Wrapper Demo-174e172143a9.json',
    'scopes' => [
        Google_Service_Analytics::ANALYTICS_READONLY,
    ],
    'analytics' => [
        'viewId' => 'ga:97783314'
    ],
]);

$analytics = new Analytics($googleCore);

$results = $analytics->getUsersAndPageviewsOverTime();

var_dump($results);

结果

这两个示例都将给出类似的结果。

GoogleController@index 的结果应如下所示

array (size=2)
    'cols' => 
        array (size=2)
            0 => string 'ga:sessions' (length=11)
            1 => string 'ga:pageviews' (length=12)
    'rows' => 
        array (size=1)
            0 => array (size=2)
                'ga:sessions' => string '100' (length=3)
                'ga:pageviews' => string '250' (length=3)

许可证

此开源软件根据 MIT 许可证 许可。