From 9807af0c8dadc5d9128befa41b822618f781c12e Mon Sep 17 00:00:00 2001 From: Ishwor Gurung Date: Mon, 21 Aug 2017 15:03:19 +1000 Subject: [PATCH 1/8] add new endpoints - custom, identify and shopify --- composer.lock | 78 +++++++++++++++++++++++++--------- csrest_events.php | 75 ++++++++++++++++++++++++++++++-- samples/events/event_track.php | 20 ++++++--- tests/all_tests.php | 3 +- tests/csrest_events_test.php | 48 +++++++++++++++------ tests/csrest_test.php | 3 +- 6 files changed, 181 insertions(+), 46 deletions(-) diff --git a/composer.lock b/composer.lock index 22193fd..0d291bf 100644 --- a/composer.lock +++ b/composer.lock @@ -1,57 +1,93 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" - ], - "hash": "eccad7521596b5fe0d81af51ab2f16a1", - "packages": [ - + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" ], + "content-hash": "d6810a171a6fbbf05f7f1156b79fdd3d", + "packages": [], "packages-dev": [ { "name": "simpletest/simpletest", - "version": "1.1.3", + "version": "v1.1.7", "source": { "type": "git", - "url": "https://github.com/simpletest/simpletest", - "reference": "1.1.3" + "url": "https://github.com/simpletest/simpletest.git", + "reference": "2f8c466c114bdb9c11028a0c3e6d1380ae6a18dc" }, "dist": { "type": "zip", - "url": "https://github.com/simpletest/simpletest/zipball/1.1.3", - "reference": "1.1.3", + "url": "https://api.github.com/repos/simpletest/simpletest/zipball/2f8c466c114bdb9c11028a0c3e6d1380ae6a18dc", + "reference": "2f8c466c114bdb9c11028a0c3e6d1380ae6a18dc", "shasum": "" }, "require": { "php": ">=5.0.5" }, + "replace": { + "lastcraft/simpletest": "self.version", + "vierbergenlars/simpletest": "self.version" + }, "type": "library", + "autoload": { + "classmap": [ + "." + ] + }, "notification-url": "https://packagist.org/downloads/", "license": [ "LGPL-2.0+" ], "authors": [ + { + "name": "Lars Vierbergen", + "email": "vierbergenlars@gmail.com" + }, + { + "name": "Lachlan Donald", + "email": "lachlan@ljd.cc" + }, { "name": "Marcus Baker", - "email": "marcus@lastcraft.com" + "email": "marcus@lastcraft.com", + "role": "Original project lead" + }, + { + "name": "Jason Sweat", + "role": "Original developer" + }, + { + "name": "Travis Swicegood", + "role": "Original developer" + }, + { + "name": "Perrick Penet", + "role": "Original developer" + }, + { + "name": "Edward Z. Yang", + "role": "Original developer" } ], "description": "Unit testing, mock objects and web testing framework for PHP built around test cases.", "homepage": "http://simpletest.org/", - "time": "2012-06-11 22:22:54" + "keywords": [ + "SimpleTest", + "code-coverage", + "selenium", + "testing", + "unit-test" + ], + "time": "2015-09-21T18:19:52+00:00" } ], - "aliases": [ - - ], + "aliases": [], "minimum-stability": "stable", - "stability-flags": [ - - ], + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, "platform": { "php": ">=5.3.0" }, - "platform-dev": [ - - ] + "platform-dev": [] } diff --git a/csrest_events.php b/csrest_events.php index cb1e8d6..2b47001 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -16,6 +16,27 @@ class CS_REST_Events extends CS_REST_Wrapper_Base { */ var $_events_base_route; + /** + * The type of event supports 'shopify', 'identify' and 'custom' + * @var string + * @access private + */ + var $_event_type; + + /** + * Client ID + * @var string + * @access private + */ + var $_client_id; + + /** + * Anonymous ID + * @var string + * @access private + */ + var $_anonymous_id; + /** * Constructor. @@ -30,6 +51,7 @@ class CS_REST_Events extends CS_REST_Wrapper_Base { * Or if using an API key: * array('api_key' => 'your api key') * @param $client_id string The client id to send event to + * @param $event_type string The event type we support - `custom`, `identify` and `shopify` * @param $protocol string The protocol to use for requests (http|https) * @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 * @param $host string The host to send API requests to. There is no need to change this @@ -41,6 +63,7 @@ class CS_REST_Events extends CS_REST_Wrapper_Base { function __construct ( $auth_details, $client_id, + $event_type, $protocol = 'https', $debug_level = CS_REST_LOG_NONE, $host = 'api.createsend.com', @@ -49,9 +72,12 @@ function __construct ( $transport = NULL) { parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport); $this->set_client_id($client_id); + if (!isset($event_type)) { + trigger_error('$event_type should be one of \'custom\', \'identify\' or \'shopify\''); + } + $this->setEventType($event_type); } - /** * Change the client id used for calls after construction * @param $client_id @@ -62,6 +88,41 @@ function set_client_id($client_id) { trigger_error('$client_id needs to be set'); } $this->_events_base_route = $this->_base_route.'events/'.$client_id.'/'; + $this->_client_id = $client_id; + } + + /** + * Set the type of event that we support: 'custom', 'identify' and 'shopify' + * @param $event_type + * @access public + */ + function setEventType($event_type) { + if (!isset($event_type)) { + trigger_error('$event_type needs to be set'); + return new CS_REST_Wrapper_Result(null, 400); + } + + if (strcmp($event_type, "custom") != 0 && + strcmp($event_type,"identify") != 0 && + strcmp($event_type,"shopify") != 0) { + trigger_error('$event_type needs to be one of \'custom\', \'identify\' or \'shopify\''); + return new CS_REST_Wrapper_Result(null, 400); + } + $this->_event_type = strtolower($event_type); + } + + /** + * Set the anonymous ID + * @param $anonymous_id + * @access public + */ + function setAnonymousID($anon_id) { + if (!isset($anon_id)) { + trigger_error('$anonymous_id needs to be set for identified events'); + return new CS_REST_Wrapper_Result(null, 400); + } + + $this->_anonymous_id = strtolower($anon_id); } /** @@ -89,7 +150,7 @@ function set_client_id($client_id) { * ) * ) */ - function track($email, $event_name, $data = NULL) { + function track($email, $event_name, $anon_id = NULL, $data = NULL) { if (!isset($email)) { trigger_error('$email needs to be set'); return new CS_REST_Wrapper_Result(null, 400); @@ -112,8 +173,14 @@ function track($email, $event_name, $data = NULL) { return new CS_REST_Wrapper_Result(null, 400); } } - $payload = array('ContactID' => array('Email' => $email), 'EventName' => $event_name, 'Data' => $data); - return $this->post_request($this->_events_base_route. 'track', $payload); + if (strcmp($this->_event_type, "identify") == 0 && isset($anon_id)) { + $this->setAnonymousID($anon_id); + $payload = array('ContactID' => array('Email' => $email, 'AnonymousID' => $this->_anonymous_id), 'EventName' => $event_name, 'Data' => $data); + } else { + $payload = array('ContactID' => array('Email' => $email), 'EventName' => $event_name, 'Data' => $data); + } + $event_url = $this->_base_route . 'events/'. $this->_event_type . '/' . $this->_client_id . '/track'; + return $this->post_request($event_url, $payload); } } } diff --git a/samples/events/event_track.php b/samples/events/event_track.php index 37014af..3f45f10 100644 --- a/samples/events/event_track.php +++ b/samples/events/event_track.php @@ -1,14 +1,15 @@ "Your API Key"); -$client_id = "Your Client ID"; -$wrap = new CS_REST_Events($auth, $client_id); +$auth = array("api_key" => "sample api key"); +$client_id = "sample client id"; +$api_event_type = "shopify"; +$wrap = new CS_REST_Events($auth, $client_id, $api_event_type); -echo "\nSending a simple event...\n"; +echo "\nSending a $api_event_type event...\n"; $contact = "joe@example.org"; -$event_type = "checkout"; +$event_type = "checkout"; $event_data = array( "Page" => "/cart/checkout", "Items" => array( @@ -27,7 +28,14 @@ "CardType" => "VISA", ); -$result = $wrap->track($contact, $event_type, $event_data); +if (strcmp($wrap->_event_type, "identify") == 0) { + // `Identify` event + $anon_id = "abcd"; + $result = $wrap->track($contact, $event_type, $anon_id, $event_data); +} else { + // Non `identify` event + $result = $wrap->track($contact, $event_type, NULL, $event_data); +} echo "\nEvent Sent! Here's the response:\n"; var_dump($result); diff --git a/tests/all_tests.php b/tests/all_tests.php index 4a7e1e9..41633c6 100644 --- a/tests/all_tests.php +++ b/tests/all_tests.php @@ -5,7 +5,8 @@ class AllTests extends TestSuite { function AllTests() { - $this->TestSuite('All Tests'); + //Do we even require it? + //$this->TestSuite('All Tests'); $this->addFile('class_tests/transport_test.php'); $this->addFile('class_tests/response_tests.php'); $this->addFile('csrest_test.php'); diff --git a/tests/csrest_events_test.php b/tests/csrest_events_test.php index fb52727..2660a47 100644 --- a/tests/csrest_events_test.php +++ b/tests/csrest_events_test.php @@ -20,11 +20,13 @@ class CS_REST_OAuthTestEvents extends CS_REST_TestEvents { abstract class CS_REST_TestEvents extends CS_REST_TestBase { var $client_id = 'fakeclientid'; var $events_base_route; + var $event_type = "identify"; function set_up_inner() { $this->events_base_route = $this->base_route.'events/'.$this->client_id.'/'; - $this->wrapper = new CS_REST_Events($this->auth, $this->client_id, $this->protocol, $this->log_level, - $this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport); + $this->wrapper = new CS_REST_Events($this->auth, $this->client_id, $this->event_type, $this->protocol, + $this->log_level, $this->api_host, $this->mock_log, + $this->mock_serialiser, $this->mock_transport); } function testtrack() { @@ -33,19 +35,35 @@ function testtrack() { $email = 'test@email.com'; $event_name = 'Widget Man!'; $data = array('ExampleField'=> 'Me'); + $anon_id = 'abcd'; $response_code = 202; - $call_options = $this->get_call_options($this->base_route.'events/'.$client_id.'/track', 'POST'); + $call_options = $this->get_call_options($this->base_route.'events/'.$this->event_type.'/'.$this->client_id.'/track', 'POST'); - $event_info = array ( - 'ContactID' => array( - 'Email' => 'test@email.com' - ), - 'EventName' => $event_name, - 'Data' => array( - 'ExampleField'=> 'Me' - ) - ); + if (strcmp($this->event_type, "identify") == 0) { + // `Identify` event + $event_info = array ( + 'ContactID' => array( + 'Email' => 'test@email.com', + 'AnonymousID' => $anon_id, + ), + 'EventName' => $event_name, + 'Data' => array( + 'ExampleField'=> 'Me' + ) + ); + } else { + // Non `identify` event + $event_info = array ( + 'ContactID' => array( + 'Email' => 'test@email.com', + ), + 'EventName' => $event_name, + 'Data' => array( + 'ExampleField'=> 'Me' + ) + ); + } $transport_result = array ( 'code' => $response_code, @@ -59,7 +77,11 @@ function testtrack() { $this->setup_transport_and_serialisation($transport_result, $call_options, $raw_result, $raw_result, 'event info was serialised to this', $event_info, $response_code); - $result = $this->wrapper->track($email, $event_name, $data); + if (strcmp($this->event_type, "identify") == 0) { + $result = $this->wrapper->track($email, $event_name, $anon_id, $data); + } else { + $result = $this->wrapper->track($email, $event_name, NULL, $data); + } $this->assertIdentical($expected_result, $result); diff --git a/tests/csrest_test.php b/tests/csrest_test.php index b1c124f..e0509a1 100644 --- a/tests/csrest_test.php +++ b/tests/csrest_test.php @@ -38,7 +38,8 @@ function setUp() { function set_up_inner() { $this->wrapper = new CS_REST_General($this->auth, $this->protocol, $this->log_level, - $this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport); + $this->api_host, $this->mock_log, $this->mock_serialiser, + $this->mock_transport); } function get_call_options($route, $method = 'GET') { From f02ecb28cdbaa29364658da523bbb132b697ab4f Mon Sep 17 00:00:00 2001 From: Ishwor Gurung Date: Tue, 22 Aug 2017 10:31:43 +1000 Subject: [PATCH 2/8] fix for simpletest test bug --- tests/all_tests.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/all_tests.php b/tests/all_tests.php index 41633c6..b71c9d9 100644 --- a/tests/all_tests.php +++ b/tests/all_tests.php @@ -4,9 +4,8 @@ require_once __DIR__.'/../vendor/simpletest/simpletest/mock_objects.php'; class AllTests extends TestSuite { - function AllTests() { - //Do we even require it? - //$this->TestSuite('All Tests'); + function __construct() { + parent::__construct('All Tests'); $this->addFile('class_tests/transport_test.php'); $this->addFile('class_tests/response_tests.php'); $this->addFile('csrest_test.php'); From 1581959ae81973aa309985c5eeaa6795ca0e6c5c Mon Sep 17 00:00:00 2001 From: ishworg Date: Tue, 22 Aug 2017 11:10:25 +1000 Subject: [PATCH 3/8] make string & type comparison --- csrest_events.php | 8 ++++---- samples/events/event_track.php | 2 +- tests/csrest_events_test.php | 35 +++++++++++++--------------------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/csrest_events.php b/csrest_events.php index 2b47001..5e3c49e 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -102,9 +102,9 @@ function setEventType($event_type) { return new CS_REST_Wrapper_Result(null, 400); } - if (strcmp($event_type, "custom") != 0 && - strcmp($event_type,"identify") != 0 && - strcmp($event_type,"shopify") != 0) { + if (strcmp($event_type, "custom") !== 0 && + strcmp($event_type,"identify") !== 0 && + strcmp($event_type,"shopify") !== 0) { trigger_error('$event_type needs to be one of \'custom\', \'identify\' or \'shopify\''); return new CS_REST_Wrapper_Result(null, 400); } @@ -173,7 +173,7 @@ function track($email, $event_name, $anon_id = NULL, $data = NULL) { return new CS_REST_Wrapper_Result(null, 400); } } - if (strcmp($this->_event_type, "identify") == 0 && isset($anon_id)) { + if (strcmp($this->_event_type, "identify") === 0 && isset($anon_id)) { $this->setAnonymousID($anon_id); $payload = array('ContactID' => array('Email' => $email, 'AnonymousID' => $this->_anonymous_id), 'EventName' => $event_name, 'Data' => $data); } else { diff --git a/samples/events/event_track.php b/samples/events/event_track.php index 3f45f10..a2c5013 100644 --- a/samples/events/event_track.php +++ b/samples/events/event_track.php @@ -28,7 +28,7 @@ "CardType" => "VISA", ); -if (strcmp($wrap->_event_type, "identify") == 0) { +if (strcmp($wrap->_event_type, "identify") === 0) { // `Identify` event $anon_id = "abcd"; $result = $wrap->track($contact, $event_type, $anon_id, $event_data); diff --git a/tests/csrest_events_test.php b/tests/csrest_events_test.php index 2660a47..f54d1ee 100644 --- a/tests/csrest_events_test.php +++ b/tests/csrest_events_test.php @@ -40,29 +40,20 @@ function testtrack() { $call_options = $this->get_call_options($this->base_route.'events/'.$this->event_type.'/'.$this->client_id.'/track', 'POST'); - if (strcmp($this->event_type, "identify") == 0) { + // Non `identify` event + $event_info = array ( + 'ContactID' => array( + 'Email' => 'test@email.com', + ), + 'EventName' => $event_name, + 'Data' => array( + 'ExampleField'=> 'Me' + ) + ); + + if (strcmp($this->event_type, "identify") === 0) { // `Identify` event - $event_info = array ( - 'ContactID' => array( - 'Email' => 'test@email.com', - 'AnonymousID' => $anon_id, - ), - 'EventName' => $event_name, - 'Data' => array( - 'ExampleField'=> 'Me' - ) - ); - } else { - // Non `identify` event - $event_info = array ( - 'ContactID' => array( - 'Email' => 'test@email.com', - ), - 'EventName' => $event_name, - 'Data' => array( - 'ExampleField'=> 'Me' - ) - ); + $event_info['ContactID']['AnonymousID'] = $anon_id; } $transport_result = array ( From 82d794f397b4db3fce984f6a805082aee010df64 Mon Sep 17 00:00:00 2001 From: ishworg Date: Tue, 22 Aug 2017 18:25:10 +1000 Subject: [PATCH 4/8] more refactoring --- csrest_events.php | 69 +++++++++++++++++++++++----------- samples/events/event_track.php | 2 +- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/csrest_events.php b/csrest_events.php index 5e3c49e..9ed69f7 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -21,21 +21,21 @@ class CS_REST_Events extends CS_REST_Wrapper_Base { * @var string * @access private */ - var $_event_type; + private $_event_type; /** * Client ID * @var string * @access private */ - var $_client_id; + private $_client_id; /** * Anonymous ID * @var string * @access private */ - var $_anonymous_id; + private $_anonymous_id; /** @@ -93,8 +93,8 @@ function set_client_id($client_id) { /** * Set the type of event that we support: 'custom', 'identify' and 'shopify' - * @param $event_type - * @access public + * @param $event_type string Event that we support: 'custom', 'identify' and 'shopify' + * @access private */ function setEventType($event_type) { if (!isset($event_type)) { @@ -111,18 +111,26 @@ function setEventType($event_type) { $this->_event_type = strtolower($event_type); } + /** - * Set the anonymous ID - * @param $anonymous_id + * Get the name of event * @access public */ + function getEventType() { + return $this->_event_type; + } + + /** + * Set the anonymous ID to use for non-identify events + * @param $anonymous_id string Anonymous ID to use for non-identify events + * @access private + */ function setAnonymousID($anon_id) { if (!isset($anon_id)) { - trigger_error('$anonymous_id needs to be set for identified events'); + trigger_error('$anonymous_id needs to be set for identify events'); return new CS_REST_Wrapper_Result(null, 400); } - - $this->_anonymous_id = strtolower($anon_id); + $this->_anonymous_id = $anon_id; } /** @@ -141,7 +149,7 @@ function setAnonymousID($anon_id) { * 'RandomFieldURL' => 'Example', * 'RandomArray' => array(1,3,5,6,7), * ) - * + * @param $anonymous_id string Anonymous ID to use for non-identify events * @access public * @return CS_REST_Wrapper_Result A successful response will include an Event ID. * array( @@ -150,7 +158,8 @@ function setAnonymousID($anon_id) { * ) * ) */ - function track($email, $event_name, $anon_id = NULL, $data = NULL) { + function track($email, $event_name, $anonymous_id = NULL, $data = NULL) + { if (!isset($email)) { trigger_error('$email needs to be set'); return new CS_REST_Wrapper_Result(null, 400); @@ -163,24 +172,42 @@ function track($email, $event_name, $anon_id = NULL, $data = NULL) { trigger_error('$event_name needs to be set'); return new CS_REST_Wrapper_Result(null, 400); } - if (strlen($event_name) > 1000 ) { + if (strlen($event_name) > 1000) { trigger_error('$event_name needs to be shorter, max length is 1000 character'); return new CS_REST_Wrapper_Result(null, 400); - } + } if (isset($data)) { - if (!is_array($data)){ - trigger_error('$data needs to be a valid array'); + if (!is_array($data)) { + trigger_error('$data needs to be a valid array'); + return new CS_REST_Wrapper_Result(null, 400); + } + } + if (strcmp($this->_event_type, "identify") === 0 && !isset($anonymous_id)) { + trigger_error('$anonymous_id needs to be a valid string'); return new CS_REST_Wrapper_Result(null, 400); - } } - if (strcmp($this->_event_type, "identify") === 0 && isset($anon_id)) { - $this->setAnonymousID($anon_id); + + if (strcmp($this->_event_type, "identify") === 0 && isset($anonymous_id)) { + $this->setAnonymousID($anonymous_id); $payload = array('ContactID' => array('Email' => $email, 'AnonymousID' => $this->_anonymous_id), 'EventName' => $event_name, 'Data' => $data); } else { $payload = array('ContactID' => array('Email' => $email), 'EventName' => $event_name, 'Data' => $data); } - $event_url = $this->_base_route . 'events/'. $this->_event_type . '/' . $this->_client_id . '/track'; - return $this->post_request($event_url, $payload); + return $this->sendTrack($payload); + } + + /* + * Send track payload + * @param $payload array Payload to send to track endpoint + * @access private + */ + function sendTrack($payload = NULL) { + if (isset($payload) && is_array($payload)) { + $event_url = $this->_base_route . 'events/' . $this->_event_type . '/' . $this->_client_id . '/track'; + return $this->post_request($event_url, $payload); + } + trigger_error('$payload needs to be a valid array'); + return new CS_REST_Wrapper_Result(null, 400); } } } diff --git a/samples/events/event_track.php b/samples/events/event_track.php index a2c5013..506a72b 100644 --- a/samples/events/event_track.php +++ b/samples/events/event_track.php @@ -3,7 +3,7 @@ $auth = array("api_key" => "sample api key"); $client_id = "sample client id"; -$api_event_type = "shopify"; +$api_event_type = "identify"; $wrap = new CS_REST_Events($auth, $client_id, $api_event_type); echo "\nSending a $api_event_type event...\n"; From cf3b939d4137c0598080df06b97e9ea2b4d338c7 Mon Sep 17 00:00:00 2001 From: ishworg Date: Tue, 22 Aug 2017 18:49:14 +1000 Subject: [PATCH 5/8] fix visibility, further refactor --- csrest_events.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/csrest_events.php b/csrest_events.php index 9ed69f7..410fa93 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -37,6 +37,12 @@ class CS_REST_Events extends CS_REST_Wrapper_Base { */ private $_anonymous_id; + /** + * Indicates invalid Event Type + * @var bool + * @access private + */ + private $_invalid_event_type; /** * Constructor. @@ -96,7 +102,7 @@ function set_client_id($client_id) { * @param $event_type string Event that we support: 'custom', 'identify' and 'shopify' * @access private */ - function setEventType($event_type) { + private function setEventType($event_type) { if (!isset($event_type)) { trigger_error('$event_type needs to be set'); return new CS_REST_Wrapper_Result(null, 400); @@ -106,6 +112,7 @@ function setEventType($event_type) { strcmp($event_type,"identify") !== 0 && strcmp($event_type,"shopify") !== 0) { trigger_error('$event_type needs to be one of \'custom\', \'identify\' or \'shopify\''); + $this->_invalid_event_type = true; return new CS_REST_Wrapper_Result(null, 400); } $this->_event_type = strtolower($event_type); @@ -125,7 +132,7 @@ function getEventType() { * @param $anonymous_id string Anonymous ID to use for non-identify events * @access private */ - function setAnonymousID($anon_id) { + private function setAnonymousID($anon_id) { if (!isset($anon_id)) { trigger_error('$anonymous_id needs to be set for identify events'); return new CS_REST_Wrapper_Result(null, 400); @@ -183,7 +190,7 @@ function track($email, $event_name, $anonymous_id = NULL, $data = NULL) } } if (strcmp($this->_event_type, "identify") === 0 && !isset($anonymous_id)) { - trigger_error('$anonymous_id needs to be a valid string'); + trigger_error('$anonymous_id needs to be a valid string for identify event'); return new CS_REST_Wrapper_Result(null, 400); } @@ -201,7 +208,12 @@ function track($email, $event_name, $anonymous_id = NULL, $data = NULL) * @param $payload array Payload to send to track endpoint * @access private */ - function sendTrack($payload = NULL) { + private function sendTrack($payload = NULL) { + if ($this->_invalid_event_type) { + trigger_error('$event_type must be one of \'identify\', \'custom\' or \'shopify\''); + return new CS_REST_Wrapper_Result(null, 400); + } + if (isset($payload) && is_array($payload)) { $event_url = $this->_base_route . 'events/' . $this->_event_type . '/' . $this->_client_id . '/track'; return $this->post_request($event_url, $payload); From 9dec9a17eb10d23ce29250c4c85a75a901354543 Mon Sep 17 00:00:00 2001 From: ishworg Date: Wed, 23 Aug 2017 15:20:36 +1000 Subject: [PATCH 6/8] add user id --- csrest_events.php | 154 +++++++++++++++++++++++++-------- samples/events/event_track.php | 42 ++++----- tests/csrest_events_test.php | 10 ++- 3 files changed, 143 insertions(+), 63 deletions(-) diff --git a/csrest_events.php b/csrest_events.php index 410fa93..4cb83c6 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -38,11 +38,25 @@ class CS_REST_Events extends CS_REST_Wrapper_Base { private $_anonymous_id; /** - * Indicates invalid Event Type + * User ID + * @var string + * @access private + */ + private $_user_id; + + /** + * Email address + * @var string + * @access private + */ + private $_email; + + /** + * Indicates invalid Event * @var bool * @access private */ - private $_invalid_event_type; + private $_invalid_event = false; /** * Constructor. @@ -112,32 +126,38 @@ private function setEventType($event_type) { strcmp($event_type,"identify") !== 0 && strcmp($event_type,"shopify") !== 0) { trigger_error('$event_type needs to be one of \'custom\', \'identify\' or \'shopify\''); - $this->_invalid_event_type = true; + $this->_invalid_event = true; return new CS_REST_Wrapper_Result(null, 400); } $this->_event_type = strtolower($event_type); } - /** - * Get the name of event - * @access public + /* + * Validate email address + * @param $email string email address + * @access private */ - function getEventType() { - return $this->_event_type; + private function validateEmail($email) { + if (!isset($email)) { + trigger_error('$email needs to be set'); + return new CS_REST_Wrapper_Result(null, 400); + } + + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + trigger_error('$email needs to be a valid email address'); + return new CS_REST_Wrapper_Result(null, 400); + } + + return $email; } /** - * Set the anonymous ID to use for non-identify events - * @param $anonymous_id string Anonymous ID to use for non-identify events - * @access private + * Get the event type name + * @access public */ - private function setAnonymousID($anon_id) { - if (!isset($anon_id)) { - trigger_error('$anonymous_id needs to be set for identify events'); - return new CS_REST_Wrapper_Result(null, 400); - } - $this->_anonymous_id = $anon_id; + function getEventType() { + return $this->_event_type; } /** @@ -156,7 +176,8 @@ private function setAnonymousID($anon_id) { * 'RandomFieldURL' => 'Example', * 'RandomArray' => array(1,3,5,6,7), * ) - * @param $anonymous_id string Anonymous ID to use for non-identify events + * @param $anonymous_id string Anonymous ID to use for identify events + * @param $user_id string User ID to use for identify events * @access public * @return CS_REST_Wrapper_Result A successful response will include an Event ID. * array( @@ -165,16 +186,9 @@ private function setAnonymousID($anon_id) { * ) * ) */ - function track($email, $event_name, $anonymous_id = NULL, $data = NULL) + function track($email, $event_name, $anonymous_id = NULL, $user_id = NULL, $data = NULL) { - if (!isset($email)) { - trigger_error('$email needs to be set'); - return new CS_REST_Wrapper_Result(null, 400); - } - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { - trigger_error('$email needs to be a valid email address'); - return new CS_REST_Wrapper_Result(null, 400); - } + // Basic validation if (!isset($event_name)) { trigger_error('$event_name needs to be set'); return new CS_REST_Wrapper_Result(null, 400); @@ -189,31 +203,95 @@ function track($email, $event_name, $anonymous_id = NULL, $data = NULL) return new CS_REST_Wrapper_Result(null, 400); } } - if (strcmp($this->_event_type, "identify") === 0 && !isset($anonymous_id)) { - trigger_error('$anonymous_id needs to be a valid string for identify event'); + + if (strcmp($this->_event_type, "identify") === 0) { + return $this->sendIdentifyTrack($email, $event_name, $anonymous_id, $user_id, $data); + } elseif (strcmp($this->_event_type, "custom") === 0 || strcmp($this->_event_type, "shopify") === 0) { + return $this->sendNonIdentifyTrack($email, $event_name, $data); + } + + trigger_error('event type is invalid. Supported - custom, identify or shopify'); + return new CS_REST_Wrapper_Result(null, 400); + } + + /* + * Send identify track event + * @param $email string email address + * @param $event_name string event name + * @param $anonymousId string anonymous id + * @param $userId string user id + * @param $data array event data + * @access private + */ + private function sendIdentifyTrack($email, $event_name, $anonymousId, $userId, $data) { + if (!isset($email)) { + trigger_error('email needs to be a set for identify event'); return new CS_REST_Wrapper_Result(null, 400); } + $minRequiredParam = 1; // anonymous id / user id + $paramPresent = 0; + if (isset($anonymousId)) { + $paramPresent += 1; + } + if (isset($userId)) { + $paramPresent += 1; + } - if (strcmp($this->_event_type, "identify") === 0 && isset($anonymous_id)) { - $this->setAnonymousID($anonymous_id); - $payload = array('ContactID' => array('Email' => $email, 'AnonymousID' => $this->_anonymous_id), 'EventName' => $event_name, 'Data' => $data); - } else { - $payload = array('ContactID' => array('Email' => $email), 'EventName' => $event_name, 'Data' => $data); + if ($paramPresent < $minRequiredParam) { + trigger_error('at least one of: anonymous id, user id needs to be set and be a valid string for identify event'); + return new CS_REST_Wrapper_Result(null, 400); } + + $this->_anonymous_id = $anonymousId; + $this->_email = $this->validateEmail($email); + $this->_user_id = $userId; + + $payload = array( + 'ContactID' => + array( + 'Email' => $this->_email, + 'AnonymousID' => $this->_anonymous_id, + 'UserID' => $this->_user_id, + ), + 'EventName' => $event_name, + 'Data' => $data + ); return $this->sendTrack($payload); } /* - * Send track payload + * Send non-identify track event (custom or shopify) + * @param $email string email + * @param $event_name string event name + * @param $data array event data + */ + private function sendNonIdentifyTrack($email, $event_name, $data) { + $payload = array( + 'ContactID' => + array( + 'Email' => $email + ), + 'EventName' => $event_name, + 'Data' => $data + ); + return $this->sendTrack($payload); + } + + /* + * Send track event payload * @param $payload array Payload to send to track endpoint * @access private */ - private function sendTrack($payload = NULL) { - if ($this->_invalid_event_type) { + private function sendTrack($payload) { + if ($this->_invalid_event) { trigger_error('$event_type must be one of \'identify\', \'custom\' or \'shopify\''); return new CS_REST_Wrapper_Result(null, 400); } - + // Basic validation before finally POST'ing + if (!isset($this->_base_route) || !isset($this->_event_type) || !isset($this->_client_id)) { + trigger_error('one of: $_base_route, $_event_type, $_client_id is missing during URL construction'); + return new CS_REST_Wrapper_Result(null, 400); + } if (isset($payload) && is_array($payload)) { $event_url = $this->_base_route . 'events/' . $this->_event_type . '/' . $this->_client_id . '/track'; return $this->post_request($event_url, $payload); diff --git a/samples/events/event_track.php b/samples/events/event_track.php index 506a72b..6f5a383 100644 --- a/samples/events/event_track.php +++ b/samples/events/event_track.php @@ -11,32 +11,32 @@ $contact = "joe@example.org"; $event_type = "checkout"; $event_data = array( - "Page" => "/cart/checkout", - "Items" => array( - array( - "Description" => "Rubber Widget", - "Quantity" => 1, - "Price" => 300, - ), - array( - "Description" => "Paint 1L", - "Quantity" => 10, - "Price" => 1, - ), - ), - "User" => "joe@example.org", - "CardType" => "VISA", + "Page" => "/cart/checkout", + "Items" => array( + array( + "Description" => "Rubber Widget", + "Quantity" => 1, + "Price" => 300, + ), + array( + "Description" => "Paint 1L", + "Quantity" => 10, + "Price" => 1, + ), + ), + "User" => "joe@example.org", + "CardType" => "VISA", ); -if (strcmp($wrap->_event_type, "identify") === 0) { +if (strcmp($wrap->getEventType(), "identify") === 0) { // `Identify` event - $anon_id = "abcd"; - $result = $wrap->track($contact, $event_type, $anon_id, $event_data); + $anon_id = "anonymousid-0"; + $user_id = "userid-0"; + $result = $wrap->track($contact, $event_type, $anon_id, $user_id, $event_data); } else { - // Non `identify` event - $result = $wrap->track($contact, $event_type, NULL, $event_data); + // `Non-identify` event (custom, shopify) + $result = $wrap->track($contact, $event_type, NULL, NULL, $event_data); } echo "\nEvent Sent! Here's the response:\n"; var_dump($result); - diff --git a/tests/csrest_events_test.php b/tests/csrest_events_test.php index f54d1ee..199f45f 100644 --- a/tests/csrest_events_test.php +++ b/tests/csrest_events_test.php @@ -35,12 +35,13 @@ function testtrack() { $email = 'test@email.com'; $event_name = 'Widget Man!'; $data = array('ExampleField'=> 'Me'); - $anon_id = 'abcd'; + $anon_id = 'anonid-0'; + $user_id = 'userid-0'; $response_code = 202; $call_options = $this->get_call_options($this->base_route.'events/'.$this->event_type.'/'.$this->client_id.'/track', 'POST'); - // Non `identify` event + // `Non-identify` event (custom, shopify) $event_info = array ( 'ContactID' => array( 'Email' => 'test@email.com', @@ -54,6 +55,7 @@ function testtrack() { if (strcmp($this->event_type, "identify") === 0) { // `Identify` event $event_info['ContactID']['AnonymousID'] = $anon_id; + $event_info['ContactID']['UserID'] = $user_id; } $transport_result = array ( @@ -69,9 +71,9 @@ function testtrack() { $raw_result, $raw_result, 'event info was serialised to this', $event_info, $response_code); if (strcmp($this->event_type, "identify") == 0) { - $result = $this->wrapper->track($email, $event_name, $anon_id, $data); + $result = $this->wrapper->track($email, $event_name, $anon_id, $user_id, $data); } else { - $result = $this->wrapper->track($email, $event_name, NULL, $data); + $result = $this->wrapper->track($email, $event_name, NULL, NULL, $data); } $this->assertIdentical($expected_result, $result); From 2b31bd7744e489006225ab15f38e602032051042 Mon Sep 17 00:00:00 2001 From: ishworg Date: Thu, 24 Aug 2017 17:56:15 +1000 Subject: [PATCH 7/8] update logic --- csrest_events.php | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/csrest_events.php b/csrest_events.php index 4cb83c6..4046cc9 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -129,7 +129,7 @@ private function setEventType($event_type) { $this->_invalid_event = true; return new CS_REST_Wrapper_Result(null, 400); } - $this->_event_type = strtolower($event_type); + $this->_event_type = $event_type; } @@ -203,11 +203,14 @@ function track($email, $event_name, $anonymous_id = NULL, $user_id = NULL, $data return new CS_REST_Wrapper_Result(null, 400); } } + if (empty($data)) { + $data = NULL; + } if (strcmp($this->_event_type, "identify") === 0) { return $this->sendIdentifyTrack($email, $event_name, $anonymous_id, $user_id, $data); } elseif (strcmp($this->_event_type, "custom") === 0 || strcmp($this->_event_type, "shopify") === 0) { - return $this->sendNonIdentifyTrack($email, $event_name, $data); + return $this->sendNonIdentifyTrack($email, $event_name, $anonymous_id, $user_id, $data); } trigger_error('event type is invalid. Supported - custom, identify or shopify'); @@ -265,11 +268,36 @@ private function sendIdentifyTrack($email, $event_name, $anonymousId, $userId, $ * @param $event_name string event name * @param $data array event data */ - private function sendNonIdentifyTrack($email, $event_name, $data) { + private function sendNonIdentifyTrack($email, $event_name, $anonymousId, $userId, $data) { + $paramPresent = 0; + if (isset($email)) { + $this->_email = $this->validateEmail($email); + $paramPresent += 1; + } else { + $this->_email = NULL; + } + $minRequiredParam = 1; // anonymous id / user id / email + if (isset($anonymousId)) { + $paramPresent += 1; + } + if (isset($userId)) { + $paramPresent += 1; + } + + if ($paramPresent < $minRequiredParam) { + trigger_error('at least one of: anonymous id, user id, email needs to be set and be a valid string for identify event'); + return new CS_REST_Wrapper_Result(null, 400); + } + + $this->_anonymous_id = $anonymousId; + $this->_user_id = $userId; + $payload = array( 'ContactID' => array( - 'Email' => $email + 'Email' => $this->_email, + 'AnonymousID' => $this->_anonymous_id, + 'UserID' => $this->_user_id ), 'EventName' => $event_name, 'Data' => $data From 67b55dbf96be57189d15cb457eddcc2609b599ea Mon Sep 17 00:00:00 2001 From: ishworg Date: Mon, 28 Aug 2017 16:04:14 +1000 Subject: [PATCH 8/8] fix wording --- csrest_events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csrest_events.php b/csrest_events.php index 4046cc9..4eb8f95 100644 --- a/csrest_events.php +++ b/csrest_events.php @@ -194,7 +194,7 @@ function track($email, $event_name, $anonymous_id = NULL, $user_id = NULL, $data return new CS_REST_Wrapper_Result(null, 400); } if (strlen($event_name) > 1000) { - trigger_error('$event_name needs to be shorter, max length is 1000 character'); + trigger_error('$event_name needs to be shorter, max length is 1000 bytes'); return new CS_REST_Wrapper_Result(null, 400); } if (isset($data)) {