| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Controllers\Ajax; |
| 4 |
|
| 5 |
use Depicter\Utility\Sanitize; |
| 6 |
use WPEmerge\Requests\RequestInterface; |
| 7 |
|
| 8 |
class AnalyticsAjaxController |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Track an analytics event |
| 12 |
* |
| 13 |
* @param RequestInterface $request |
| 14 |
* @param $view |
| 15 |
* |
| 16 |
* @return mixed |
| 17 |
*/ |
| 18 |
public function track(RequestInterface $request, $view) |
| 19 |
{ |
| 20 |
$sourceId = Sanitize::int($request->body('sourceId', 0)); |
| 21 |
$eventType = Sanitize::textfield($request->body('eventType', '')); |
| 22 |
$metaData = $request->body('metaData', []); |
| 23 |
$csrfToken = Sanitize::textfield($request->body('_csrfToken', '')); |
| 24 |
|
| 25 |
if (empty($csrfToken) || !wp_verify_nonce($csrfToken, 'depicter-csrf-lead-' . $sourceId)) { |
| 26 |
return \Depicter::json([ |
| 27 |
'errors' => [__('Invalid or missing CSRF token. The request could not be completed.', 'depicter')] |
| 28 |
])->withStatus(400); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
if (empty($sourceId) || empty($eventType)) { |
| 33 |
return \Depicter::json([ |
| 34 |
'errors' => [__('Source ID and event type are required.', 'depicter')] |
| 35 |
])->withStatus(400); |
| 36 |
} |
| 37 |
|
| 38 |
// Sanitize meta data if provided |
| 39 |
if (!empty($metaData) && is_array($metaData)) { |
| 40 |
$sanitizedMeta = []; |
| 41 |
foreach ($metaData as $key => $value) { |
| 42 |
$sanitizedMeta[Sanitize::textfield($key)] = Sanitize::textfield($value); |
| 43 |
} |
| 44 |
$metaData = $sanitizedMeta; |
| 45 |
} else { |
| 46 |
$metaData = []; |
| 47 |
} |
| 48 |
|
| 49 |
try { |
| 50 |
$result = \Depicter::analytics()->track($sourceId, $eventType, $metaData); |
| 51 |
|
| 52 |
if ($result['success']) { |
| 53 |
return \Depicter::json($result)->withStatus(200); |
| 54 |
} |
| 55 |
|
| 56 |
return \Depicter::json($result)->withStatus(400); |
| 57 |
|
| 58 |
} catch (\Exception $e) { |
| 59 |
return \Depicter::json(['errors' => [$e->getMessage()]])->withStatus(400); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get analytics for a document |
| 65 |
* |
| 66 |
* @param RequestInterface $request |
| 67 |
* @param $view |
| 68 |
* |
| 69 |
* @return mixed |
| 70 |
*/ |
| 71 |
public function get(RequestInterface $request, $view) |
| 72 |
{ |
| 73 |
$sourceId = Sanitize::int($request->query('sourceId', 0)); |
| 74 |
|
| 75 |
if (empty($sourceId)) { |
| 76 |
return \Depicter::json([ |
| 77 |
'errors' => [__('Source ID is required.', 'depicter')] |
| 78 |
])->withStatus(400); |
| 79 |
} |
| 80 |
|
| 81 |
$args = [ |
| 82 |
'event_type' => Sanitize::textfield($request->query('eventType', '')), |
| 83 |
'dateStart' => Sanitize::textfield($request->query('dateStart', '')), |
| 84 |
'dateEnd' => Sanitize::textfield($request->query('dateEnd', '')), |
| 85 |
'order' => Sanitize::textfield($request->query('order', 'DESC')), |
| 86 |
'orderBy' => Sanitize::textfield($request->query('orderBy', 'created_at')), |
| 87 |
'page' => Sanitize::int($request->query('page', 1)), |
| 88 |
'perPage' => Sanitize::int($request->query('perpage', 10)) |
| 89 |
]; |
| 90 |
|
| 91 |
try { |
| 92 |
$response = \Depicter::analytics()->get($sourceId, $args); |
| 93 |
return \Depicter::json($response); |
| 94 |
|
| 95 |
} catch (\Exception $e) { |
| 96 |
return \Depicter::json(['errors' => [$e->getMessage()]])->withStatus(400); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get analytics count for a document |
| 102 |
* |
| 103 |
* @param RequestInterface $request |
| 104 |
* @param $view |
| 105 |
* |
| 106 |
* @return mixed |
| 107 |
*/ |
| 108 |
public function stats(RequestInterface $request, $view) |
| 109 |
{ |
| 110 |
$sourceId = Sanitize::int($request->query('sourceId', 0)); |
| 111 |
|
| 112 |
if (empty($sourceId)) { |
| 113 |
return \Depicter::json([ |
| 114 |
'errors' => [__('Source ID is required.', 'depicter')] |
| 115 |
])->withStatus(400); |
| 116 |
} |
| 117 |
|
| 118 |
$args = [ |
| 119 |
'event_type' => Sanitize::textfield($request->query('eventType', '')), |
| 120 |
'dateStart' => Sanitize::textfield($request->query('dateStart', '')), |
| 121 |
'dateEnd' => Sanitize::textfield($request->query('dateEnd', '')) |
| 122 |
]; |
| 123 |
|
| 124 |
try { |
| 125 |
$response = \Depicter::analytics()->getCount($sourceId, $args); |
| 126 |
return \Depicter::json(['count' => $response]); |
| 127 |
|
| 128 |
} catch (\Exception $e) { |
| 129 |
return \Depicter::json(['errors' => [$e->getMessage()]])->withStatus(400); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Delete analytics records |
| 135 |
* |
| 136 |
* @param RequestInterface $request |
| 137 |
* @param $view |
| 138 |
* |
| 139 |
* @return mixed |
| 140 |
*/ |
| 141 |
public function delete(RequestInterface $request, $view) |
| 142 |
{ |
| 143 |
$id = Sanitize::int($request->body('ID', 0)); |
| 144 |
|
| 145 |
if (empty($id)) { |
| 146 |
return \Depicter::json([ |
| 147 |
'errors' => [__('Analytics ID is required.', 'depicter')] |
| 148 |
])->withStatus(400); |
| 149 |
} |
| 150 |
|
| 151 |
try { |
| 152 |
if (\Depicter::documentAnalyticsRepository()->delete($id)) { |
| 153 |
return \Depicter::json(['success' => true])->withStatus(200); |
| 154 |
} |
| 155 |
} catch (\Exception $e) { |
| 156 |
return \Depicter::json(['errors' => [$e->getMessage()]])->withStatus(400); |
| 157 |
} |
| 158 |
|
| 159 |
return \Depicter::json(['errors' => [__('Error while deleting analytics.', 'depicter')]])->withStatus(400); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get conversion rate between two event types |
| 164 |
* |
| 165 |
* @param RequestInterface $request |
| 166 |
* @param $view |
| 167 |
* |
| 168 |
* @return mixed |
| 169 |
*/ |
| 170 |
public function getConversionRate(RequestInterface $request, $view) |
| 171 |
{ |
| 172 |
$sourceId = Sanitize::int($request->query('sourceId', 0)); |
| 173 |
$fromEventType = Sanitize::textfield($request->query('fromEventType', '')); |
| 174 |
$toEventType = Sanitize::textfield($request->query('toEventType', '')); |
| 175 |
|
| 176 |
if (empty($sourceId) || empty($fromEventType) || empty($toEventType)) { |
| 177 |
return \Depicter::json([ |
| 178 |
'errors' => [__('Source ID, from event type, and to event type are required.', 'depicter')] |
| 179 |
])->withStatus(400); |
| 180 |
} |
| 181 |
|
| 182 |
$args = [ |
| 183 |
'dateStart' => Sanitize::textfield($request->query('dateStart', '')), |
| 184 |
'dateEnd' => Sanitize::textfield($request->query('dateEnd', '')), |
| 185 |
'uniqueSessions' => $request->query('uniqueSessions', true) |
| 186 |
]; |
| 187 |
|
| 188 |
try { |
| 189 |
$response = \Depicter::analytics()->getConversionRate($sourceId, $fromEventType, $toEventType, $args); |
| 190 |
|
| 191 |
if ($response['success']) { |
| 192 |
return \Depicter::json($response)->withStatus(200); |
| 193 |
} |
| 194 |
|
| 195 |
return \Depicter::json($response)->withStatus(400); |
| 196 |
|
| 197 |
} catch (\Exception $e) { |
| 198 |
return \Depicter::json(['errors' => [$e->getMessage()]])->withStatus(400); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|