-
Notifications
You must be signed in to change notification settings - Fork 2
[SPE-105] feat(analytics): add Segment PHP client bootstrap #23
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
597a8bd
75235c8
25aa8a2
456428e
a080b46
6514452
0c4f2ca
2f699d8
b8a3178
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Segment dans `ps_onepagecheckout` | ||
|
|
||
| Ce document décrit la **configuration** et l’**intégration technique** de Segment via le **SDK PHP** (`segmentio/analytics-php`), sur le même principe que le module [PrestaShop autoupgrade](https://github.com/PrestaShop/autoupgrade) (classe [`Analytics`](https://github.com/PrestaShop/autoupgrade/blob/dev/classes/Analytics.php)). | ||
|
|
||
| **Comportement actuel** : initialisation de `Segment::init()` lorsque les conditions sont réunies. **Aucun** appel `track` / `flush` n’est encore envoyé depuis le module — cela fera l’objet de tickets ultérieurs. | ||
|
|
||
| **Résumé** : write key **en dur** dans `Analytics::SEGMENT_CLIENT_KEY_PHP` → `Segment::init()` sur les requêtes BO qui passent par le hook concerné, **dès que le module est activé**. | ||
|
|
||
| La classe PHP s’appelle **`Analytics`** (nom volontairement **générique**) pour limiter les renommages si le fournisseur change. | ||
|
|
||
| **Front office** : aucun chargement du SDK navigateur (`analytics.js`) ; l’ancien bundle `opc-segment-init` a été retiré. | ||
|
|
||
| ## Dépendance Composer | ||
|
|
||
| - `segmentio/analytics-php` (voir `composer.json`). Après clone : `composer install` à la racine du module pour disposer du vendeur et de l’autoload. | ||
|
|
||
| ## Clés et constantes | ||
|
|
||
| | Source | Identifiant | Rôle | Défaut | | ||
| |--------|-------------|------|--------| | ||
| | Code PHP | `Analytics::SEGMENT_CLIENT_KEY_PHP` | Write key de la **source PHP** Segment — **seule source de vérité** (pas de `configuration`). | `''` | | ||
|
|
||
| ## Architecture PHP | ||
|
|
||
| | Fichier | Rôle | | ||
| |---------|------| | ||
| | `src/Analytics/Analytics.php` | `bootstrap(bool $moduleSegmentEnabled)` : vérifie activation module, clé non vide, puis `Segment\Segment::init($writeKey)`. Garde statique pour n’initialiser qu’une fois par requête. | | ||
|
|
||
| `ps_onepagecheckout.php` appelle `Analytics::bootstrap(true)` depuis `bootstrapPhpSegmentClient()` (Segment activé tant que le module est activé), invoqué dans `hookActionAdminControllerSetMedia` lorsque `isBackOfficeConfigurationContext()` est vrai. | ||
|
|
||
| ### Différences notables avec l’ancienne version (navigateur) | ||
|
|
||
| - Plus de `window.psopc_segment` ni de `opc-segment-init.bundle.js`. | ||
| - La clé **PHP** (`SEGMENT_CLIENT_KEY_PHP`) n’est pas la même source Segment que l’ancienne clé **JavaScript** ; à configurer dans l’espace Segment (source PHP). | ||
|
|
||
| ## Où cela s’exécute | ||
|
|
||
| Hook **`actionAdminControllerSetMedia`**, uniquement sur la **configuration du module** en BO (`AdminPsOnePageCheckout`, `configure=ps_onepagecheckout`, ou `AdminPsOnePageCheckoutController`), comme auparavant pour le chargement JS — sauf qu’il ne s’agit plus que d’initialiser le client PHP. | ||
|
|
||
| ## Configuration Back Office | ||
|
|
||
| Segment est considéré **activé** tant que le module est activé (et si la write key est non vide). | ||
|
|
||
| ## Événements (`track`) — à venir | ||
|
|
||
| Les appels `Segment::track()` / `flush()` seront ajoutés dans de futurs tickets, une fois les événements métier définis. | ||
|
|
||
| ## Cycle de vie du module | ||
|
|
||
| Pas de clé de configuration dédiée à Segment : l’activation suit l’activation du module. | ||
|
|
||
| ## Fichiers utiles | ||
|
|
||
| - `src/Analytics/Analytics.php` | ||
| - `ps_onepagecheckout.php` — `hookActionAdminControllerSetMedia`, `bootstrapPhpSegmentClient()` | ||
| - `composer.json` — dépendance `segmentio/analytics-php` | ||
|
|
||
| ## Limites | ||
|
|
||
| - La write key en dur dans le dépôt : politique de secret / environnements à définir côté équipe. | ||
| - `Segment::init` est appelé dans le contexte des requêtes qui déclenchent le hook (typiquement pages de config du module en BO). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?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 | ||
| * license@prestashop.com so we can send you a copy immediately. | ||
| * | ||
| * @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
| * @copyright Since 2007 PrestaShop SA and Contributors | ||
| * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PrestaShop\Module\PsOnePageCheckout\Analytics; | ||
|
|
||
| use Segment\Segment; | ||
|
|
||
| /** | ||
| * Segment PHP SDK bootstrap, same approach as | ||
| * {@link https://github.com/PrestaShop/autoupgrade/blob/dev/classes/Analytics.php PrestaShop\Module\AutoUpgrade\Analytics} | ||
| * (constant write key). | ||
| * | ||
| * Event tracking (track) will be added in follow-up work. | ||
| */ | ||
| final class Analytics | ||
| { | ||
| /** | ||
| * Segment PHP source write key — single source of truth (not stored in configuration). | ||
| */ | ||
| public const SEGMENT_CLIENT_KEY_PHP = ''; | ||
|
|
||
| private static bool $clientInitialized = false; | ||
|
|
||
| public static function bootstrap(bool $moduleSegmentEnabled): void | ||
| { | ||
| if (!$moduleSegmentEnabled || self::$clientInitialized) { | ||
| return; | ||
| } | ||
|
|
||
| $writeKey = trim((string) self::SEGMENT_CLIENT_KEY_PHP); | ||
| if ($writeKey === '') { | ||
| return; | ||
| } | ||
|
|
||
| Segment::init($writeKey); | ||
| self::$clientInitialized = true; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On pourrait ajouter une méthode générique pour initialiser & tracker les événements Exemple: private const EVENT_GUEST_INIT_SUCCEEDED = 'opc_guest_init_succeeded';
public static function bootstrap(): void
{
if (self::$clientInitialized === true) {
return;
}
$writeKey = trim((string) self::SEGMENT_CLIENT_KEY_PHP);
if ($writeKey === '') {
return;
}
Segment::init($writeKey);
self::$clientInitialized = true;
}
/**
* @param array<string, mixed> $properties
* @param array<string, mixed> $context
*/
private static function track(
string $event,
array $properties = [],
array $context = []
): void {
try {
self::bootstrap();
if (self::$clientInitialized === false) {
return;
}
Segment::track([
'event' => $event,
'properties' => $properties,
'context' => $context,
]);
} catch (Throwable $exception) {
PrestaShopLogger::addLog(
sprintf('ps_onepagecheckout analytics track failed for "%s": %s', $event, $exception->getMessage()),
2
);
}
}
public static function trackGuestInitSucceeded(array $properties = []): void
{
self::track(EVENT_GUEST_INIT_SUCCEEDED, $properties);
}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dylanDenizonPresta ce commentaire est bloquant et doit être résolu afin d'avoir un tracking robuste, réutilisable et centralisé |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?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 | ||
| * license@prestashop.com so we can send you a copy immediately. | ||
| * | ||
| * @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
| * @copyright Since 2007 PrestaShop SA and Contributors | ||
| * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
| */ | ||
| header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | ||
| header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | ||
|
|
||
| header('Cache-Control: no-store, no-cache, must-revalidate'); | ||
| header('Cache-Control: post-check=0, pre-check=0', false); | ||
| header('Pragma: no-cache'); | ||
|
|
||
| header('Location: ../'); | ||
| exit; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * For the full copyright and license information, please view the | ||
| * docs/licenses/LICENSE.txt file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Unit\Analytics; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use PrestaShop\Module\PsOnePageCheckout\Analytics\Analytics; | ||
|
|
||
| class AnalyticsTest extends TestCase | ||
| { | ||
| public function testBootstrapDoesNothingWhenModuleSegmentDisabled(): void | ||
| { | ||
| Analytics::bootstrap(false); | ||
|
|
||
| self::assertTrue(true); | ||
| } | ||
|
|
||
| public function testBootstrapDoesNothingWhenWriteKeyConstantEmpty(): void | ||
| { | ||
| if (trim((string) Analytics::SEGMENT_CLIENT_KEY_PHP) !== '') { | ||
| self::markTestSkipped('SEGMENT_CLIENT_KEY_PHP is set; empty-key path not exercised.'); | ||
| } | ||
|
|
||
| Analytics::bootstrap(true); | ||
|
|
||
| self::assertTrue(true); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.