daikazu/laravel-ga4-event-tracking 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

daikazu/laravel-ga4-event-tracking

Composer 安装命令:

composer require daikazu/laravel-ga4-event-tracking

包简介

Laravel package to easily send events to Google Analytics 4

README 文档

README

A Laravel package to use Measurement Protocol for Google Analytics 4.

Installation

  1. Install package via Composer
composer require daikazu/laravel-ga4-event-tracking
  1. Set MEASUREMENT_ID and MEASUREMENT_PROTOCOL_API_SECRET in your .env file. You can find this information from: Google Analytics > Admin > Data Streams > [Select Site] > Measurement Protocol API secrets

  2. Optional: You can publish the config file by running this command in your terminal/cmd:

php artisan vendor:publish --tag=ga4-event-tracking.config
  1. client_id is required to post an event to Google Analytics. This package provides a Blade directive which you can put in your layout file after the Google Analytics Code tracking code. It makes a POST request to the backend to store the client id in the session which is later used to post events to Google Analytics 4.
<!-- Google Analytics Code -->
@sendClientID
<!-- </head> -->

The other option is to call the setClientId($clientId) method on the GA4 facade everytime before calling the sendEvent() method.

Usage

This package provides two ways to send events to Google Analytics 4.

  1. Directly

Sending event directly is as simple as calling the sendEvent($eventData) method on the GA4 facade from anywhere in your backend to post event to Google Analytics 4. $eventData contains the name and params of the event as per this reference page. For example:

GA4::sendEvent([
    'name' => 'login',
    'params' => [
        'method' => 'Google',
    ],
]);

sendEvent() method will return an array with the status of the request.

  1. Broadcast events to Google Analytics 4 via the Laravel Event System

Add the ShouldBroadcastToAnalytics interface to your event, and you're ready! You don't have to manually bind any listeners.

<?php
namespace App\Events;

use App\Order;
use Daikazu\GA4EventTracking\ShouldBroadcastToAnalytics;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class OrderWasCreated implements ShouldBroadcastToAnalytics
{
    use Dispatchable, SerializesModels;
    public $order;
    public function __construct(Order $order)
    {
        $this->order = $order;
    }
}

There are two additional methods that lets you customize the call to Google Analytics 4.

With the broadcastGA4EventAs method you can customize the name of the Event Action. By default, we use the class name with the class's namespace removed. This method gives you access to the underlying GA4 class as well.

With the withGA4Parameters method you can set the parameters GA4 Event.

<?php
namespace App\Events;
use App\Order;
use Daikazu\GA4EventTracking\ShouldBroadcastToAnalytics;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class OrderWasCreated implements ShouldBroadcastToAnalytics
{
    use Dispatchable, SerializesModels;
    public $order;
    public function __construct(Order $order)
    {
        $this->order = $order;
    }
    
    public function withGA4Parameters(GA4 $ga4){
        return [
            'method' => 'order_form',
            'value' => $this->order->sum_in_cents / 100
        ];
    }


    public function broadcastGA4EventAs(GA4 $ga4)
    {
        return 'CustomEventAction';
    }

}

Handle framework and 3rd-party events

If you want to handle events where you can't add the ShouldBroadcastToAnalytics interface, you can manually register them in your EventServiceProvider using the DispatchAnalyticsJob listener.

<?php
namespace App\Providers;

use Daikazu\GA4EventTracking\Listeners\DispatchAnalyticsJob;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider

{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
            DispatchAnalyticsJob::class,
        ],
    ];
}

Debugging Mode

You can also enable debugging mode by calling enableDebugging() method before calling the sendEvent() method. Like so - GA4::enableDebugging()->sendEvent($eventData). The sendEvent() method will return the response (array) from Google Analytics request in that case.

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email daikazu@gmail.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.

daikazu/laravel-ga4-event-tracking 适用场景与选型建议

daikazu/laravel-ga4-event-tracking 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 358 次下载、GitHub Stars 达 4, 最近一次更新时间为 2022 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「analytics」 「laravel」 「laravel-analytics」 「analytics-event-tracking」 「ga4」 「google-analytics-4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 daikazu/laravel-ga4-event-tracking 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 daikazu/laravel-ga4-event-tracking 我们能提供哪些服务?
定制开发 / 二次开发

基于 daikazu/laravel-ga4-event-tracking 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 358
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-12