This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Reduce duplication across engagement responses

  • Loading branch information...
Pawel Badenski
Pawel Badenski committed Jan 23, 2013
1 parent 98efb08 commit bed7dbfaca7b658bd34caaf37a15de7393e1d341
@@ -0,0 +1,23 @@
+module ContentEngagementMagic
+ ENTRY_LABEL = "Entry"
+ SUCCESS_LABEL = "Success"
+
+ def collect_engagement_by_key(rows)
+ weeks = { }
+ rows.each do |key, action, value|
+ weeks[key] ||= [0, 0]
+ if action == ENTRY_LABEL
+ weeks[key][0] += value.to_i
+ elsif action == SUCCESS_LABEL
+ weeks[key][1] += value.to_i
+ else
+ logger.warn { "Unrecognized action '#{action}' for key '#{key}'" }
+ end
+ end
+ weeks.map(&:flatten)
+ end
+
+ def normalize_format(format, category_prefix)
+ format.gsub(/^#{category_prefix}/, '')
+ end
+end
@@ -1,9 +1,11 @@
require_relative "weekly_response"
require_relative "extract_weekly_dates"
+require_relative "content_engagement_magic"
module GoogleAnalytics
class WeeklyContentEngagementDetailResponse < BaseResponse
include ExtractWeeklyDates
+ include ContentEngagementMagic
def initialize(response, config_class)
@site = config_class::SITE_KEY
@@ -12,46 +14,32 @@ def initialize(response, config_class)
end
private
- ENTRY_LABEL = "Entry"
- SUCCESS_LABEL = "Success"
def create_all_messages(response)
start_date = response.first["query"]["start-date"]
end_date = response.first["query"]["end-date"]
- rows = response.flat_map { |r| r["rows"] }
+ rows = response.flat_map { |r| r["rows"] || [] }
create_messages(rows, start_date, end_date)
end
def create_messages(rows, start_date, end_date)
- collect_by_slug_and_format(rows).map do |(slug, format, entries, successes)|
+ collect_engagement_by_key(
+ rows.map { |_, format, slug, action, value|
+ [[slug, format], action, value]
+ }
+ ).map do |(slug, format, entries, successes)|
create_message({
start_at: extract_start_at(start_date),
end_at: extract_end_at(end_date),
value: {
site: @site,
- format: format,
+ format: normalize_format(format, @config::CATEGORY_PREFIX),
entries: entries.to_i,
successes: successes.to_i,
slug: slug
}
})
end
end
-
- # returns [ [slug, format, entries, successes], ... ]
- def collect_by_slug_and_format(rows)
- weeks = {}
- rows.each do |(_, format, slug, action, value)|
- weeks[[slug, format]] ||= [0, 0]
- if action == ENTRY_LABEL
- weeks[[slug, format]][0] += value.to_i
- elsif action == SUCCESS_LABEL
- weeks[[slug, format]][1] += value.to_i
- else
- logger.warn { "Unrecognized action '#{action}' for format '#{format}'" }
- end
- end
- weeks.map(&:flatten)
- end
end
end
@@ -1,9 +1,11 @@
require_relative "weekly_response"
require_relative "extract_weekly_dates"
+require_relative "content_engagement_magic"
module GoogleAnalytics
class WeeklyContentEngagementResponse < BaseResponse
include ExtractWeeklyDates
+ include ContentEngagementMagic
def initialize(response, config_class)
@site = config_class::SITE_KEY
@@ -12,43 +14,28 @@ def initialize(response, config_class)
end
private
- ENTRY_LABEL = "Entry"
- SUCCESS_LABEL = "Success"
def create_messages(response)
- rows = response.reduce([]) { |accumulator, item| accumulator + (item["rows"] || []) }
+ start_date = response.first["query"]["start-date"]
+ end_date = response.first["query"]["end-date"]
+ rows = response.flat_map { |r| r["rows"] || [] }
- collect_by_format(rows).map do |(format, entries, successes)|
+ collect_engagement_by_key(
+ rows.map { |_, format, action, value|
+ [[format], action, value]
+ }).map do |(format, entries, successes)|
create_message ({
- :start_at => extract_start_at(response.first["query"]["start-date"]),
- :end_at => extract_end_at(response.first["query"]["end-date"]),
+ :start_at => extract_start_at(start_date),
+ :end_at => extract_end_at(end_date),
:value => {
:site => @site,
- :format => normalize_format(format),
+ :format => normalize_format(format, @config::CATEGORY_PREFIX),
:entries => entries,
:successes => successes
}
})
end
end
-
- def collect_by_format(rows)
- weeks = {}
- rows.each do |(_, format, action, value)|
- weeks[format] ||= [0, 0]
- if action == ENTRY_LABEL
- weeks[format][0] += value.to_i
- elsif action == SUCCESS_LABEL
- weeks[format][1] += value.to_i
- else
- logger.warn { "Unrecognized action '#{action}' for format '#{format}'" }
- end
- end
- weeks.map(&:flatten)
- end
-
- def normalize_format(format)
- format.gsub(/^#{@config::CATEGORY_PREFIX}/, '')
- end
end
-end
+end
+
@@ -30,15 +30,15 @@
)
response[0].should have_payload_value(
"site" => "govuk",
- "format" => "MS_answer",
+ "format" => "answer",
"entries" => 193,
"successes" => 179,
"slug" => "accepting-returns-and-giving-refunds"
)
response[1].should have_payload_value(
"site" => "govuk",
- "format" => "MS_answer",
+ "format" => "answer",
"entries" => 101,
"successes" => 71,
"slug" => "acoustic-neuroma-and-driving"
@@ -47,7 +47,7 @@
.map { |each| JSON.parse(each) }
.group_by { |each| each["payload"]["value"]["format"] }
- grouped_by_format.should have_key("MS_answer")
- grouped_by_format.should have_key("MS_guide")
+ grouped_by_format.should have_key("answer")
+ grouped_by_format.should have_key("guide")
end
end
View
@@ -16,4 +16,5 @@ def load_data(filename)
class DummyConfig
METRIC = "ga:dummy"
SITE_KEY = "govuk"
+ CATEGORY_PREFIX = ""
end

0 comments on commit bed7dbf

Please sign in to comment.