1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EmailCampaigns\Jobs; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Queue\SerializesModels; |
7
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
8
|
|
|
use Spatie\EmailCampaigns\Models\Campaign; |
9
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
10
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
11
|
|
|
use Spatie\EmailCampaigns\Models\CampaignLink; |
12
|
|
|
use Spatie\EmailCampaigns\Events\CampaignStatisticsCalculated; |
13
|
|
|
|
14
|
|
|
class CalculateStatisticsJob implements ShouldQueue |
15
|
|
|
{ |
16
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
17
|
|
|
|
18
|
|
|
/** @var \Spatie\EmailCampaigns\Models\Campaign */ |
19
|
|
|
public $campaign; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
public $queue; |
23
|
|
|
|
24
|
|
|
public function __construct(Campaign $campaign) |
25
|
|
|
{ |
26
|
|
|
$this->campaign = $campaign; |
27
|
|
|
|
28
|
|
|
$this->queue = config('email-campaigns.perform_on_queue.calculate_statistics_job'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function handle() |
32
|
|
|
{ |
33
|
|
|
if ($this->campaign->sends()->count() > 0) { |
34
|
|
|
$this |
35
|
|
|
->calculateCampaignStatistics() |
36
|
|
|
->calculateLinkStatistics(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->campaign->update(['statistics_calculated_at' => now()]); |
40
|
|
|
|
41
|
|
|
event(new CampaignStatisticsCalculated($this->campaign)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function calculateCampaignStatistics() |
45
|
|
|
{ |
46
|
|
|
$sendToNumberOfSubscribers = $this->campaign->sends()->count(); |
47
|
|
|
$openCount = $this->campaign->opens()->count(); |
48
|
|
|
$uniqueOpenCount = $this->campaign->opens()->groupBy('email_list_subscriber_id')->toBase()->getCountForPagination(['email_list_subscriber_id']); |
49
|
|
|
|
50
|
|
|
$openRate = round($uniqueOpenCount / $sendToNumberOfSubscribers, 2) * 100; |
51
|
|
|
|
52
|
|
|
$clickCount = $this->campaign->clicks()->count(); |
53
|
|
|
$uniqueClickCount = $this->campaign->clicks()->groupBy('email_list_subscriber_id')->toBase()->getCountForPagination(['email_list_subscriber_id']); |
54
|
|
|
$clickRate = round($uniqueClickCount / $sendToNumberOfSubscribers, 2) * 100; |
55
|
|
|
|
56
|
|
|
$unsubscribeCount = $this->campaign->unsubscribes()->count(); |
57
|
|
|
$unsubscribeRate = round($unsubscribeCount / $sendToNumberOfSubscribers, 2) * 100; |
58
|
|
|
|
59
|
|
|
$bounceCount = $this->campaign->bounces()->count(); |
60
|
|
|
$bounceRate = round($bounceCount / $sendToNumberOfSubscribers, 2) * 100; |
61
|
|
|
|
62
|
|
|
$this->campaign->update([ |
63
|
|
|
'sent_to_number_of_subscribers' => $sendToNumberOfSubscribers, |
64
|
|
|
'open_count' => $openCount, |
65
|
|
|
'unique_open_count' => $uniqueOpenCount, |
66
|
|
|
'open_rate' => $openRate, |
67
|
|
|
'click_count' => $clickCount, |
68
|
|
|
'unique_click_count' => $uniqueClickCount, |
69
|
|
|
'click_rate' => $clickRate, |
70
|
|
|
'unsubscribe_count' => $unsubscribeCount, |
71
|
|
|
'unsubscribe_rate' => $unsubscribeRate, |
72
|
|
|
'bounce_count' => $bounceCount, |
73
|
|
|
'bounce_rate' => $bounceRate, |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function calculateLinkStatistics() |
80
|
|
|
{ |
81
|
|
|
$this->campaign->links->each(function (CampaignLink $link) { |
82
|
|
|
$link->update([ |
83
|
|
|
'click_count' => $link->clicks()->count(), |
84
|
|
|
'unique_click_count' => $link->clicks()->select('email_list_subscriber_id')->groupBy('email_list_subscriber_id')->toBase()->getCountForPagination(['email_list_subscriber_id']), |
85
|
|
|
]); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|