actcmsvn/laravel-thong-ke-truy-cap

使用 Google Analytics v4 对 Laravel 进行访问统计

1.0.2 2024-04-21 06:27 UTC

This package is auto-updated.

Last update: 2024-09-25 02:57:18 UTC


README

Total Downloads Latest Stable Version License

laravel-thong-ke-truy-cap

使用 Google Analytics v4 对 Laravel 进行访问统计

关于

使用 Google API 从 Google Analytics 版本 4 检索所有数据,如页面浏览量、事件、电子商务交易等。

安装

您可以通过 composer 安装此包

composer require actcmsvn/laravel-thong-ke-truy-cap

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="thong-ke-truy-cap-config"

这是已发布的配置文件的内容

return [

    /*
    |--------------------------------------------------------------------------
    | Google Analytics ID
    |--------------------------------------------------------------------------
    |
    | The Google Analytics ID of the website you want to track.
    |
    */
    'property_id' => env('GOOGLE_ANALYTICS_PROPERTY_ID', null),

    /*
    |--------------------------------------------------------------------------
    | Google Analytics Client Secret
    |--------------------------------------------------------------------------
    |
    | The Google Analytics Client Secret of the website you want to track.
    |
    */
    'credentials' => env('GOOGLE_ANALYTICS_CREDENTIALS', storage_path('app/analytics/google-credentials.json')),
];

使用方法

首先,您需要在 Google Cloud Console 中创建一个服务帐户。您可以在以下链接找到说明:[https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php](https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php)。

创建服务帐户后,您需要下载凭据并将其保存在 storage/app/analytics 文件夹中。您可以根据需要更改配置文件中凭据的位置。

完成此操作后,您可以使用此包,如下所示

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the average session duration for the last 7 days:
$averageSessionDuration = Analytics::averageSessionDuration(Period::days(7));

可用时间段

// Set the period to the last x minutes:
Period::minutes(30);

// Set the period to the last x hours:
Period::hours(1);

// Set the period to the last x days:
Period::days(2);

// Set the period to the last x weeks:
Period::weeks(3);

// Set the period to the last x months:
Period::months(4);

// Set the period to the last x years:
Period::years(5);

// Set the period to a custom date range using a Carbon object:
$startDate = Carbon::now()->subDays(7);
$endDate = Carbon::now();

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

// Set the period to since the given date:
Period::since(Carbon::now()->subDays(7));

注意:Period::create() 方法中使用多个 Carbon 对象可能会导致意外的行为。建议将起始和结束日期作为单独的 Carbon 实例传递。例如:Period::create(Carbon::instance($startDate), Carbon::instance($endDate))。这将确保起始和结束日期设置正确。

可用方法

人口统计分析

用于检索网站或应用程序的人口统计数据分析的方法。您可以使用这些方法获取有关顶级使用语言、按城市或国家统计的总用户数以及按性别统计的总用户数等信息。所有方法均接受 Period 对象作为参数,以指定分析数据的日期范围。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the top users by language for the last 7 weeks, limit to top 10:
$data = Analytics::topUsersByLanguage(period: Period::weeks(7), limit: 10);

// Get the total users by language for the last 7 weeks:
$data = Analytics::totalUsersByLanguage(Period::weeks(7));

// Get the top users by city for the last 7 weeks, limit to top 5:
$data = Analytics::topUsersByCity(period: Period::weeks(7), limit: 5);

// Get the total users by city for the last 7 weeks:
$data = Analytics::totalUsersByCity(Period::weeks(7));

// Get the top users by country for the last 7 weeks, limit to top 10:
$data = Analytics::topUsersByCountry(period: Period::weeks(7), limit: 10);

// Get the total users by country for the last 7 weeks:
$data = Analytics::totalUsersByCountry(Period::weeks(7));

// Get the total users by gender for the last 7 weeks:
$data = Analytics::totalUsersByGender(Period::weeks(7));

// Get the total users by age group for the last 7 weeks
$data = Analytics::totalUsersByAgeGroup(Period::weeks(7));

// Get the total users by date per page for the last 7 weeks:
$data = Analytics::totalUsersByDatePerPage(Period::weeks(7));

设备和操作系统分析

用于检索网站或应用程序的设备和操作系统数据分析的方法。您可以使用这些方法获取有关顶级流行浏览器、屏幕分辨率和访问者使用的移动设备等信息。所有方法均接受 Period 对象作为参数,以指定分析数据的日期范围。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the top users by device category for the last 1 year:
$data = Analytics::topUsersByDeviceCategory(Period::years(1));

// Get the top users by operating system for the last 1 year:
$data = Analytics::topUsersByOperatingSystem(Period::years(1));

// Get the top users by browser for the last 1 year, limit to top 10:
$data = Analytics::topUsersByBrowser(period: Period::years(1), limit: 10);

// Get the total users by browser for the last 1 year:
$data = Analytics::totalUsersByBrowser(Period::years(1));

// Get the top users by screen resolution for the last 1 year, limit to top 5:
$data = Analytics::topUsersByScreenResolution(period: Period::years(1), limit: 5);

// Get the total users by operating system for the last 1 year:
$data = Analytics::totalUsersByOperatingSystem(Period::years(1));

// Get the total users by device category for the last 1 year:
$data = Analytics::totalUsersByDeviceCategory(Period::years(1));

// Get the top users by mobile device branding for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileDeviceBranding(period: Period::years(1), limit: 10);

// Get the total users by mobile device branding for the last 1 year:
$data = Analytics::totalUsersByMobileDeviceBranding(Period::years(1));

// Get the top users by mobile device model for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileDeviceModel(period: Period::years(1), limit: 10);

// Get the total users by mobile device model for the last 1 year:
$data = Analytics::totalUsersByMobileDeviceModel(Period::years(1));

// Get the top users by mobile input selector for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileInputSelector(period: Period::years(1), limit: 10);

// Get the total users by mobile input selector for the last 1 year:
$data = Analytics::totalUsersByMobileInputSelector(Period::years(1));

// Get the top users by mobile device info for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileDeviceInfo(period: Period::years(1), limit: 10);

// Get the total users by mobile device info for the last 1 year:
$data = Analytics::totalUsersByMobileDeviceInfo(Period::years(1));

// Get the top users by mobile device marketing name for the last 1 year, limit to top 10:
$data = Analytics::getTopUsersByMobileDeviceMarketingName(period: Period::years(1), limit: 10);

// Get the total users by mobile device marketing name for the last 1 year:
$data = Analytics::getTotalUsersByMobileDeviceMarketingName(Period::years(1));

页面浏览量分析

用于检索网站或应用程序的页面浏览量数据分析的方法。您可以使用这些方法获取有关访问者的总浏览量或按页面浏览量的信息。所有方法均接受 Period 对象作为参数,以指定分析数据的日期范围。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the total pageviews for the last 14 days:
$data = Analytics::totalViews(Period::days(14));

// Get the total pageviews for the last 14 days, grouped by date:
$data = Analytics::totalViewsByDate(Period::days(14));

// Get the total pageviews for the last 14 days, grouped by page:
$data = Analytics::totalViewsByPage(Period::days(14));

// Get the top viewed pages for the last 14 days:
$data = Analytics::topViewsByPage(Period::days(14));

// Get the least viewed pages for the last 14 days:
$data = Analytics::leastViewsByPage(Period::days(14));

// Get the total views for the last 14 days, grouped by page path:
$data = Analytics::totalViewsByPagePath(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by page path:
$data = Analytics::topViewsByPagePath(Period::days(14));

// Get the total views for the last 14 days, grouped by page title:
$data = Analytics::totalViewsByPageTitle(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by page title:
$data = Analytics::topViewsByPageTitle(Period::days(14));

// Get the total views for the last 14 days, grouped by page url:
$data = Analytics::totalViewsByPageUrl(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by page url:
$data = Analytics::topViewsByPageUrl(Period::days(14));

// Get the total views for the last 14 days, grouped by country:
$data = Analytics::totalViewsByCountry(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by country:
$data = Analytics::topViewsByCountry(Period::days(14));

// Get the total views for the last 14 days, grouped by city:
$data = Analytics::totalViewsByCity(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by city:
$data = Analytics::topViewsByCity(Period::days(14));

实时分析

用于检索网站或应用程序的实时数据分析的方法。您可以使用这些方法获取有关网站上的当前活跃访问者的信息。所有方法均接受 Period 对象作为参数,以指定分析数据的日期范围。如果没有传递 Period 对象,默认时间范围为 30 分钟。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the total active users for the last 30 minutes:
$data = Analytics::activeUsers();

资源分析

用于检索网站或应用程序的资源数据分析的方法。您可以使用这些方法获取有关顶级着陆页、退出页和引用者的信息,以及您最重要的社交媒体。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the top 10 referrals for the last 14 days:
$data = Analytics::getTopReferrers(period: Period::days(14), limit: 10);

// Get the top 10 referrals for the last 14 days, by page path:
$data = Analytics::getTopReferrersByPagePath(period: Period::days(14), limit: 10);

// Get the top 10 referrals for the last 14 days, grouped by page title:
$data = Analytics::getTopReferrersByPageTitle(period: Period::days(14), limit: 10);

// Get the top 20 landing pages by page title for the last 14 days:
$data = Analytics::getTopLandingPagesByPageTitle(period: Period::days(14), limit: 20);

// Get the top 20 landing pages for the last 14 days:
$data = Analytics::getTopLandingPages(period: Period::days(14), limit: 20);

// Get the top 20 landing pages with query string by page title for the last 14 days, grouped by page title:
$data = Analytics::getLandingPagesPlusQueryStringAByPageTitle(period: Period::days(14), limit: 20);

// Get the top 5 traffic sources for the last 14 days:
$data = Analytics::getTopTrafficSources(period: Period::days(14), limit: 5);

// Get the top 10 backlinks for the last 14 days:
$data = Analytics::getTopBacklinks(period: Period::days(14), limit: 10);

会话分析

用于检索网站或应用程序的会话持续时间数据分析的方法。您可以使用这些方法获取有关访问者的平均会话时间等信息。所有方法均接受 Period 对象作为参数,以指定分析数据的日期范围。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get total sessions for the last 7 days:
$data = Analytics::sessions(Period::days(7));

// Get total sessions for the last 7 days per page:
$data = Analytics::sessionsPerPage(Period::days(7));

// Get the average session duration for the last 7 days:
$data = Analytics::averageSessionDuration(Period::days(7));

// Get the average session duration for the last 7 days, grouped by date:
$data = Analytics::averageSessionDurationByDate(Period::days(7));

// Get the average session duration in seconds for the last 7 days, grouped by page:
$data = Analytics::averageSessionDurationInSecondsByPage(Period::days(7));

// Get the average page views per session for the last 7 days:
$data = Analytics::averagePageViewsPerSession(Period::days(7));

// Get the average page views per session for the last 7 days, grouped by date:
$data = Analytics::averagePageViewsPerSessionByDate(Period::days(7));

// Get the average session duration in seconds for the last 7 days:
$data = Analytics::averageSessionDurationInSeconds(Period::days(7));

// Get the average session duration in seconds for the last 7 days, grouped by date:
$data = Analytics::averageSessionDurationInSecondsByDate(Period::days(7));

// Get bounce rate for the last 7 days:
$data = Analytics::bounceRate(Period::days(7));

// Get bounce rate for the last 7 days, grouped by date:
$data = Analytics::bounceRateByDate(Period::days(7));

// Get bounce rate by page for the last 7 days:
$data = Analytics::bounceRateByPage(Period::days(7));

用户分析

获取网站或应用程序的用户分析数据的方法。您可以使用这些方法获取总访问量或按设备统计的访问量等信息。所有方法均以Period对象为参数,用于指定分析数据的时间范围。

以下是一些使用这些方法的示例

use ACTCMS\Analytics\Facades\Analytics;
use ACTCMS\Analytics\Period;

// Get the total users for the last 5 weeks:
$data = Analytics::totalUsers(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by date:
$data = Analytics::totalUsersByDate(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by session source:
$data = Analytics::totalUsersBySessionSource(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by session medium:
$data = Analytics::totalUsersBySessionMedium(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by session device:
$data = Analytics::totalUsersBySessionDevice(Period::weeks(5));

测试

composer test

更新日志

有关最近变更的信息,请参阅更新日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞,请审查我们的安全策略

鸣谢

许可证

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