fr30n / ganalytics

一个用于获取 Google Analytics 数据的 Laravel 5 包。

1.0 2019-03-28 13:33 UTC

This package is auto-updated.

Last update: 2024-09-29 02:15:29 UTC


README

Latest Version Software License Build Status Quality Score StyleCI Total Downloads

使用此包可以轻松地从 Google Analytics 获取数据。

以下是一些提供的方法示例

use GAnalytics;
use Fr3on\GAnalytics\Period;

//fetch the most visited pages for today and the past week
GAnalytics::fetchMostVisitedPages(Period::days(7));

//fetch visitors and page views for the past week
GAnalytics::fetchVisitorsAndPageViews(Period::days(7));

大多数方法将返回一个包含结果的 \Illuminate\Support\Collection 对象。

Fr3on 是一家位于埃及开罗的软件代理公司。您可以在我们的网站上找到所有开源项目的概述 (点击访问)

安装

此包可以通过 Composer 安装。

composer require fr30n/ganalytics

可选地,您可以使用以下命令发布此包的配置文件

php artisan vendor:publish --provider="Fr30n\GAnalytics\GAnalyticsServiceProvider"

以下配置文件将发布到 config/ganalytics.php

return [

    /*
     * The view id of which you want to display data.
     */
    'view_id' => env('ANALYTICS_VIEW_ID'),

    /*
     * Path to the client secret json file. Take a look at the README of this package
     * to learn how to get this file. You can also pass the credentials as an array 
     * instead of a file path.
     */
    'service_account_credentials_json' => storage_path('app/ganalytics/service-account-credentials.json'),

    /*
     * The amount of minutes the Google API responses will be cached.
     * If you set this to zero, the responses won't be cached at all.
     */
    'cache_lifetime_in_minutes' => 60 * 24,

    /*
     * Here you may configure the "store" that the underlying Google_Client will
     * use to store it's data.  You may also add extra parameters that will
     * be passed on setCacheConfig (see docs for google-api-php-client).
     *
     * Optional parameters: "lifetime", "prefix"
     */
    'cache' => [
        'store' => 'file',
    ],
];

使用

安装完成后,您可以轻松地获取 Analytics 数据。几乎所有方法都将返回一个 Illuminate\Support\Collection 实例。

以下是一些使用时期的示例

//retrieve visitors and pageview data for the current day and the last seven days
$analyticsData = GAnalytics::fetchVisitorsAndPageViews(Period::days(7));

//retrieve visitors and pageviews since the 6 months ago
$analyticsData = GAnalytics::fetchVisitorsAndPageViews(Period::months(6));

//retrieve sessions and pageviews with yearMonth dimension since 1 year ago 
$analyticsData = GAnalytics::performQuery(
    Period::years(1),
    'ga:sessions',
    [
        'metrics' => 'ga:sessions, ga:pageviews',
        'dimensions' => 'ga:yearMonth'
    ]
);

$analyticsData 是一个 Collection,其中每个项目都是一个包含 datevisitorspageViews 键的数组。

如果您想要更精确地控制要获取数据的时期,可以向时期对象传递一个 startDate 和一个 endDate

$startDate = Carbon::now()->subYear();
$endDate = Carbon::now();

Period::create($startDate, $endDate);

提供的方法

访客和页面浏览量

public function fetchVisitorsAndPageViews(Period $period): Collection

该函数返回一个包含 datevisitorspageTitlepageViews 键的数组的 Collection

总访客和页面浏览量

public function fetchTotalVisitorsAndPageViews(Period $period): Collection

该函数返回一个包含 datevisitorspageViews 键的数组的 Collection

最受欢迎的页面

public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection

该函数返回一个包含 urlpageTitlepageViews 键的数组的 Collection

顶级引用者

public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection

该函数返回一个包含 urlpageViews 键的数组的 Collection

用户类型

public function fetchUserTypes(Period $period): Collection

该函数返回一个包含 typesessions 键的数组的 Collection

顶级浏览器

public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection

该函数返回一个包含 browsersessions 键的数组的 Collection

所有其他 Google Analytics 查询

要执行对 Google Analytics 资源的查询,请使用 performQuery。有关可能使用的指标和维度的更多信息,请参阅 Google 的核心报告 API

public function performQuery(Period $period, string $metrics, array $others = [])

您可以访问底层的 Google_Service_Analytics 对象

GAnalytics::getAnalyticsService();

测试

使用以下命令运行测试

vendor/bin/phpunit

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现任何安全问题,请通过电子邮件 hello@fr30n.com 而不是使用问题跟踪器。

致谢

支持我们

Fr3on 是一家位于埃及开罗的软件代理公司。您可以在我们的网站上找到所有开源项目的概述 (点击访问)

您的业务依赖于我们的贡献吗?请与我们联系,并在 Patreon 上支持我们。所有承诺都将用于分配人力资源以维护和新奇事物。

许可证

麻省理工学院许可证(MIT)。有关更多信息,请参阅许可文件