-
Notifications
You must be signed in to change notification settings - Fork 119
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0115af5
Introduce class for analytics
Quetzacoalt91 30b43b0
Plug to project
Quetzacoalt91 33771d6
Add first events
Quetzacoalt91 df187f9
Trigger event on failure
Quetzacoalt91 e4ceda0
Add rollback failed + succeeded
Quetzacoalt91 ade4f9e
Rename TaskRunner as Task
Quetzacoalt91 d651411
Introduce Runner of a single task
Quetzacoalt91 a908fe5
Implement type in tasks
Quetzacoalt91 2080a20
Add specific event properties for rollback
Quetzacoalt91 9281979
Uate composer.lock
Quetzacoalt91 ec52e8e
Fix CI after rebase
Quetzacoalt91 705f3ed
Allow users to disable anonymous analytics
Quetzacoalt91 27d53f6
Mention the data transparency page in the readme to get details about…
Quetzacoalt91 441395d
Add php doc block
Quetzacoalt91 1c29232
Fix new issues
Quetzacoalt91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 matchesV(?<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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you go