Нет описания

Analytics.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace MailPoet\Analytics;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Settings\SettingsController;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. use MailPoetVendor\Carbon\Carbon;
  7. class Analytics {
  8. const SETTINGS_LAST_SENT_KEY = 'analytics_last_sent';
  9. const SEND_AFTER_DAYS = 7;
  10. const ANALYTICS_FILTER = 'mailpoet_analytics';
  11. /** @var Reporter */
  12. private $reporter;
  13. /** @var SettingsController */
  14. private $settings;
  15. /** @var WPFunctions */
  16. private $wp;
  17. public function __construct(
  18. Reporter $reporter,
  19. SettingsController $settingsController
  20. ) {
  21. $this->reporter = $reporter;
  22. $this->settings = $settingsController;
  23. $this->wp = new WPFunctions;
  24. }
  25. /** @return array|null */
  26. public function generateAnalytics() {
  27. if ($this->shouldSend()) {
  28. $data = $this->wp->applyFilters(self::ANALYTICS_FILTER, $this->reporter->getData());
  29. $this->recordDataSent();
  30. return $data;
  31. }
  32. return null;
  33. }
  34. /** @return bool */
  35. public function isEnabled() {
  36. $analyticsSettings = $this->settings->get('analytics', []);
  37. return !empty($analyticsSettings['enabled']) === true;
  38. }
  39. public function setPublicId($newPublicId) {
  40. $currentPublicId = $this->settings->get('public_id');
  41. if ($currentPublicId !== $newPublicId) {
  42. $this->settings->set('public_id', $newPublicId);
  43. $this->settings->set('new_public_id', 'true');
  44. // Force user data to be resent
  45. $this->settings->delete(Analytics::SETTINGS_LAST_SENT_KEY);
  46. }
  47. }
  48. /** @return string */
  49. public function getPublicId() {
  50. $publicId = $this->settings->get('public_id', '');
  51. // if we didn't get the user public_id from the shop yet : we create one based on mixpanel distinct_id
  52. if (empty($publicId) && !empty($_COOKIE['mixpanel_distinct_id'])) {
  53. // the public id has to be diffent that mixpanel_distinct_id in order to be used on different browser
  54. $mixpanelDistinctId = md5($_COOKIE['mixpanel_distinct_id']);
  55. $this->settings->set('public_id', $mixpanelDistinctId);
  56. $this->settings->set('new_public_id', 'true');
  57. return $mixpanelDistinctId;
  58. }
  59. return $publicId;
  60. }
  61. /**
  62. * Returns true if a the public_id was added and update new_public_id to false
  63. * @return bool
  64. */
  65. public function isPublicIdNew() {
  66. $newPublicId = $this->settings->get('new_public_id');
  67. if ($newPublicId === 'true') {
  68. $this->settings->set('new_public_id', 'false');
  69. return true;
  70. }
  71. return false;
  72. }
  73. private function shouldSend() {
  74. if (!$this->isEnabled()) {
  75. return false;
  76. }
  77. $lastSent = $this->settings->get(Analytics::SETTINGS_LAST_SENT_KEY);
  78. if (!$lastSent) {
  79. return true;
  80. }
  81. $lastSentCarbon = Carbon::createFromTimestamp(strtotime($lastSent))->addDays(Analytics::SEND_AFTER_DAYS);
  82. return $lastSentCarbon->isPast();
  83. }
  84. private function recordDataSent() {
  85. $this->settings->set(Analytics::SETTINGS_LAST_SENT_KEY, Carbon::now());
  86. }
  87. }