Tailoring for SEO


(Benny) #1

Hello,
How would you tailor a project using PushType so that SEO can be maximized? For example, customizing meta description and meta keyword tags for a given article or blog post? Would this involve modifying views/layouts/application.html.erb, as well? Thank you for this AWESOME cms! :smiley:
Cheers!


(Aaron Russell) #2

Good question. There’s no right or wrong thing to do, and no built in thing that PushType magically does, but there’s a few steps I tend to take.

1. Create a meta structure

I usually define a reusable structure so I can store page meta titles and descriptions:

rails g push_type:structure meta title:string description:string

This gives us a small class which can be used as a field in all public facing nodes.

# app/models/metum.rb
class Metum < PushType::Structure
  field :title, :string
  field :description, :string
end

# reuse in all public facing nodes, eg:
class Page < PushType::Node
  field :meta, :structure
  # ... other fields
end

2. Throw in some helper methods

These aren’t built in to PushType, but I normally define something like this in every project.

# app/helpers/meta_helper.rb
module MetaHelper
  # Store the title content string
  def title(str)
    content_for :title, str
  end

  # Store meta content as an HTML meta tag
  def meta(name, content)
    content_for(:meta) { tag :meta, name: name.to_s, content: content }
  end

  # We try to use the meta title, but by default
  # fall back to the node title
  def meta_title(node)
    (node.try(:meta).try(:title) || node.title).html_safe
  end
end

3. Make use of these helpers in our templates/views

In our Node template/views:

# app/views/page.html.erb
<% title meta_title(@node) %>
<% meta :description, @node.meta.description.html_safe if @node.try(:meta).try(:description).present? %>

# we can add as many meta tags as our content requires. eg:
<% meta 'fb:app_id', 'MY_FACEBOOK_APP_ID' %>
<% meta 'og:site_name', 'My Site Name' %>
<% meta 'og:title', @node.title %>
<% meta 'og:url', node_url(@node) %>
<% meta 'og:type', 'article' %>

Then in our application layout:

<title><%= [yield(:title), 'My Site Name'].compact.join(' - ') %></title>
<%= yield :meta %>

4. Throw in a site map

Site maps are pretty easy. For brevity I won’t share the full xml builder code, but if you start with the following, with a couple of helpers you can iterate through the hash tree and map it into a XML sitemap pretty easily.

# config/routes.rb
get 'sitemap.xml' => 'sitemaps#show', format: 'xml', as: 'sitemap'

# app/controllers/sitemaps_controller.rb
def show
  @nodes = PushType::Node.published.exposed.hash_tree

  respond_to do |format|
    format.xml
  end
end

(Benny) #3

Thank you @aaron :sunglasses: I’m keen to implement your recommendations on my project! Really appreciate the thorough, and helpful, response! :smiley: