File size: 4,352 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

namespace Mautic\PageBundle\Helper;

use Mautic\CacheBundle\Cache\CacheProvider;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\Serializer;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Tracker\ContactTracker;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\RequestStack;

class TrackingHelper
{
    public function __construct(
        protected ContactTracker $contactTracker,
        protected CacheProvider $cache,
        protected CoreParametersHelper $coreParametersHelper,
        protected RequestStack $requestStack,
    ) {
    }

    /**
     * @return array<string, 'facebook_pixel'|'google_analytics'>
     */
    public function getEnabledServices(): array
    {
        $keys = [
            'google_analytics' => 'Google Analytics',
            'facebook_pixel'   => 'Facebook Pixel',
        ];
        $result = [];
        foreach ($keys as $key => $service) {
            if ($id = $this->coreParametersHelper->get($key.'_id')) {
                $result[$service] = $key;
            }
        }

        return $result;
    }

    /**
     * @return string|null
     */
    private function getCacheKey()
    {
        $lead = $this->contactTracker->getContact();

        return $lead instanceof Lead ? 'mtc-tracking-pixel-events-'.$lead->getId() : null;
    }

    /**
     * @param mixed[] $values
     *
     * @throws InvalidArgumentException
     */
    public function updateCacheItem(array $values): void
    {
        $cacheKey = $this->getCacheKey();
        if (null !== $cacheKey) {
            /** @var CacheItemInterface $item */
            $item = $this->cache->getItem($cacheKey);
            $item->set(serialize(array_merge($values, $this->getCacheItem())));
            $item->expiresAfter(86400); // one day in seconds

            $this->cache->save($item);
        }
    }

    /**
     * @return mixed[]
     *
     * @throws InvalidArgumentException
     */
    public function getCacheItem(bool $remove = false): array
    {
        $cacheKey   = $this->getCacheKey();
        $cacheValue = [];

        /* @var CacheItemInterface $item */
        if (null !== $cacheKey) {
            $item = $this->cache->getItem($cacheKey);
            if ($item->isHit()) {
                $cacheValue = Serializer::decode($item->get(), ['allowed_classes' => false]);
                if ($remove) {
                    $this->cache->deleteItem($cacheKey);
                }
            }
        }

        return (array) $cacheValue;
    }

    /**
     * @return bool|mixed
     */
    public function displayInitCode($service)
    {
        $pixelId = $this->coreParametersHelper->get($service.'_id');

        if ($pixelId && $this->coreParametersHelper->get($service.'_landingpage_enabled') && $this->isLandingPage()) {
            return $pixelId;
        }
        if ($pixelId && $this->coreParametersHelper->get($service.'_trackingpage_enabled') && !$this->isLandingPage()) {
            return $pixelId;
        }

        return false;
    }

    /**
     * @return Lead|null
     */
    public function getLead()
    {
        return $this->contactTracker->getContact();
    }

    public function getAnonymizeIp()
    {
        return $this->coreParametersHelper->get('google_analytics_anonymize_ip');
    }

    protected function isLandingPage(): bool
    {
        $server = $this->requestStack->getCurrentRequest()->server;
        if (!str_contains((string) $server->get('HTTP_REFERER'), $this->coreParametersHelper->get('site_url'))) {
            return false;
        }

        return true;
    }

    /**
     * @deprecated No session for anonymous users. Use getCacheKey.
     */
    public function getSessionName(): ?string
    {
        return $this->getCacheKey();
    }

    /**
     * @deprecated No session for anonymous users. Use updateCacheItem.
     *
     * @param mixed[] $values
     *
     * @return mixed[]
     */
    public function updateSession(array $values): array
    {
        $this->updateCacheItem($values);

        return (array) $values;
    }

    /**
     * @deprecated No session for anonymous users. Use getCacheItem.
     */
    public function getSession(bool $remove = false): array
    {
        return $this->getCacheItem($remove);
    }
}