├── composer.json ├── LICENSE └── src └── Customerio ├── Response.php ├── Api.php └── Request.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "userscape/customerio", 3 | "description": "PHP API for Customer.io", 4 | "keywords": ["customer.io", "customerio", "php customer.io", "php customerio"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "fideloper", 9 | "email": "fideloper@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "guzzlehttp/guzzle": "6.*|7.*" 14 | }, 15 | "require-dev": { 16 | "phpunit/phpunit": "5.5.*" 17 | }, 18 | "autoload": { 19 | "psr-0": { 20 | "Customerio":"src/" 21 | } 22 | }, 23 | "extra": { 24 | "branch-alias": { 25 | "dev-master": "2.0-dev" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 UserScape, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Customerio/Response.php: -------------------------------------------------------------------------------- 1 | code = $code; 31 | $this->message = $message; 32 | 33 | $this->parseCode($code); 34 | } 35 | 36 | /** 37 | * Whether request is successful 38 | * or not 39 | * @return bool 40 | */ 41 | public function success() 42 | { 43 | return $this->success; 44 | } 45 | 46 | /** 47 | * Return status code message 48 | * @return String 49 | */ 50 | public function message() 51 | { 52 | return $this->message; 53 | } 54 | 55 | /** 56 | * Parse code to determine 57 | * success 58 | * @param $code 59 | */ 60 | protected function parseCode($code) 61 | { 62 | switch($code) 63 | { 64 | case 200 : 65 | $this->success = true; 66 | break; 67 | default : 68 | $this->success = false; 69 | break; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Customerio/Api.php: -------------------------------------------------------------------------------- 1 | siteId = $siteId; 14 | $this->apiSecret = $apiSecret; 15 | $this->request = $request; 16 | } 17 | 18 | public function createCustomer($id, $email, $attributes=array()) 19 | { 20 | return $this->request->authenticate($this->siteId, $this->apiSecret)->customer($id, $email, $attributes); 21 | } 22 | 23 | public function updateCustomer($id, $email, $attributes=array()) 24 | { 25 | return $this->createCustomer($id, $email, $attributes); 26 | } 27 | 28 | public function deleteCustomer($id) 29 | { 30 | return $this->request->authenticate($this->siteId, $this->apiSecret)->deleteCustomer($id); 31 | } 32 | 33 | public function fireEvent($id, $name, $data=array(), $timestamp=null) 34 | { 35 | return $this->request->authenticate($this->siteId, $this->apiSecret)->event($id, $name, $data, $timestamp); 36 | } 37 | 38 | public function fireAnonymousEvent($name, $data=array()) 39 | { 40 | return $this->request->authenticate($this->siteId, $this->apiSecret)->anonymousEvent($name, $data); 41 | } 42 | 43 | public function recordPageview($id, $url, $referrer) 44 | { 45 | return $this->request->authenticate($this->siteId, $this->apiSecret)->pageview($id, $url, $referrer); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Customerio/Request.php: -------------------------------------------------------------------------------- 1 | client = $client; 32 | return; 33 | } 34 | 35 | $this->client = new \GuzzleHttp\Client([ 36 | 'base_uri' => 'https://track.customer.io', 37 | ]); 38 | } 39 | 40 | /** 41 | * Send Create/Update Customer Request 42 | * @param $id 43 | * @param $email 44 | * @param $attributes 45 | * @return Response 46 | */ 47 | public function customer($id, $email, $attributes) 48 | { 49 | $body = array_merge(array('email' => $email), $attributes); 50 | 51 | try { 52 | $response = $this->client->put('/api/v1/customers/'.$id, array( 53 | 'auth' => $this->auth, 54 | 'json' => $body, 55 | )); 56 | } catch (BadResponseException $e) { 57 | $response = $e->getResponse(); 58 | } catch (RequestException $e) { 59 | return new Response($e->getCode(), $e->getMessage()); 60 | } 61 | 62 | return new Response($response->getStatusCode(), $response->getReasonPhrase()); 63 | } 64 | 65 | /** 66 | * Send Delete Customer Request 67 | * @param $id 68 | * @return Response 69 | */ 70 | public function deleteCustomer($id) 71 | { 72 | try { 73 | $response = $this->client->delete('/api/v1/customers/'.$id, array( 74 | 'auth' => $this->auth, 75 | )); 76 | } catch (BadResponseException $e) { 77 | $response = $e->getResponse(); 78 | } catch (RequestException $e) { 79 | return new Response($e->getCode(), $e->getMessage()); 80 | } 81 | 82 | return new Response($response->getStatusCode(), $response->getReasonPhrase()); 83 | } 84 | 85 | /** 86 | * Send a pageview to Customer.io 87 | * @param $id 88 | * @param $url 89 | * @param $referrer 90 | * @return Response 91 | */ 92 | public function pageview($id, $url, $referrer = '') 93 | { 94 | $body = array_merge( array('name' => $url, 'type' => 'page'), $this->parseData( array( 'referrer' => $referrer ) ) ); 95 | 96 | try { 97 | $response = $this->client->post('/api/v1/customers/'.$id.'/events', array( 98 | 'auth' => $this->auth, 99 | 'json' => $body, 100 | )); 101 | } catch (BadResponseException $e) { 102 | $response = $e->getResponse(); 103 | } catch (RequestException $e) { 104 | return new Response($e->getCode(), $e->getMessage()); 105 | } 106 | 107 | return new Response($response->getStatusCode(), $response->getReasonPhrase()); 108 | } 109 | 110 | /** 111 | * Send and Event to Customer.io 112 | * @param $id 113 | * @param $name 114 | * @param $data 115 | * @param $timestamp (optional) 116 | * @return Response 117 | */ 118 | public function event($id, $name, $data, $timestamp) 119 | { 120 | if (is_null($timestamp)) { 121 | $body = array_merge( array('name' => $name), $this->parseData($data) ); 122 | } else { 123 | $body = array_merge( array('name' => $name, 'timestamp' => $timestamp), $this->parseData($data) ); 124 | } 125 | 126 | try { 127 | $response = $this->client->post('/api/v1/customers/'.$id.'/events', array( 128 | 'auth' => $this->auth, 129 | 'json' => $body, 130 | )); 131 | } catch (BadResponseException $e) { 132 | $response = $e->getResponse(); 133 | } catch (RequestException $e) { 134 | return new Response($e->getCode(), $e->getMessage()); 135 | } 136 | 137 | return new Response($response->getStatusCode(), $response->getReasonPhrase()); 138 | } 139 | 140 | /** 141 | * Send an Event to Customer.io not associated to any existing Customer.io user 142 | * @param $name 143 | * @param $data 144 | * @return Response 145 | */ 146 | public function anonymousEvent($name, $data) 147 | { 148 | $body = array_merge( array('name' => $name), $this->parseData($data) ); 149 | 150 | try { 151 | $response = $this->client->post('/api/v1/events', array( 152 | 'auth' => $this->auth, 153 | 'json' => $body, 154 | )); 155 | } catch (BadResponseException $e) { 156 | $response = $e->getResponse(); 157 | } catch (RequestException $e) { 158 | return new Response($e->getCode(), $e->getMessage()); 159 | } 160 | 161 | return new Response($response->getStatusCode(), $response->getReasonPhrase()); 162 | } 163 | 164 | /** 165 | * Set Authentication credentials 166 | * @param $apiKey 167 | * @param $apiSecret 168 | * @return $this 169 | */ 170 | public function authenticate($apiKey, $apiSecret) 171 | { 172 | $this->auth[0] = $apiKey; 173 | $this->auth[1] = $apiSecret; 174 | 175 | return $this; 176 | } 177 | 178 | /** 179 | * Parse data as specified by customer.io 180 | * @link http://customer.io/docs/api/rest.html 181 | * @param array $data 182 | * @return array 183 | */ 184 | protected function parseData(array $data) 185 | { 186 | if (empty($data)) 187 | { 188 | $data = new stdClass(); 189 | } 190 | 191 | return array('data' => $data); 192 | } 193 | 194 | } 195 | --------------------------------------------------------------------------------