setono / google-analytics-server-side-tracking-bundle
A Symfony bundle that allows you to track your visitors server side instead of client side
Installs: 22 225
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 9
Type:symfony-bundle
Requires
- php: >=7.4
- doctrine/doctrine-bundle: ^1.12 || ^2.2
- doctrine/orm: ^2.7
- doctrine/persistence: ^1.3 || ^2.2
- gedmo/doctrine-extensions: ^2.4 || ^3.1
- nyholm/psr7: ^1.3
- setono/bot-detection-bundle: ^1.7
- setono/client-id-bundle: ^0.2
- setono/client-id-contracts: ^0.2
- setono/consent-bundle: ^0.1
- setono/consent-contracts: ^0.1.3
- setono/doctrine-object-manager-trait: ^1.0
- setono/google-analytics-measurement-protocol: ^0.4.3
- setono/symfony-main-request-trait: ^1.0
- symfony/config: ^4.4 || ^5.4 || ^6.0
- symfony/console: ^4.4 || ^5.4 || ^6.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.0
- symfony/event-dispatcher: ^4.4 || ^5.4 || ^6.0
- symfony/http-client: ^4.4 || ^5.4 || ^6.0
- symfony/http-foundation: ^4.4 || ^5.4 || ^6.0
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.0
- symfony/lock: ^4.4 || ^5.4 || ^6.0
- symfony/messenger: ^4.4 || ^5.4 || ^6.0
- symfony/uid: ^5.4 || ^6.0
- symfony/workflow: ^4.4 || ^5.4 || ^6.0
- webmozart/assert: ^1.11
Requires (Dev)
- kriswallsmith/buzz: ^1.2
- matthiasnoback/symfony-dependency-injection-test: ^4.3
- nyholm/symfony-bundle-test: ^1.8
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.17
- psalm/plugin-symfony: ^3.1
- roave/security-advisories: dev-latest
- setono/code-quality-pack: ^2.2
- symfony/security-bundle: ^4.4 || ^5.4 || ^6.0
- weirdan/doctrine-psalm-plugin: ^2.3
- dev-master
- v0.3.9
- v0.3.8
- v0.3.7
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.0
- dev-dependabot/composer/setono/google-analytics-measurement-protocol-tw-1.0.0
- dev-dependabot/composer/doctrine/persistence-tw-3.2.0
- dev-dependabot/composer/psalm/plugin-phpunit-tw-0.18
- dev-dependabot/composer/psalm/plugin-symfony-tw-4.0
- dev-dependabot/composer/nyholm/symfony-bundle-test-tw-2.0
- dev-pageview
This package is auto-updated.
Last update: 2024-11-04 19:19:40 UTC
README
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.