PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / AnalyticsService.php

AnalyticsService.php in Depicter — Popup & Slider Builder trunk, at app/src/Services/AnalyticsService.php

227 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Depicter\Services;
4
5 use Averta\Core\Utility\Arr;
6 use WPEmerge\Requests\RequestInterface;
7
8 class AnalyticsService
9 {
10 /**
11 * Track an analytics event
12 *
13 * @param int $sourceId Document ID
14 * @param string $eventType Event type (e.g., 'view', 'click', 'impression')
15 * @param string $sessionID Session ID
16 * @param array $metaData Optional meta data (e.g., ['browser_width' => '1920', 'referrer' => 'https://...'])
17 *
18 * @return array
19 */
20 public function track($sourceId, $eventType, $metaData = [])
21 {
22 try {
23
24 $analyticsId = \Depicter::documentAnalyticsRepository()->create( $sourceId, $eventType );
25
26 if (!$analyticsId) {
27 return [
28 'success' => false,
29 'errors' => [__('Error while tracking analytics', 'depicter')]
30 ];
31 }
32
33 // Store meta data if provided
34 if (!empty($metaData) && is_array($metaData)) {
35 foreach ($metaData as $key => $value) {
36 \Depicter::metaRepository()->update($analyticsId, $key, $value, 'document_analytics');
37 }
38 }
39
40 // Fire action hook for extensibility
41 do_action('depicter/analytics/tracked', $analyticsId, $sourceId, $eventType, $metaData);
42
43 return ['success' => true, 'id' => $analyticsId];
44
45 } catch (\Exception $e) {
46 return [
47 'success' => false,
48 'errors' => [$e->getMessage()]
49 ];
50 }
51 }
52
53 /**
54 * Get analytics for a document
55 *
56 * @param int $sourceId
57 * @param array $args
58 *
59 * @return array
60 */
61 public function get($sourceId, $args = [])
62 {
63 try {
64 $analytics = \Depicter::documentAnalyticsRepository()->getByDocumentId($sourceId, $args);
65
66 return [
67 'success' => true,
68 'analytics' => $analytics,
69 'total' => count($analytics)
70 ];
71
72 } catch (\Exception $e) {
73 return [
74 'success' => false,
75 'errors' => [$e->getMessage()]
76 ];
77 }
78 }
79
80 /**
81 * Get analytics count
82 *
83 * @param int $sourceId
84 * @param array $args
85 *
86 * @return int
87 */
88 public function getCount($sourceId, $args = [])
89 {
90 try {
91 $eventType = $args['event_type'] ?? '';
92 return \Depicter::documentAnalyticsRepository()->getCount($sourceId, $eventType, $args);
93
94 } catch (\Exception $e) {
95 return 0;
96 }
97 }
98
99 /**
100 * Get analytics statistics for a document
101 *
102 * @param int $sourceId
103 * @param array $args
104 *
105 * @return array
106 */
107 public function getStats($sourceId, $args = [])
108 {
109 try {
110 $analytics = \Depicter::documentAnalyticsRepository()->getByDocumentId($sourceId, $args);
111
112 // Group by event type
113 $stats = [];
114 foreach ($analytics as $analytic) {
115 $eventType = $analytic['event_type'];
116 if (!isset($stats[$eventType])) {
117 $stats[$eventType] = 0;
118 }
119 $stats[$eventType]++;
120 }
121
122 return [
123 'success' => true,
124 'stats' => $stats,
125 'total' => count($analytics)
126 ];
127
128 } catch (\Exception $e) {
129 return [
130 'success' => false,
131 'errors' => [$e->getMessage()]
132 ];
133 }
134 }
135
136 /**
137 * Calculate conversion rate between two event types
138 *
139 * @param int $sourceId Document ID
140 * @param string $fromEventType Starting event type (e.g., 'view', 'impression')
141 * @param string $toEventType Conversion event type (e.g., 'submission', 'click')
142 * @param array $args Optional arguments (dateStart, dateEnd, uniqueSessions)
143 *
144 * @return array
145 */
146 public function getConversionRate($sourceId, $fromEventType, $toEventType, $args = [])
147 {
148 try {
149 $uniqueSessions = $args['uniqueSessions'] ?? true;
150 $dateArgs = [];
151
152 if (!empty($args['dateStart'])) {
153 $dateArgs['dateStart'] = $args['dateStart'];
154 }
155 if (!empty($args['dateEnd'])) {
156 $dateArgs['dateEnd'] = $args['dateEnd'];
157 }
158
159 // Get count of starting events
160 $fromCount = \Depicter::documentAnalyticsRepository()->getCount(
161 $sourceId,
162 $fromEventType,
163 $dateArgs
164 );
165
166 // Get count of conversion events
167 $toCount = \Depicter::documentAnalyticsRepository()->getCount(
168 $sourceId,
169 $toEventType,
170 $dateArgs
171 );
172
173 // If counting unique sessions, get actual analytics to count unique sessions
174 if ($uniqueSessions) {
175 $fromAnalytics = \Depicter::documentAnalyticsRepository()->getByDocumentId(
176 $sourceId,
177 array_merge($dateArgs, ['event_type' => $fromEventType])
178 );
179
180 $toAnalytics = \Depicter::documentAnalyticsRepository()->getByDocumentId(
181 $sourceId,
182 array_merge($dateArgs, ['event_type' => $toEventType])
183 );
184
185 // Count unique sessions
186 $fromSessions = [];
187 foreach ($fromAnalytics as $event) {
188 if (!empty($event['session_id'])) {
189 $fromSessions[$event['session_id']] = true;
190 }
191 }
192 $fromCount = count($fromSessions);
193
194 $toSessions = [];
195 foreach ($toAnalytics as $event) {
196 if (!empty($event['session_id'])) {
197 $toSessions[$event['session_id']] = true;
198 }
199 }
200 $toCount = count($toSessions);
201 }
202
203 // Calculate conversion rate
204 $conversionRate = 0;
205 if ($fromCount > 0) {
206 $conversionRate = ($toCount / $fromCount) * 100;
207 }
208
209 return [
210 'success' => true,
211 'from_event' => $fromEventType,
212 'to_event' => $toEventType,
213 'from_count' => $fromCount,
214 'to_count' => $toCount,
215 'conversion_rate' => round($conversionRate, 2),
216 'unique_sessions' => $uniqueSessions
217 ];
218
219 } catch (\Exception $e) {
220 return [
221 'success' => false,
222 'errors' => [$e->getMessage()]
223 ];
224 }
225 }
226 }
227