Skip to content

Commit b9bd709

Browse files
committed
Add GTM config to admin panel and remove google analytics invalid naming convention
1 parent 9b1f4c9 commit b9bd709

14 files changed

Lines changed: 129 additions & 48 deletions

File tree

config/events.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ sylius_ui:
55
user_com_configuration:
66
template: '@BitBagSyliusUserComPlugin/UserApiResource/_userComCredentials.html.twig'
77
priority: 25
8+
sylius.shop.layout.before_body:
9+
blocks:
10+
user_com_gtm:
11+
template: '@BitBagSyliusUserComPlugin/Scripts/_userComGTM.html.twig'
12+
priority: 0
13+
sylius.shop.layout.stylesheets:
14+
blocks:
15+
user_com_scripts:
16+
template: '@BitBagSyliusUserComPlugin/Scripts/_userComScripts.html.twig'
17+
priority: 0

config/services/twig.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false"/>
6+
<service
7+
id="bit_bag.sylius_user_com_plugin.twig.user_com_api_aware_extension"
8+
class="BitBag\SyliusUserComPlugin\Twig\UserComApiAwareExtension"
9+
public="true"
10+
>
11+
<argument type="service" id="bit_bag.sylius_user_com_plugin.provider.user_com_api_aware_resource_provider"/>
12+
<tag name="twig.extension"/>
13+
</service>
14+
</services>
15+
</container>

doc/installation.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
2. Add required environment variables to your `.env` file:
77
```dotenv
88
USER_COM_FRONTEND_API_KEY=""
9-
TAG_MANAGER_ID=""
9+
GOOGLE_ANALYTICS_TAG=""
1010
USER_COM_ENCRYPTION_KEY=your-32-character-long-key
1111
USER_COM_ENCRYPTION_IV=your-16-character-long-iv
1212
MESSENGER_USER_COM_ASYNCHRONOUS=""
@@ -56,11 +56,10 @@
5656
6. Take advantage of channel based configuration of GoogleAnalyticsPlugin by adding :
5757
```yaml
5858
spinbits_sylius_google_analytics4:
59-
id: "%env(TAG_MANAGER_ID)%"
60-
templateName: "@BitBagSyliusUserComPlugin/_userComScripts.html.twig"
59+
id: "%env(GOOGLE_ANALYTICS_TAG)%"
6160
enabled: true
6261
```
63-
7. Add api credentials to your channel configuration in admin panel. If you decided to extend different object, please make sure that api credentials are set
62+
7. Add api credentials and GTM to your channel configuration in admin panel. If you decided to extend different object, please make sure that api credentials are set
6463
8. Run yarn install and yarn build to compile assets, or the alternative solution you use for your project
6564
```bash
6665
yarn install && yarn build

src/Form/Extension/UserComApiAwareTypeExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5656
],
5757
],
5858
)
59+
->add(
60+
'userComGTMContainerId',
61+
TextType::class,
62+
[
63+
'label' => 'bitbag_sylius_user_com_plugin.ui.gtm_container_id',
64+
'required' => false,
65+
],
66+
)
5967
->addEventListener(FormEvents::SUBMIT, function (SubmitEvent $event) {
6068
$data = $event->getData();
6169
$form = $event->getForm();

src/Trait/UserComApiAwareInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public function setUserComUrl(string $userComUrl): void;
2727
public function getUserComApiKey(): ?string;
2828

2929
public function setUserComApiKey(?string $userComApiKey): void;
30+
31+
public function getUserComGTMContainerId(): ?string;
32+
33+
public function setUserComGTMContainerId(?string $userComGTMContainerId): void;
3034
}

src/Trait/UserComApiAwareTrait.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ trait UserComApiAwareTrait
1717
{
1818
#[ORM\Column(name: 'user_com_url', type: 'string', nullable: true)]
1919
/** @ORM\Column(name="user_com_url", type="string", nullable=true) */
20-
private ?string $userComUrl;
20+
private ?string $userComUrl = null;
2121

2222
#[ORM\Column(name: 'user_com_api_key', type: 'string', nullable: true)]
2323
/** @ORM\Column(name="user_com_api_key", type="string", nullable=true) */
24-
private ?string $userComApiKey;
24+
private ?string $userComApiKey = null;
25+
26+
#[ORM\Column(name: 'user_com_gtmcontainer_id', type: 'string', nullable: true)]
27+
/** @ORM\Column(name="user_com_gtmcontainer_id", type="string", nullable=true) */
28+
private ?string $userComGTMContainerId = null;
2529

2630
public function getUserComUrl(): ?string
2731
{
@@ -42,4 +46,14 @@ public function setUserComApiKey(?string $userComApiKey): void
4246
{
4347
$this->userComApiKey = $userComApiKey;
4448
}
49+
50+
public function getUserComGTMContainerId(): ?string
51+
{
52+
return $this->userComGTMContainerId;
53+
}
54+
55+
public function setUserComGTMContainerId(?string $userComGTMContainerId): void
56+
{
57+
$this->userComGTMContainerId = $userComGTMContainerId;
58+
}
4559
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Twig;
13+
14+
use BitBag\SyliusUserComPlugin\Provider\UserComApiAwareResourceProviderInterface;
15+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
16+
use Twig\Extension\AbstractExtension;
17+
use Twig\TwigFunction;
18+
19+
final class UserComApiAwareExtension extends AbstractExtension
20+
{
21+
public function __construct(
22+
private readonly UserComApiAwareResourceProviderInterface $userComApiAwareResourceProvider,
23+
) {
24+
}
25+
26+
public function getFunctions(): array
27+
{
28+
return [
29+
new TwigFunction('getUserComApiAwareResource', [$this, 'getUserComApiAware']),
30+
];
31+
}
32+
33+
public function getUserComApiAware(): ?UserComApiAwareInterface
34+
{
35+
return $this->userComApiAwareResourceProvider->getApiAwareResource();
36+
}
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% set apiAwareResource = getUserComApiAwareResource() %}
2+
3+
{% if null != apiAwareResource and null != apiAwareResource.userComGTMContainerId %}
4+
<!-- Google Tag Manager (noscript) -->
5+
<noscript><iframe src="{{ "https://www.googletagmanager.com/ns.html?id=" ~ apiAwareResource.userComGTMContainerId }}"
6+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
7+
<!-- End Google Tag Manager (noscript) -->
8+
{% endif %}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% set genderMap = constant('BitBag\\SyliusUserComPlugin\\Builder\\Payload\\CustomerPayloadBuilderInterface::GENDER_MAP')%}
2+
{% set active = constant('BitBag\\SyliusUserComPlugin\\Builder\\Payload\\CustomerPayloadBuilderInterface::STATUS_USER')%}
3+
{% set visitor = constant('BitBag\\SyliusUserComPlugin\\Builder\\Payload\\CustomerPayloadBuilderInterface::STATUS_VISITOR')%}
4+
{% set customer = sylius.customer|default(null) %}
5+
<script data-cfasync="false">const user_com_customer_info= {
6+
name: "{{ customer.name|default(null) }}",
7+
user_id: "{{ customer.email|default(null) }}",
8+
email: "{{ customer.email|default(null) }}",
9+
gender: {{ null != customer ? genderMap[customer.gender] : genderMap['u'] }},
10+
status: {{ null != customer ? active : visitor }},
11+
phone_number: "{{ customer.phone_number|default(null)}}",
12+
}
13+
</script>
14+
{% set apiAwareResource = getUserComApiAwareResource() %}
15+
16+
{% if null != apiAwareResource and null != apiAwareResource.userComGTMContainerId %}
17+
<!-- Google Tag Manager -->
18+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
19+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
20+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
21+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
22+
})(window,document,'script','dataLayer',{{ apiAwareResource.userComGTMContainerId }});</script>
23+
<!-- End Google Tag Manager -->
24+
{% endif %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<div class="ui attached segment">
22
{{ form_row(form.userComApiKey) }}
33
{{ form_row(form.userComUrl) }}
4+
{{ form_row(form.userComGTMContainerId) }}
45
</div>

0 commit comments

Comments
 (0)