wooPurchasesRepository = $wooPurchasesRepository; $this->newslettersRepository = $newslettersRepository; $this->wooHelper = $wooHelper; $this->loggerFactory = $loggerFactory; } public function addTrackingData(array $data): array { try { $currency = $this->wooHelper->getWoocommerceCurrency(); $analyticsData = $this->newslettersRepository->getAnalytics(); $data['extensions']['mailpoet'] = [ 'campaigns_count' => $analyticsData['campaigns_count'], ]; $campaignData = $this->formatCampaignsData($this->wooPurchasesRepository->getRevenuesByCampaigns($currency)); $data['extensions']['mailpoet'] = array_merge($data['extensions']['mailpoet'], $campaignData); } catch (\Throwable $e) { $this->loggerFactory->getLogger(LoggerFactory::TOPIC_TRACKING)->error($e->getMessage()); return $data; } return $data; } /** * @param array $campaignsData * @return array */ private function formatCampaignsData(array $campaignsData): array { return array_reduce($campaignsData, function($result, array $campaign): array { $newsletter = $this->newslettersRepository->findOneById((int)$campaign['campaign_id']); $keyPrefix = 'campaign_' . $campaign['campaign_id']; $result[$keyPrefix . '_revenue'] = $campaign['revenue']; $result[$keyPrefix . '_orders_count'] = $campaign['orders_count']; $result[$keyPrefix . '_type'] = $campaign['campaign_type']; $result[$keyPrefix . '_event'] = $newsletter ? (string)$newsletter->getOptionValue(NewsletterOptionFieldEntity::NAME_EVENT) : ''; return $result; }, []); } }