Track last engagement only once per minute

[MAILPOET-3762]
This commit is contained in:
Rostislav Wolny
2021-08-31 13:05:06 +02:00
committed by Veljko V
parent 9c3fc95a6d
commit b215ab13c8
2 changed files with 26 additions and 0 deletions

View File

@@ -312,6 +312,10 @@ class SubscribersRepository extends Repository {
if ($userAgent instanceof UserAgentEntity && $userAgent->getUserAgentType() === UserAgentEntity::USER_AGENT_TYPE_MACHINE) {
return;
}
// Do not update engagement if was recently updated to avoid unnecessary updates in DB
if ($subscriberEntity->getLastEngagementAt() && $subscriberEntity->getLastEngagementAt() > Carbon::now()->subMinute()) {
return;
}
// Update last engagement for human (and also unknown) user agent
$subscriberEntity->setLastEngagementAt(Carbon::now());
$this->flush();

View File

@@ -545,6 +545,28 @@ class ClicksTest extends \MailPoetTest {
expect($this->subscriber->getLastEngagementAt())->null();
}
public function testItWontUpdateSubscriberThatWasRecentlyUpdated() {
$lastEngagement = Carbon::now()->subSeconds(10);
$clicksRepository = $this->diContainer->get(StatisticsClicksRepository::class);
$this->subscriber->setLastEngagementAt($lastEngagement);
$data = $this->trackData;
$data->userAgent = UserAgentEntity::MACHINE_USER_AGENTS[0];
$clicks = Stub::construct($this->clicks, [
$this->settingsController,
new Cookies(),
$this->diContainer->get(Shortcodes::class),
$this->diContainer->get(Opens::class),
$clicksRepository,
$this->diContainer->get(UserAgentsRepository::class),
$this->diContainer->get(LinkShortcodeCategory::class),
$this->diContainer->get(SubscribersRepository::class),
], [
'redirectToUrl' => null,
], $this);
$clicks->track($data);
expect($this->subscriber->getLastEngagementAt())->equals($lastEngagement);
}
public function cleanup() {
$this->truncateEntity(NewsletterEntity::class);
$this->truncateEntity(SubscriberEntity::class);