Skip to content

Commit

Permalink
Include all stats when syncing campaign reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Oct 15, 2023
1 parent 0c635d5 commit 104403d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Disabled validation when saving elements in specific cases.
- Unsubscribed, complained and bounced counts are now included when syncing campaign reports.

## 2.9.0 - 2023-10-09

Expand Down
3 changes: 3 additions & 0 deletions src/services/ReportsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ public function sync(): void
$campaignRecord->recipients = $this->_getCampaignColumnCount($campaignRecord->id, 'sent');
$campaignRecord->opened = $this->_getCampaignColumnCount($campaignRecord->id, 'opened');
$campaignRecord->clicked = $this->_getCampaignColumnCount($campaignRecord->id, 'clicked');
$campaignRecord->unsubscribed = $this->_getCampaignColumnCount($campaignRecord->id, 'unsubscribed');
$campaignRecord->complained = $this->_getCampaignColumnCount($campaignRecord->id, 'complained');
$campaignRecord->bounced = $this->_getCampaignColumnCount($campaignRecord->id, 'bounced');
$campaignRecord->opens = $this->_getCampaignColumnSum($campaignRecord->id, 'opens');
$campaignRecord->clicks = $this->_getCampaignColumnSum($campaignRecord->id, 'clicks');
$campaignRecord->save();
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/services/CampaignsServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* @copyright Copyright (c) PutYourLightsOn
*/

namespace putyourlightson\campaigntests\unit\services;

use putyourlightson\campaign\Campaign;
use putyourlightson\campaign\elements\CampaignElement;
use putyourlightson\campaign\elements\ContactElement;
use putyourlightson\campaign\elements\SendoutElement;
use putyourlightson\campaign\records\ContactCampaignRecord;
use putyourlightson\campaigntests\fixtures\CampaignsFixture;
use putyourlightson\campaigntests\fixtures\ContactsFixture;
use putyourlightson\campaigntests\fixtures\SendoutsFixture;
use putyourlightson\campaigntests\unit\BaseUnitTest;

/**
* @since 2.9.1
*/
class CampaignsServiceTest extends BaseUnitTest
{
public function _fixtures(): array
{
return [
'campaigns' => [
'class' => CampaignsFixture::class,
],
'contacts' => [
'class' => ContactsFixture::class,
],
'sendouts' => [
'class' => SendoutsFixture::class,
],
];
}

protected CampaignElement $campaign;
protected ContactElement $contact;
protected SendoutElement $sendout;

protected function _before(): void
{
parent::_before();

$this->campaign = CampaignElement::find()->one();
$this->contact = ContactElement::find()->one();
$this->sendout = SendoutElement::find()->one();
}

public function testAddContactInteraction(): void
{
$contactCampaignRecord = new ContactCampaignRecord([
'campaignId' => $this->campaign->id,
'contactId' => $this->contact->id,
'sendoutId' => $this->sendout->id,
]);
$contactCampaignRecord->save();

Campaign::$plugin->campaigns->addContactInteraction($this->contact, $this->sendout, 'clicked');
Campaign::$plugin->campaigns->addContactInteraction($this->contact, $this->sendout, 'unsubscribed');
Campaign::$plugin->campaigns->addContactInteraction($this->contact, $this->sendout, 'bounced');

/** @var ContactCampaignRecord $contactCampaignRecord */
$contactCampaignRecord = ContactCampaignRecord::find()->one();

$this->assertNotNull($contactCampaignRecord->opened);
$this->assertNotNull($contactCampaignRecord->unsubscribed);
$this->assertNotNull($contactCampaignRecord->bounced);
$this->assertEquals(1, $contactCampaignRecord->opens);
$this->assertEquals(1, $contactCampaignRecord->clicks);

$campaign = CampaignElement::find()->one();

$this->assertNotNull($campaign->opened);
$this->assertEquals(1, $campaign->opens);
$this->assertEquals(1, $campaign->clicks);
$this->assertEquals(1, $campaign->unsubscribed);
$this->assertEquals(1, $campaign->bounced);
}
}

0 comments on commit 104403d

Please sign in to comment.