|
| 1 | +# Exponea API SDK for PHP |
| 2 | + |
| 3 | +Library contains only basic functionality which is needed for Exponea integration. If you miss some method, please post merge request as our integration just does not need them. |
| 4 | + |
| 5 | +Entire library uses asynchronous Guzzle requests. Please keep in mind that every Promise returned by methods must be called with wait() to be executed. |
| 6 | + |
| 7 | +Public and private key authorization is used in Exponea API so you will need to get 3 values to make valid requests: |
| 8 | + |
| 9 | +- public key |
| 10 | +- private key |
| 11 | +- project token |
| 12 | + |
| 13 | +Exponea API reference: https://documentation.bloomreach.com/engagement/reference/welcome |
| 14 | + |
| 15 | +## Usage example |
| 16 | + |
| 17 | +Please check following source implementing API intialization and getSystemTime() method: |
| 18 | + |
| 19 | +Usage: |
| 20 | +```php |
| 21 | +use belenka\ExponeaApi\Client; |
| 22 | + |
| 23 | +$client = new Client([ |
| 24 | + 'public_key' => getenv('EXPONEA_PUBLIC_KEY'), |
| 25 | + 'private_key' => getenv('EXPONEA_PRIVATE_KEY'), |
| 26 | + 'project_token' => getenv('EXPONEA_PROJECT_TOKEN'), |
| 27 | +]); |
| 28 | +try { |
| 29 | + $systemTime = $client->tracking()->getSystemTime()->wait(); // returns SystemTime object |
| 30 | +} catch (...) { ... } |
| 31 | +``` |
| 32 | + |
| 33 | +## Tracking API |
| 34 | + |
| 35 | +All methods are contained inside `$client->tracking()` method. |
| 36 | + |
| 37 | +### Set contact agreements (consents) |
| 38 | + |
| 39 | +Both e-mail and SMS agreements are called *consents* in Exponea. They can be granted or revoked. |
| 40 | + |
| 41 | +```php |
| 42 | +$event = new Consent( |
| 43 | + new RegisteredCustomer('example@example.com'), |
| 44 | + Consent::CATEGORY_NEWSLETTER, |
| 45 | + Consent::ACTION_GRANT |
| 46 | +); |
| 47 | +try { |
| 48 | + $client->tracking()->addEvent($event)->wait(); // does not return anything |
| 49 | +} catch (...) { ... } |
| 50 | +``` |
| 51 | + |
| 52 | +### Send purchase |
| 53 | + |
| 54 | +Exponea needs you to send at least two events: Purchase and PurchaseItem (one for every purchase item). |
| 55 | + |
| 56 | +```php |
| 57 | +$purchase = new Purchase( |
| 58 | + new RegisteredCustomer('example@example.com'), |
| 59 | + 'PREFIX12345', // purchase id |
| 60 | + [ |
| 61 | + new Item('012345', 2.99, 1), |
| 62 | + ], // purchase items |
| 63 | + 'COD' // payment method |
| 64 | +); |
| 65 | +$purchaseItem = new PurchaseItem( |
| 66 | + new RegisteredCustomer('example@example.com'), |
| 67 | + 'PREFIX12345', // purchase id |
| 68 | + '012345', // item id |
| 69 | + 2.99, // price |
| 70 | + 2, // quantity |
| 71 | + 'SKU012345', // sku (stock keeping unit) |
| 72 | + 'Product name', |
| 73 | + new Category('CAT1', 'Some > Category > Breadcrumb') |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +You can optionally send voucher used during purchase. Please refer to `$voucher` argument of `Purchase` constructor. |
| 78 | + |
| 79 | + |
| 80 | +### Update customer properties |
| 81 | + |
| 82 | +```php |
| 83 | +try { |
| 84 | + $properties = [ |
| 85 | + 'fidelity_points' => 657, |
| 86 | + 'first_name' => 'Marian', |
| 87 | + ]; |
| 88 | + |
| 89 | + $client->tracking()->updateCustomerProperties( |
| 90 | + new RegisteredCustomer('marian@exponea.com'), $properties |
| 91 | + )->wait(); |
| 92 | +} catch (...) { ... } |
| 93 | +``` |
| 94 | + |
| 95 | +With this method you can update customer properties. Required field in properties is 'first_name'. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +## Catalog API |
| 100 | + |
| 101 | +All methods are contained inside `$client->catalog()` method. |
| 102 | + |
| 103 | +### Get catalog name |
| 104 | + |
| 105 | +```php |
| 106 | +try { |
| 107 | + $catalog = new Catalog('<exponea_catalog_id>'); |
| 108 | + |
| 109 | + $response = $this->client |
| 110 | + ->catalog() |
| 111 | + ->getCatalogName($catalog) |
| 112 | + ->wait() |
| 113 | + ; |
| 114 | +} catch (...) { ... } |
| 115 | +``` |
| 116 | + |
| 117 | +### Get catalog items |
| 118 | + |
| 119 | +```php |
| 120 | +try { |
| 121 | + $catalog = new Catalog('<exponea_catalog_id>'); |
| 122 | + $catalog->setQueryParameters([ |
| 123 | + 'query' => 1, |
| 124 | + 'field' => 'item_id', |
| 125 | + 'count' => 1 |
| 126 | + ]); |
| 127 | + |
| 128 | + $response = $this->client |
| 129 | + ->catalog() |
| 130 | + ->getCatalogItems($catalog) |
| 131 | + ->wait() |
| 132 | + ; |
| 133 | +} catch (...) { ... } |
| 134 | +``` |
| 135 | + |
| 136 | +### Get catalog item by ID |
| 137 | + |
| 138 | +```php |
| 139 | +try { |
| 140 | + $catalog = new Catalog('<exponea_catalog_id>'); |
| 141 | + $catalog->setItemID(1); |
| 142 | + |
| 143 | + $response = $this->client |
| 144 | + ->catalog() |
| 145 | + ->getCatalogItem($catalog) |
| 146 | + ->wait() |
| 147 | + ; |
| 148 | +} catch (...) { ... } |
| 149 | +``` |
| 150 | + |
| 151 | +### Create catalog item |
| 152 | + |
| 153 | +```php |
| 154 | +try { |
| 155 | + $catalogItem = new CatalogItem(1, '<exponea_catalog_id>'); |
| 156 | + $catalogItem->setProperties([ |
| 157 | + 'code' => 'product_code', |
| 158 | + 'active' => false, |
| 159 | + 'title' => 'product title' |
| 160 | + // etc ... |
| 161 | + ]); |
| 162 | + |
| 163 | + $response = $this->client |
| 164 | + ->catalog() |
| 165 | + ->createCatalogItem($catalogItem) |
| 166 | + ->wait() |
| 167 | + ; |
| 168 | +} catch (...) { ... } |
| 169 | +``` |
| 170 | + |
| 171 | +### Update catalog item |
| 172 | + |
| 173 | +```php |
| 174 | +try { |
| 175 | + $catalogItem = new CatalogItem(1, '<exponea_catalog_id>'); |
| 176 | + $catalogItem->setProperties([ |
| 177 | + 'title' => 'new product title' |
| 178 | + // etc ... |
| 179 | + ]); |
| 180 | + |
| 181 | + $response = $this->client |
| 182 | + ->catalog() |
| 183 | + ->updateCatalogItem($catalogItem) |
| 184 | + ->wait() |
| 185 | + ; |
| 186 | +} catch (...) { ... } |
| 187 | +``` |
0 commit comments