Send Analytics Events Data
This guide assumes you have installed the latest version of klevu/php-sdk , either via composer or from source, and autoloading is functioning as expected.
Examples in this guide are for vanilla PHP and, as such, will use new to instantiate objects. If you are using a modern framework such as Magento or Symfony, you should utilise your object manager for dependency injection and preferencing of interfaces where possible.
Sending Analytics Events Data
To send analytics event data to Klevu, you will need your Klevu API key (see Quickstart for details). Note that, unlike other services, the analytics endpoint belongs to the Klevu Storefront API and, as such, does not require a private key (REST AUTH Key).
Next, extract the order and customer information to be sent from your application, including all applicable line items. As each system differs in storage and retrieval of data, the examples below simply contain hardcoded data.
Instantiate a new EventIterator object and populate with Event items. Note, while you can create these using new Event, it is recommended to use the EventFactory class for ease of development and future compatibility. Events can be added to the iterator's constructor, or after instantiation using addItem()
Finally, create an Analytics\CollectService object and call the send method with the Event collection, catching and handling any exception thrown during execution.
At this point, you will have an ApiResponseInterface which can be checked for success status, as well as any errors returned by the Klevu API.
<?php
declare(strict_types=1);
use Klevu\PhpSDK\Exception\Api\BadRequestException;
use Klevu\PhpSDK\Exception\Api\BadResponseException;
use Klevu\PhpSDK\Exception\ValidationException;
use Klevu\PhpSDK\Model\Analytics\Collect\EventFactory;
use Klevu\PhpSDK\Model\Analytics\Collect\EventIterator;
use Klevu\PhpSDK\Model\Analytics\Collect\EventType;
use Klevu\PhpSDK\Service\Analytics\CollectService;
$eventFactory = new EventFactory();
$events = new EventIterator(
data: [
$eventFactory->create(
data: [
'event' => EventType::ORDER_PURCHASE->value,
'apikey' => 'klevu-1234567890',
'version' => '1.0.0',
'userProfile' => [
'ipAddress' => '127.0.0.1',
'email' => '[email protected]',
],
'data' => [
'items' => [
[
'order_id' => 'KLEVU12345',
'order_line_id' => '12343',
'item_name' => 'Example Product',
'item_id' => 'PROD_42-VAR_001',
'item_group_id' => 'PROD_42',
'item_variant_id' => 'VAR_001',
'unit_price' => 3.14,
'currency' => 'EUR',
'units' => 42,
],
],
],
],
),
],
);
$events->addItem(
item: $eventFactory->create(
data: [
'event' => 'order_purchase',
'apikey' => 'klevu-12345',
'version' => '1.0.0',
'userProfile' => [
'ipAddress' => '111.11.11.1',
'email' => '[email protected]',
],
'data' => [
'items' => [
[
'order_id' => '1223434',
'order_line_id' => 'ABC-12343',
'item_name' => '45RU,800x1070,N-Type,D,NoS,CM,WH,EA',
'item_id' => '75882',
'item_group_id' => '75882',
'item_variant_id' => '75882',
'unit_price' => 6033.42,
'currency' => 'USD',
'units' => 2,
],
[
'order_id' => '1223435',
'order_line_id' => 'ABC-12344',
'item_name' => 'NoS,CM,WH,EA',
'item_id' => '75883',
'item_group_id' => '75883',
'item_variant_id' => '75883',
'unit_price' => 6033.42,
'currency' => 'USD',
'units' => 1,
],
],
],
],
),
);
$analyticsCollectService = new CollectService(
// Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
);
$response = null;
try {
$response = $analyticsCollectService->send($events);
} catch (ValidationException $exception) {
// One or more supplied arguments are missing or invalid
} catch (BadRequestException $exception) {
// Klevu API rejected the request as invalid
} catch (BadResponseException $exception) {
// Klevu API responded in an unexpected manner
}
$sentSuccessfully = $response?->isSuccess() ?: false;
$messages = $response?->getMessages() ?: [];