Skip to content

Commit 5a16e0d

Browse files
committed
Update readme
1 parent 7354e21 commit 5a16e0d

File tree

1 file changed

+94
-10
lines changed

1 file changed

+94
-10
lines changed

README.md

+94-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
[![Software License][ico-license]](LICENSE)
55
[![Build Status][ico-github-actions]][link-github-actions]
66

7-
This plugin adds Google Analytics tracking to your store. It injects the tags directly
8-
and hence does not depend on third party tools like Google Tag Manager.
7+
This plugin adds Google Analytics tracking to your store. You can choose between a gtag and tag manager integration.
8+
The gtag integration will output the traditional `gtag()` functions when tracking events, while the tag manager integration
9+
will populate the `dataLayer` with event data.
910

1011
## Installation
1112

@@ -36,9 +37,12 @@ $bundles = [
3637
```yaml
3738
# config/packages/setono_sylius_analytics.yaml
3839
imports:
39-
# ...
4040
- { resource: "@SetonoSyliusAnalyticsPlugin/Resources/config/app/config.yaml" }
41-
# ...
41+
42+
setono_google_analytics:
43+
gtag: ~
44+
# If you want to use tag manager instead of gtag, just comment the line above and remove the comment below
45+
# tag_manager: ~
4246
```
4347

4448
### Step 4: Import routing
@@ -56,19 +60,99 @@ php bin/console doctrine:migrations:diff
5660
php bin/console doctrine:migrations:migrate
5761
```
5862

59-
### Step 6: Create a property
60-
When you create a property in Google Analytics you receive a measurement id which looks something like G-12345678.
61-
62-
Now create a new property in your Sylius shop by navigating to `/admin/google-analytics/properties/new`.
63-
Remember to enable the property and enable the channels you want to track.
63+
### Step 6: Create a property / container
64+
Click the Google Analytics link in your backend and create a new property/container.
6465

6566
### Step 7: You're ready!
67+
Your Sylius store will start tracking now!
68+
6669
The events that are tracked are located in the [EventSubscriber folder](src/EventSubscriber).
6770

71+
Read on if you want to enrich events with more data.
72+
73+
## Enrich events with more data
74+
When we want to track an event inside the Sylius application the `Setono\GoogleAnalyticsBundle\Event\ClientSideEvent` is fired.
75+
76+
That event holds the actual Google Analytics event, e.g. `Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\PurchaseEvent`.
77+
This way, if you subscribe to the `ClientSideEvent`, you can manipulate everything about the event before it's rendered (and sent to Google).
78+
79+
But there are other ways to change the data enrichment. This can be done via resolvers. The plugin uses resolvers to
80+
resolve a brand from a product, a category from a product etc. Read on to figure out how to use this functionality.
81+
82+
### Brand resolver
83+
An item has a brand property (`item_brand`), but the plugin doesn't know how you have chosen to
84+
add brand data in your application. Therefore, you need to implement your own `BrandResolver`. Here is an example:
85+
86+
```php
87+
<?php
88+
use Setono\SyliusAnalyticsPlugin\Resolver\Brand\BrandResolverInterface;
89+
use Sylius\Component\Core\Model\ProductInterface;
90+
use Sylius\Component\Core\Model\ProductVariantInterface;
91+
92+
final class BrandResolver implements BrandResolverInterface
93+
{
94+
public function resolveFromProduct(ProductInterface $product): ?string
95+
{
96+
return $product->getBrand(); // here we assume the getBrand() method will return a brand name or null (if not set)
97+
}
98+
99+
public function resolveFromProductVariant(ProductVariantInterface $productVariant): ?string
100+
{
101+
return $this->resolveFromProduct($productVariant->getProduct());
102+
}
103+
}
104+
```
105+
106+
When you implement the [`BrandResolverInterface`](src/Resolver/Brand/BrandResolverInterface.php) and register your class as a service it will automatically
107+
be tagged with `setono_sylius_analytics.brand_resolver` and used when tracking.
108+
109+
### Category resolver
110+
An item has category properties (`item_category`, `item_category2`, etc.) and by default the plugin will resolve these
111+
properties based on either a product's main taxon or (if no main taxon is set) the first taxon in the collection of taxons.
112+
113+
You can see the implementation inside the
114+
[`Setono\SyliusAnalyticsPlugin\Resolver\Category\CategoryResolver`](src/Resolver/Category/CategoryResolver.php) class.
115+
116+
### Item resolver
117+
The job of the item resolver is to return an `Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\Item\Item`
118+
either from an order item or a product. This resolver also has a default implementation which you can see in the class
119+
[`Setono\SyliusAnalyticsPlugin\Resolver\Item\ItemResolver`](src/Resolver/Item/ItemResolver.php).
120+
121+
### Items resolver
122+
The items resolver must return an array of items given an order as input. The default implementation is found in the class
123+
[`Setono\SyliusAnalyticsPlugin\Resolver\Items\ItemsResolver`](src/Resolver/Items/ItemsResolver.php).
124+
125+
### Variant resolver
126+
An item has a variant property (`item_variant`). How you want to view your variant data inside the Analytics
127+
user interface is up to you, however, the plugin provides two default resolvers, namely the
128+
[`Setono\SyliusAnalyticsPlugin\Resolver\Variant\NameBasedVariantResolver`](src/Resolver/Variant/NameBasedVariantResolver.php)
129+
and the [`Setono\SyliusAnalyticsPlugin\Resolver\Variant\OptionBasedVariantResolver`](src/Resolver/Variant/OptionBasedVariantResolver.php).
130+
131+
The option based version has the highest priority of the two and will therefore be tried first. It tries to create a
132+
variant string based on the options on the variant (if any).
133+
134+
The name based version returns the `\Sylius\Component\Product\Model\ProductVariantInterface::getName()`.
135+
136+
To implement your own resolver just implement the `Setono\SyliusAnalyticsPlugin\Resolver\Variant\VariantResolverInterface`.
137+
Here is an example:
138+
139+
```php
140+
<?php
141+
use Setono\SyliusAnalyticsPlugin\Resolver\Variant\VariantResolverInterface;
142+
use Sylius\Component\Core\Model\ProductVariantInterface;
143+
144+
final class ProductNameBasedVariantResolver implements VariantResolverInterface
145+
{
146+
public function resolve(ProductVariantInterface $productVariant): ?string
147+
{
148+
return $productVariant->getProduct()->getName();
149+
}
150+
}
151+
```
152+
68153
## Contribute
69154
Ways you can contribute:
70155
* Translate [messages](src/Resources/translations/messages.en.yaml) and [validators](src/Resources/translations/validators.en.yaml) into your mother tongue
71-
* Create Behat tests that verifies the scripts are outputted on the respective pages
72156
* Create new event subscribers that handle Analytics events which are not implemented
73157

74158
Thank you!

0 commit comments

Comments
 (0)