funnydevjsc / google-analytic-laravel-integrate
Laravel GA4 包
1.0.5
2024-05-18 14:39 UTC
Requires
- php: ^8.1
- google/apiclient: ^2.16
- google/cloud: ^0.244.0
Requires (Dev)
- illuminate/console: ^8.83
- illuminate/http: ^8.83
- illuminate/routing: ^8.83
- illuminate/support: ^8.0
README
这是一个免费的 Laravel 包,可以帮助您将数据与 GA4 集成
使用场景
- 从 GA4 爬取并解析结果,以重新格式化和排序的值
- 易于理解并绘制图表
- 示例爬虫命令
功能
- 从 config/google-service.php 动态获取 Google 服务凭据
- 从 config/google-analytic.php 动态获取 Google 分析属性
- 使用简单的一行代码轻松爬取数据
已知问题
- 此包使用 Google 的最新官方 SDK、库和方法,因此可能较大(依赖包 google/apiclient 大约 30MB),适用于共享主机。
- 请在使用此包之前考虑您的服务器环境。
- 然而,我们仍然建议您遵循 Google 库的最新编写风格,以确保安全性、合规性、CI/CD,最重要的是如果您正在使用服务
要求
- PHP: 8.1 或更高版本
- Laravel 9.0 或更高版本
快速开始
如果您希望将此包安装到您自己的 Laravel 应用程序中,请按照以下安装步骤操作
安装
步骤 1. 如果您还没有,请安装一个 Laravel 项目
https://laravel.net.cn/docs/installation
步骤 2. 使用 composer 需求此包
composer require funnydevjsc/google-analytic-laravel-integrate
步骤 3. 发布控制器文件和配置文件
php artisan vendor:publish --provider="FunnyDev\GoogleAnalytic\GoogleAnalyticServiceProvider" --tag="google-analytic"
如果发布文件失败,请在此包中创建相应的文件在路径 config/google-analytic.php
、config/google-service.php
和 app\Console\Commands\GoogleAnalyticCommand.php
。您还可以进一步自定义 GoogleAnalyticCommand.php 文件以适应您的项目。
步骤 4. 创建 Google 服务凭据
- 前往 https://console.cloud.google.com/iam-admin/serviceaccounts?hl=en&project=your_google_project_id 创建一个 Google 服务帐户。
- 前往 https://console.cloud.google.com/iam-admin/serviceaccounts/details/your_google_service_account_id/keys?hl=en&project=your_google_project_id 创建一个类似这样的 JSON 私钥文件
- 然后将其下载到您的计算机上作为
application_default_credentials.json
并将其移动到 Laravel 项目的 storage_path。但请记住将其从 git 忽略,以保护您的项目。
步骤 5. 更新发布配置文件中的各种配置设置
- 发布包资产后,配置文件将位于
config/google-analytic.php
和config/google-service.php
。 - 请使用您的 Google 服务凭据值从
application_default_credentials.json
填充到config/google-service.php
文件中。 - 找到您的分析属性 ID 并将其填充到
config/google-analytic.php
文件中,如下所示
- 在这种情况下,我们的分析属性 ID 是
281673130
,它显示在当前 URL 上。
步骤 6. 将分析属性 ID 迁移到前端
您可以使用如 spatie/laravel-googletagmanager
的库为网站的前端设置 GA4,然后使用 JavaScript 声明用户和业务的事件和活动,以触发这些事件并向 GA 发送报告。
步骤 7. 将数据保存到数据库并绘制图表
您可以使用或优化 app/Console/Commands/GoogleAnalyticCommand.php
来获取数据,并将收集到的数据存储到数据库中,以便于研究、评估和绘制图表。可以通过 JavaScript 库来绘制图表。以下是一个示例收集到的数据(以下数据为示例,并非真实数据):
{ "events": { //Trigged when user does something on the website "page_view": { "users": "2707", "counts": "20797" }, "user_engagement": { "users": "2020", "counts": "16380" }, "scroll": { "users": "2034", "counts": "13507" }, "session_start": { "users": "2705", "counts": "9126" }, "first_visit": { "users": "2592", "counts": "5672" }, "click": { "users": "1101", "counts": "3572" }, "form_start": { "users": "794", "counts": "3044" }, "form_submit": { "users": "172", "counts": "483" } }, "users": { //Analyze users information "countries": { "VN": "1386", "US": "308" }, "cities": { "Hanoi": "426", "Ho Chi Minh City": "302" }, "gender": { "male": "653", "female": "199", "unknown": "1928" }, "ages": { "unknown": "1995", "18-24": "523", "25-34": "169", "35-44": "75", "45-54": "34", "55-64": 0, "65+": 0 }, "languages": { "English": "1328", "Vietnamese": "905" }, "favorites": { "Technology/Technophiles": "533", "Technology/Mobile Enthusiasts": "372" }, "channels": { "Direct": "1895", "Organic Search": "715", "Organic Video": "493", "Referral": "156", "Organic Social": "105", "Paid Search": "71", "Unassigned": "57" }, "devices": { "types": { "desktop": "2001", "mobile": "717" }, "brands": { "Google": "1465", "Apple": "562" }, "names": { "Galaxy S23 Ultra": "13", "Galaxy S9+": "8" }, "models": { "Chrome": "1377", "iPhone": "314" }, "os": { "Windows": "1775", "iOS": "374" }, "versions": { "Windows 10": "1301", "Windows 11": "430" }, "browsers": { "Chrome": "1955", "Safari": "248" } } }, "interaction": { //Analyze user actions "average": { "session": 1.973175548315205, "event": 21.525577711803326 }, "active": { "2024-05-13": "75", "2024-05-12": "71", "2024-05-11": "60", "2024-01-31": "14", "2024-01-30": "9", "2024-01-29": "4" }, "sources": { "(direct)": "2951", "youtube.com": "584", "google": "559", "l.facebook.com": "69" }, "pages": { "https://software.funnydev.vn/": 1511, "https://account.funnydev.vn/login": 1029, "https://funnydev.vn/": 926 }, "referrers": { "https://software.funnydev.vn/": 2004, "https://funnydev.vn/": 996, "https://www.youtube.com/": 543, "https://www.google.com/": 466 }, "dau-mau": { "2024-05-13": "0.4625", "2024-05-12": "0.4", "2024-05-11": "0.2937062937062937", "2024-01-31": "0.017902813299232736", "2024-01-30": "0.012676056338028169", "2024-01-29": "0.012539184952978056" }, "dau-wau": { "2024-05-13": "1", "2024-05-12": "0.4625", "2024-05-11": "0.37777777777777777", "2024-01-31": "0.085271317829457363", "2024-01-30": "0.085271317829457363", "2024-01-29": "0.027777777777777776" }, "wau-mau": { "2024-05-13": "1", "2024-05-12": "1", "2024-05-11": "1", "2024-01-31": "0.16624040920716113", "2024-01-30": "0.13988095238095238", "2024-01-29": "0.13877551020408163" } }, "retention-rate": { //Calculate retention rate of users "new": "2592", "returning": "653", "evolution": { "2024-05-13": "235", "2024-05-12": "210", "2024-05-11": "193", "2024-01-31": "186", "2024-01-30": "180", "2024-01-29": "172" }, "engagement": { "2024-05-13": "0.78125", "2024-05-12": "0.77611940298507465", "2024-05-11": "0.775", "2024-05-10": "0.77419354838709675", "2024-01-31": "0.40196078431372551", "2024-01-30": "0.34482758620689657", "2024-01-29": "0.33333333333333331" } }, "business": { //Analyze user actions on products by 4 steps view -> add to card -> purchase -> get revenue "products": { "viewed": { "Product 1": 10000, "Product 2": 9000, "Product 3": 800, "Product 4": 700 }, "added": { "Product 1": 1000, "Product 2": 900, "Product 3": 800, "Product 4": 700 }, "purchased": { "Product 1": 100, "Product 2": 90, "Product 3": 80 }, "revenue": { "Product 1": 10, "Product 2": 9, "Product 3": 8 } }, "promotional": { "Promotion of Product 1": 500, "Promotion of Product 2": 400 } } }
一些示例图表
测试
php artisan google-analytic:crawl
反馈
请在 Laravel Việt Nam 上尊重我们
贡献
有关详细信息,请参阅 CONTRIBUTING
安全
如果您发现任何与安全相关的问题,请发送电子邮件至 contact@funnydev.vn 或使用问题跟踪器。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件