-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSwagGoogle.php
160 lines (136 loc) · 4.9 KB
/
SwagGoogle.php
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
<?php
/**
* (c) shopware AG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace SwagGoogle;
use Shopware\Bundle\CookieBundle\CookieCollection;
use Shopware\Bundle\CookieBundle\Structs\CookieGroupStruct;
use Shopware\Bundle\CookieBundle\Structs\CookieStruct;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Configuration\CachedReader;
use Shopware\Components\Plugin\Configuration\ReaderInterface;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Models\Shop\Shop;
use Zend_Locale as Locale;
class SwagGoogle extends Plugin
{
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}
public function deactivate(DeactivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}
public function uninstall(UninstallContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_ALL);
}
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onPostDispatch',
'Enlight_Controller_Action_PostDispatchSecure_Widgets' => 'onPostDispatch',
'CookieCollector_Collect_Cookies' => 'addGoogleAnalyticsCookie',
];
}
public function onPostDispatch(\Enlight_Event_EventArgs $args): void
{
$controller = $args->get('subject');
/** @var \Enlight_Controller_Request_Request $request */
$request = $controller->Request();
/** @var \Enlight_View_Default $view */
$view = $controller->View();
$view->addTemplateDir($this->getPath() . '/Resources/views');
if ($request->isXmlHttpRequest()) {
return;
}
$config = $this->getConfig();
if (!empty($config['conversion_code'])) {
$this->handleConversionCode($view, $config);
}
if (!empty($config['tracking_code'])) {
$this->handleTrackingCode($view, $config);
}
}
/**
* @return CookieCollection
*/
public function addGoogleAnalyticsCookie()
{
$config = $this->getConfig();
$collection = new CookieCollection();
$trackingLib = isset($config['trackingLib']) ? $config['trackingLib'] : 'ua';
$collection->add($this->getCookieStruct($trackingLib));
return $collection;
}
/**
* @param string $usedLibraryKey
*
* @return CookieStruct
*/
private function getCookieStruct($usedLibraryKey)
{
if ($usedLibraryKey === 'ga') {
return new CookieStruct(
'__utm',
'/^__utm.*$/',
'Google Analytics',
CookieGroupStruct::STATISTICS
);
}
return new CookieStruct(
'_ga',
'/(^_g(a|at|id)$)|AMP_TOKEN|^_gac_.*$/',
'Google Analytics',
CookieGroupStruct::STATISTICS
);
}
/**
* @return array<string, mixed>
*/
private function getConfig(): array
{
$shop = $this->container->get('shop');
if (!$shop instanceof Shop) {
throw new \RuntimeException('Shop not found');
}
$config = $this->container->get(CachedReader::class);
if (!$config instanceof ReaderInterface) {
throw new \RuntimeException('CachedConfigReader not found');
}
return $config->getByPluginName($this->getName(), $shop->getId());
}
/**
* @param array<string, mixed> $config
*/
private function handleConversionCode(\Enlight_View_Default $view, array $config): void
{
$locale = $this->container->get('locale');
if (!$locale instanceof Locale) {
throw new \RuntimeException('Locale not found');
}
$view->assign('GoogleConversionID', trim($config['conversion_code']));
$view->assign('GoogleConversionLabel', trim($config['conversion_label']));
$view->assign('GoogleConversionLanguage', $locale->getLanguage());
$view->assign('GoogleIncludeInHead', $config['include_header']);
}
/**
* @param array<string, mixed> $config
*/
private function handleTrackingCode(\Enlight_View_Default $view, array $config): void
{
$view->assign('GoogleTrackingID', trim($config['tracking_code']));
$view->assign('GoogleAnonymizeIp', $config['anonymize_ip']);
$view->assign('GoogleOptOutCookie', $config['include_opt_out_cookie']);
$view->assign('GoogleTrackingLibrary', $config['trackingLib']);
$view->assign('GoogleIncludeInHead', $config['include_header']);
}
}