helpscout: use replies_sent to compute contact rate
The `conversations_count` we previously used counts replies, but also all conversations that were tagged or re-tagged during the month – with made counting the actual work spent on user support brittle. Counting the replies is a better estimation of what we get. Unfortunately this also removes the filtering-by-tag feature. To mitigate this, the reports are now scoped to a specific mailbox. This allows to create extra mailboxes for conversations that should't be counted in the stats.
This commit is contained in:
parent
52226804d0
commit
503c393a87
6 changed files with 57 additions and 62 deletions
|
@ -26,7 +26,7 @@ class Helpscout::API
|
|||
body = {
|
||||
subject: subject,
|
||||
customer: customer(email),
|
||||
mailboxId: mailbox_id,
|
||||
mailboxId: user_support_mailbox_id,
|
||||
type: 'email',
|
||||
status: 'active',
|
||||
threads: [
|
||||
|
@ -44,7 +44,7 @@ class Helpscout::API
|
|||
|
||||
def add_phone_number(email, phone)
|
||||
query = URI.encode("(email:#{email})")
|
||||
response = call_api(:get, "#{CUSTOMERS}?mailbox=#{mailbox_id}&query=#{query}")
|
||||
response = call_api(:get, "#{CUSTOMERS}?mailbox=#{user_support_mailbox_id}&query=#{query}")
|
||||
if response.success?
|
||||
body = parse_response_body(response)
|
||||
if body[:page][:totalElements] > 0
|
||||
|
@ -57,17 +57,18 @@ class Helpscout::API
|
|||
end
|
||||
end
|
||||
|
||||
def conversations_report(year, month)
|
||||
Rails.logger.info("[HelpScout API] Retrieving conversations report for #{month}-#{year}…")
|
||||
def productivity_report(year, month)
|
||||
Rails.logger.info("[HelpScout API] Retrieving productivity report for #{month}-#{year}…")
|
||||
|
||||
params = {
|
||||
mailboxes: [user_support_mailbox_id].join(','),
|
||||
start: Time.utc(year, month).iso8601,
|
||||
end: Time.utc(year, month).next_month.iso8601
|
||||
}
|
||||
|
||||
response = call_api(:get, 'reports/conversations?' + params.to_query)
|
||||
response = call_api(:get, 'reports/productivity?' + params.to_query)
|
||||
if !response.success?
|
||||
raise StandardError, "Error while fetching conversation report: #{response.response_code} '#{response.body}'"
|
||||
raise StandardError, "Error while fetching productivity report: #{response.response_code} '#{response.body}'"
|
||||
end
|
||||
|
||||
parse_response_body(response)
|
||||
|
@ -107,7 +108,7 @@ class Helpscout::API
|
|||
end
|
||||
|
||||
def fetch_custom_fields
|
||||
call_api(:get, "#{MAILBOXES}/#{mailbox_id}/#{FIELDS}")
|
||||
call_api(:get, "#{MAILBOXES}/#{user_support_mailbox_id}/#{FIELDS}")
|
||||
end
|
||||
|
||||
def call_api(method, path, body = nil)
|
||||
|
@ -135,7 +136,7 @@ class Helpscout::API
|
|||
JSON.parse(response.body, symbolize_names: true)
|
||||
end
|
||||
|
||||
def mailbox_id
|
||||
def user_support_mailbox_id
|
||||
Rails.application.secrets.helpscout[:mailbox_id]
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
# Fetch and compute monthly reports about the users conversations on Helpscout
|
||||
class Helpscout::UserConversationsAdapter
|
||||
EXCLUDED_TAGS = ['openlab', 'bizdev', 'admin', 'webinaire']
|
||||
|
||||
def initialize(from, to)
|
||||
@from = from
|
||||
@to = to
|
||||
|
@ -23,19 +21,12 @@ class Helpscout::UserConversationsAdapter
|
|||
private
|
||||
|
||||
def report(year, month)
|
||||
report = fetch_conversations_report(year, month)
|
||||
|
||||
total_conversations = report.dig(:current, :totalConversations)
|
||||
excluded_conversations = report
|
||||
.dig(:tags, :top)
|
||||
.select { |tag| tag[:name]&.in?(EXCLUDED_TAGS) }
|
||||
.map { |tag| tag[:count] }
|
||||
.sum
|
||||
report = fetch_productivity_report(year, month)
|
||||
|
||||
{
|
||||
start_date: report.dig(:current, :startDate).to_datetime,
|
||||
end_date: report.dig(:current, :endDate).to_datetime,
|
||||
conversations_count: total_conversations - excluded_conversations
|
||||
start_date: report.dig(:current, :startDate).to_datetime,
|
||||
end_date: report.dig(:current, :endDate).to_datetime,
|
||||
replies_sent: report.dig(:current, :repliesSent)
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -43,13 +34,13 @@ class Helpscout::UserConversationsAdapter
|
|||
@api_client ||= Helpscout::API.new
|
||||
end
|
||||
|
||||
def fetch_conversations_report(year, month)
|
||||
def fetch_productivity_report(year, month)
|
||||
if year == Date.today.year && month == Date.today.month
|
||||
raise ArgumentError, 'The report for the current month will change in the future, and cannot be cached.'
|
||||
end
|
||||
|
||||
Rails.cache.fetch("helpscout-conversation-report-#{year}-#{month}") do
|
||||
api_client.conversations_report(year, month)
|
||||
Rails.cache.fetch("helpscout-productivity-report-#{year}-#{month}") do
|
||||
api_client.productivity_report(year, month)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue