Analytics.php
9 months ago
Reporter.php
2 months ago
ReporterCampaignData.php
2 weeks ago
UnsubscribeReporter.php
3 years ago
index.php
3 years ago
ReporterCampaignData.php
483 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Analytics; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Newsletter\Sending\NewsletterReplayMetadata; |
| 10 | use MailPoet\Newsletter\Sending\SendingQueuesRepository; |
| 11 | use MailPoet\UnexpectedValueException; |
| 12 | |
| 13 | class ReporterCampaignData { |
| 14 | |
| 15 | |
| 16 | const STANDARD_7_DAYS = 'Number of standard newsletters sent in last 7 days'; |
| 17 | const STANDARD_30_DAYS = 'Number of standard newsletters sent in last 30 days'; |
| 18 | const STANDARD_3_MONTHS = 'Number of standard newsletters sent in last 3 months'; |
| 19 | const STANDARD_SEGMENT_7_DAYS = 'Number of standard newsletters sent to segment in last 7 days'; |
| 20 | const STANDARD_SEGMENT_30_DAYS = 'Number of standard newsletters sent to segment in last 30 days'; |
| 21 | const STANDARD_SEGMENT_3_MONTHS = 'Number of standard newsletters sent to segment in last 3 months'; |
| 22 | const STANDARD_FILTERED_SEGMENT_7_DAYS = 'Number of standard newsletters filtered by segment in last 7 days'; |
| 23 | const STANDARD_FILTERED_SEGMENT_30_DAYS = 'Number of standard newsletters filtered by segment in last 30 days'; |
| 24 | const STANDARD_FILTERED_SEGMENT_3_MONTHS = 'Number of standard newsletters filtered by segment in last 3 months'; |
| 25 | const AUTOMATION_7_DAYS = 'Number of automations campaigns sent in the last 7 days'; |
| 26 | const AUTOMATION_30_DAYS = 'Number of automations campaigns sent in the last 30 days'; |
| 27 | const AUTOMATION_3_MONTHS = 'Number of automations campaigns sent in the last 3 months'; |
| 28 | const RE_ENGAGEMENT_7_DAYS = 'Number of re-engagement campaigns sent in the last 7 days'; |
| 29 | const RE_ENGAGEMENT_30_DAYS = 'Number of re-engagement campaigns sent in the last 30 days'; |
| 30 | const RE_ENGAGEMENT_3_MONTHS = 'Number of re-engagement campaigns sent in the last 3 months'; |
| 31 | const RE_ENGAGEMENT_SEGMENT_7_DAYS = 'Number of re-engagement campaigns sent to segment in the last 7 days'; |
| 32 | const RE_ENGAGEMENT_SEGMENT_30_DAYS = 'Number of re-engagement campaigns sent to segment in the last 30 days'; |
| 33 | const RE_ENGAGEMENT_SEGMENT_3_MONTHS = 'Number of re-engagement campaigns sent to segment in the last 3 months'; |
| 34 | const RE_ENGAGEMENT_FILTERED_SEGMENT_7_DAYS = 'Number of re-engagement campaigns filtered by segment in the last 7 days'; |
| 35 | const RE_ENGAGEMENT_FILTERED_SEGMENT_30_DAYS = 'Number of re-engagement campaigns filtered by segment in the last 30 days'; |
| 36 | const RE_ENGAGEMENT_FILTERED_SEGMENT_3_MONTHS = 'Number of re-engagement campaigns filtered by segment in the last 3 months'; |
| 37 | const POST_NOTIFICATION_7_DAYS = 'Number of post notification campaigns sent in the last 7 days'; |
| 38 | const POST_NOTIFICATION_30_DAYS = 'Number of post notification campaigns sent in the last 30 days'; |
| 39 | const POST_NOTIFICATION_3_MONTHS = 'Number of post notification campaigns sent in the last 3 months'; |
| 40 | const POST_NOTIFICATION_SEGMENT_7_DAYS = 'Number of post notification campaigns sent to segment in the last 7 days'; |
| 41 | const POST_NOTIFICATION_SEGMENT_30_DAYS = 'Number of post notification campaigns sent to segment in the last 30 days'; |
| 42 | const POST_NOTIFICATION_SEGMENT_3_MONTHS = 'Number of post notification campaigns sent to segment in the last 3 months'; |
| 43 | const POST_NOTIFICATION_FILTERED_SEGMENT_7_DAYS = 'Number of post notification campaigns filtered by segment in the last 7 days'; |
| 44 | const POST_NOTIFICATION_FILTERED_SEGMENT_30_DAYS = 'Number of post notification campaigns filtered by segment in the last 30 days'; |
| 45 | const POST_NOTIFICATION_FILTERED_SEGMENT_3_MONTHS = 'Number of post notification campaigns filtered by segment in the last 3 months'; |
| 46 | |
| 47 | const LEGACY_WELCOME_7_DAYS = 'Number of legacy welcome email campaigns sent in the last 7 days'; |
| 48 | const LEGACY_WELCOME_30_DAYS = 'Number of legacy welcome email campaigns sent in the last 30 days'; |
| 49 | const LEGACY_WELCOME_3_MONTHS = 'Number of legacy welcome email campaigns sent in the last 3 months'; |
| 50 | |
| 51 | const LEGACY_ABANDONED_CART_7_DAYS = 'Number of legacy abandoned cart campaigns sent in the last 7 days'; |
| 52 | const LEGACY_ABANDONED_CART_30_DAYS = 'Number of legacy abandoned cart campaigns sent in the last 30 days'; |
| 53 | const LEGACY_ABANDONED_CART_3_MONTHS = 'Number of legacy abandoned cart campaigns sent in the last 3 months'; |
| 54 | |
| 55 | const LEGACY_FIRST_PURCHASE_7_DAYS = 'Number of legacy first purchase campaigns sent in the last 7 days'; |
| 56 | const LEGACY_FIRST_PURCHASE_30_DAYS = 'Number of legacy first purchase campaigns sent in the last 30 days'; |
| 57 | const LEGACY_FIRST_PURCHASE_3_MONTHS = 'Number of legacy first purchase campaigns sent in the last 3 months'; |
| 58 | |
| 59 | const LEGACY_PURCHASED_IN_CATEGORY_7_DAYS = 'Number of legacy purchased in category campaigns sent in the last 7 days'; |
| 60 | const LEGACY_PURCHASED_IN_CATEGORY_30_DAYS = 'Number of legacy purchased in category campaigns sent in the last 30 days'; |
| 61 | const LEGACY_PURCHASED_IN_CATEGORY_3_MONTHS = 'Number of legacy purchased in category campaigns sent in the last 3 months'; |
| 62 | |
| 63 | const LEGACY_PURCHASED_PRODUCT_7_DAYS = 'Number of legacy purchased product campaigns sent in the last 7 days'; |
| 64 | const LEGACY_PURCHASED_PRODUCT_30_DAYS = 'Number of legacy purchased product campaigns sent in the last 30 days'; |
| 65 | const LEGACY_PURCHASED_PRODUCT_3_MONTHS = 'Number of legacy purchased product campaigns sent in the last 3 months'; |
| 66 | |
| 67 | const TOTAL_CAMPAIGNS_7_DAYS = 'Number of campaigns sent in the last 7 days'; |
| 68 | const TOTAL_CAMPAIGNS_30_DAYS = 'Number of campaigns sent in the last 30 days'; |
| 69 | const TOTAL_CAMPAIGNS_3_MONTHS = 'Number of campaigns sent in the last 3 months'; |
| 70 | |
| 71 | |
| 72 | const TOTAL_CAMPAIGNS_SEGMENT_7_DAYS = 'Number of campaigns sent to segment in the last 7 days'; |
| 73 | const TOTAL_CAMPAIGNS_SEGMENT_30_DAYS = 'Number of campaigns sent to segment in the last 30 days'; |
| 74 | const TOTAL_CAMPAIGNS_SEGMENT_3_MONTHS = 'Number of campaigns sent to segment in the last 3 months'; |
| 75 | |
| 76 | const TOTAL_CAMPAIGNS_FILTERED_SEGMENT_7_DAYS = 'Number of campaigns filtered by segment in the last 7 days'; |
| 77 | const TOTAL_CAMPAIGNS_FILTERED_SEGMENT_30_DAYS = 'Number of campaigns filtered by segment in the last 30 days'; |
| 78 | const TOTAL_CAMPAIGNS_FILTERED_SEGMENT_3_MONTHS = 'Number of campaigns filtered by segment in the last 3 months'; |
| 79 | |
| 80 | /** @var SendingQueuesRepository */ |
| 81 | private $sendingQueuesRepository; |
| 82 | |
| 83 | public function __construct( |
| 84 | SendingQueuesRepository $sendingQueuesRepository |
| 85 | ) { |
| 86 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 87 | } |
| 88 | |
| 89 | public function getCampaignAnalyticsProperties(): array { |
| 90 | $returnData = [ |
| 91 | self::STANDARD_7_DAYS => 0, |
| 92 | self::STANDARD_30_DAYS => 0, |
| 93 | self::STANDARD_3_MONTHS => 0, |
| 94 | |
| 95 | self::STANDARD_SEGMENT_7_DAYS => 0, |
| 96 | self::STANDARD_SEGMENT_30_DAYS => 0, |
| 97 | self::STANDARD_SEGMENT_3_MONTHS => 0, |
| 98 | |
| 99 | self::STANDARD_FILTERED_SEGMENT_7_DAYS => 0, |
| 100 | self::STANDARD_FILTERED_SEGMENT_30_DAYS => 0, |
| 101 | self::STANDARD_FILTERED_SEGMENT_3_MONTHS => 0, |
| 102 | |
| 103 | self::AUTOMATION_7_DAYS => 0, |
| 104 | self::AUTOMATION_30_DAYS => 0, |
| 105 | self::AUTOMATION_3_MONTHS => 0, |
| 106 | |
| 107 | self::RE_ENGAGEMENT_7_DAYS => 0, |
| 108 | self::RE_ENGAGEMENT_30_DAYS => 0, |
| 109 | self::RE_ENGAGEMENT_3_MONTHS => 0, |
| 110 | |
| 111 | self::RE_ENGAGEMENT_SEGMENT_7_DAYS => 0, |
| 112 | self::RE_ENGAGEMENT_SEGMENT_30_DAYS => 0, |
| 113 | self::RE_ENGAGEMENT_SEGMENT_3_MONTHS => 0, |
| 114 | |
| 115 | self::RE_ENGAGEMENT_FILTERED_SEGMENT_7_DAYS => 0, |
| 116 | self::RE_ENGAGEMENT_FILTERED_SEGMENT_30_DAYS => 0, |
| 117 | self::RE_ENGAGEMENT_FILTERED_SEGMENT_3_MONTHS => 0, |
| 118 | |
| 119 | self::POST_NOTIFICATION_7_DAYS => 0, |
| 120 | self::POST_NOTIFICATION_30_DAYS => 0, |
| 121 | self::POST_NOTIFICATION_3_MONTHS => 0, |
| 122 | |
| 123 | self::POST_NOTIFICATION_SEGMENT_7_DAYS => 0, |
| 124 | self::POST_NOTIFICATION_SEGMENT_30_DAYS => 0, |
| 125 | self::POST_NOTIFICATION_SEGMENT_3_MONTHS => 0, |
| 126 | |
| 127 | self::POST_NOTIFICATION_FILTERED_SEGMENT_7_DAYS => 0, |
| 128 | self::POST_NOTIFICATION_FILTERED_SEGMENT_30_DAYS => 0, |
| 129 | self::POST_NOTIFICATION_FILTERED_SEGMENT_3_MONTHS => 0, |
| 130 | |
| 131 | // Legacy |
| 132 | self::LEGACY_WELCOME_7_DAYS => 0, |
| 133 | self::LEGACY_WELCOME_30_DAYS => 0, |
| 134 | self::LEGACY_WELCOME_3_MONTHS => 0, |
| 135 | |
| 136 | self::LEGACY_ABANDONED_CART_7_DAYS => 0, |
| 137 | self::LEGACY_ABANDONED_CART_30_DAYS => 0, |
| 138 | self::LEGACY_ABANDONED_CART_3_MONTHS => 0, |
| 139 | |
| 140 | self::LEGACY_FIRST_PURCHASE_7_DAYS => 0, |
| 141 | self::LEGACY_FIRST_PURCHASE_30_DAYS => 0, |
| 142 | self::LEGACY_FIRST_PURCHASE_3_MONTHS => 0, |
| 143 | |
| 144 | self::LEGACY_PURCHASED_IN_CATEGORY_7_DAYS => 0, |
| 145 | self::LEGACY_PURCHASED_IN_CATEGORY_30_DAYS => 0, |
| 146 | self::LEGACY_PURCHASED_IN_CATEGORY_3_MONTHS => 0, |
| 147 | |
| 148 | self::LEGACY_PURCHASED_PRODUCT_7_DAYS => 0, |
| 149 | self::LEGACY_PURCHASED_PRODUCT_30_DAYS => 0, |
| 150 | self::LEGACY_PURCHASED_PRODUCT_3_MONTHS => 0, |
| 151 | |
| 152 | // Totals |
| 153 | self::TOTAL_CAMPAIGNS_7_DAYS => 0, |
| 154 | self::TOTAL_CAMPAIGNS_30_DAYS => 0, |
| 155 | self::TOTAL_CAMPAIGNS_3_MONTHS => 0, |
| 156 | self::TOTAL_CAMPAIGNS_SEGMENT_7_DAYS => 0, |
| 157 | self::TOTAL_CAMPAIGNS_SEGMENT_30_DAYS => 0, |
| 158 | self::TOTAL_CAMPAIGNS_SEGMENT_3_MONTHS => 0, |
| 159 | self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_7_DAYS => 0, |
| 160 | self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_30_DAYS => 0, |
| 161 | self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_3_MONTHS => 0, |
| 162 | ]; |
| 163 | |
| 164 | $processedResults = $this->getProcessedCampaignAnalytics(); |
| 165 | |
| 166 | foreach ($processedResults as $campaignId => $processedResult) { |
| 167 | $isNewerThan7DaysAgo = $processedResult['sentLast7Days'] ?? false; |
| 168 | $isNewerThan30DaysAgo = $processedResult['sentLast30Days'] ?? false; |
| 169 | $isNewerThan3MonthsAgo = $processedResult['sentLast3Months'] ?? false; |
| 170 | |
| 171 | $newsletterType = $processedResult['newsletterType']; |
| 172 | |
| 173 | $wasSentToDynamicSegment = $processedResult['sentToSegment'] ?? false; |
| 174 | $wasFilteredBySegment = $processedResult['filteredBySegment'] ?? false; |
| 175 | |
| 176 | // Totals |
| 177 | if ($isNewerThan7DaysAgo) { |
| 178 | $returnData[self::TOTAL_CAMPAIGNS_7_DAYS]++; |
| 179 | $returnData[self::TOTAL_CAMPAIGNS_30_DAYS]++; |
| 180 | $returnData[self::TOTAL_CAMPAIGNS_3_MONTHS]++; |
| 181 | if ($wasSentToDynamicSegment) { |
| 182 | $returnData[self::TOTAL_CAMPAIGNS_SEGMENT_7_DAYS]++; |
| 183 | $returnData[self::TOTAL_CAMPAIGNS_SEGMENT_30_DAYS]++; |
| 184 | $returnData[self::TOTAL_CAMPAIGNS_SEGMENT_3_MONTHS]++; |
| 185 | } |
| 186 | if ($wasFilteredBySegment) { |
| 187 | $returnData[self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_7_DAYS]++; |
| 188 | $returnData[self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_30_DAYS]++; |
| 189 | $returnData[self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_3_MONTHS]++; |
| 190 | } |
| 191 | } elseif ($isNewerThan30DaysAgo) { |
| 192 | $returnData[self::TOTAL_CAMPAIGNS_30_DAYS]++; |
| 193 | $returnData[self::TOTAL_CAMPAIGNS_3_MONTHS]++; |
| 194 | if ($wasSentToDynamicSegment) { |
| 195 | $returnData[self::TOTAL_CAMPAIGNS_SEGMENT_30_DAYS]++; |
| 196 | $returnData[self::TOTAL_CAMPAIGNS_SEGMENT_3_MONTHS]++; |
| 197 | } |
| 198 | if ($wasFilteredBySegment) { |
| 199 | $returnData[self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_30_DAYS]++; |
| 200 | $returnData[self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_3_MONTHS]++; |
| 201 | } |
| 202 | } elseif ($isNewerThan3MonthsAgo) { |
| 203 | $returnData[self::TOTAL_CAMPAIGNS_3_MONTHS]++; |
| 204 | if ($wasSentToDynamicSegment) { |
| 205 | $returnData[self::TOTAL_CAMPAIGNS_SEGMENT_3_MONTHS]++; |
| 206 | } |
| 207 | if ($wasFilteredBySegment) { |
| 208 | $returnData[self::TOTAL_CAMPAIGNS_FILTERED_SEGMENT_3_MONTHS]++; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | switch ($newsletterType) { |
| 213 | case NewsletterEntity::TYPE_STANDARD: |
| 214 | if ($isNewerThan7DaysAgo) { |
| 215 | $returnData[self::STANDARD_7_DAYS]++; |
| 216 | $returnData[self::STANDARD_30_DAYS]++; |
| 217 | $returnData[self::STANDARD_3_MONTHS]++; |
| 218 | if ($wasFilteredBySegment) { |
| 219 | $returnData[self::STANDARD_FILTERED_SEGMENT_7_DAYS]++; |
| 220 | $returnData[self::STANDARD_FILTERED_SEGMENT_30_DAYS]++; |
| 221 | $returnData[self::STANDARD_FILTERED_SEGMENT_3_MONTHS]++; |
| 222 | } |
| 223 | if ($wasSentToDynamicSegment) { |
| 224 | $returnData[self::STANDARD_SEGMENT_7_DAYS]++; |
| 225 | $returnData[self::STANDARD_SEGMENT_30_DAYS]++; |
| 226 | $returnData[self::STANDARD_SEGMENT_3_MONTHS]++; |
| 227 | } |
| 228 | } elseif ($isNewerThan30DaysAgo) { |
| 229 | $returnData[self::STANDARD_30_DAYS]++; |
| 230 | $returnData[self::STANDARD_3_MONTHS]++; |
| 231 | if ($wasFilteredBySegment) { |
| 232 | $returnData[self::STANDARD_FILTERED_SEGMENT_30_DAYS]++; |
| 233 | $returnData[self::STANDARD_FILTERED_SEGMENT_3_MONTHS]++; |
| 234 | } |
| 235 | if ($wasSentToDynamicSegment) { |
| 236 | $returnData[self::STANDARD_SEGMENT_30_DAYS]++; |
| 237 | $returnData[self::STANDARD_SEGMENT_3_MONTHS]++; |
| 238 | } |
| 239 | } elseif ($isNewerThan3MonthsAgo) { |
| 240 | $returnData[self::STANDARD_3_MONTHS]++; |
| 241 | if ($wasFilteredBySegment) { |
| 242 | $returnData[self::STANDARD_FILTERED_SEGMENT_3_MONTHS]++; |
| 243 | } |
| 244 | if ($wasSentToDynamicSegment) { |
| 245 | $returnData[self::STANDARD_SEGMENT_3_MONTHS]++; |
| 246 | } |
| 247 | } |
| 248 | break; |
| 249 | case NewsletterEntity::TYPE_NOTIFICATION_HISTORY: |
| 250 | if ($isNewerThan7DaysAgo) { |
| 251 | $returnData[self::POST_NOTIFICATION_7_DAYS]++; |
| 252 | $returnData[self::POST_NOTIFICATION_30_DAYS]++; |
| 253 | $returnData[self::POST_NOTIFICATION_3_MONTHS]++; |
| 254 | if ($wasSentToDynamicSegment) { |
| 255 | $returnData[self::POST_NOTIFICATION_SEGMENT_7_DAYS]++; |
| 256 | $returnData[self::POST_NOTIFICATION_SEGMENT_30_DAYS]++; |
| 257 | $returnData[self::POST_NOTIFICATION_SEGMENT_3_MONTHS]++; |
| 258 | } |
| 259 | if ($wasFilteredBySegment) { |
| 260 | $returnData[self::POST_NOTIFICATION_FILTERED_SEGMENT_7_DAYS]++; |
| 261 | $returnData[self::POST_NOTIFICATION_FILTERED_SEGMENT_30_DAYS]++; |
| 262 | $returnData[self::POST_NOTIFICATION_FILTERED_SEGMENT_3_MONTHS]++; |
| 263 | } |
| 264 | } elseif ($isNewerThan30DaysAgo) { |
| 265 | $returnData[self::POST_NOTIFICATION_30_DAYS]++; |
| 266 | $returnData[self::POST_NOTIFICATION_3_MONTHS]++; |
| 267 | if ($wasSentToDynamicSegment) { |
| 268 | $returnData[self::POST_NOTIFICATION_SEGMENT_30_DAYS]++; |
| 269 | $returnData[self::POST_NOTIFICATION_SEGMENT_3_MONTHS]++; |
| 270 | } |
| 271 | if ($wasFilteredBySegment) { |
| 272 | $returnData[self::POST_NOTIFICATION_FILTERED_SEGMENT_30_DAYS]++; |
| 273 | $returnData[self::POST_NOTIFICATION_FILTERED_SEGMENT_3_MONTHS]++; |
| 274 | } |
| 275 | } elseif ($isNewerThan3MonthsAgo) { |
| 276 | $returnData[self::POST_NOTIFICATION_3_MONTHS]++; |
| 277 | if ($wasSentToDynamicSegment) { |
| 278 | $returnData[self::POST_NOTIFICATION_SEGMENT_3_MONTHS]++; |
| 279 | } |
| 280 | if ($wasFilteredBySegment) { |
| 281 | $returnData[self::POST_NOTIFICATION_FILTERED_SEGMENT_3_MONTHS]++; |
| 282 | } |
| 283 | } |
| 284 | break; |
| 285 | case NewsletterEntity::TYPE_RE_ENGAGEMENT: |
| 286 | if ($isNewerThan7DaysAgo) { |
| 287 | $returnData[self::RE_ENGAGEMENT_7_DAYS]++; |
| 288 | $returnData[self::RE_ENGAGEMENT_30_DAYS]++; |
| 289 | $returnData[self::RE_ENGAGEMENT_3_MONTHS]++; |
| 290 | if ($wasSentToDynamicSegment) { |
| 291 | $returnData[self::RE_ENGAGEMENT_SEGMENT_7_DAYS]++; |
| 292 | $returnData[self::RE_ENGAGEMENT_SEGMENT_30_DAYS]++; |
| 293 | $returnData[self::RE_ENGAGEMENT_SEGMENT_3_MONTHS]++; |
| 294 | } |
| 295 | if ($wasFilteredBySegment) { |
| 296 | $returnData[self::RE_ENGAGEMENT_FILTERED_SEGMENT_7_DAYS]++; |
| 297 | $returnData[self::RE_ENGAGEMENT_FILTERED_SEGMENT_30_DAYS]++; |
| 298 | $returnData[self::RE_ENGAGEMENT_FILTERED_SEGMENT_3_MONTHS]++; |
| 299 | } |
| 300 | } elseif ($isNewerThan30DaysAgo) { |
| 301 | $returnData[self::RE_ENGAGEMENT_30_DAYS]++; |
| 302 | $returnData[self::RE_ENGAGEMENT_3_MONTHS]++; |
| 303 | if ($wasSentToDynamicSegment) { |
| 304 | $returnData[self::RE_ENGAGEMENT_SEGMENT_30_DAYS]++; |
| 305 | $returnData[self::RE_ENGAGEMENT_SEGMENT_3_MONTHS]++; |
| 306 | } |
| 307 | if ($wasFilteredBySegment) { |
| 308 | $returnData[self::RE_ENGAGEMENT_FILTERED_SEGMENT_30_DAYS]++; |
| 309 | $returnData[self::RE_ENGAGEMENT_FILTERED_SEGMENT_3_MONTHS]++; |
| 310 | } |
| 311 | } elseif ($isNewerThan3MonthsAgo) { |
| 312 | $returnData[self::RE_ENGAGEMENT_3_MONTHS]++; |
| 313 | if ($wasSentToDynamicSegment) { |
| 314 | $returnData[self::RE_ENGAGEMENT_SEGMENT_3_MONTHS]++; |
| 315 | } |
| 316 | if ($wasFilteredBySegment) { |
| 317 | $returnData[self::RE_ENGAGEMENT_FILTERED_SEGMENT_3_MONTHS]++; |
| 318 | } |
| 319 | } |
| 320 | break; |
| 321 | case NewsletterEntity::TYPE_WELCOME: |
| 322 | if ($isNewerThan7DaysAgo) { |
| 323 | $returnData[self::LEGACY_WELCOME_7_DAYS]++; |
| 324 | $returnData[self::LEGACY_WELCOME_30_DAYS]++; |
| 325 | $returnData[self::LEGACY_WELCOME_3_MONTHS]++; |
| 326 | } elseif ($isNewerThan30DaysAgo) { |
| 327 | $returnData[self::LEGACY_WELCOME_30_DAYS]++; |
| 328 | $returnData[self::LEGACY_WELCOME_3_MONTHS]++; |
| 329 | } elseif ($isNewerThan3MonthsAgo) { |
| 330 | $returnData[self::LEGACY_WELCOME_3_MONTHS]++; |
| 331 | } |
| 332 | break; |
| 333 | case NewsletterEntity::TYPE_AUTOMATION: |
| 334 | if ($isNewerThan7DaysAgo) { |
| 335 | $returnData[self::AUTOMATION_7_DAYS]++; |
| 336 | $returnData[self::AUTOMATION_30_DAYS]++; |
| 337 | $returnData[self::AUTOMATION_3_MONTHS]++; |
| 338 | } elseif ($isNewerThan30DaysAgo) { |
| 339 | $returnData[self::AUTOMATION_30_DAYS]++; |
| 340 | $returnData[self::AUTOMATION_3_MONTHS]++; |
| 341 | } elseif ($isNewerThan3MonthsAgo) { |
| 342 | $returnData[self::AUTOMATION_3_MONTHS]++; |
| 343 | } |
| 344 | break; |
| 345 | // Legacy automatic emails. |
| 346 | case 'purchasedProduct': |
| 347 | if ($isNewerThan7DaysAgo) { |
| 348 | $returnData[self::LEGACY_PURCHASED_PRODUCT_7_DAYS]++; |
| 349 | $returnData[self::LEGACY_PURCHASED_PRODUCT_30_DAYS]++; |
| 350 | $returnData[self::LEGACY_PURCHASED_PRODUCT_3_MONTHS]++; |
| 351 | } elseif ($isNewerThan30DaysAgo) { |
| 352 | $returnData[self::LEGACY_PURCHASED_PRODUCT_30_DAYS]++; |
| 353 | $returnData[self::LEGACY_PURCHASED_PRODUCT_3_MONTHS]++; |
| 354 | } elseif ($isNewerThan3MonthsAgo) { |
| 355 | $returnData[self::LEGACY_PURCHASED_PRODUCT_3_MONTHS]++; |
| 356 | } |
| 357 | break; |
| 358 | case 'purchasedInCategory': |
| 359 | if ($isNewerThan7DaysAgo) { |
| 360 | $returnData[self::LEGACY_PURCHASED_IN_CATEGORY_7_DAYS]++; |
| 361 | $returnData[self::LEGACY_PURCHASED_IN_CATEGORY_30_DAYS]++; |
| 362 | $returnData[self::LEGACY_PURCHASED_IN_CATEGORY_3_MONTHS]++; |
| 363 | } elseif ($isNewerThan30DaysAgo) { |
| 364 | $returnData[self::LEGACY_PURCHASED_IN_CATEGORY_30_DAYS]++; |
| 365 | $returnData[self::LEGACY_PURCHASED_IN_CATEGORY_3_MONTHS]++; |
| 366 | } elseif ($isNewerThan3MonthsAgo) { |
| 367 | $returnData[self::LEGACY_PURCHASED_IN_CATEGORY_3_MONTHS]++; |
| 368 | } |
| 369 | break; |
| 370 | case 'abandonedCart': |
| 371 | if ($isNewerThan7DaysAgo) { |
| 372 | $returnData[self::LEGACY_ABANDONED_CART_7_DAYS]++; |
| 373 | $returnData[self::LEGACY_ABANDONED_CART_30_DAYS]++; |
| 374 | $returnData[self::LEGACY_ABANDONED_CART_3_MONTHS]++; |
| 375 | } elseif ($isNewerThan30DaysAgo) { |
| 376 | $returnData[self::LEGACY_ABANDONED_CART_30_DAYS]++; |
| 377 | $returnData[self::LEGACY_ABANDONED_CART_3_MONTHS]++; |
| 378 | } elseif ($isNewerThan3MonthsAgo) { |
| 379 | $returnData[self::LEGACY_ABANDONED_CART_3_MONTHS]++; |
| 380 | } |
| 381 | break; |
| 382 | case 'firstPurchase': |
| 383 | if ($isNewerThan7DaysAgo) { |
| 384 | $returnData[self::LEGACY_FIRST_PURCHASE_7_DAYS]++; |
| 385 | $returnData[self::LEGACY_FIRST_PURCHASE_30_DAYS]++; |
| 386 | $returnData[self::LEGACY_FIRST_PURCHASE_3_MONTHS]++; |
| 387 | } elseif ($isNewerThan30DaysAgo) { |
| 388 | $returnData[self::LEGACY_FIRST_PURCHASE_30_DAYS]++; |
| 389 | $returnData[self::LEGACY_FIRST_PURCHASE_3_MONTHS]++; |
| 390 | } elseif ($isNewerThan3MonthsAgo) { |
| 391 | $returnData[self::LEGACY_FIRST_PURCHASE_3_MONTHS]++; |
| 392 | } |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return $returnData; |
| 398 | } |
| 399 | |
| 400 | public function getProcessedCampaignAnalytics(): array { |
| 401 | $rawData = $this->sendingQueuesRepository->getCampaignAnalyticsQuery()->getArrayResult(); |
| 402 | $processedResults = []; |
| 403 | |
| 404 | foreach ($rawData as $sendingInfo) { |
| 405 | $meta = $sendingInfo['sendingQueueMeta']; |
| 406 | $campaignId = $meta['campaignId'] ?? null; |
| 407 | $newsletterType = $sendingInfo['newsletterType']; |
| 408 | |
| 409 | // "Send latest newsletter" automation replays re-send a standard newsletter. For this report we |
| 410 | // count them as automation activity grouped per automation, instead of merging into the source |
| 411 | // newsletter's campaign or skipping them entirely. |
| 412 | if (NewsletterReplayMetadata::isLatestNewsletterReplayMeta($meta)) { |
| 413 | $automationId = $meta['automation']['id'] ?? null; |
| 414 | if (!is_numeric($automationId)) { |
| 415 | continue; |
| 416 | } |
| 417 | $campaignId = 'automation_' . (int)$automationId; |
| 418 | $newsletterType = NewsletterEntity::TYPE_AUTOMATION; |
| 419 | } |
| 420 | |
| 421 | if (!is_string($campaignId)) { |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | if (!isset($processedResults[$campaignId])) { |
| 426 | $processedData = [ |
| 427 | 'campaignId' => $campaignId, |
| 428 | 'newsletterType' => $newsletterType, |
| 429 | 'automaticSubType' => null, |
| 430 | 'sentToSegment' => (bool)$sendingInfo['sentToSegment'], |
| 431 | 'sentLast7Days' => (bool)$sendingInfo['sentLast7Days'], |
| 432 | 'sentLast30Days' => (bool)$sendingInfo['sentLast30Days'], |
| 433 | 'sentLast3Months' => (bool)$sendingInfo['sentLast3Months'], |
| 434 | 'filteredBySegment' => !!($meta['filterSegment'] ?? null), |
| 435 | ]; |
| 436 | $processedResults[$campaignId] = $processedData; |
| 437 | if ($newsletterType === NewsletterEntity::TYPE_AUTOMATIC) { |
| 438 | try { |
| 439 | // Although we could determine the subtype by joining the appropriate newsletter option field, using |
| 440 | // the meta should be just as reliable, and we need the meta anyway, so this keeps our query simpler. |
| 441 | $subType = $this->getLegacyAutomaticEmailSubtypeFromMeta($meta); |
| 442 | $processedResults[$campaignId]['newsletterType'] = $subType; |
| 443 | } catch (UnexpectedValueException $e) { |
| 444 | // Ignore this error, the `automatic` email type won't be counted |
| 445 | } |
| 446 | } |
| 447 | } else { |
| 448 | if ($sendingInfo['sentLast7Days']) { |
| 449 | $processedResults[$campaignId]['sentLast7Days'] = true; |
| 450 | } |
| 451 | if ($sendingInfo['sentLast30Days']) { |
| 452 | $processedResults[$campaignId]['sentLast30Days'] = true; |
| 453 | } |
| 454 | if ($sendingInfo['sentLast3Months']) { |
| 455 | $processedResults[$campaignId]['sentLast3Months'] = true; |
| 456 | } |
| 457 | if ($sendingInfo['sentToSegment']) { |
| 458 | $processedResults[$campaignId]['sentToSegment'] = true; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return $processedResults; |
| 464 | } |
| 465 | |
| 466 | private function getLegacyAutomaticEmailSubtypeFromMeta(array $meta): string { |
| 467 | if (array_key_exists('orderedProducts', $meta)) { |
| 468 | return 'purchasedProduct'; |
| 469 | } |
| 470 | if (array_key_exists('orderedProductCategories', $meta)) { |
| 471 | return 'purchasedInCategory'; |
| 472 | } |
| 473 | if (array_key_exists('cart_product_ids', $meta)) { |
| 474 | return 'abandonedCart'; |
| 475 | } |
| 476 | if (array_key_exists('order_amount', $meta) && array_key_exists('order_date', $meta) && array_key_exists('order_id', $meta)) { |
| 477 | return 'firstPurchase'; |
| 478 | } |
| 479 | |
| 480 | throw new UnexpectedValueException('Unknown automatic email type based on meta data'); |
| 481 | } |
| 482 | } |
| 483 |