Skip to content

Commit

Permalink
Improve Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Sep 12, 2024
1 parent 659563d commit 1b74c36
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
38 changes: 19 additions & 19 deletions classes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class Analytics
const SEGMENT_CLIENT_KEY_PHP = 'NrWZk42rDrA56DkEt9Tj18DBirLoRLhj';
const SEGMENT_CLIENT_KEY_JS = 'RM87m03McDSL4Fvm3GJ3piBPbAL3Fa2i';

const WITH_COMMON_PROPERTIES = 0;
const WITH_UPGRADE_PROPERTIES = 1;
const WITH_ROLLBACK_PROPERTIES = 2;
const COMMON_PROPERTIES = 0;
const UPGRADE_PROPERTIES = 1;
const ROLLBACK_PROPERTIES = 2;

// Reusing environment variable from Distribution API
public const URL_TRACKING_ENV_NAME = 'PS_URL_TRACKING';
Expand All @@ -47,7 +47,7 @@ class Analytics
private $anonymousId;

/**
* @var array<string, mixed>
* @var array<int, array<string, mixed>>
*/
private $properties;

Expand All @@ -62,19 +62,18 @@ class Analytics
private $state;

/**
* @param string $anonymousUserId
* @param array{'properties'?: array<string, mixed>} $options
* @param array{'properties'?: array<int, array<string, mixed>>} $options
*/
public function __construct(
UpgradeConfiguration $upgradeConfiguration,
State $state,
$anonymousUserId,
string $anonymousUserId,
array $options
) {
$this->upgradeConfiguration = $upgradeConfiguration;
$this->state = $state;

$this->anonymousId = hash('sha256', $anonymousUserId, false);
$this->anonymousId = hash('sha256', $anonymousUserId);
$this->properties = $options['properties'] ?? [];

if ($this->hasOptedOut()) {
Expand All @@ -86,11 +85,9 @@ public function __construct(

/**
* @param string $event
* @param self::WITH_*_PROPERTIES $propertiesType
*
* @return void
* @param int $propertiesType
*/
public function track($event, $propertiesType = self::WITH_COMMON_PROPERTIES)
public function track(string $event, int $propertiesType = self::COMMON_PROPERTIES): void
{
if ($this->hasOptedOut()) {
return;
Expand All @@ -104,14 +101,12 @@ public function track($event, $propertiesType = self::WITH_COMMON_PROPERTIES)
}

/**
* @param self::WITH_*_PROPERTIES $type
*
* @return array<string, mixed>
*/
public function getProperties($type)
public function getProperties(int $type): array
{
switch ($type) {
case self::WITH_UPGRADE_PROPERTIES:
case self::UPGRADE_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->state->getOriginVersion(),
'to_ps_version' => $this->state->getInstallVersion(),
Expand All @@ -122,22 +117,27 @@ public function getProperties($type)
'switch_to_default_theme' => $this->upgradeConfiguration->shouldSwitchToDefaultTheme(),
'keep_customized_email_templates' => $this->upgradeConfiguration->shouldKeepMails(),
];
$upgradeProperties = $this->properties[self::UPGRADE_PROPERTIES] ?? [];
$additionalProperties = array_merge($upgradeProperties, $additionalProperties);
break;
case self::WITH_ROLLBACK_PROPERTIES:
case self::ROLLBACK_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->properties['ps_version'] ?? null,
'from_ps_version' => $this->properties[self::COMMON_PROPERTIES]['ps_version'] ?? null,
'to_ps_version' => $this->state->getRestoreVersion(),
];
$rollbackProperties = $this->properties[self::ROLLBACK_PROPERTIES] ?? [];
$additionalProperties = array_merge($rollbackProperties, $additionalProperties);
break;
default:
$additionalProperties = [];
}

$commonProperties = $this->properties[self::COMMON_PROPERTIES] ?? [];
return [
'anonymousId' => $this->anonymousId,
'channel' => 'browser',
'properties' => array_merge(
$this->properties,
$commonProperties,
$additionalProperties,
[
'module' => 'autoupgrade',
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function setErrorFlag(): void
if (static::TASK_TYPE) {
$this->container->getAnalytics()->track(
ucfirst(static::TASK_TYPE) . ' Failed',
static::TASK_TYPE === 'upgrade' ? Analytics::WITH_UPGRADE_PROPERTIES : Analytics::WITH_ROLLBACK_PROPERTIES
static::TASK_TYPE === 'upgrade' ? Analytics::UPGRADE_PROPERTIES : Analytics::ROLLBACK_PROPERTIES
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Upgrade/UpgradeComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function run(): int

// removing temporary files
$this->container->getFileConfigurationStorage()->cleanAll();
$this->container->getAnalytics()->track('Upgrade Succeeded', Analytics::WITH_UPGRADE_PROPERTIES);
$this->container->getAnalytics()->track('Upgrade Succeeded', Analytics::UPGRADE_PROPERTIES);

return ExitCode::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Task/Upgrade/UpgradeNow.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function run(): int
$this->logger->debug($this->translator->trans('Downloaded archive will come from %s', [$upgrader->link]));
$this->logger->debug($this->translator->trans('MD5 hash will be checked against %s', [$upgrader->md5]));
}
$this->container->getAnalytics()->track('Upgrade Launched', Analytics::WITH_UPGRADE_PROPERTIES);
$this->container->getAnalytics()->track('Upgrade Launched', Analytics::UPGRADE_PROPERTIES);

return ExitCode::SUCCESS;
}
Expand Down
17 changes: 10 additions & 7 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
namespace PrestaShop\Module\AutoUpgrade;

use Exception;
use Language;
use PrestaShop\Module\AutoUpgrade\Log\LegacyLogger;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\Parameters\FileConfigurationStorage;
Expand Down Expand Up @@ -245,11 +244,15 @@ public function getAnalytics(): Analytics
$this->getState(),
$this->getProperty(self::WORKSPACE_PATH), [
'properties' => [
'ps_version' => $this->getProperty(self::PS_VERSION),
'php_version' => VersionUtils::getHumanReadableVersionOf(PHP_VERSION_ID),
'autoupgrade_version' => $this->getPrestaShopConfiguration()->getModuleVersion(),
'disable_all_overrides' => class_exists('\Configuration', false) ? UpgradeConfiguration::isOverrideAllowed() : null,
'regenerate_rtl_stylesheet' => $this->shouldUpdateRTLFiles(),
Analytics::COMMON_PROPERTIES => [
'ps_version' => $this->getProperty(self::PS_VERSION),
'php_version' => VersionUtils::getHumanReadableVersionOf(PHP_VERSION_ID),
'autoupgrade_version' => $this->getPrestaShopConfiguration()->getModuleVersion(),
],
Analytics::UPGRADE_PROPERTIES => [
'disable_all_overrides' => class_exists('\Configuration', false) ? UpgradeConfiguration::isOverrideAllowed() : null,
'regenerate_rtl_stylesheet' => $this->shouldUpdateRTLFiles(),
],
],
]);
}
Expand Down Expand Up @@ -689,7 +692,7 @@ public function resetOpcache(): void
*/
public function shouldUpdateRTLFiles(): bool
{
$languages = Language::getLanguages(false);
$languages = \Language::getLanguages(false);

foreach ($languages as $lang) {
if ($lang['is_rtl']) {
Expand Down
36 changes: 18 additions & 18 deletions tests/unit/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,37 @@ public function testProperties()
'somePathToAutoupgradeModule',
[
'properties' => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
'disable_all_overrides' => true,
'regenerate_rtl_stylesheet' => false,
Analytics::COMMON_PROPERTIES => [
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
],
Analytics::UPGRADE_PROPERTIES => [
'disable_all_overrides' => true,
'regenerate_rtl_stylesheet' => false,
],
],
]
);

$this->assertEquals([
'anonymousId' => '3cbc0821f904fd952a8526f17b9b92a8abde4b394a66c9171cf35c9beb2b4784',
'channel' => 'browser',
'properties' => array_merge(
'properties' =>
[
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
'disable_all_overrides' => true,
'module' => 'autoupgrade',
'regenerate_rtl_stylesheet' => false,
]),
],
],
$analytics->getProperties(Analytics::WITH_COMMON_PROPERTIES)
$analytics->getProperties(Analytics::COMMON_PROPERTIES)
);

$this->assertEquals([
'anonymousId' => '3cbc0821f904fd952a8526f17b9b92a8abde4b394a66c9171cf35c9beb2b4784',
'channel' => 'browser',
'properties' => array_merge(
'properties' =>
[
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
Expand All @@ -97,28 +99,26 @@ public function testProperties()
'switch_to_default_theme' => true,
'keep_customized_email_templates' => false,
'regenerate_rtl_stylesheet' => false,
]),
],
],
$analytics->getProperties(Analytics::WITH_UPGRADE_PROPERTIES)
$analytics->getProperties(Analytics::UPGRADE_PROPERTIES)
);

$this->assertEquals([
'anonymousId' => '3cbc0821f904fd952a8526f17b9b92a8abde4b394a66c9171cf35c9beb2b4784',
'channel' => 'browser',
'properties' => array_merge(
'properties' =>
[
'ps_version' => '8.8.8',
'php_version' => '6.0.8',
'autoupgrade_version' => '9.8.7',
'disable_all_overrides' => true,
'module' => 'autoupgrade',

'from_ps_version' => '8.8.8',
'to_ps_version' => '1.2.3',
'regenerate_rtl_stylesheet' => false,
]),
],
],
$analytics->getProperties(Analytics::WITH_ROLLBACK_PROPERTIES)
$analytics->getProperties(Analytics::ROLLBACK_PROPERTIES)
);
}
}

0 comments on commit 1b74c36

Please sign in to comment.