Real-time product interaction tracking is the foundation for responsive Amazon Personalize recommendations in WooCommerce. Instead of waiting for a scheduled export of interaction data, an agency can send events such as product views, add-to-cart actions, and purchases to an Amazon Personalize event tracker as they occur.
What Amazon Personalize receives
Amazon Personalize collects real-time interactions through the PutEvents API. Each request identifies the event tracker, shopper session, and one or more interaction events.
- Tracking ID: The event tracker ID created for the Amazon Personalize dataset group.
- User ID: A stable, pseudonymous identifier for the shopper. Do not send an email address unless your privacy and data governance policies explicitly permit it.
- Session ID: An identifier that groups activity during a browsing session.
- Event type: A value such as
view,add-to-cart, orpurchase. - Item ID: The WooCommerce product or variation identifier used in the interaction dataset.
- Properties: Optional JSON data such as quantity, price, currency, or storefront context.
The item identifiers sent in real time must use the same identifier format used by the Items and Interactions datasets. If the catalog uses parent product IDs but the browser sends variation IDs, Amazon Personalize will treat them as different items.
Choose identifiers that survive the customer journey
Logged-in customers can use a stable internal customer identifier. For guests, create a first-party session or visitor identifier and keep it consistent across page views, cart actions, and checkout. A common implementation stores the identifier in a secure cookie and sends the same value to the WordPress endpoint for every event.
Do not use a new random user ID for every page view. That prevents Amazon Personalize from connecting the shopper’s sequence of interactions. Likewise, do not use an order ID as the user ID; an order identifies a transaction, not a person or browsing profile.
Capture browser interactions without exposing AWS credentials
The browser should not contain AWS access keys. A safer pattern is to send a small request to a WordPress REST endpoint and let the server-side AWS SDK call Amazon Personalize. The endpoint should validate the product ID, event type, session identifier, nonce, and request origin before forwarding the event.
A product page can emit a view event after the product data is available:
<script>
window.addEventListener('load', function () {
const productId = String(window.wcProductTracking.productId);
const sessionId = window.wcProductTracking.sessionId;
fetch('/wp-json/storefront/v1/personalize-event', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': window.wcProductTracking.nonce
},
body: JSON.stringify({
eventType: 'view',
itemId: productId,
sessionId: sessionId,
properties: {
page_type: 'product'
}
})
});
});
</script>
For a production storefront, the endpoint URL, nonce, product ID, and session ID should be injected with WordPress rather than hard-coded. Agencies should also consider delaying low-value events until consent has been granted where local privacy requirements classify personalization tracking as non-essential.
Send events from a WordPress server endpoint
The endpoint can use the AWS SDK for PHP and credentials supplied through the WordPress hosting environment, an IAM role, or a secrets manager. The following example shows the core API call. It assumes that request validation and authorization have already been implemented.
<?php
use Aws\PersonalizeEvents\PersonalizeEventsClient;
$client = new PersonalizeEventsClient([
'version' => 'latest',
'region' => getenv('AWS_REGION'),
]);
$client->putEvents([
'trackingId' => getenv('PERSONALIZE_TRACKING_ID'),
'userId' => $user_id,
'sessionId' => $session_id,
'eventList' => [[
'eventType' => $event_type,
'itemId' => $item_id,
'sentAt' => (int) round(microtime(true) * 1000),
'properties' => wp_json_encode([
'quantity' => $quantity,
'currency' => $currency,
]),
]],
]);
In an actual WordPress implementation, register the route with register_rest_route(), use permission_callback for the appropriate access checks, sanitize all values, and permit only an allowlist of event types. The AWS client should be created outside the request handler when the hosting runtime supports reuse, but the tracking ID and credentials should remain server-side configuration.
Map WooCommerce actions to interaction events
Use a consistent event vocabulary across the storefront, historical interaction exports, and recommendation use cases. A practical mapping is:
viewwhen a product detail page is meaningfully viewed.add-to-cartafter WooCommerce confirms that the item was added to the cart.purchasewhen payment has been successfully completed or the order reaches the status selected by the business.remove-from-cartif negative or abandonment signals are part of the model design.searchonly when the event schema and downstream analysis have a clear use for search interactions.
For add-to-cart tracking, attach the event to WooCommerce’s successful add-to-cart response rather than the initial button click. This avoids recording an interaction when validation fails, stock is unavailable, or the request is interrupted.
Purchase events should normally be sent server-side from a WooCommerce order hook. Use an order meta flag or an idempotency record so a webhook retry, status transition, or administrator action does not send the same purchase repeatedly. For an order containing multiple products, send one event for each purchased item or use the event structure selected for the dataset design.
Batch events when the storefront generates several signals
PutEvents accepts an event list, so a server endpoint can forward multiple events in one request. Batching can reduce request overhead, but do not hold events for so long that recommendations become stale. For a high-traffic site, a queue or short-lived buffer can protect the storefront from Amazon Personalize latency while retaining event timestamps.
When using a queue, preserve the original sentAt value and monitor delivery failures. A delayed event can still be useful, but it should not be given a new timestamp that falsely suggests the shopper just performed the action.
Connect event tracking to recommendation delivery
Sending events does not automatically change every recommendation response. The events must be sent to the event tracker associated with the dataset group used by the solution and campaign or recommender. The recommendation request must also use the same user identifier or session context where supported.
After deploying tracking, verify the complete path:
- Open a product page in a test browser and record the generated user and session identifiers.
- Confirm that the WordPress endpoint accepts the request and returns an appropriate status without exposing AWS details.
- Check Amazon Personalize event metrics and CloudWatch or application logs for rejected requests.
- Request recommendations for the same test identity and confirm that the request uses the intended campaign or recommender.
- Compare event counts with WooCommerce analytics to identify blocked consent, duplicate events, or missing variation mappings.
Handle privacy, reliability, and observability
Real-time tracking introduces operational concerns beyond the API call. Record request latency, response status, event type, item ID, and a correlation ID in application logs, but avoid logging raw customer identifiers or sensitive checkout data. Set timeouts on outbound AWS calls so a recommendation event cannot delay the storefront response.
Event delivery should fail gracefully. A temporary Amazon Personalize or network failure should not prevent a customer from viewing a product or completing checkout. For important purchase events, enqueue a retryable server-side job. For low-value view events, a bounded retry policy or controlled drop rate may be more appropriate than unlimited retries.
Finally, document the event contract for every WooCommerce implementation: identifier source, event names, timestamp units, item mapping, consent behavior, retry policy, and the WooCommerce hook responsible for each event. That contract makes the integration maintainable when the catalog, checkout flow, or recommendation strategy changes.