-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathAnalytics.php
More file actions
176 lines (153 loc) · 5.97 KB
/
Analytics.php
File metadata and controls
176 lines (153 loc) · 5.97 KB
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
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
namespace PrestaShop\Module\AutoUpgrade;
use PrestaShop\Module\AutoUpgrade\Parameters\BackupConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\UpdateConfiguration;
use PrestaShop\Module\AutoUpgrade\State\RestoreState;
use PrestaShop\Module\AutoUpgrade\State\UpdateState;
class Analytics
{
const SEGMENT_CLIENT_KEY_PHP = 'NrWZk42rDrA56DkEt9Tj18DBirLoRLhj';
const WITH_COMMON_PROPERTIES = 0;
const WITH_UPDATE_PROPERTIES = 1;
const WITH_BACKUP_PROPERTIES = 2;
const WITH_RESTORE_PROPERTIES = 3;
// Reusing environment variable from Distribution API
public const URL_TRACKING_ENV_NAME = 'PS_URL_TRACKING';
/**
* @var string
*/
private $anonymousId;
/**
* @var array<int, array<string, mixed>>
*/
private $properties;
/**
* @var UpdateConfiguration
*/
private $updateConfiguration;
/**
* @var BackupConfiguration
*/
private $backupConfiguration;
/**
* @var array{'restore': RestoreState, 'update': UpdateState}
*/
private $states;
/**
* @var Environment
*/
private $environment;
/**
* @param array{'properties'?: array<int, array<string, mixed>>} $options
* @param array{'restore': RestoreState, 'update': UpdateState} $states
*/
public function __construct(
UpdateConfiguration $updateConfiguration,
BackupConfiguration $backupConfiguration,
Environment $environment,
array $states,
string $anonymousUserId,
array $options
) {
$this->updateConfiguration = $updateConfiguration;
$this->backupConfiguration = $backupConfiguration;
$this->states = $states;
$this->anonymousId = $anonymousUserId;
$this->properties = $options['properties'] ?? [];
$this->environment = $environment;
if (!$this->environment->getBoolean(Environment::URL_TRACKING_ENV_NAME, true)) {
return;
}
\Segment::init(self::SEGMENT_CLIENT_KEY_PHP);
}
/**
* @param string $event
* @param self::WITH_*_PROPERTIES $propertiesType
* @param array<string, mixed> $extraProperties
*/
public function track(string $event, $propertiesType = self::WITH_COMMON_PROPERTIES, array $extraProperties = []): void
{
if (!$this->environment->getBoolean(Environment::URL_TRACKING_ENV_NAME, true)) {
return;
}
$dataToSend = array_merge(
['event' => '[SUE] ' . $event],
$this->getProperties($propertiesType)
);
$dataToSend['properties'] = array_merge(
$dataToSend['properties'],
$extraProperties
);
\Segment::track($dataToSend);
\Segment::flush();
}
/**
* @param self::WITH_*_PROPERTIES $type
*
* @return array<string, mixed>
*/
public function getProperties($type): array
{
switch ($type) {
case self::WITH_BACKUP_PROPERTIES:
$additionalProperties = [
'backup_images' => $this->backupConfiguration->shouldBackupImages(),
];
$upgradeProperties = $this->properties[self::WITH_BACKUP_PROPERTIES] ?? [];
$additionalProperties = array_merge($upgradeProperties, $additionalProperties);
break;
case self::WITH_UPDATE_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->states['update']->getCurrentVersion(),
'to_ps_version' => $this->states['update']->getDestinationVersion(),
'upgrade_channel' => $this->updateConfiguration->getChannel(),
'update_type' => $this->updateConfiguration->getUpdateType(),
'disable_non_native_modules' => $this->updateConfiguration->shouldDeactivateCustomModules(),
'regenerate_customized_email_templates' => $this->updateConfiguration->shouldRegenerateMailTemplates(),
];
$upgradeProperties = $this->properties[self::WITH_UPDATE_PROPERTIES] ?? [];
$additionalProperties = array_merge($upgradeProperties, $additionalProperties);
break;
case self::WITH_RESTORE_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->properties[self::WITH_COMMON_PROPERTIES]['ps_version'] ?? null,
'to_ps_version' => $this->states['restore']->getRestoreVersion(),
];
$rollbackProperties = $this->properties[self::WITH_RESTORE_PROPERTIES] ?? [];
$additionalProperties = array_merge($rollbackProperties, $additionalProperties);
break;
default:
$additionalProperties = [];
}
$commonProperties = $this->properties[self::WITH_COMMON_PROPERTIES] ?? [];
return [
'userId' => $this->anonymousId,
'channel' => 'browser',
'properties' => array_merge(
$commonProperties,
$additionalProperties,
[
'module' => 'autoupgrade',
]
),
];
}
}