Add user quota to Google Analytics fetch (#765)
* Add page_views_count to articles and track from analytics * Modification in google analytics fetch * Chunk article requests in analytics requests * Fix test * Fix test in analytics * Add user quota to Google Analytics fetch
This commit is contained in:
parent
e8b05b3a41
commit
dffd0218e1
2 changed files with 12 additions and 5 deletions
|
|
@ -6,8 +6,9 @@ class GoogleAnalytics
|
|||
include Google::Apis::AnalyticsreportingV4
|
||||
include Google::Auth
|
||||
|
||||
def initialize(article_ids = [])
|
||||
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
|
||||
end
|
||||
|
|
@ -42,7 +43,7 @@ class GoogleAnalytics
|
|||
end
|
||||
|
||||
def fetch_analytics_for(*report_requests)
|
||||
grr = GetReportsRequest.new(report_requests: 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.totals[0].values[0]
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class AnalyticsController < ApplicationController
|
|||
def get_articles_that_qualify(articles_to_check)
|
||||
qualified_articles = []
|
||||
articles_to_check.each do |article|
|
||||
if article.positive_reactions_count > article.previous_positive_reactions_count
|
||||
if should_fetch(article)
|
||||
qualified_articles << article
|
||||
end
|
||||
end
|
||||
|
|
@ -27,11 +27,11 @@ class AnalyticsController < ApplicationController
|
|||
|
||||
def fetch_and_update_page_views_and_reaction_counts(qualified_articles)
|
||||
qualified_articles.each_slice(25).to_a.each do |chunk|
|
||||
pageviews = GoogleAnalytics.new(chunk.pluck(:id)).get_pageviews
|
||||
pageviews = GoogleAnalytics.new(chunk.pluck(:id), current_user.id).get_pageviews
|
||||
page_views_obj = pageviews.to_h
|
||||
chunk.each do |article|
|
||||
article.update_columns(page_views_count: page_views_obj[article.id].to_i,
|
||||
previous_positive_reactions_count: article.positive_reactions_count)
|
||||
previous_positive_reactions_count: article.positive_reactions_count)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -43,6 +43,12 @@ class AnalyticsController < ApplicationController
|
|||
render json: finalized_object.to_json
|
||||
end
|
||||
|
||||
def should_fetch(article)
|
||||
new_reactions = (article.positive_reactions_count > article.previous_positive_reactions_count)
|
||||
random = (rand(80) == 1)
|
||||
new_reactions || random
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def analytics_params
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue