csrest_campaigns.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. require_once dirname(__FILE__).'/class/base_classes.php';
  3. /**
  4. * Class to access a campaigns resources from the create send API.
  5. * This class includes functions to create and send campaigns,
  6. * along with accessing lists of campaign specific resources i.e reporting statistics
  7. * @author tobyb
  8. *
  9. */
  10. class CS_REST_Campaigns extends CS_REST_Wrapper_Base {
  11. /**
  12. * The base route of the campaigns resource.
  13. * @var string
  14. * @access private
  15. */
  16. var $_campaigns_base_route;
  17. /**
  18. * Constructor.
  19. * @param $campaign_id string The campaign id to access (Ignored for create requests)
  20. * @param $auth_details array Authentication details to use for API calls.
  21. * This array must take one of the following forms:
  22. * If using OAuth to authenticate:
  23. * array(
  24. * 'access_token' => 'your access token',
  25. * 'refresh_token' => 'your refresh token')
  26. *
  27. * Or if using an API key:
  28. * array('api_key' => 'your api key')
  29. * @param $protocol string The protocol to use for requests (http|https)
  30. * @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
  31. * @param $host string The host to send API requests to. There is no need to change this
  32. * @param $log CS_REST_Log The logger to use. Used for dependency injection
  33. * @param $serialiser The serialiser to use. Used for dependency injection
  34. * @param $transport The transport to use. Used for dependency injection
  35. * @access public
  36. */
  37. function __construct (
  38. $campaign_id,
  39. $auth_details,
  40. $protocol = 'https',
  41. $debug_level = CS_REST_LOG_NONE,
  42. $host = 'api.createsend.com',
  43. $log = NULL,
  44. $serialiser = NULL,
  45. $transport = NULL) {
  46. parent::__construct($auth_details, $protocol, $debug_level, $host, $log, $serialiser, $transport);
  47. $this->set_campaign_id($campaign_id);
  48. }
  49. /**
  50. * Change the campaign id used for calls after construction
  51. * @param $campaign_id
  52. * @access public
  53. */
  54. function set_campaign_id($campaign_id) {
  55. $this->_campaigns_base_route = $this->_base_route.'campaigns/'.$campaign_id.'/';
  56. }
  57. /**
  58. * Creates a new campaign based on the provided campaign info.
  59. * At least on of the ListIDs and Segments parameters must be provided
  60. * @param string $client_id The client to create the campaign for
  61. * @param array $campaign_info The campaign information to use during creation.
  62. * This array should be of the form
  63. * array(
  64. * 'Subject' => string required The campaign subject
  65. * 'Name' => string required The campaign name
  66. * 'FromName' => string required The From name for the campaign
  67. * 'FromEmail' => string required The From address for the campaign
  68. * 'ReplyTo' => string required The Reply-To address for the campaign
  69. * 'HtmlUrl' => string required A url to download the campaign HTML from
  70. * 'TextUrl' => string optional A url to download the campaign
  71. * text version from. If not provided, text content will be
  72. * automatically generated from HTML content.
  73. * 'ListIDs' => array<string> optional An array of list ids to send the campaign to
  74. * 'SegmentIDs' => array<string> optional An array of segment ids to send the campaign to.
  75. * )
  76. * @access public
  77. * @return CS_REST_Wrapper_Result A successful response will be the ID of the newly created campaign
  78. */
  79. function create($client_id, $campaign_info) {
  80. return $this->post_request($this->_base_route.'campaigns/'.$client_id.'.json', $campaign_info);
  81. }
  82. /**
  83. * Creates a new campaign from a template based on the info provided.
  84. * At least on of the ListIDs and Segments parameters must be provided
  85. * @param string $client_id The client to create the campaign for
  86. * @param array $campaign_info The campaign information to use during creation.
  87. * This array should be of the form
  88. * array(
  89. * 'Subject' => string required The campaign subject
  90. * 'Name' => string required The campaign name
  91. * 'FromName' => string required The From name for the campaign
  92. * 'FromEmail' => string required The From address for the campaign
  93. * 'ReplyTo' => string required The Reply-To address for the campaign
  94. * 'ListIDs' => array<string> optional An array of list ids to send the campaign to
  95. * 'SegmentIDs' => array<string> optional An array of segment ids to send the campaign to
  96. * 'TemplateID' => string required The ID of the template to use
  97. * 'TemplateContent' => array required The content which will be used to fill the editable areas of the template
  98. * )
  99. * @access public
  100. * @return CS_REST_Wrapper_Result A successful response will be the ID of the newly created campaign
  101. */
  102. function create_from_template($client_id, $campaign_info) {
  103. return $this->post_request($this->_base_route.'campaigns/'.$client_id.'/fromtemplate.json', $campaign_info);
  104. }
  105. /**
  106. * Sends a preview of an existing campaign to the specified recipients.
  107. * @param array<string> $recipients The recipients to send the preview to.
  108. * @param string $personalize How to personalize the campaign content. Valid options are:
  109. * 'Random': Choose a random campaign recipient and use their personalisation data
  110. * 'Fallback': Use the fallback terms specified in the campaign content
  111. * @access public
  112. * @return CS_REST_Wrapper_Result A successful response will be empty
  113. */
  114. function send_preview($recipients, $personalize = 'Random') {
  115. $preview_data = array(
  116. 'PreviewRecipients' => $recipients,
  117. 'Personalize' => $personalize
  118. );
  119. return $this->post_request($this->_campaigns_base_route.'sendpreview.json', $preview_data);
  120. }
  121. /**
  122. * Sends an existing campaign based on the scheduling information provided
  123. * @param array $schedule The campaign scheduling information.
  124. * This array should be of the form
  125. * array (
  126. * 'ConfirmationEmail' => string required The email address to send a confirmation email to,
  127. * 'SendDate' => string required The date to send the campaign or 'immediately'.
  128. * The date should be in the format 'y-M-d'
  129. * )
  130. * @access public
  131. * @return CS_REST_Wrapper_Result A successful response will be empty
  132. */
  133. function send($schedule) {
  134. return $this->post_request($this->_campaigns_base_route.'send.json', $schedule);
  135. }
  136. /**
  137. * Unschedules the campaign, moving it back into the drafts. If the campaign has been sent or is
  138. * in the process of sending, this api request will fail.
  139. * @access public
  140. * @return CS_REST_Wrapper_Result A successful response will be empty
  141. */
  142. function unschedule() {
  143. return $this->post_request($this->_campaigns_base_route.'unschedule.json', NULL);
  144. }
  145. /**
  146. * Deletes an existing campaign from the system
  147. * @access public
  148. * @return CS_REST_Wrapper_Result A successful response will be empty
  149. */
  150. function delete() {
  151. return $this->delete_request(trim($this->_campaigns_base_route, '/').'.json');
  152. }
  153. /**
  154. * Gets all email addresses on the current clients suppression list
  155. * @param int $page_number The page number to get
  156. * @param int $page_size The number of records per page
  157. * @param string $order_field The field to order the record set by ('EMAIL', 'LIST')
  158. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  159. * @access public
  160. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  161. * {
  162. * 'ResultsOrderedBy' => The field the results are ordered by
  163. * 'OrderDirection' => The order direction
  164. * 'PageNumber' => The page number for the result set
  165. * 'PageSize' => The page size used
  166. * 'RecordsOnThisPage' => The number of records returned
  167. * 'TotalNumberOfRecords' => The total number of records available
  168. * 'NumberOfPages' => The total number of pages for this collection
  169. * 'Results' => array(
  170. * {
  171. * 'EmailAddress' => The suppressed email address
  172. * 'ListID' => The ID of the list this subscriber comes from
  173. * }
  174. * )
  175. * }
  176. */
  177. function get_recipients($page_number = NULL, $page_size = NULL, $order_field = NULL,
  178. $order_direction = NULL) {
  179. return $this->get_request_paged($this->_campaigns_base_route.'recipients.json', $page_number,
  180. $page_size, $order_field, $order_direction, '?');
  181. }
  182. /**
  183. * Gets all bounces recorded for a campaign
  184. * @param string $since The date to start getting bounces from
  185. * @param int $page_number The page number to get
  186. * @param int $page_size The number of records per page
  187. * @param string $order_field The field to order the record set by ('EMAIL', 'LIST', 'DATE')
  188. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  189. * @access public
  190. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  191. * {
  192. * 'ResultsOrderedBy' => The field the results are ordered by
  193. * 'OrderDirection' => The order direction
  194. * 'PageNumber' => The page number for the result set
  195. * 'PageSize' => The page size used
  196. * 'RecordsOnThisPage' => The number of records returned
  197. * 'TotalNumberOfRecords' => The total number of records available
  198. * 'NumberOfPages' => The total number of pages for this collection
  199. * 'Results' => array(
  200. * {
  201. * 'EmailAddress' => The email that bounced
  202. * 'ListID' => The ID of the list the subscriber was on
  203. * 'BounceType' => The type of bounce
  204. * 'Date' => The date the bounce message was received
  205. * 'Reason' => The reason for the bounce
  206. * }
  207. * )
  208. * }
  209. * )
  210. */
  211. function get_bounces($since = '', $page_number = NULL, $page_size = NULL, $order_field = NULL,
  212. $order_direction = NULL) {
  213. return $this->get_request_paged($this->_campaigns_base_route.'bounces.json?date='.urlencode($since),
  214. $page_number, $page_size, $order_field, $order_direction);
  215. }
  216. /**
  217. * Gets the lists a campaign was sent to
  218. * @access public
  219. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  220. * {
  221. * 'Lists' => array(
  222. * {
  223. * 'ListID' => The list id
  224. * 'Name' => The list name
  225. * }
  226. * ),
  227. * 'Segments' => array(
  228. * {
  229. * 'ListID' => The list id of the segment
  230. * 'SegmentID' => The id of the segment
  231. * 'Title' => The title of the segment
  232. * }
  233. * )
  234. * }
  235. */
  236. function get_lists_and_segments() {
  237. return $this->get_request($this->_campaigns_base_route.'listsandsegments.json');
  238. }
  239. /**
  240. * Gets a summary of all campaign reporting statistics
  241. * @access public
  242. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  243. * {
  244. * 'Recipients' => The total recipients of the campaign
  245. * 'TotalOpened' => The total number of opens recorded
  246. * 'Clicks' => The total number of recorded clicks
  247. * 'Unsubscribed' => The number of recipients who unsubscribed
  248. * 'Bounced' => The number of recipients who bounced
  249. * 'UniqueOpened' => The number of recipients who opened
  250. * 'WebVersionURL' => The url of the web version of the campaign
  251. * 'WebVersionTextURL' => The url of the web version of the text version of the campaign
  252. * 'WorldviewURL' => The public Worldview URL for the campaign
  253. * 'Forwards' => The number of times the campaign has been forwarded to a friend
  254. * 'Likes' => The number of times the campaign has been 'liked' on Facebook
  255. * 'Mentions' => The number of times the campaign has been tweeted about
  256. * 'SpamComplaints' => The number of recipients who marked the campaign as spam
  257. * }
  258. */
  259. function get_summary() {
  260. return $this->get_request($this->_campaigns_base_route.'summary.json');
  261. }
  262. /**
  263. * Gets the email clients that subscribers used to open the campaign
  264. * @access public
  265. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  266. * array(
  267. * {
  268. * Client => The email client name
  269. * Version => The email client version
  270. * Percentage => The percentage of subscribers who used this email client
  271. * Subscribers => The actual number of subscribers who used this email client
  272. * }
  273. * )
  274. */
  275. function get_email_client_usage() {
  276. return $this->get_request($this->_campaigns_base_route.'emailclientusage.json');
  277. }
  278. /**
  279. * Gets all opens recorded for a campaign since the provided date
  280. * @param string $since The date to start getting opens from
  281. * @param int $page_number The page number to get
  282. * @param int $page_size The number of records per page
  283. * @param string $order_field The field to order the record set by ('EMAIL', 'LIST', 'DATE')
  284. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  285. * @access public
  286. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  287. * {
  288. * 'ResultsOrderedBy' => The field the results are ordered by
  289. * 'OrderDirection' => The order direction
  290. * 'PageNumber' => The page number for the result set
  291. * 'PageSize' => The page size used
  292. * 'RecordsOnThisPage' => The number of records returned
  293. * 'TotalNumberOfRecords' => The total number of records available
  294. * 'NumberOfPages' => The total number of pages for this collection
  295. * 'Results' => array(
  296. * {
  297. * 'EmailAddress' => The email address of the subscriber who opened
  298. * 'ListID' => The list id of the list containing the subscriber
  299. * 'Date' => The date of the open
  300. * 'IPAddress' => The ip address where the open originated
  301. * 'Latitude' => The geocoded latitude from the IP address
  302. * 'Longitude' => The geocoded longitude from the IP address
  303. * 'City' => The geocoded city from the IP address
  304. * 'Region' => The geocoded region from the IP address
  305. * 'CountryCode' => The geocoded two letter country code from the IP address
  306. * 'CountryName' => The geocoded full country name from the IP address
  307. * }
  308. * )
  309. * }
  310. */
  311. function get_opens($since = '', $page_number = NULL, $page_size = NULL, $order_field = NULL,
  312. $order_direction = NULL) {
  313. return $this->get_request_paged($this->_campaigns_base_route.'opens.json?date='.urlencode($since),
  314. $page_number, $page_size, $order_field, $order_direction);
  315. }
  316. /**
  317. * Gets all clicks recorded for a campaign since the provided date
  318. * @param string $since The date to start getting clicks from
  319. * @param int $page_number The page number to get
  320. * @param int $page_size The number of records per page
  321. * @param string $order_field The field to order the record set by ('EMAIL', 'LIST', 'DATE')
  322. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  323. * @access public
  324. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  325. * {
  326. * 'ResultsOrderedBy' => The field the results are ordered by
  327. * 'OrderDirection' => The order direction
  328. * 'PageNumber' => The page number for the result set
  329. * 'PageSize' => The page size used
  330. * 'RecordsOnThisPage' => The number of records returned
  331. * 'TotalNumberOfRecords' => The total number of records available
  332. * 'NumberOfPages' => The total number of pages for this collection
  333. * 'Results' => array(
  334. * {
  335. * 'EmailAddress' => The email address of the subscriber who clicked
  336. * 'ListID' => The list id of the list containing the subscriber
  337. * 'Date' => The date of the click
  338. * 'IPAddress' => The ip address where the click originated
  339. * 'URL' => The url that the subscriber clicked on
  340. * 'Latitude' => The geocoded latitude from the IP address
  341. * 'Longitude' => The geocoded longitude from the IP address
  342. * 'City' => The geocoded city from the IP address
  343. * 'Region' => The geocoded region from the IP address
  344. * 'CountryCode' => The geocoded two letter country code from the IP address
  345. * 'CountryName' => The geocoded full country name from the IP address
  346. * }
  347. * )
  348. * }
  349. */
  350. function get_clicks($since = '', $page_number = NULL, $page_size = NULL, $order_field = NULL,
  351. $order_direction = NULL) {
  352. return $this->get_request_paged($this->_campaigns_base_route.'clicks.json?date='.urlencode($since),
  353. $page_number, $page_size, $order_field, $order_direction);
  354. }
  355. /**
  356. * Gets all unsubscribes recorded for a campaign since the provided date
  357. * @param string $since The date to start getting unsubscribes from
  358. * @param int $page_number The page number to get
  359. * @param int $page_size The number of records per page
  360. * @param string $order_field The field to order the record set by ('EMAIL', 'LIST', 'DATE')
  361. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  362. * @access public
  363. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  364. * {
  365. * 'ResultsOrderedBy' => The field the results are ordered by
  366. * 'OrderDirection' => The order direction
  367. * 'PageNumber' => The page number for the result set
  368. * 'PageSize' => The page size used
  369. * 'RecordsOnThisPage' => The number of records returned
  370. * 'TotalNumberOfRecords' => The total number of records available
  371. * 'NumberOfPages' => The total number of pages for this collection
  372. * 'Results' => array(
  373. * {
  374. * 'EmailAddress' => The email address of the subscriber who unsubscribed
  375. * 'ListID' => The list id of the list containing the subscriber
  376. * 'Date' => The date of the unsubscribe
  377. * 'IPAddress' => The ip address where the unsubscribe originated
  378. * }
  379. * )
  380. * }
  381. */
  382. function get_unsubscribes($since = '', $page_number = NULL, $page_size = NULL, $order_field = NULL,
  383. $order_direction = NULL) {
  384. return $this->get_request_paged($this->_campaigns_base_route.'unsubscribes.json?date='.urlencode($since),
  385. $page_number, $page_size, $order_field, $order_direction);
  386. }
  387. /**
  388. * Gets all spam complaints recorded for a campaign since the provided date
  389. * @param string $since The date to start getting spam complaints from
  390. * @param int $page_number The page number to get
  391. * @param int $page_size The number of records per page
  392. * @param string $order_field The field to order the record set by ('EMAIL', 'LIST', 'DATE')
  393. * @param string $order_direction The direction to order the record set ('ASC', 'DESC')
  394. * @access public
  395. * @return CS_REST_Wrapper_Result A successful response will be an object of the form
  396. * {
  397. * 'ResultsOrderedBy' => The field the results are ordered by
  398. * 'OrderDirection' => The order direction
  399. * 'PageNumber' => The page number for the result set
  400. * 'PageSize' => The page size used
  401. * 'RecordsOnThisPage' => The number of records returned
  402. * 'TotalNumberOfRecords' => The total number of records available
  403. * 'NumberOfPages' => The total number of pages for this collection
  404. * 'Results' => array(
  405. * {
  406. * 'EmailAddress' => The email address of the subscriber who unsubscribed
  407. * 'ListID' => The list id of the list containing the subscriber
  408. * 'Date' => The date of the unsubscribe
  409. * }
  410. * )
  411. * }
  412. */
  413. function get_spam($since = '', $page_number = NULL, $page_size = NULL, $order_field = NULL,
  414. $order_direction = NULL) {
  415. return $this->get_request_paged($this->_campaigns_base_route.'spam.json?date='.urlencode($since),
  416. $page_number, $page_size, $order_field, $order_direction);
  417. }
  418. }