1: <?php
2: namespace Ctct\Services;
3:
4: use Ctct\Exceptions\CtctException;
5: use Ctct\Util\Config;
6: use Ctct\Components\Tracking\BounceActivity;
7: use Ctct\Components\Tracking\ClickActivity;
8: use Ctct\Components\Tracking\ForwardActivity;
9: use Ctct\Components\Tracking\OpenActivity;
10: use Ctct\Components\Tracking\UnsubscribeActivity;
11: use Ctct\Components\Tracking\SendActivity;
12: use Ctct\Components\Tracking\TrackingSummary;
13: use Ctct\Components\ResultSet;
14: use GuzzleHttp\Exception\ClientException;
15:
16: 17: 18: 19: 20: 21:
22: class ContactTrackingService extends BaseService
23: {
24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: public function getBounces($accessToken, $contactId, Array $params = array())
37: {
38: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_bounces'), $contactId);
39:
40: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
41: if ($params) {
42: $query = $request->getQuery();
43: foreach ($params as $name => $value) {
44: $query->add($name, $value);
45: }
46: }
47:
48: try {
49: $response = parent::getClient()->send($request);
50: } catch (ClientException $e) {
51: throw parent::convertException($e);
52: }
53:
54: $body = $response->json();
55: $bounces = array();
56: foreach ($body['results'] as $bounceActivity) {
57: $bounces[] = BounceActivity::create($bounceActivity);
58: }
59:
60: return new ResultSet($bounces, $body['meta']);
61: }
62:
63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74:
75: public function getClicks($accessToken, $contactId, Array $params = array())
76: {
77: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_clicks'), $contactId);
78:
79: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
80: if ($params) {
81: $query = $request->getQuery();
82: foreach ($params as $name => $value) {
83: $query->add($name, $value);
84: }
85: }
86:
87: try {
88: $response = parent::getClient()->send($request);
89: } catch (ClientException $e) {
90: throw parent::convertException($e);
91: }
92:
93: $body = $response->json();
94: $clicks = array();
95: foreach ($body['results'] as $click_activity) {
96: $clicks[] = ClickActivity::create($click_activity);
97: }
98:
99: return new ResultSet($clicks, $body['meta']);
100: }
101:
102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function getForwards($accessToken, $contactId, Array $params = array())
115: {
116: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_forwards'), $contactId);
117:
118: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
119: if ($params) {
120: $query = $request->getQuery();
121: foreach ($params as $name => $value) {
122: $query->add($name, $value);
123: }
124: }
125:
126: try {
127: $response = parent::getClient()->send($request);
128: } catch (ClientException $e) {
129: throw parent::convertException($e);
130: }
131:
132: $body = $response->json();
133: $forwards = array();
134: foreach ($body['results'] as $forward_activity) {
135: $forwards[] = ForwardActivity::create($forward_activity);
136: }
137:
138: return new ResultSet($forwards, $body['meta']);
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152:
153: public function getOpens($accessToken, $contactId, Array $params = array())
154: {
155: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_opens'), $contactId);
156:
157: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
158: if ($params) {
159: $query = $request->getQuery();
160: foreach ($params as $name => $value) {
161: $query->add($name, $value);
162: }
163: }
164:
165: try {
166: $response = parent::getClient()->send($request);
167: } catch (ClientException $e) {
168: throw parent::convertException($e);
169: }
170:
171: $body = $response->json();
172: $opens = array();
173: foreach ($body['results'] as $open_activity) {
174: $opens[] = OpenActivity::create($open_activity);
175: }
176:
177: return new ResultSet($opens, $body['meta']);
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191:
192: public function getSends($accessToken, $contactId, Array $params = array())
193: {
194: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_sends'), $contactId);
195:
196: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
197: if ($params) {
198: $query = $request->getQuery();
199: foreach ($params as $name => $value) {
200: $query->add($name, $value);
201: }
202: }
203:
204: try {
205: $response = parent::getClient()->send($request);
206: } catch (ClientException $e) {
207: throw parent::convertException($e);
208: }
209:
210: $body = $response->json();
211: $sends = array();
212: foreach ($body['results'] as $send_activity) {
213: $sends[] = SendActivity::create($send_activity);
214: }
215:
216: return new ResultSet($sends, $body['meta']);
217: }
218:
219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230:
231: public function getUnsubscribes($accessToken, $contactId, Array $params = array())
232: {
233: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_unsubscribes'), $contactId);
234:
235: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
236: if ($params) {
237: $query = $request->getQuery();
238: foreach ($params as $name => $value) {
239: $query->add($name, $value);
240: }
241: }
242:
243: try {
244: $response = parent::getClient()->send($request);
245: } catch (ClientException $e) {
246: throw parent::convertException($e);
247: }
248:
249: $body = $response->json();
250: $opt_outs = array();
251: foreach ($body['results'] as $opt_out_activity) {
252: $opt_outs[] = UnsubscribeActivity::create($opt_out_activity);
253: }
254:
255: return new ResultSet($opt_outs, $body['meta']);
256: }
257:
258: 259: 260: 261: 262: 263: 264:
265: public function getSummary($accessToken, $contactId)
266: {
267: $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact_tracking_summary'), $contactId);
268:
269: $request = parent::createBaseRequest($accessToken, 'GET', $baseUrl);
270:
271: try {
272: $response = parent::getClient()->send($request);
273: } catch (ClientException $e) {
274: throw parent::convertException($e);
275: }
276:
277: return TrackingSummary::create($response->json());
278: }
279: }
280: