[deploy] Remove Google Analytics page counts (#6969)

* Remove Google Analytics page counts

* Update spec

Co-authored-by: rhymes <rhymesete@gmail.com>
Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
This commit is contained in:
Michael Kohl 2020-04-14 21:44:05 +07:00 committed by GitHub
parent 4f68e1907e
commit c8845e647a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1 additions and 121 deletions

View file

@ -121,7 +121,6 @@ variable :HONEYCOMB_API_KEY, :String, default: ""
# Google analytic
# (https://developers.google.com/analytics/devguides/reporting/core/v4)
variable :GA_SERVICE_ACCOUNT_JSON, :String, default: "Optional"
variable :GA_TRACKING_ID, :String, default: "Optional"
# Mailchimp for mails

View file

@ -48,7 +48,6 @@ gem "fog-aws", "~> 3.6" # 'fog' gem to support Amazon Web Services
gem "front_matter_parser", "~> 0.2" # Parse a front matter from syntactically correct strings or files
gem "gemoji", "~> 4.0.0.rc2" # Character information and metadata for standard and custom emoji
gem "gibbon", "~> 3.3" # API wrapper for MailChimp's API
gem "google-api-client", "~> 0.37" # Client for accessing Google APIs
gem "honeybadger", "~> 4.6" # Used for tracking application errors
gem "honeycomb-beeline", "~> 2.0.0" # Monitoring and Observability gem
gem "html_truncator", "~> 0.4" # Truncate an HTML string properly

View file

@ -210,8 +210,6 @@ GEM
ddtrace (0.34.1)
msgpack
debug_inspector (0.0.3)
declarative (0.0.10)
declarative-option (0.1.0)
derailed_benchmarks (1.7.0)
benchmark-ips (~> 2)
get_process_mem (~> 0)
@ -355,21 +353,6 @@ GEM
multi_json (>= 1.11.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
google-api-client (0.37.3)
addressable (~> 2.5, >= 2.5.1)
googleauth (~> 0.9)
httpclient (>= 2.8.1, < 3.0)
mini_mime (~> 1.0)
representable (~> 3.0)
retriable (>= 2.0, < 4.0)
signet (~> 0.12)
googleauth (0.11.0)
faraday (>= 0.17.3, < 2.0)
jwt (>= 1.4, < 3.0)
memoist (~> 0.16)
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
signet (~> 0.12)
guard (2.16.2)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
@ -472,7 +455,6 @@ GEM
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
memoist (0.16.2)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
memory_profiler (0.9.14)
@ -529,7 +511,6 @@ GEM
omniauth-oauth (~> 1.1)
rack
orm_adapter (0.5.0)
os (1.1.0)
parallel (1.19.1)
parallel_tests (2.32.0)
parallel
@ -636,10 +617,6 @@ GEM
redis-store (1.8.1)
redis (>= 4, < 5)
regexp_parser (1.7.0)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
request_store (1.5.0)
rack (>= 1.4)
responders (3.0.0)
@ -650,7 +627,6 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
retriable (3.1.2)
reverse_markdown (2.0.0)
nokogiri
rexml (3.2.4)
@ -815,7 +791,6 @@ GEM
simple_oauth (~> 0.3.0)
tzinfo (1.2.7)
thread_safe (~> 0.1)
uber (0.1.0)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
ulid (1.2.0)
@ -924,7 +899,6 @@ DEPENDENCIES
front_matter_parser (~> 0.2)
gemoji (~> 4.0.0.rc2)
gibbon (~> 3.3)
google-api-client (~> 0.37)
guard (~> 2.16)
guard-livereload (~> 2.5)
guard-rspec (~> 4.7)

View file

@ -1,85 +0,0 @@
require "google/apis/analyticsreporting_v4"
require "googleauth"
require "ostruct"
class GoogleAnalytics
include Google::Apis::AnalyticsreportingV4
include Google::Auth
def initialize(article_ids = [], user_id = "base")
@article_ids = article_ids
@user_id = user_id.to_s
@client = AnalyticsReportingService.new
@client.authorization = create_service_account_credential
@start_date = "201#{rand(5..8)}-0#{rand(1..8)}-0#{rand(1..9)}"
end
def get_pageviews
requests = @article_ids.map do |id|
article = Article.find_by(id: id)
make_report_request("ga:pagePath=@#{article.slug}", "ga:pageviews")
end
pageviews = fetch_all_results(requests)
@article_ids.zip(pageviews).to_h
end
def get_feed_impression_info
requests = @article_ids.map do |id|
make_report_request("ga:eventAction==featured-feed-impression;ga:eventLabel==articles-#{id}",
"ga:totalEvents")
end
results = fetch_all_results(requests)
@article_ids.zip(results).to_h
end
def get_feed_click_info
requests = @article_ids.map do |id|
make_report_request("ga:eventAction==featured-feed-click;ga:eventLabel==articles-#{id}",
"ga:totalEvents")
end
results = fetch_all_results(requests)
@article_ids.zip(results).to_h
end
private
def fetch_all_results(requests)
results = []
i = 0
while i < requests.length
done_request = fetch_analytics_for(*requests[i..i + 4])
results.concat(done_request)
i += 5
end
results
end
def make_report_request(filters_expression_string, metrics_string)
ReportRequest.new(
view_id: SiteConfig.ga_view_id,
filters_expression: filters_expression_string,
metrics: [Metric.new(expression: metrics_string)],
dimensions: [Dimension.new(name: "ga:segment")],
segments: [Segment.new(segment_id: "gaid::-1"),
Segment.new(segment_id: "gaid::-2"),
Segment.new(segment_id: "gaid::-19"),
Segment.new(segment_id: "gaid::-7")],
date_ranges: [DateRange.new(start_date: @start_date, end_date: "2020-01-01")],
)
end
def fetch_analytics_for(*report_requests)
grr = GetReportsRequest.new(report_requests: report_requests, quota_user: @user_id.to_s)
response = @client.batch_get_reports(grr)
response.reports.map do |report|
(report.data.maximums || report.data.totals)[0].values[0]
end
end
def create_service_account_credential
ServiceAccountCredentials.make_creds(
json_key_io: OpenStruct.new(read: ApplicationConfig["GA_SERVICE_ACCOUNT_JSON"]),
scope: [AUTH_ANALYTICS_READONLY],
)
end
end

View file

@ -21,15 +21,10 @@ module Articles
attr_reader :user
def fetch_and_update_page_views_and_reaction_counts(qualified_articles)
qualified_articles.each_slice(15).to_a.each do |chunk|
pageviews = GoogleAnalytics.new(chunk.pluck(:id), user.id).get_pageviews
page_views_obj = pageviews.to_h
qualified_articles.each_slice(15) do |chunk|
chunk.each do |article|
article.update_columns(previous_positive_reactions_count: article.positive_reactions_count)
Notification.send_milestone_notification(type: "Reaction", article_id: article.id)
next if article.page_views_count > page_views_obj[article.id].to_i
article.update_columns(page_views_count: page_views_obj[article.id].to_i)
Notification.send_milestone_notification(type: "View", article_id: article.id)
end
end

View file

@ -7,7 +7,6 @@ RSpec.describe Articles::AnalyticsUpdater, type: :service do
before do
srand(2) # disabling #occasionally_force_fetch
allow(Notification).to receive(:send_milestone_notification)
allow(GoogleAnalytics).to receive(:new).and_return(stubbed_ga)
end
describe "#call" do
@ -42,7 +41,6 @@ RSpec.describe Articles::AnalyticsUpdater, type: :service do
it "updates appropriate column" do
expect(article).to have_received(:update_columns).with(previous_positive_reactions_count: article.positive_reactions_count)
expect(article).to have_received(:update_columns).with(page_views_count: counts)
end
end
end