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