Skip to content

[SPE-105] feat(analytics): add Segment PHP client bootstrap#23

Draft
dylanDenizonPresta wants to merge 9 commits intomainfrom
SPE-105/segment-php-bootstrap
Draft

[SPE-105] feat(analytics): add Segment PHP client bootstrap#23
dylanDenizonPresta wants to merge 9 commits intomainfrom
SPE-105/segment-php-bootstrap

Conversation

@dylanDenizonPresta
Copy link
Copy Markdown
Contributor

@dylanDenizonPresta dylanDenizonPresta commented Apr 8, 2026

Summary

  • Add segmentio/analytics-php and bootstrap Segment::init() from Analytics::bootstrap() when the module is enabled.
  • Hook actionAdminControllerSetMedia on module configuration BO context only.
  • Document Segment setup in docs/SEGMENT.md and link from README.

Notes

  • Write key is currently defined in Analytics::SEGMENT_CLIENT_KEY_PHP (empty by default until set).
  • Event tracking (track / flush) is planned for follow-up work.

Test plan

  • composer install at module root
  • ./scripts/run-tests.sh unit

- Add segmentio/analytics-php dependency and PHP bootstrap (Segment::init)
- Bootstrap from BO configuration context when module is enabled
- Add Segment documentation and adjust tests accordingly

Segment::init($writeKey);
self::$clientInitialized = true;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion(blocking)

On pourrait ajouter une méthode générique pour initialiser & tracker les événements
Tu n'aurais plu besoin de la logique dans hookActionAdminControllerSetMedia
L’initialisation de Segment ne devrait pas dépendre d'un hook.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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é

@ps-jarvis ps-jarvis added the waiting for author Status: Waiting for Author Feedback label Apr 9, 2026
@github-project-automation github-project-automation bot moved this to Ready for review in PR Dashboard Apr 9, 2026
@ThbPS
Copy link
Copy Markdown
Collaborator

ThbPS commented Apr 9, 2026

todo(blocking)

Comment nous allons gérer les clés de préprod et prod ?
Les tests en locale ou en préprod ne doivent pas envoyer des événements en prod

Copy link
Copy Markdown
Contributor Author

@dylanDenizonPresta dylanDenizonPresta left a comment

Choose a reason for hiding this comment

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

Merci ! Pour éviter d'envoyer des events vers la prod en local/préprod, je propose de ne pas embarquer de write key en dur: on la lit depuis une variable d'environnement (ou un secret CI/serveur), avec une clé différente par environnement. Dans le module, on bootstrape Segment uniquement si la clé est non vide. Je peux pousser un fix qui remplace la constante par une lecture env (ex: PS_OPC_SEGMENT_WRITE_KEY) + doc.

Move Segment PHP write key out of the repository by reading it from PS_OPC_SEGMENT_WRITE_KEY.

This prevents local/preprod environments from sending events to the production Segment source when the prod key is not present.
@dylanDenizonPresta
Copy link
Copy Markdown
Contributor Author

J’ai poussé un fix: la write key n’est plus en dur, elle est lue depuis la variable d’environnement (doc + tests mis à jour). Du coup chaque environnement (local/préprod/prod) peut avoir sa clé propre, et sans clé on n’initialise pas Segment.

@dylanDenizonPresta
Copy link
Copy Markdown
Contributor Author

Correctif: la write key est lue depuis la variable d’environnement PS_OPC_SEGMENT_WRITE_KEY (plus de clé en dur). Doc + tests mis à jour. Sans clé, Segment n’est pas initialisé.

Read Segment write keys from environment variables and select the correct one using APP_ENV.

- APP_ENV=prod|production -> SEGMENT_PROD_KEY
- otherwise -> SEGMENT_PREPROD_KEY
Use _PS_MODE_DEV_ to select which Segment write key to load from env.

- _PS_MODE_DEV_=true  -> SEGMENT_PREPROD_KEY
- _PS_MODE_DEV_=false -> SEGMENT_PROD_KEY

Also stop tracking .env and provide .env.dist template.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting for author Status: Waiting for Author Feedback

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

3 participants