Skip to content

Commit

Permalink
Merge pull request #46 from TheDMSGroup/ENG-197-cost-stat
Browse files Browse the repository at this point in the history
[ENG-197] Only log source cost stat when accepted.
  • Loading branch information
heathdutton authored Apr 5, 2018
2 parents 9235853 + 43f1d33 commit 4410c17
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions Model/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Api
protected $scrubRate;

/** @var int */
protected $attribution = 0;
protected $cost = 0;

/** @var string */
protected $utmSource;
Expand Down Expand Up @@ -139,6 +139,9 @@ class Api
/** @var EntityManager */
protected $em;

/** @var int */
protected $attribution;

/**
* Api constructor.
*
Expand Down Expand Up @@ -299,11 +302,13 @@ private function parseSourceCampaignSettings()
);
}
// Establish parameters from campaign settings.
$this->realTime = (bool) isset($campaignSettings->realTime) && $campaignSettings->realTime;
$this->limits = isset($campaignSettings->limits) ? $campaignSettings->limits : [];
$this->scrubRate = isset($campaignSettings->scrubRate) ? intval($campaignSettings->scrubRate) : 0;
$this->attribution = isset($campaignSettings->cost) ? (abs(floatval($campaignSettings->cost)) * -1) : 0;
$this->utmSource = !empty($this->contactSource->getUtmSource()) ? $this->contactSource->getUtmSource() : null;
$this->realTime = (bool) isset($campaignSettings->realTime) && $campaignSettings->realTime;
$this->limits = isset($campaignSettings->limits) ? $campaignSettings->limits : [];
$this->scrubRate = isset($campaignSettings->scrubRate) ? intval($campaignSettings->scrubRate) : 0;
$this->cost = isset($campaignSettings->cost) ? (abs(floatval($campaignSettings->cost))) : 0;
$this->utmSource = !empty(
$this->contactSource->getUtmSource()
) ? $this->contactSource->getUtmSource() : null;
// Apply field overrides
if ($this->utmSource) {
$this->fieldsProvided['utm_source'] = $this->utmSource;
Expand Down Expand Up @@ -1015,7 +1020,7 @@ private function getCacheModel()
*/
private function saveContact()
{
$exception = null;
$exception = null;
$this->dispatchContextCreate();
try {
$this->getContactModel()->saveEntity($this->contact);
Expand Down Expand Up @@ -1056,7 +1061,7 @@ private function addContactToCampaign()
{
if ($this->contact->getId()) {
// Add the contact directly to the campaign without duplicate checking.
$this->addContactsToCampaign($this->campaign, [$this->contact], false, $this->realTime);
$this->addContactsToCampaign($this->campaign, [$this->contact], false);
}

return $this;
Expand All @@ -1068,15 +1073,13 @@ private function addContactToCampaign()
* @param Campaign $campaign
* @param array $contacts
* @param bool $manuallyAdded
* @param bool $realTime
*
* @throws \Exception
*/
public function addContactsToCampaign(
Campaign $campaign,
$contacts = [],
$manuallyAdded = false,
$realTime = false
$manuallyAdded = false
) {
foreach ($contacts as $contact) {
$campaignContact = new CampaignContact();
Expand Down Expand Up @@ -1203,15 +1206,15 @@ private function processRealTime()
*/
private function applyAttribution()
{
if (0 == $this->attribution) {
if (0 == $this->cost) {
return;
}

if (Stat::TYPE_ACCEPTED === $this->status) {
$this->contact = $this->getContactModel()->getEntity($this->contact->getId());
$originalAttribution = $this->contact->getAttribution();
// Attribution is always a negative number to represent cost.
$newAttribution = $originalAttribution + $this->attribution;
$newAttribution = $originalAttribution + ($this->cost * -1);
if ($newAttribution != $originalAttribution) {
$this->contact->addUpdatedField(
'attribution',
Expand All @@ -1220,9 +1223,7 @@ private function applyAttribution()
$this->dispatchContextCreate();
$this->getContactModel()->saveEntity($this->contact);
}
} else {
// Since we did NOT accept the lead, invalidate attribution.
$this->attribution = 0;
$this->attribution = ($this->cost * -1);
}
}

Expand Down Expand Up @@ -1250,12 +1251,10 @@ private function logResults()
$sourceModel = $this->container->get('mautic.contactsource.model.contactsource');

if ($this->valid) {
$statLevel = 'INFO';
$message = 'Contact '.$this->contact->getId(
$message = 'Contact '.$this->contact->getId(
).' was imported successfully from Campaign: '.$this->campaign->getname();
} else {
$statLevel = 'ERROR';
$message = isset($this->errors) ? implode(PHP_EOL, $this->errors) : '';
$message = isset($this->errors) ? implode(PHP_EOL, $this->errors) : '';
if ($this->eventErrors) {
$message = implode(PHP_EOL.' ', $this->eventErrors);
}
Expand Down

0 comments on commit 4410c17

Please sign in to comment.