From 91bd6322ad83cc26a3c9baa84d48a75fb0f39d0f Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 19 Mar 2019 09:32:26 -0400 Subject: [PATCH] Add organic page view sums and modify seo boostables (#2109) * Add organic page view sums and modify seo boostables * Add score minimum for SEO boostable --- app/models/article.rb | 11 ++-- .../articles/_sidebar_additional.html.erb | 4 +- .../tags/_sidebar_additional.html.erb | 4 +- ...3433_add_organic_page_views_to_articles.rb | 7 +++ db/schema.rb | 5 +- spec/models/article_spec.rb | 58 ++++++------------- 6 files changed, 37 insertions(+), 52 deletions(-) create mode 100644 db/migrate/20190318223433_add_organic_page_views_to_articles.rb diff --git a/app/models/article.rb b/app/models/article.rb index ab37b31d6..b9bcee439 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -365,16 +365,13 @@ class Article < ApplicationRecord end def self.seo_boostable(tag = nil) - keyword_paths = SearchKeyword. - where("google_position > ? AND google_position < ? AND google_volume > ? AND google_difficulty < ?", - 3, 20, 1000, 40).pluck(:google_result_path) if tag - Article.where(path: keyword_paths, published: true, featured: true). - tagged_with(tag). + Article.where(published: true). + cached_tagged_with(tag).order("organic_page_views_count DESC").limit(20).where("score > ?", 10). pluck(:path, :title, :comments_count, :created_at) - else - Article.where(path: keyword_paths, published: true, featured: true). + Article.where(published: true). + order("organic_page_views_count DESC").limit(20).where("score > ?", 10). pluck(:path, :title, :comments_count, :created_at) end end diff --git a/app/views/articles/_sidebar_additional.html.erb b/app/views/articles/_sidebar_additional.html.erb index beb6ac38e..712eac617 100644 --- a/app/views/articles/_sidebar_additional.html.erb +++ b/app/views/articles/_sidebar_additional.html.erb @@ -145,7 +145,7 @@ <% end %> <% else %> - <% cache("seo-boostable-posts-homepage", expires_in: 18.hours) do %> + <% cache("seo-boostable-posts-homepage-xoxo", expires_in: 18.hours) do %> <% @boostable_posts = Article.seo_boostable %> <% if @boostable_posts.any? %>
@@ -154,7 +154,7 @@
diff --git a/app/views/articles/tags/_sidebar_additional.html.erb b/app/views/articles/tags/_sidebar_additional.html.erb index 4befe7ffa..d5ee50630 100644 --- a/app/views/articles/tags/_sidebar_additional.html.erb +++ b/app/views/articles/tags/_sidebar_additional.html.erb @@ -20,7 +20,7 @@
">
<% else %> - <% cache("seo-boostable-posts-for-tag#{@tag}", expires_in: 18.hours) do %> + <% cache("seo-boostable-posts-for-tag-#{@tag}", expires_in: 18.hours) do %> <% @boostable_posts = Article.seo_boostable(@tag) %> <% if @boostable_posts.any? %>
@@ -29,7 +29,7 @@
diff --git a/db/migrate/20190318223433_add_organic_page_views_to_articles.rb b/db/migrate/20190318223433_add_organic_page_views_to_articles.rb new file mode 100644 index 000000000..26de9209f --- /dev/null +++ b/db/migrate/20190318223433_add_organic_page_views_to_articles.rb @@ -0,0 +1,7 @@ +class AddOrganicPageViewsToArticles < ActiveRecord::Migration[5.1] + def change + add_column :articles, :organic_page_views_count, :integer, default: 0 + add_column :articles, :organic_page_views_past_month_count, :integer, default: 0 + add_column :articles, :organic_page_views_past_week_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 843bf683c..71f2f5cc2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190315222044) do +ActiveRecord::Schema.define(version: 20190318223433) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -92,6 +92,9 @@ ActiveRecord::Schema.define(version: 20190315222044) do t.string "main_image_background_hex_color", default: "#dddddd" t.string "main_tag_name_for_social" t.string "name_within_collection" + t.integer "organic_page_views_count", default: 0 + t.integer "organic_page_views_past_month_count", default: 0 + t.integer "organic_page_views_past_week_count", default: 0 t.integer "organization_id" t.datetime "originally_published_at" t.integer "page_views_count", default: 0 diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index d21359dbb..79fa9327b 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -239,49 +239,27 @@ RSpec.describe Article, type: :model do end describe "queries" do - let(:search_keyword) do - create(:search_keyword, - google_result_path: article.path, - google_position: 8, - google_volume: 2000, - google_difficulty: 10) - end - - it "returns article plucked objects that match keyword query" do - search_keyword - article.update(featured: true) + it "returns articles ordered by organic_page_views_count" do + create(:article, score: 30) + create(:article, score: 30) + top_article = create(:article, organic_page_views_count: 20, score: 30) articles = Article.seo_boostable - expect(articles.flatten[0]).to eq(article.path) + expect(articles.first[0]).to eq(top_article.path) end - - it "returns keyword articles by tag" do - search_keyword - article.update(featured: true) - articles = Article.seo_boostable(article.tags.first) - expect(articles.flatten[0]).to eq(article.path) + it "returns articles ordered by organic_page_views_count by tag" do + create(:article, score: 30) + create(:article, organic_page_views_count: 30, score: 30) + top_article = create(:article, organic_page_views_count: 20, score: 30) + top_article.update_column(:cached_tag_list, "good, greatalicious") + articles = Article.seo_boostable("greatalicious") + expect(articles.first[0]).to eq(top_article.path) end - - it "does not return articles when none match based on tag" do - search_keyword - article.update(featured: true) - articles = Article.seo_boostable("woozle-wozzle-20000") - expect(articles.size).to eq(0) - end - - it "doesn't return keywords that don't match criteria plucked objects matching keyword query" do - search_keyword.update_attributes(google_position: 33) # too high of a position - articles = Article.seo_boostable - expect(articles.size).to eq(0) - end - - it "does not return unpublished articles" do - search_keyword - articles = Article.seo_boostable - article.update(published: false) - expect(articles.size).to eq(0) - end - it "returns empty relation if no articles match" do - articles = Article.seo_boostable + it "returns nothing if no tagged articles" do + create(:article, score: 30) + create(:article, organic_page_views_count: 30) + top_article = create(:article, organic_page_views_count: 20, score: 30) + top_article.update_column(:cached_tag_list, "good, greatalicious") + articles = Article.seo_boostable("godsdsdsdsgoo") expect(articles.size).to eq(0) end end