Skip to content

Commit

Permalink
Adjust dashboard analytics to be more reliable (forem#1115)
Browse files Browse the repository at this point in the history
* Update analytics

* Remove analytics file

* Remove unused files

* Add delete button back in on dashboard
  • Loading branch information
benhalpern authored Nov 14, 2018
1 parent 652ec00 commit c580720
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 249 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/initializePage.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function callInitalizers(){
initNotifications();
initializeSplitTestTracking();
initializeStylesheetAppend();
initializeAnalytics();
initializeCommentDropdown();
initializeSettings();
initializeFooterMod();
Expand Down
32 changes: 0 additions & 32 deletions app/assets/javascripts/initializers/initializeAnalytics.js

This file was deleted.

69 changes: 38 additions & 31 deletions app/assets/stylesheets/dashboard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
max-width:96%;
margin:auto;
h1{
font-size:30px;
font-size:22px;
font-family: $helvetica-condensed;
font-stretch:condensed;
padding-bottom: 20px;
a{
padding: 0px 20px;
border-radius: 3px;
padding: 6px 25px;
border-radius: 100px;
margin-right: 18px;
display: inline-block;
color: $black;
&:hover{
background: lighten($light-medium-gray, 3%);
}
&.active{
background: $black;
background: $dark-gray;
color:white;
}
}
Expand Down Expand Up @@ -70,39 +73,49 @@
.dashboard-analytics-header-wrapper{
text-align: center;
margin-bottom: 28px;
background: $dark-gray;
box-shadow: $bold-shadow;
padding: 15px 0px;
}
.dashboard-analytics-header{
color:$black;
color: white;
margin:5px auto;
font-size:calc(0.77em + 0.22vw);
padding: 20px 0px;
font-size:calc(1.1em + 0.1vw);
padding: 18px 0px 7px;
padding-left: 10px;
text-align: left;
font-family: $monospace;
border-radius: 200px;
border: 1px solid $light-medium-gray;
box-shadow: $bold-shadow;
// border-radius: 200px;
border: 1px solid $light-gray;
// box-shadow: $bold-shadow;
display: inline-block;
width: 90%;
text-align: center;
margin: 1.1%;
@media screen and ( min-width: 800px ) {
width: 43%;
margin: 1.2%;
}
span{
opacity:0.3;
font-size: 1.8em;
font-weight: bold;
margin-left: 8px;
display: inline-block;
}
img{
width: calc(0.9em + 1.2vw);
height: calc(0.9em + 1.2vw);
margin-right: 0.4em;
margin-left: 1.1em;
vertical-align: calc(-0.6vw - 2px);
}
&.loaded{
span{
opacity:1;
}
width: calc(1.2em + 0.2vw);
height: calc(1.2em + 0.2vw);
margin-left: 0.4em;
vertical-align: calc(-0.2vw - 0.3em);
background: $sky-blue;
border-radius: 100px;
border: 2px solid darken($light-gray, 7%);
}
.dashboard-analytics-sub-indicator{
color: darken($light-gray, 7%);
padding: 10px 0px;
font-weight: bold;
font-size:0.66em;
}
}

Expand Down Expand Up @@ -152,7 +165,7 @@
color:rgb(67, 78, 87);
}
.dashboard-actions{
padding: 8px 0px;
padding: 5px 0px;
}
.pill{
background:$bold-blue;
Expand All @@ -173,22 +186,16 @@
background:$red;
}
&.black{
background: $black;
background: $dark-gray;
}
}
.dashboard-pageviews-indicator{
display:inline-block;
font-family: $monospace;
color: $black;
color: $dark-gray;
background: white;
text-align:center;
opacity:0.4;
height: 23px;
line-height: 22px;
padding: 2px 8px;
&.loaded{
opacity:1;
}
}
form{
padding: 10px 0px;
Expand Down Expand Up @@ -241,4 +248,4 @@
min-height: 350px;
}
}
}
}
4 changes: 1 addition & 3 deletions app/black_box/google_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ 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|
p report.data
p report.data
(report.data.maximums || report.data.totals)[0].values[0]
end
end
Expand All @@ -84,4 +82,4 @@ def create_service_account_credential
scope: [AUTH_ANALYTICS_READONLY],
)
end
end
end
58 changes: 0 additions & 58 deletions app/controllers/analytics_controller.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/controllers/async_info_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def base_data
remember_me(current_user)
end
@user = current_user.decorate
# Updates article analytics periodically:
ArticleAnalyticsFetcher.new.delay.update_analytics(@user.id) if rand(20) == 1
respond_to do |format|
format.json do
render json: {
Expand Down Expand Up @@ -60,4 +62,4 @@ def user_cache_key
#{current_user&.articles_count}__
#{cookies[:remember_user_token]}"
end
end
end
7 changes: 6 additions & 1 deletion app/controllers/dashboards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ def show
elsif params[:which] == "user_followers"
@follows = Follow.where(followable_id: @user.id, followable_type: "User").
includes(:follower).order("created_at DESC").limit(80)
elsif params[:which] == "organization_user_followers"
@follows = Follow.where(followable_id: @user.organization_id, followable_type: "Organization").
includes(:follower).order("created_at DESC").limit(80)
elsif @user&.organization && @user&.org_admin && params[:which] == "organization"
@articles = @user.organization.articles.order("created_at DESC").decorate
elsif @user
@articles = @user.articles.order("created_at DESC").decorate
end
# Updates analytics in background if appropriate:
ArticleAnalyticsFetcher.new.delay.update_analytics(current_user.id) if @articles
end
end
end
34 changes: 34 additions & 0 deletions app/labor/article_analytics_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ArticleAnalyticsFetcher
def update_analytics(user_id)
articles_to_check = Article.where(user_id: user_id, published: true)
qualified_articles = get_articles_that_qualify(articles_to_check)
return if qualified_articles.none?
fetch_and_update_page_views_and_reaction_counts(qualified_articles, user_id)
end

def fetch_and_update_page_views_and_reaction_counts(qualified_articles, user_id)
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
chunk.each do |article|
article.update_columns(previous_positive_reactions_count: article.positive_reactions_count)
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)
end
end
end

def get_articles_that_qualify(articles_to_check)
qualified_articles = []
articles_to_check.each do |article|
if should_fetch(article)
qualified_articles << article
end
end
qualified_articles
end

def should_fetch(article)
article.positive_reactions_count > article.previous_positive_reactions_count
end
end
18 changes: 18 additions & 0 deletions app/views/dashboards/_analytics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% num_views = @articles.sum(&:page_views_count) %>

<div class="dashboard-analytics-header-wrapper">
<div class="dashboard-analytics-header">
<span><%= number_with_delimiter(@articles.sum(&:positive_reactions_count), delimeter: ",") %></span>
<div class="dashboard-analytics-sub-indicator">
<img src="<%= asset_path "emoji/emoji-one-heart.png" %>" /><img src="<%= asset_path "emoji/emoji-one-unicorn.png" %>" /><img src="<%= asset_path "emoji/emoji-one-bookmark.png" %>" />
Total Post Reactions
</div>
</div>
<div class="dashboard-analytics-header" id="dashboard-analytics-header">
<span><%= num_views > 500 ? number_with_delimiter(num_views, delimeter: ",") : "under 500" %></span>
<div class="dashboard-analytics-sub-indicator">
<img id="dashboard-analytics-image" src="<%= asset_path "emoji/apple-eyes.png" %>" />
Total Post Views
</div>
</div>
</div>
22 changes: 22 additions & 0 deletions app/views/dashboards/_dashboard_article.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="single-article">
<a href="<%= article.current_state_path %>"><h2><%= article.title %></h2></a>
<div class="dashboard-actions">
<% if !article.published %>
<span class="pill cta yellow">DRAFT</span>
<% end %>
<a href="<%= article.path %>/edit" class="pill cta green">EDIT</a>
<a href="<%= article.path %>/delete_confirm" data-no-instant class="cta pill black">DELETE</a>
<% if article.published && current_user.can_view_analytics? %>
<span id="pageviews-<%= article.id %>" class="cta pill dashboard-pageviews-indicator" data-analytics-pageviews data-article-id="<%= article.id %>">
<%= article.page_views_count > 100 ? article.page_views_count : "under 100" %> views // <%= article.positive_reactions_count %> reactions
</span>
<% end %>
</div>
<% if org_admin %>
<%= form_for(article) do |f| %>
<input type="hidden" name="destination" value="/dashboard" />
AUTHOR: <%= f.select(:user_id, options_for_select(@user.organization.users.map {|x| [x.name, x.id] }, article.user_id )) %>
<%= f.submit "SUBMIT AUTHOR CHANGE", class:"cta pill black" %>
<% end %>
<% end %>
</div>
Loading

0 comments on commit c580720

Please sign in to comment.