Skip to content

Commit bbcb5ec

Browse files
authored
Merge pull request #7 from BitBagCommerce/feature/UC-13-ga4-plugin
Add GA-4 Plugin
2 parents 6a2ba31 + e115f01 commit bbcb5ec

15 files changed

Lines changed: 139 additions & 21 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": "^8.1",
99
"bitbag/coding-standard": "^3.0",
1010
"sylius/sylius": "~1.13.0",
11-
"symfony/webpack-encore-bundle": "^1.15"
11+
"symfony/webpack-encore-bundle": "^1.15",
12+
"bitbag/google-analytics-4-plugin": "^1.0"
1213
},
1314
"require-dev": {
1415
"behat/behat": "^3.6.1",

config/bundles.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
Spinbits\SyliusGoogleAnalytics4Plugin\SpinbitsSyliusGoogleAnalytics4Plugin::class => ['all' => true],
5+
];

config/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
parameters:
2+
user_com.frontend_api_key: '%env(USER_COM_FRONTEND_API_KEY)%'
3+
4+
twig:
5+
globals:
6+
user_com_frontend_api_key: '%user_com.frontend_api_key%'
7+
18
imports:
29
- { resource: "state_machine/*" }
10+
- { resource: "events.yaml" }
11+

config/events.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sylius_ui:
2+
events:
3+
sylius.admin.channel.form.first_column_content:
4+
blocks:
5+
user_com_configuration:
6+
template: '@BitBagSyliusUserComPlugin/UserApiResource/_userComCredentials.html.twig'
7+
priority: 25
8+
sylius.shop.layout.stylesheets:
9+
blocks:
10+
user_com:
11+
template: '@BitBagSyliusUserComPlugin/_userComScripts.html.twig'
12+
priority: 25

config/services/form.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@
99
<argument type="service" id="sylius.context.customer"/>
1010
<tag name="form.type_extension" />
1111
</service>
12+
13+
<service id="bit_bag.sylius_user_com_plugin.form.extension.user_com_api_aware_type_extension" class="BitBag\SyliusUserComPlugin\Form\Extension\UserComApiAwareTypeExtension">
14+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
15+
<tag name="form.type_extension" extended-type="Sylius\Bundle\ChannelBundle\Form\Type\ChannelType" priority="200" />
16+
</service>
1217
</services>
1318
</container>

doc/adjustments.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Adjustments
2+
### 1. Payloads for Customer, Product or Order
3+
To modify data sent for occurences related to one of resources, you can adjust classes responsible for payload building.
4+
These classes are located under `src/Builder/Payload` directory and have meaningful names corresponding to data that will be provided, e.g. `CustomerPayloadBuilder`or `OrderPayloadBuilder`.
5+
6+
### 2. Requests from User.com on designated endpoint.
7+
Requests send on endpoint designed by User.com are handled by `\BitBag\SyliusUserComPlugin\Controller\UserComAgreementsController`.
8+
You can adjust this controller to handle requests in a way that suits your needs.
9+
There is designated validator to check if request payload contains required fields.
10+
If there will be any field added by User.com, you can adjust this validator to check if this field is present in request payload.
11+
Also, in `BitBag\SyliusUserComPlugin\Assigner\AgreementsAssigner` you can adjust existing logic to assign agreements to customer in a way that suits your needs.
12+
13+
```php
14+
public function assign(CustomerInterface $customer, array $agreements): void
15+
{
16+
foreach ($agreements as $key => $value) {
17+
match ($key) {
18+
'email_agreement' => $customer->setSubscribedToNewsletter($value),
19+
default => $this->logger->error(
20+
sprintf(
21+
'Agreement not found. Key = %s, Value = %s, CustomerId = %s',
22+
$key,
23+
$value,
24+
$customer->getId(),
25+
),
26+
),
27+
};
28+
}
29+
}
30+
}
31+
```

doc/installation.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Installation
2+
1. Add package to your project via composer:
3+
```bash
4+
composer require bitbag/user-com-plugin
5+
```
6+
>If `bitbag/user-com-plugin` is not found by composer, try adding repository to your `composer.json` file:
7+
> ```json
8+
> "repositories": [
9+
> {
10+
> "type": "vcs",
11+
> "url": "https://github.com/BitBagCommerce/SyliusUserComPlugin.git"
12+
> },
13+
> ],
14+
> ```
15+
2. Add required environment variables to your `.env` file:
16+
```dotenv
17+
USER_COM_FRONTEND_API_KEY=""
18+
TAG_MANAGER_ID=""
19+
```
20+
3. Add plugin dependencies to config/bundles.php file:
21+
```php
22+
return [
23+
...
24+
BitBag\SyliusUserComPlugin\BitBagSyliusUserComPlugin::class => ['all' => true],
25+
Spinbits\SyliusGoogleAnalytics4Plugin\SpinbitsSyliusGoogleAnalytics4Plugin::class => ['all' => true],
26+
...
27+
];
28+
```
29+
4. Import required config in your `config/packages/_sylius.yaml` file:
30+
```yaml
31+
imports:
32+
...
33+
- { resource: "@BitBagSyliusUserComPlugin/config/config.yml" }
34+
...
35+
```
36+
5. Extend `Channel` entity `UserComApiAwareTrait` and implement `UserComApiAwareInterface`
37+
```php
38+
class Channel extends BaseChannel implements ChannelInterface
39+
{
40+
use UserComApiAwareTrait;
41+
}
42+
```
43+
44+
```php
45+
interface ChannelInterface extends BaseChannelInterface, UserComApiAwareInterface
46+
{
47+
}
48+
```
49+
>`UserComApiAwareTrait` contains mapping for annotations and for attributes which are required by UserCom integration.
50+
> If you're using xml mapping, you should add mapping for those properties in your `Channel.orm.xml` file.
51+
52+
6. Take advantage of channel based configuration of GoogleAnalyticsPlugin by adding :
53+
```yaml
54+
spinbits_sylius_google_analytics4:
55+
id: "%env(TAG_MANAGER_ID)%"
56+
channels_ids:
57+
FASHION_WEB: "G-DF1P3RRJ8S"
58+
OTHER_CHANNEL: "G-WX1RJ8SP3R"
59+
```

src/Trait/UserComApiAwareTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111

1212
namespace BitBag\SyliusUserComPlugin\Trait;
1313

14+
use Doctrine\ORM\Mapping as ORM;
15+
1416
trait UserComApiAwareTrait
1517
{
18+
#[ORM\Column(name: 'user_com_url', type: 'string', nullable: true)]
19+
/** @ORM\Column(name="user_com_url", type="string", nullable=true) */
1620
private ?string $userComUrl;
1721

22+
#[ORM\Column(name: 'user_com_api_key', type: 'string', nullable: true)]
23+
/** @ORM\Column(name="user_com_api_key", type="string", nullable=true) */
1824
private ?string $userComApiKey;
1925

2026
public function getUserComUrl(): ?string

symfony.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@
202202
"ref": "9c8a1e8ce2b1f215015ed16652c4ed18eb5867fd"
203203
}
204204
},
205+
"spinbits/google-analytics-4-plugin": {
206+
"version": "dev-test/bump-sylius-version"
207+
},
205208
"squizlabs/php_codesniffer": {
206209
"version": "3.7",
207210
"recipe": {

templates/_userComScripts.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% set channel = sylius.channel %}
2+
23
{% if null != channel.userComApiKey and null != channel.userComUrl%}
34
{% set customer = sylius.customer|default(null) %}
45
{% set genderMap = constant('BitBag\\SyliusUserComPlugin\\Builder\\Payload\\CustomerPayloadBuilderInterface::GENDER_MAP')%}

0 commit comments

Comments
 (0)