承接 setono/google-analytics-server-side-tracking-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

setono/google-analytics-server-side-tracking-bundle

Composer 安装命令:

composer require setono/google-analytics-server-side-tracking-bundle

包简介

A Symfony bundle that allows you to track your visitors server side instead of client side

README 文档

README

Latest Version Software License Build Status

Use this bundle to track your visitors with Google Analytics, but using server side integration instead of the usual client side integration.

This bundle is based on the Google Analytics measurement protocol library which in turn is based on the Universal Analytics solution from Google. The new GA4 tracking solution will be implemented when it's feature complete and out of beta.

Installation

This bundle also depends on the Client Id Bundle and the Consent Bundle, which generates client ids and provides consent services respectively. If you're not bound by the EU cookie laws, take a look at the configuration of the Consent Bundle to have consent granted by default.

To install this bundle, simply run:

composer require setono/google-analytics-server-side-tracking-bundle

This will install the bundle and enable it if you're using Symfony Flex. If you're not using Flex, add the bundle manually to bundles.php instead.

Create migration

Hits are saved in the database, so you need to create a migration file:

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

Configuration

To enable the tracking, you need to supply Google Analytics properties to track:

# config/packages/setono_google_analytics_server_side_tracking.yaml
setono_google_analytics_server_side_tracking:
    properties:
        - "UA-123456-78"
        - "UA-345656-81" # Notice you can add as many properties as you'd like

If you have your properties stored somewhere else, i.e. in a database, you can just implement the Setono\GoogleAnalyticsServerSideTrackingBundle\Provider\PropertyProviderInterface which returns a list of properties.

You can also configure the minimum amount of seconds before hits are sent to Google:

# config/packages/setono_google_analytics_server_side_tracking.yaml
setono_google_analytics_server_side_tracking:
    send_delay: 600 # Wait a minimum of 10 minutes before sending a hit

Usage

Out of the box, the bundle will start tracking visitors just like the client side integration will, but just as with the client side integration you may want to add custom tracking specific to your application. Here's an example of the ecommerce event Purchase:

<?php

use Setono\GoogleAnalyticsMeasurementProtocol\DTO\Event\PurchaseEventData;
use Setono\GoogleAnalyticsMeasurementProtocol\DTO\ProductData;
use Setono\GoogleAnalyticsServerSideTrackingBundle\Factory\HitBuilderFactoryInterface;

final class YourService
{
    private HitBuilderFactoryInterface $hitBuilderFactory;

    public function __construct(HitBuilderFactoryInterface $hitBuilderFactory)
    {
        $this->hitBuilderFactory = $hitBuilderFactory;
    }

    public function track(): void
    {
        $hitBuilder = $this->hitBuilderFactory->createEventHitBuilder();

        $purchaseEvent = new PurchaseEventData('ORDER123', 'example.com', 431.25, 'EUR', 8.43, 2.56);

        $product1 = ProductData::createAsProductType('BLACK_T_SHIRT_981', 'Black T-shirt');
        $product1->brand = 'Gucci';
        $product1->quantity = 2;
        $product1->price = 145.23;
        $product1->variant = 'Black';
        $product1->category = 'T-Shirts';

        $product2 = ProductData::createAsProductType('BLUE_T_SHIRT_981', 'Blue T-shirt');
        $product2->brand = 'Chanel';
        $product2->quantity = 1;
        $product2->price = 148.99;
        $product2->variant = 'Blue';
        $product2->category = 'T-Shirts';

        $purchaseEvent->products[] = $product1;
        $purchaseEvent->products[] = $product2;

        $purchaseEvent->applyTo($hitBuilder);
    }
}

Send hits to Google

Use a cronjob to send hits to Google regularly:

* * * * * bin/console setono:google-analytics:send-hits

No matter the interval in the cronjob, hits will never be sent before the send_delay has passed (see configuration).

FAQ

How are hit builders persisted?

When creating a hit builder using the hit builder factory, the factory will add the hit builder to the so-called hit builder stack. A response listener is then persisting the hit builders at the end of the request-response lifecycle. The same logic as the client side library is applied. This means that page views are persisted when the HTTP status code is one of 2xx, 4xx, or 5xx. All events are persisted no matter what, though. You can see this logic here.

Do I have to populate request/response parameters?

No. Out of the box, hit builders are populated with request and response parameters, i.e. the url, user agent, document title etc.

setono/google-analytics-server-side-tracking-bundle 适用场景与选型建议

setono/google-analytics-server-side-tracking-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.33k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 setono/google-analytics-server-side-tracking-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 22.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 21
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-20