PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/csrest_events.php

http://github.com/campaignmonitor/createsend-php
PHP | 332 lines | 173 code | 30 blank | 129 comment | 29 complexity | d7ec099d6462833b1e3963a58cab88a2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once dirname(__FILE__).'/class/base_classes.php';
  3. /**
  4. * Class to send event data to the create send API.
  5. * @author cameronn
  6. *
  7. */
  8. if (!class_exists('CS_REST_Events')) {
  9. class CS_REST_Events extends CS_REST_Wrapper_Base {
  10. /**
  11. * The base route of the clients resource.
  12. * @var string
  13. * @access private
  14. */
  15. var $_events_base_route;
  16. /**
  17. * The type of event supports 'shopify', 'identify' and 'custom'
  18. * @var string
  19. * @access private
  20. */
  21. private $_event_type;
  22. /**
  23. * Client ID
  24. * @var string
  25. * @access private
  26. */
  27. private $_client_id;
  28. /**
  29. * Anonymous ID
  30. * @var string
  31. * @access private
  32. */
  33. private $_anonymous_id;
  34. /**
  35. * User ID
  36. * @var string
  37. * @access private
  38. */
  39. private $_user_id;
  40. /**
  41. * Email address
  42. * @var string
  43. * @access private
  44. */
  45. private $_email;
  46. /**
  47. * Indicates invalid Event
  48. * @var bool
  49. * @access private
  50. */
  51. private $_invalid_event = false;
  52. /**
  53. * Constructor.
  54. *
  55. * @param $auth_details array Authentication details to use for API calls.
  56. * This array must take one of the following forms:
  57. * If using OAuth to authenticate:
  58. * array(
  59. * 'access_token' => 'your access token',
  60. * 'refresh_token' => 'your refresh token')
  61. *
  62. * Or if using an API key:
  63. * array('api_key' => 'your api key')
  64. * @param $client_id string The client id to send event to
  65. * @param $event_type string The event type we support - `custom`, `identify` and `shopify`
  66. * @param $protocol string The protocol to use for requests (http|https)
  67. * @param $debug_level int The level of debugging required CS_REST_LOG_NONE | CS_REST_LOG_ERROR | CS_REST_LOG_WARNING | CS_REST_LOG_VERBOSE
  68. * @param $host string The host to send API requests to. There is no need to change this
  69. * @param $log CS_REST_Log The logger to use. Used for dependency injection
  70. * @param $serialiser The serialiser to use. Used for dependency injection
  71. * @param $transport The transport to use. Used for dependency injection
  72. * @access public
  73. */
  74. function __construct (
  75. $auth_details,
  76. $client_id,
  77. $event_type,
  78. $protocol = 'https',
  79. $debug_level = CS_REST_LOG_NONE,
  80. $host = 'api.createsend.com',
  81. $log = NULL,
  82. $serialiser = NULL,
  83. $transport = NULL) {
  84. parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport);
  85. $this->set_client_id($client_id);
  86. if (!isset($event_type)) {
  87. trigger_error('$event_type should be one of \'custom\', \'identify\' or \'shopify\'');
  88. }
  89. $this->setEventType($event_type);
  90. }
  91. /**
  92. * Change the client id used for calls after construction
  93. * @param $client_id
  94. * @access public
  95. */
  96. function set_client_id($client_id) {
  97. if (!isset($client_id)) {
  98. trigger_error('$client_id needs to be set');
  99. }
  100. $this->_events_base_route = $this->_base_route.'events/'.$client_id.'/';
  101. $this->_client_id = $client_id;
  102. }
  103. /**
  104. * Set the type of event that we support: 'custom', 'identify' and 'shopify'
  105. * @param $event_type string Event that we support: 'custom', 'identify' and 'shopify'
  106. * @access private
  107. */
  108. private function setEventType($event_type) {
  109. if (!isset($event_type)) {
  110. trigger_error('$event_type needs to be set');
  111. return new CS_REST_Wrapper_Result(null, 400);
  112. }
  113. if (strcmp($event_type, "custom") !== 0 &&
  114. strcmp($event_type,"identify") !== 0 &&
  115. strcmp($event_type,"shopify") !== 0) {
  116. trigger_error('$event_type needs to be one of \'custom\', \'identify\' or \'shopify\'');
  117. $this->_invalid_event = true;
  118. return new CS_REST_Wrapper_Result(null, 400);
  119. }
  120. $this->_event_type = $event_type;
  121. }
  122. /*
  123. * Validate email address
  124. * @param $email string email address
  125. * @access private
  126. */
  127. private function validateEmail($email) {
  128. if (!isset($email)) {
  129. trigger_error('$email needs to be set');
  130. return new CS_REST_Wrapper_Result(null, 400);
  131. }
  132. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  133. trigger_error('$email needs to be a valid email address');
  134. return new CS_REST_Wrapper_Result(null, 400);
  135. }
  136. return $email;
  137. }
  138. /**
  139. * Get the event type name
  140. * @access public
  141. */
  142. function getEventType() {
  143. return $this->_event_type;
  144. }
  145. /**
  146. * Tracks an event
  147. * @param string $email required email in the form "user@example.com"
  148. *
  149. * @param string $event_name. Name to group events by for reporting max length 1000
  150. * For example "Page View", "Order confirmation"
  151. *
  152. * @param array $data optional. Event payload.
  153. * This should be an array, with details of the event
  154. * array(
  155. * 'RandomFieldObject' => array(
  156. * 'Example'' => 'test'
  157. * ),
  158. * 'RandomFieldURL' => 'Example',
  159. * 'RandomArray' => array(1,3,5,6,7),
  160. * )
  161. * @param $anonymous_id string Anonymous ID to use for identify events
  162. * @param $user_id string User ID to use for identify events
  163. * @access public
  164. * @return CS_REST_Wrapper_Result A successful response will include an Event ID.
  165. * array(
  166. * array(
  167. * 'EventID' => 'string'
  168. * )
  169. * )
  170. */
  171. function track($email, $event_name, $anonymous_id = NULL, $user_id = NULL, $data = NULL)
  172. {
  173. // Basic validation
  174. if (!isset($event_name)) {
  175. trigger_error('$event_name needs to be set');
  176. return new CS_REST_Wrapper_Result(null, 400);
  177. }
  178. if (strlen($event_name) > 1000) {
  179. trigger_error('$event_name needs to be shorter, max length is 1000 bytes');
  180. return new CS_REST_Wrapper_Result(null, 400);
  181. }
  182. if (isset($data)) {
  183. if (!is_array($data)) {
  184. trigger_error('$data needs to be a valid array');
  185. return new CS_REST_Wrapper_Result(null, 400);
  186. }
  187. }
  188. if (empty($data)) {
  189. $data = NULL;
  190. }
  191. if (strcmp($this->_event_type, "identify") === 0) {
  192. return $this->sendIdentifyTrack($email, $event_name, $anonymous_id, $user_id, $data);
  193. } elseif (strcmp($this->_event_type, "custom") === 0 || strcmp($this->_event_type, "shopify") === 0) {
  194. return $this->sendNonIdentifyTrack($email, $event_name, $anonymous_id, $user_id, $data);
  195. }
  196. trigger_error('event type is invalid. Supported - custom, identify or shopify');
  197. return new CS_REST_Wrapper_Result(null, 400);
  198. }
  199. /*
  200. * Send identify track event
  201. * @param $email string email address
  202. * @param $event_name string event name
  203. * @param $anonymousId string anonymous id
  204. * @param $userId string user id
  205. * @param $data array event data
  206. * @access private
  207. */
  208. private function sendIdentifyTrack($email, $event_name, $anonymousId, $userId, $data) {
  209. if (!isset($email)) {
  210. trigger_error('email needs to be a set for identify event');
  211. return new CS_REST_Wrapper_Result(null, 400);
  212. }
  213. $minRequiredParam = 1; // anonymous id / user id
  214. $paramPresent = 0;
  215. if (isset($anonymousId)) {
  216. $paramPresent += 1;
  217. }
  218. if (isset($userId)) {
  219. $paramPresent += 1;
  220. }
  221. if ($paramPresent < $minRequiredParam) {
  222. trigger_error('at least one of: anonymous id, user id needs to be set and be a valid string for identify event');
  223. return new CS_REST_Wrapper_Result(null, 400);
  224. }
  225. $this->_anonymous_id = $anonymousId;
  226. $this->_email = $this->validateEmail($email);
  227. $this->_user_id = $userId;
  228. $payload = array(
  229. 'ContactID' =>
  230. array(
  231. 'Email' => $this->_email,
  232. 'AnonymousID' => $this->_anonymous_id,
  233. 'UserID' => $this->_user_id,
  234. ),
  235. 'EventName' => $event_name,
  236. 'Data' => $data
  237. );
  238. return $this->sendTrack($payload);
  239. }
  240. /*
  241. * Send non-identify track event (custom or shopify)
  242. * @param $email string email
  243. * @param $event_name string event name
  244. * @param $data array event data
  245. */
  246. private function sendNonIdentifyTrack($email, $event_name, $anonymousId, $userId, $data) {
  247. $paramPresent = 0;
  248. if (isset($email)) {
  249. $this->_email = $this->validateEmail($email);
  250. $paramPresent += 1;
  251. } else {
  252. $this->_email = NULL;
  253. }
  254. $minRequiredParam = 1; // anonymous id / user id / email
  255. if (isset($anonymousId)) {
  256. $paramPresent += 1;
  257. }
  258. if (isset($userId)) {
  259. $paramPresent += 1;
  260. }
  261. if ($paramPresent < $minRequiredParam) {
  262. trigger_error('at least one of: anonymous id, user id, email needs to be set and be a valid string for identify event');
  263. return new CS_REST_Wrapper_Result(null, 400);
  264. }
  265. $this->_anonymous_id = $anonymousId;
  266. $this->_user_id = $userId;
  267. $payload = array(
  268. 'ContactID' =>
  269. array(
  270. 'Email' => $this->_email,
  271. 'AnonymousID' => $this->_anonymous_id,
  272. 'UserID' => $this->_user_id
  273. ),
  274. 'EventName' => $event_name,
  275. 'Data' => $data
  276. );
  277. return $this->sendTrack($payload);
  278. }
  279. /*
  280. * Send track event payload
  281. * @param $payload array Payload to send to track endpoint
  282. * @access private
  283. */
  284. private function sendTrack($payload) {
  285. if ($this->_invalid_event) {
  286. trigger_error('$event_type must be one of \'identify\', \'custom\' or \'shopify\'');
  287. return new CS_REST_Wrapper_Result(null, 400);
  288. }
  289. // Basic validation before finally POST'ing
  290. if (!isset($this->_base_route) || !isset($this->_event_type) || !isset($this->_client_id)) {
  291. trigger_error('one of: $_base_route, $_event_type, $_client_id is missing during URL construction');
  292. return new CS_REST_Wrapper_Result(null, 400);
  293. }
  294. if (isset($payload) && is_array($payload)) {
  295. $event_url = $this->_base_route . 'events/' . $this->_event_type . '/' . $this->_client_id . '/track';
  296. return $this->post_request($event_url, $payload);
  297. }
  298. trigger_error('$payload needs to be a valid array');
  299. return new CS_REST_Wrapper_Result(null, 400);
  300. }
  301. }
  302. }