Class: TransactionalEmailAnalyticsJob

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/transactional_email_analytics_job.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransactionalEmailAnalyticsJob

Returns a new instance of TransactionalEmailAnalyticsJob.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 8

def initialize
  unless FeatureFlipper.send_email?
    raise Common::Exceptions::ParameterMissing.new(
      'GovDelivery token or server',
      detail: 'It should be configured in settings.yml'
    )
  end
  if Settings.google_analytics.tracking_id.blank?
    raise Common::Exceptions::ParameterMissing.new(
      'Google Analytics tracking ID',
      detail: 'It should be configured in settings.yml'
    )
  end
  @tracker = Staccato.tracker(Settings.google_analytics.tracking_id)
  @time_range_start = 1445.minutes.ago
  @time_range_end = 5.minutes.ago
end

Class Method Details

.mailersObject

mailers descendant of TransactionalEmailMailer these are declared explicitly because ‘.descendants` doesn’t play well with zeitwerk autoloading



41
42
43
44
45
46
47
48
49
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 41

def self.mailers
  [
    StemApplicantConfirmationMailer,
    SchoolCertifyingOfficialsMailer,
    DirectDepositMailer,
    StemApplicantScoMailer,
    StemApplicantDenialMailer
  ]
end

Instance Method Details

#eval_email(email, mailer) ⇒ Object (private)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 86

def eval_email(email, mailer)
  email.failed.get
  event_params = {
    category: 'email',
    non_interactive: true,
    campaign_name: mailer::GA_CAMPAIGN_NAME,
    campaign_medium: 'email',
    campaign_source: 'gov-delivery',
    document_title: email.subject,
    document_path: mailer::GA_DOCUMENT_PATH,
    label: mailer::GA_LABEL
  }
  @tracker.event(event_params.merge(action: 'completed'))
  @tracker.event(event_params.merge(action: 'failed')) if email.failed.collection.count.positive?
end

#govdelivery_clientObject (private)



78
79
80
81
82
83
84
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 78

def govdelivery_client
  @govdelivery_client ||= GovDelivery::TMS::Client.new(
    Settings.govdelivery.token,
    api_root: "https://#{Settings.govdelivery.server}",
    logger: ENV['CI'] == 'true' ? nil : logger
  )
end

#performObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 26

def perform
  page = 0
  loop do
    page += 1
    relevant_emails(page).each do |mailer, emails|
      emails.each do |email|
        eval_email(email, mailer)
      end
    end
    break if we_should_break?
  end
end

#relevant_emails(page) ⇒ Object (private)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 57

def relevant_emails(page)
  @all_emails = govdelivery_client.email_messages.get(
    page:,
    sort_by: 'created_at',
    sort_order: 'DESC',
    page_size: 50
  )

  grouped_emails = TransactionalEmailAnalyticsJob.mailers.index_with { |_mailer| [] }

  @all_emails.collection.each do |email|
    created_at = Time.zone.parse(email.created_at)
    if created_at > @time_range_start && created_at <= @time_range_end && email.status == 'completed'
      TransactionalEmailMailer.descendants.each_with_object(grouped_emails) do |mailer, grouped|
        grouped[mailer] << email if email.subject == mailer::SUBJECT
      end
    end
  end
  grouped_emails
end

#we_should_break?Boolean (private)

Returns:

  • (Boolean)


53
54
55
# File 'app/sidekiq/transactional_email_analytics_job.rb', line 53

def we_should_break?
  Time.zone.parse(@all_emails.collection.last.created_at) < @time_range_start || @all_emails.collection.count < 50
end