phonghaw2/laravel-google-analytics

一个用于检索Google Analytics数据的Laravel包。

dev-main 2023-11-11 16:03 UTC

This package is auto-updated.

Last update: 2024-09-11 17:55:42 UTC


README

MIT Licensed Total Downloads

安装

适用于Laravel ^6.0|^7.0|^8.0

此包可以通过Composer安装。

composer require phonghaw2/laravel-google-analytics

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

php artisan vendor:publish --provider="Phonghaw2\Analytics\AnalyticsServiceProvider"

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

return [

    /*
     * The property id of which you want to display data.
     */
    'property_id' => env('ANALYTICS_PROPERTY_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/analytics/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',
    ],
];

获取凭证

将json文件保存到您的Laravel项目中,位置在包配置文件的 service_account_credentials_json 键指定的位置。由于json文件可能包含敏感信息,我不建议将其提交到您的git仓库。

授予Analytics属性权限

我假设您已经在Analytics网站上创建了Analytics账户,并正在使用新的GA4属性。

首先,您需要知道您的属性ID。在Analytics中,转到设置 > 属性设置。

在这里,您可以复制您的属性ID。将此值用于.env文件中的ANALYTICS_PROPERTY_ID键。

使用方法

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

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

use Phonghaw2\Analytics\AnalyticsFacade;
use Phonghaw2\Analytics\Period;

// You can set data with
Period::set($startDate, $endDate);
// https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange
// startDate, $endDate : The format NdaysAgo, yesterday, or today is also accepted

// Fetch the pages view for today and the 20 days ago
AnalyticsFacade::fetchPageViews(Period::set('20daysAgo', 'today'));

// Retrieve pageview data for the current day and the last seven days
$analyticsData = Analytics::fetchPageViews(Period::days(7));

// Retrieve pageviews since the 6 months ago
$analyticsData = Analytics::fetchPageViews(Period::months(6));

// fetch data 
use \Google\Service\AnalyticsData\Dimension;
use \Google\Service\AnalyticsData\Metric;
$dimensions = new Dimension(array('name' => 'pageTitle'));
$metrics = new Metric(array('name' => 'screenPageViews'));
Analytics::fetch(Period::days(7), $dimensions, $metrics);

提供的方法

或您的网站或应用在用户设备前台的总时间(以秒为单位)。

public function fetchUserEngagementReport(Period $period): Collection

所有方法都将返回一个包含结果的 \Illuminate\Support\Collection 对象。

所有其他Google Analytics查询

要执行Google Analytics资源上的所有其他查询,请使用 performQuery。有关可能使用的指标和维度的更多信息,请参阅 Google的Core Reporting API

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

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件