Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce anonymous product analytics #746

Merged
merged 15 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The following parameters are mandatory:

* **--dir**: Tells where the admin directory is.
* **--channel**: Selects what upgrade to run (minor, major etc.)
* **--action**: Advanced users only. Sets the step you want to start from (Default: `UpgradeNow`, [other values available](classes/TaskRunner/Upgrade/)).
* **--action**: Advanced users only. Sets the step you want to start from (Default: `UpgradeNow`, [other values available](classes/Task/Upgrade/)).

```
$ php cli-upgrade.php --dir=admin-dev --channel=major
Expand All @@ -82,7 +82,8 @@ $ php cli-rollback.php --dir=admin-dev --backup=V1.7.5.1_20190502-191341-22e883

## Documentation

Documentation is hosted on [the Developer documentation][doc].
* Documentation is hosted on [the Developer documentation][doc].
* Privacy documentation is hosted [on the PrestaShop Project website][prestashop-privacy].

## Contributing

Expand All @@ -104,6 +105,7 @@ This module is released under the [Academic Free License 3.0][AFL-3.0]

[report-issue]: https://github.com/PrestaShop/PrestaShop/issues/new/choose
[prestashop]: https://www.prestashop-project.org/
[prestashop-privacy]: https://www.prestashop-project.org/data-transparency/
[contribution-guidelines]: https://devdocs.prestashop-project.org/8/contribute/contribution-guidelines/project-modules/
[AFL-3.0]: https://opensource.org/licenses/AFL-3.0
[doc]: https://devdocs.prestashop-project.org/8/basics/keeping-up-to-date/upgrade-module/
Expand Down
7 changes: 4 additions & 3 deletions ajax-upgradetab.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PrestaShop\Module\AutoUpgrade\Task\Runner\SingleTask;
use PrestaShop\Module\AutoUpgrade\Tools14;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\TaskRepository;

/**
* This file is the entrypoint for all ajax requests during a upgrade, rollback or configuration.
Expand All @@ -48,7 +49,7 @@
exit(1);
}

$controller = TaskRepository::get(Tools14::getValue('action'), $container);
$controller->init();
$controller = new SingleTask($container);
$controller->setOptions(['action' => Tools14::getValue('action')]);
$controller->run();
echo $controller->getJsonResponse();
158 changes: 158 additions & 0 deletions classes/Analytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?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 3.0 (AFL-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.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @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 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;

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;

// Reusing environment variable from Distribution API
public const URL_TRACKING_ENV_NAME = 'PS_URL_TRACKING';

/**
* @var string
*/
private $anonymousId;

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

/**
* @var UpgradeConfiguration
*/
private $upgradeConfiguration;

/**
* @var State
*/
private $state;

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

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

if ($this->hasOptedOut()) {
return;
}

\Segment::init(self::SEGMENT_CLIENT_KEY_PHP);
}

/**
* @param string $event
* @param self::WITH_*_PROPERTIES $propertiesType
*
* @return void
*/
public function track($event, $propertiesType = self::WITH_COMMON_PROPERTIES)
{
if ($this->hasOptedOut()) {
return;
}

\Segment::track(array_merge(
['event' => $event],
$this->getProperties($propertiesType)
));
\Segment::flush();
}

/**
* @param self::WITH_*_PROPERTIES $type
*
* @return array<string, mixed>
*/
public function getProperties($type)
{
switch ($type) {
case self::WITH_UPGRADE_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->state->getOriginVersion(),
'to_ps_version' => $this->state->getInstallVersion(),
'upgrade_channel' => $this->upgradeConfiguration->getChannel(),
'backup_files_and_databases' => $this->upgradeConfiguration->get('PS_AUTOUP_BACKUP')
|| !$this->upgradeConfiguration->get('skip_backup'),
'backup_images' => $this->upgradeConfiguration->shouldBackupImages(),
'server_performance' => $this->upgradeConfiguration->getPerformanceLevel(),
'disable_non_native_modules' => $this->upgradeConfiguration->shouldDeactivateCustomModules(),
'upgrade_default_theme' => $this->upgradeConfiguration->shouldUpdateDefaultTheme(),
'switch_to_default_theme' => $this->upgradeConfiguration->shouldSwitchToDefaultTheme(),
'regenerate_rtl_stylesheet' => $this->upgradeConfiguration->shouldUpdateRTLFiles(),
'keep_customized_email_templates' => $this->upgradeConfiguration->shouldKeepMails(),
];
break;
case self::WITH_ROLLBACK_PROPERTIES:
$additionalProperties = [
'from_ps_version' => $this->properties['ps_version'] ?? null,
'to_ps_version' => $this->state->getRestoreVersion(),
];
break;
default:
$additionalProperties = [];
}

return [
'anonymousId' => $this->anonymousId,
'channel' => 'browser',
'properties' => array_merge(
$this->properties,
$additionalProperties,
[
'module' => 'autoupgrade',
]
),
];
}

private function hasOptedOut(): bool
{
return isset($_SERVER[self::URL_TRACKING_ENV_NAME])
&& ((bool) $_SERVER[self::URL_TRACKING_ENV_NAME] === false || $_SERVER[self::URL_TRACKING_ENV_NAME] === 'false');
}
}
31 changes: 31 additions & 0 deletions classes/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
*/
class State
{
/**
* @var string
*/
private $originVersion; // Origin version of PrestaShop
/**
* @var string
*/
Expand Down Expand Up @@ -176,6 +180,11 @@ function ($v) { return $v['iso_code']; },
}

// GETTERS
public function getOriginVersion(): string
{
return $this->originVersion;
}

public function getInstallVersion(): string
{
return $this->install_version;
Expand Down Expand Up @@ -263,6 +272,13 @@ public function getWarningExists(): bool
}

// SETTERS
public function setOriginVersion(string $originVersion): State
{
$this->originVersion = $originVersion;

return $this;
}

public function setInstallVersion(string $install_version): State
{
$this->install_version = $install_version;
Expand Down Expand Up @@ -348,6 +364,21 @@ public function setRestoreDbFilenames(array $restoreDbFilenames): State
return $this;
}

/**
* Pick version from restoration file name in the format v[version]_[date]-[time]-[random]
*/
public function getRestoreVersion(): ?string
{
$matches = [];
preg_match(
'/^V(?<version>[1-9\.]+)_/',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: explain in phpDoc why you exclude some patterns

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I got your message properly. What do you mean by excluding when I try to pickup the version from a string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I call getRestoreVersion(), you check $this->getRestoreName() and verify if it matches V(?<version>[1-9\.]+)

If it matches, you return it. If it does not you return null. So I said you "exclude" patterns which do not match.

An implementation that does not do exclusion would be simply returning `$this->getRestoreName()

I suggest explaining the reason behind the above logic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go

$this->getRestoreName(),
$matches
);

return $matches[1] ?? null;
}

/**
* @param string[] $installedLanguagesIso
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\TaskRunner;
namespace PrestaShop\Module\AutoUpgrade\Task;

use Exception;
use PrestaShop\Module\AutoUpgrade\AjaxResponse;
use PrestaShop\Module\AutoUpgrade\Analytics;
use PrestaShop\Module\AutoUpgrade\Log\Logger;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
Expand Down Expand Up @@ -59,6 +60,11 @@ abstract class AbstractTask
*/
protected $container;

/**
* @var 'upgrade'|'rollback'|null
*/
const TASK_TYPE = null;

// Task progress details
/**
* @var bool
Expand Down Expand Up @@ -138,6 +144,19 @@ private function checkTaskMayRun(): void
}
}

public function setErrorFlag(): void
{
$this->error = true;
// TODO: Add this? $this->next = 'error';

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
);
}
}

/**
* @throws Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\TaskRunner;
namespace PrestaShop\Module\AutoUpgrade\Task;

class ExitCode
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\TaskRunner\Miscellaneous;
namespace PrestaShop\Module\AutoUpgrade\Task\Miscellaneous;

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask;
use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\TaskRunner\Miscellaneous;
namespace PrestaShop\Module\AutoUpgrade\Task\Miscellaneous;

use Exception;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask;
use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\VersionUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\AutoUpgrade\TaskRunner\Miscellaneous;
namespace PrestaShop\Module\AutoUpgrade\Task\Miscellaneous;

use Exception;
use PrestaShop\Module\AutoUpgrade\ChannelInfo;
use PrestaShop\Module\AutoUpgrade\TaskRunner\AbstractTask;
use PrestaShop\Module\AutoUpgrade\TaskRunner\ExitCode;
use PrestaShop\Module\AutoUpgrade\Task\AbstractTask;
use PrestaShop\Module\AutoUpgrade\Task\ExitCode;
use PrestaShop\Module\AutoUpgrade\Twig\Block\ChannelInfoBlock;
use Twig\Error\LoaderError;

Expand Down
Loading
Loading