Skip to content

Event Tracking beta

Order Lifecycle automatically tracks key events throughout an order's lifecycle in Craft Commerce. All tracking happens via Craft Commerce's native event system - no extra code is required for automatic events.

Automatically Tracked Events

Cart Events

Event typeTrigger
cartCreatedNew cart saved for the first time
cartUpdatedGeneral cart save (line items, addresses, etc. changed)

Line Item Events

Event typeTrigger
lineItemAddedProduct added to cart
lineItemRemovedProduct removed from cart
lineItemUpdatedQuantity or options changed

Coupon Events

Event typeTrigger
couponAppliedCoupon code applied to cart
couponRemovedCoupon code removed from cart

Address Events

Event typeTrigger
shippingAddressSetShipping address added or updated
shippingAddressRemovedShipping address removed
billingAddressSetBilling address added or updated
billingAddressRemovedBilling address removed

Customer Events

Event typeTrigger
customerSetCustomer email set on the order
customerRemovedCustomer email removed from the order

Shipping Events

Event typeTrigger
shippingMethodSetShipping method selected or changed

Order Events

Event typeTrigger
statusChangedOrder status changed
orderCompletedOrder marked as complete
orderPaidOrder fully paid

Payment Events

Event typeTrigger
paymentAttemptPayment attempt failed (successful first-try payments are not logged)
paymentProcessedPayment successfully processed
paymentAuthorizedPayment authorized
paymentCapturedPayment captured
paymentRefundedRefund issued
paymentTransactionAny transaction state change (requires "Log All Payment Transactions" enabled)

Email Events

Event typeTrigger
emailSentOrder email sent successfully
emailFailedOrder email failed to send

Checkout Events

Event typeTrigger
checkoutStartedCustomer visited the checkout page (manual - see below)

Manual Checkout Tracking

The checkoutStarted event is not automatic because Craft Commerce does not fire a native event when a customer visits the checkout page. Call it from your checkout template to record it once per order:

twig
{# In your checkout template #}
{% do craft.orderLifecycle.logCheckoutStarted(cart) %}

The call is idempotent - if checkout has already been logged for that order, it does nothing.

You can also call it from PHP:

php
use johnhenry\orderlifecycle\OrderLifecycle;

OrderLifecycle::getInstance()->logger->logCheckoutStarted($order);
// returns true if logged, false if already recorded

Logging Custom Events

Use the logger service to record any business-specific event against an order:

php
use johnhenry\orderlifecycle\OrderLifecycle;
use johnhenry\orderlifecycle\enums\EventType;

// Log with a custom message
OrderLifecycle::getInstance()->logger->log(
    $order,
    EventType::STATUS_CHANGED,
    ['note' => 'Manually escalated by support'],
    'Order escalated to priority fulfilment'
);

The log() signature:

php
public function log(
    Order $order,
    EventType $type,
    array $payload = [],
    ?string $message = null
): void
  • $order - The Commerce Order element
  • $type - An EventType enum case
  • $payload - Extra data stored in the snapshot's payload key
  • $message - Custom message. If null, a message is auto-generated by comparing the current order state to the last snapshot

Auto-Generated Change Messages

When $message is null, the logger compares the current order snapshot to the previous one and generates a human-readable description of changes, for example:

'WIDGET-A' quantity increased from 1 to 3
Total price changed from 25.00 EUR to 49.99 EUR
Coupon code 'SAVE10' applied
Order status changed to 'processing'
Shipping method changed from Standard to Express

Multiple changes are joined with semicolons.

Controlling What Is Tracked

Each event category has a corresponding toggle in Settings → Order Lifecycle. See Configuration for the full list.

Event Retention

By default, logs are kept indefinitely. Set Auto-Prune Logs After (Days) in the plugin settings to automatically delete old records, or run the purge command manually:

bash
php craft order-lifecycle/logs/purge --days=90

Privacy Considerations

Event logs may contain:

  • Customer email addresses (in snapshots)
  • IP addresses (if Collect User IP Address is enabled)
  • User IDs (if Collect User ID is enabled)

Disable collection settings in Settings → Order Lifecycle if you need to minimise PII storage for GDPR or similar compliance.

Next Steps