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
Analytics.php
136 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Analytics; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Settings\SettingsController; |
| 9 | use MailPoet\Util\Security; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | use MailPoetVendor\Carbon\Carbon; |
| 12 | |
| 13 | class Analytics { |
| 14 | |
| 15 | const SETTINGS_LAST_SENT_KEY = 'analytics_last_sent'; |
| 16 | const SETTINGS_LAST_SENT_TRACKS_KEY = 'analytics_last_sent_tracks'; |
| 17 | const SEND_AFTER_DAYS = 7; |
| 18 | const ANALYTICS_FILTER = 'mailpoet_analytics'; |
| 19 | |
| 20 | /** @var Reporter */ |
| 21 | private $reporter; |
| 22 | |
| 23 | /** @var SettingsController */ |
| 24 | private $settings; |
| 25 | |
| 26 | /** @var WPFunctions */ |
| 27 | private $wp; |
| 28 | |
| 29 | public function __construct( |
| 30 | Reporter $reporter, |
| 31 | SettingsController $settingsController |
| 32 | ) { |
| 33 | $this->reporter = $reporter; |
| 34 | $this->settings = $settingsController; |
| 35 | $this->wp = new WPFunctions; |
| 36 | } |
| 37 | |
| 38 | /** @return array|null */ |
| 39 | public function generateAnalytics() { |
| 40 | if ($this->shouldSendToMixpanel()) { |
| 41 | $data = $this->getAnalyticsData(); |
| 42 | $this->recordMixpanelDataSent(); |
| 43 | return $data; |
| 44 | } |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | public function getAnalyticsData() { |
| 49 | return $this->wp->applyFilters(self::ANALYTICS_FILTER, $this->reporter->getData()); |
| 50 | } |
| 51 | |
| 52 | /** @return bool */ |
| 53 | public function isEnabled() { |
| 54 | $analyticsSettings = $this->settings->get('analytics', []); |
| 55 | return !empty($analyticsSettings['enabled']) === true; |
| 56 | } |
| 57 | |
| 58 | public function setPublicId($newPublicId) { |
| 59 | $currentPublicId = $this->settings->get('public_id'); |
| 60 | if ($currentPublicId !== $newPublicId) { |
| 61 | $this->settings->set('public_id', $newPublicId); |
| 62 | $this->settings->set('new_public_id', 'true'); |
| 63 | // Force user data to be resent |
| 64 | $this->settings->delete(Analytics::SETTINGS_LAST_SENT_KEY); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** @return string */ |
| 69 | public function getPublicId() { |
| 70 | $publicId = $this->settings->get('public_id', ''); |
| 71 | if (empty($publicId)) { |
| 72 | // The previous implementation used md5, so this is just to ensure consistency |
| 73 | $randomId = md5(Security::generateRandomString(32)); |
| 74 | $this->settings->set('public_id', $randomId); |
| 75 | $this->settings->set('new_public_id', 'true'); |
| 76 | return $randomId; |
| 77 | } |
| 78 | return $publicId; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns true if a the public_id was added and update new_public_id to false |
| 83 | * @return bool |
| 84 | */ |
| 85 | public function isPublicIdNew() { |
| 86 | $newPublicId = $this->settings->get('new_public_id'); |
| 87 | if ($newPublicId === 'true') { |
| 88 | $this->settings->set('new_public_id', 'false'); |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | public function shouldSendToMixpanel() { |
| 95 | if (!$this->isEnabled()) { |
| 96 | return false; |
| 97 | } |
| 98 | $nextSend = $this->getNextSendDateForMixpanel(); |
| 99 | return $nextSend->isPast(); |
| 100 | } |
| 101 | |
| 102 | public function getNextSendDateForMixpanel(): Carbon { |
| 103 | $lastSent = $this->settings->get(Analytics::SETTINGS_LAST_SENT_KEY); |
| 104 | if (!$lastSent) { |
| 105 | return Carbon::now()->subMinute(); |
| 106 | } |
| 107 | |
| 108 | return Carbon::createFromTimestamp(strtotime($lastSent))->addDays(self::SEND_AFTER_DAYS); |
| 109 | } |
| 110 | |
| 111 | public function recordMixpanelDataSent() { |
| 112 | $this->settings->set(Analytics::SETTINGS_LAST_SENT_KEY, Carbon::now()); |
| 113 | } |
| 114 | |
| 115 | public function recordTracksDataSent() { |
| 116 | $this->settings->set(Analytics::SETTINGS_LAST_SENT_TRACKS_KEY, Carbon::now()); |
| 117 | } |
| 118 | |
| 119 | public function shouldSendToTracks() { |
| 120 | if (!$this->isEnabled()) { |
| 121 | return false; |
| 122 | } |
| 123 | $nextSend = $this->getNextSendDateForTracks(); |
| 124 | return $nextSend->isPast(); |
| 125 | } |
| 126 | |
| 127 | public function getNextSendDateForTracks(): Carbon { |
| 128 | $lastSent = $this->settings->get(Analytics::SETTINGS_LAST_SENT_TRACKS_KEY); |
| 129 | if (!$lastSent) { |
| 130 | return Carbon::now()->subMinute(); |
| 131 | } |
| 132 | |
| 133 | return Carbon::createFromTimestamp(strtotime($lastSent))->addDays(self::SEND_AFTER_DAYS); |
| 134 | } |
| 135 | } |
| 136 |