Refactor click tracking to Doctrine
[MAILPOET-3735]
This commit is contained in:
@@ -237,6 +237,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Statistics\Track\WooCommercePurchases::class);
|
$container->autowire(\MailPoet\Statistics\Track\WooCommercePurchases::class);
|
||||||
$container->autowire(\MailPoet\Statistics\Track\Unsubscribes::class)->setPublic(true);
|
$container->autowire(\MailPoet\Statistics\Track\Unsubscribes::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Statistics\StatisticsFormsRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Statistics\StatisticsFormsRepository::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Statistics\StatisticsClicksRepository::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Statistics\StatisticsOpensRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Statistics\StatisticsOpensRepository::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Statistics\StatisticsUnsubscribesRepository::class);
|
$container->autowire(\MailPoet\Statistics\StatisticsUnsubscribesRepository::class);
|
||||||
$container->autowire(\MailPoet\Statistics\StatisticsWooCommercePurchasesRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Statistics\StatisticsWooCommercePurchasesRepository::class)->setPublic(true);
|
||||||
|
@@ -14,25 +14,6 @@ use DateTimeInterface;
|
|||||||
class StatisticsClicks extends Model {
|
class StatisticsClicks extends Model {
|
||||||
public static $_table = MP_STATISTICS_CLICKS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
|
public static $_table = MP_STATISTICS_CLICKS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
|
||||||
|
|
||||||
public static function createOrUpdateClickCount($linkId, $subscriberId, $newsletterId, $queueId) {
|
|
||||||
$statistics = self::where('link_id', $linkId)
|
|
||||||
->where('subscriber_id', $subscriberId)
|
|
||||||
->where('newsletter_id', $newsletterId)
|
|
||||||
->where('queue_id', $queueId)
|
|
||||||
->findOne();
|
|
||||||
if (!$statistics instanceof self) {
|
|
||||||
$statistics = self::create();
|
|
||||||
$statistics->linkId = $linkId;
|
|
||||||
$statistics->subscriberId = $subscriberId;
|
|
||||||
$statistics->newsletterId = $newsletterId;
|
|
||||||
$statistics->queueId = $queueId;
|
|
||||||
$statistics->count = 1;
|
|
||||||
} else {
|
|
||||||
$statistics->count++;
|
|
||||||
}
|
|
||||||
return $statistics->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getAllForSubscriber(Subscriber $subscriber) {
|
public static function getAllForSubscriber(Subscriber $subscriber) {
|
||||||
return static::tableAlias('clicks')
|
return static::tableAlias('clicks')
|
||||||
->select('clicks.id', 'id')
|
->select('clicks.id', 'id')
|
||||||
|
40
lib/Statistics/StatisticsClicksRepository.php
Normal file
40
lib/Statistics/StatisticsClicksRepository.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MailPoet\Statistics;
|
||||||
|
|
||||||
|
use MailPoet\Doctrine\Repository;
|
||||||
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
|
use MailPoet\Entities\NewsletterLinkEntity;
|
||||||
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Repository<StatisticsClickEntity>
|
||||||
|
*/
|
||||||
|
class StatisticsClicksRepository extends Repository {
|
||||||
|
protected function getEntityClassName(): string {
|
||||||
|
return StatisticsClickEntity::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createOrUpdateClickCount(
|
||||||
|
NewsletterLinkEntity $link,
|
||||||
|
SubscriberEntity $subscriber,
|
||||||
|
NewsletterEntity $newsletter,
|
||||||
|
SendingQueueEntity $queue
|
||||||
|
): StatisticsClickEntity {
|
||||||
|
$statistics = $this->findOneBy([
|
||||||
|
'link' => $link,
|
||||||
|
'newsletter' => $newsletter,
|
||||||
|
'subscriber' => $subscriber,
|
||||||
|
'queue' => $queue,
|
||||||
|
]);
|
||||||
|
if (!$statistics instanceof StatisticsClickEntity) {
|
||||||
|
$statistics = new StatisticsClickEntity($newsletter, $queue, $subscriber, $link, 1);
|
||||||
|
$this->persist($statistics);
|
||||||
|
} else {
|
||||||
|
$statistics->setCount($statistics->getCount() + 1);
|
||||||
|
}
|
||||||
|
return $statistics;
|
||||||
|
}
|
||||||
|
}
|
@@ -5,11 +5,12 @@ namespace MailPoet\Statistics\Track;
|
|||||||
use MailPoet\Entities\NewsletterEntity;
|
use MailPoet\Entities\NewsletterEntity;
|
||||||
use MailPoet\Entities\NewsletterLinkEntity;
|
use MailPoet\Entities\NewsletterLinkEntity;
|
||||||
use MailPoet\Entities\SendingQueueEntity;
|
use MailPoet\Entities\SendingQueueEntity;
|
||||||
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
use MailPoet\Models\StatisticsClicks;
|
|
||||||
use MailPoet\Newsletter\Shortcodes\Categories\Link as LinkShortcodeCategory;
|
use MailPoet\Newsletter\Shortcodes\Categories\Link as LinkShortcodeCategory;
|
||||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
|
use MailPoet\Statistics\StatisticsClicksRepository;
|
||||||
use MailPoet\Util\Cookies;
|
use MailPoet\Util\Cookies;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
@@ -36,11 +37,15 @@ class Clicks {
|
|||||||
/** @var Opens */
|
/** @var Opens */
|
||||||
private $opens;
|
private $opens;
|
||||||
|
|
||||||
|
/** @var StatisticsClicksRepository */
|
||||||
|
private $statisticsClicksRepository;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SettingsController $settingsController,
|
SettingsController $settingsController,
|
||||||
Cookies $cookies,
|
Cookies $cookies,
|
||||||
Shortcodes $shortcodes,
|
Shortcodes $shortcodes,
|
||||||
Opens $opens,
|
Opens $opens,
|
||||||
|
StatisticsClicksRepository $statisticsClicksRepository,
|
||||||
LinkShortcodeCategory $linkShortcodeCategory
|
LinkShortcodeCategory $linkShortcodeCategory
|
||||||
) {
|
) {
|
||||||
$this->settingsController = $settingsController;
|
$this->settingsController = $settingsController;
|
||||||
@@ -48,6 +53,7 @@ class Clicks {
|
|||||||
$this->shortcodes = $shortcodes;
|
$this->shortcodes = $shortcodes;
|
||||||
$this->linkShortcodeCategory = $linkShortcodeCategory;
|
$this->linkShortcodeCategory = $linkShortcodeCategory;
|
||||||
$this->opens = $opens;
|
$this->opens = $opens;
|
||||||
|
$this->statisticsClicksRepository = $statisticsClicksRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,12 +75,13 @@ class Clicks {
|
|||||||
// log statistics only if the action did not come from
|
// log statistics only if the action did not come from
|
||||||
// a WP user previewing the newsletter
|
// a WP user previewing the newsletter
|
||||||
if (!$wpUserPreview) {
|
if (!$wpUserPreview) {
|
||||||
$statisticsClicks = StatisticsClicks::createOrUpdateClickCount(
|
$statisticsClicks = $this->statisticsClicksRepository->createOrUpdateClickCount(
|
||||||
$link->getId(),
|
$link,
|
||||||
$subscriber->getId(),
|
$subscriber,
|
||||||
$newsletter->getId(),
|
$newsletter,
|
||||||
$queue->getId()
|
$queue
|
||||||
);
|
);
|
||||||
|
$this->statisticsClicksRepository->flush();
|
||||||
$this->sendRevenueCookie($statisticsClicks);
|
$this->sendRevenueCookie($statisticsClicks);
|
||||||
$this->sendAbandonedCartCookie($subscriber);
|
$this->sendAbandonedCartCookie($subscriber);
|
||||||
// track open event
|
// track open event
|
||||||
@@ -84,12 +91,12 @@ class Clicks {
|
|||||||
$this->redirectToUrl($url);
|
$this->redirectToUrl($url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sendRevenueCookie(StatisticsClicks $clicks) {
|
private function sendRevenueCookie(StatisticsClickEntity $clicks) {
|
||||||
if ($this->settingsController->get('woocommerce.accept_cookie_revenue_tracking.enabled')) {
|
if ($this->settingsController->get('woocommerce.accept_cookie_revenue_tracking.enabled')) {
|
||||||
$this->cookies->set(
|
$this->cookies->set(
|
||||||
self::REVENUE_TRACKING_COOKIE_NAME,
|
self::REVENUE_TRACKING_COOKIE_NAME,
|
||||||
[
|
[
|
||||||
'statistics_clicks' => $clicks->id,
|
'statistics_clicks' => $clicks->getId(),
|
||||||
'created_at' => time(),
|
'created_at' => time(),
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
|
@@ -17,6 +17,7 @@ use MailPoet\Models\StatisticsOpens;
|
|||||||
use MailPoet\Newsletter\Shortcodes\Categories\Link as LinkShortcodeCategory;
|
use MailPoet\Newsletter\Shortcodes\Categories\Link as LinkShortcodeCategory;
|
||||||
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
use MailPoet\Newsletter\Shortcodes\Shortcodes;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
|
use MailPoet\Statistics\StatisticsClicksRepository;
|
||||||
use MailPoet\Statistics\Track\Clicks;
|
use MailPoet\Statistics\Track\Clicks;
|
||||||
use MailPoet\Statistics\Track\Opens;
|
use MailPoet\Statistics\Track\Opens;
|
||||||
use MailPoet\Subscribers\LinkTokens;
|
use MailPoet\Subscribers\LinkTokens;
|
||||||
@@ -89,6 +90,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class)
|
$this->diContainer->get(LinkShortcodeCategory::class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -100,6 +102,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class),
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
], [
|
], [
|
||||||
'abort' => Expected::exactly(2),
|
'abort' => Expected::exactly(2),
|
||||||
@@ -122,6 +125,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class),
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
], [
|
], [
|
||||||
'redirectToUrl' => null,
|
'redirectToUrl' => null,
|
||||||
@@ -138,6 +142,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class),
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
], [
|
], [
|
||||||
'redirectToUrl' => null,
|
'redirectToUrl' => null,
|
||||||
@@ -153,6 +158,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class),
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
], [
|
], [
|
||||||
'redirectToUrl' => Expected::exactly(1),
|
'redirectToUrl' => Expected::exactly(1),
|
||||||
@@ -166,6 +172,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class),
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
], [
|
], [
|
||||||
'redirectToUrl' => null,
|
'redirectToUrl' => null,
|
||||||
@@ -193,6 +200,7 @@ class ClicksTest extends \MailPoetTest {
|
|||||||
new Cookies(),
|
new Cookies(),
|
||||||
$this->diContainer->get(Shortcodes::class),
|
$this->diContainer->get(Shortcodes::class),
|
||||||
$this->diContainer->get(Opens::class),
|
$this->diContainer->get(Opens::class),
|
||||||
|
$this->diContainer->get(StatisticsClicksRepository::class),
|
||||||
$this->diContainer->get(LinkShortcodeCategory::class),
|
$this->diContainer->get(LinkShortcodeCategory::class),
|
||||||
], [
|
], [
|
||||||
'abort' => Expected::exactly(1),
|
'abort' => Expected::exactly(1),
|
||||||
|
Reference in New Issue
Block a user