Examples / How To
Analytics
Send Analytics Events Data
4min
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' => 'contact\@klevu com', ], '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' => 'user\@klevu com', ], '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() ? \[]; source code reference exception\api\ badrequestexception exception\api\ badresponseexception exception\ validationexception model\analytics\collect\ event model\analytics\collect\ eventfactory model\analytics\collect\ eventiterator model\analytics\collect\ eventtype service\ analytics\collectservice