Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Last active March 22, 2021 17:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save resistorsoftware/1216871 to your computer and use it in GitHub Desktop.
Save resistorsoftware/1216871 to your computer and use it in GitHub Desktop.
Enhanced Meta Tagger snippet
{% comment %}
- add this snippet to any product.liquid template, at the top of the file, and you can access the extra data provided by
the new version of Meta Tagger that supports Google Shopping.
{% endcomment %}
{% if template contains 'product' %}
{% if product.metafields.global %}
{% for mf in product.metafields.global %}
{% case mf.first %}
{% when 'MPN' %}
{% assign MPN = mf.last %}
{% when 'UPC' %}
{% assign UPC = mf.last %}
{% when 'JAN' %}
{% assign JAN = mf.last %}
{% when 'EAN' %}
{% assign EAN = mf.last %}
{% when 'ISBN' %}
{% assign ISBN = mf.last %}
{% endcase %}
{% endfor %}
{% endif %}
{% if product.metafields.google %}
{% for mf in product.metafields.google %}
{% case mf.first %}
{% when 'age_group' %}
{% assign age_group = mf.last %}
{% when 'gender' %}
{% assign gender = mf.last %}
{% when 'google_product_type' %}
{% assign google_product_type = mf.last %}
{% endcase %}
{% endfor %}
{% endif %}
{% endif %}
{% comment %}
- now you can use any of these tags... simple Liquid...
eg:
<span itemprop="identifier" content="mpn:{{MPN}}">MPN</span>
<span itemprop="identifier" content="upc:{{UPC}}">UPC</span>
<span itemprop="identifier" content="ISBN:{{ISBN}}">ISBN</span>
{% endcomment %}
<link rel="canonical" href="{{ canonical_url | split: "?" | first}}" />
{%- assign maxmeta = 155 -%}
{%- if template contains 'product' -%}
{%- assign mf = product.metafields.meta_data -%}
{%- unless mf == empty -%}
{%- for mf in product.metafields.meta_data -%}
{%- capture key -%}{{ mf | first }}{%- endcapture -%}
{%- if key == 'title' -%}
<title>{{mf | last}}</title>
{%- endif -%}
{%- if key == 'meta_description' -%}
<meta name="description" content="{{mf | last}}" />
{%- endif -%}
{%- if key == 'keywords' -%}
<meta name="keywords" content="{{mf | last}}" />
{%- endif -%}
{%- endfor -%}
{%- else -%}
<title>{{ page_title | escape }}</title>
<meta name="description" content="{{product.description | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{%- endunless -%}
{%- elsif template contains 'page' -%}
{%- assign mf = page.metafields.meta_data -%}
{%- unless mf == empty -%}
{%- for mf in page.metafields.meta_data -%}
{%- capture key -%}{{ mf | first }}{%- endcapture -%}
{%- if key == 'title' -%}
<title>{{mf | last}}</title>
{%- endif -%}
{%- if key == 'meta_description' -%}
<meta name="description" content="{{mf | last}}" />
{%- endif -%}
{%- if key == 'keywords' -%}
<meta name="keywords" content="{{mf | last}}" />
{%- endif -%}
{%- endfor -%}
{%- else -%}
<title>{{ page_title | escape }}</title>
<meta name="description" content="{{page.content | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{%- endunless -%}
{%- elsif template contains 'collection' -%}
{%- assign mf = collection.metafields.meta_data -%}
{%- unless mf == empty or mf == nil -%}
{%- for mf in collection.metafields.meta_data -%}
{%- capture key -%}{{ mf | first }}{%- endcapture -%}
{%- if key == 'title' -%}
<title>{{mf | last}} | Page {{current_page }}</title>
{%- endif -%}
{%- if key == 'meta_description' -%}
<meta name="description" content="{{mf | last}}" />
{%- endif -%}
{%- if key == 'keywords' -%}
<meta name="keywords" content="{{mf | last}}" />
{%- endif -%}
{%- endfor -%}
{%- else -%}
<title>{{ shop.name }} - {{ page_title | escape }}</title>
<meta name="description" content="{{collection.description | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{%- endunless -%}
{%- elsif template contains 'index' -%}
<title>{{ shop.name }} - Welcome</title>
{%- if shop.description != '' -%}
<meta name="description" content="{{shop.description | escape }}" />
{%- endif -%}
{%- elsif template contains 'blog' -%}
{%- assign mf = blog.metafields.meta_data -%}
{%- unless mf == empty -%}
{%- for mf in blog.metafields.meta_data -%}
{%- capture key -%}{{ mf | first }}{%- endcapture -%}
{%- if key == 'title' -%}
<title>{{mf | last}}</title>
{%- endif -%}
{%- if key == 'meta_description' -%}
<meta name="description" content="{{mf | last}}" />
{%- endif -%}
{%- if key == 'keywords' -%}
<meta name="keywords" content="{{mf | last}}" />
{%- endif -%}
{%- endfor -%}
{%- else -%}
<title>{{ page_title | escape }} {{ shop.name }}</title>
<meta name="description" content="{{ shop.name }} {{ blog.title | escape }} blog" />
{%- endunless -%}
{%- elsif template contains 'article' -%}
{%- assign mf = article.metafields.meta_data -%}
{%- unless mf == empty -%}
{%- for mf in article.metafields.meta_data -%}
{%- capture key -%}{{ mf | first }}{%- endcapture -%}
{%- if key == 'title' -%}
<title>{{mf | last}}</title>
{%- endif -%}
{%- if key == 'meta_description' -%}
<meta name="description" content="{{mf | last}}" />
{%- endif -%}
{%- if key == 'keywords' -%}
<meta name="keywords" content="{{mf | last}}" />
{%- endif -%}
{%- endfor -%}
{%- else -%}
<title>{{ page_title | escape }}</title>
<meta name="description" content=" {{ article.excerpt_or_content | strip_html | strip_newlines | escape | truncate: maxmeta }}" />
{%- endunless -%}
{%- else -%}
<title>{{ shop.name }} - Welcome {%- if template == "404" -%} ==> Page Not Found <== {%- endif -%}</title>
{%- if shop.description != '' -%}
<meta name="description" content="{{shop.description | escape }}" />
{%- endif -%}
{%- endif -%}
{% comment %} this is useful as Shopify ensures canonical just works {% endcomment %}
<link rel="canonical" href="{{ canonical_url }}" />
{% comment %} this is simple a limit for when we blindly dump Shopify data for SEO, the old hail mary pass {% endcomment %}
{% assign maxmeta = 155 %}
{% comment %} PRODUCT SEO!!! {% endcomment %}
{% if template contains 'product' %}
{% comment %} Okay, does this Product have any meta_data from the App {% endcomment %}
{% assign mf = product.metafields.meta_data %}
{% unless mf == empty %}
{% comment %} Meta Tagger was used for this Product {% endcomment %}
{% for mf in product.metafields.meta_data %}
{% capture key %}{{ mf | first }}{% endcapture %}
{% comment %} Looking for the title {% endcomment %}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% comment %} Looking for the description {% endcomment %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% comment %} Looking for the keywords {% endcomment %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
{% comment %} Meta Tagger was NOT used, dump out some Hail Mary defaults!! {% endcomment %}
<title>{% if page_title != '' %}{{ page_title | escape }}{% else %}{{product.title}}{% endif %}</title>
<meta name="description" content="{{product.description | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{% endunless %}
{% comment %} PAGE SEO!!! {% endcomment %}
{% elsif template contains 'page' %}
{% comment %} Does this Page have any meta_data from the App {% endcomment %}
{% assign mf = page.metafields.meta_data %}
{% unless mf == empty %}
{% comment %} Meta Tagger was used for this Page {% endcomment %}
{% for mf in page.metafields.meta_data %}
{% capture key%}{{ mf | first }}{%endcapture%}
{% comment %} Looking for the title {% endcomment %}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% comment %} Looking for the description {% endcomment %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% comment %} Looking for the keywords {% endcomment %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
{% comment %} Meta Tagger was NOT used, dump out some Hail Mary defaults for the Page! {% endcomment %}
<title>{% if page_title != '' %}{{ page_title | escape }}{% else %}{{page.title}}{% endif %}</title>
<meta name="description" content="{{page.content | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{% endunless %}
{% comment %} COLLECTION SEO!!! {% endcomment %}
{% elsif template contains 'collection' %}
{% comment %} Does this Collection have any meta_data from the App {% endcomment %}
{% assign mf = collection.metafields.meta_data %}
{% unless mf == empty or mf == nil %}
{% comment %} Meta Tagger was used for this Collection {% endcomment %}
{% for mf in collection.metafields.meta_data %}
{% capture key%}{{ mf | first }}{%endcapture%}
{% comment %} Looking for the title {% endcomment %}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% comment %} Looking for the description {% endcomment %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% comment %} Looking for the keywords {% endcomment %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
{% comment %} Meta Tagger was NOT used, dump out some Hail Mary defaults for the Collection! {% endcomment %}
<title>{{ shop.name }} - {% if page_title != '' %}{{ page_title | escape }}{% else %}{{collection.title}}{% endif %}</title>
<meta name="description" content="{{collection.description | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{% endunless %}
{% comment %}
Your Shop HOMEPAGE SEO!!! This is probably what you are interested in... too
{% endcomment %}
{% elsif template contains 'index' %}
{% comment %}
The *** TITLE ***
- Yes, this default is totally boring. Usually you can merge something clever from your Shop's Theme into here
- Some themes use a setting in the SEO, like settings.title. Others make up some dumb connection with pages.
- Just paste in whatever you want.. it is not like you change this everyday...
{% endcomment %}
<title>{{ shop.name }} - Welcome</title>
{% comment %} This comes from your Shop Preferences. Use this wisely and well. {% endcomment %}
{% if shop.description != '' %}
<meta name="description" content="{{shop.description | escape }}" />
{% endif %}
{% comment %} If you have a Blog, this is the SEO that gets rendered. Customize as you wish {% endcomment %}
{% elsif template contains 'blog' %}
<title>{% if page_title != '' %}{{ page_title | escape }}{% else %}{{ blog.title }} {{ shop.name }}{% endif %}</title>
<meta name="description" content="{{ shop.name }} {{ blog.title | escape }} blog" />
{% comment %} If you have a Blog Article, this is the SEO that gets rendered. Customize as you wish {% endcomment %}
{% elsif template contains 'article' %}
<title>{% if page_title != '' %}{{ page_title | escape }}{% else %}{{ shop.name }} - {{ article.title }}{% endif %}</title>
<meta name="description" content=" {{ article.excerpt_or_content | strip_html | strip_newlines | escape | truncate: maxmeta }}" />
{% comment %}
The end of the line. This is the default stuff nothing else triggers. copy of your homepage SEO here maybe?
{% endcomment %}
{% else %}
<title>{{ shop.name }} - Welcome {% if template == "404" %} ==> Page Not Found <== {% endif %}</title>
{% if shop.description != '' %}
<meta name="description" content="{{shop.description | escape }}" />
{% endif %}
{% endif %}
<link rel="canonical" href="{{ canonical_url }}" />
{% assign maxmeta = 155 %}
{% if template contains 'product' %}
{% assign mf = product.metafields.meta_data %}
{% unless mf == empty %}
{% for mf in product.metafields.meta_data %}
{% capture key %}{{ mf | first }}{% endcapture %}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
{% capture productdesc %}{% include 'process-html-strip' with product.description %}{% endcapture %}
<title>{{ product.title }}{% if product.vendor != '' and settings.prod_show_vendor_meta %} by {{ product.vendor }}{% endif %} | {{ shop.name }}</title>
<meta name="description" content="{{ productdesc | strip_html | strip_newlines | truncatewords: maxwords | escape }}" />
{% endunless %}
{% elsif template contains 'page' %}
{% assign mf = page.metafields.meta_data %}
{% unless mf == empty %}
{% for mf in page.metafields.meta_data %}
{% capture key%}{{ mf | first }}{%endcapture%}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
<title>{{ page_title }} | {{ shop.name }}</title>
<meta name="description" content="{{ page.content | strip_html | strip_newlines | truncatewords: maxwords | escape }}" />
{% endunless %}
{% elsif template contains 'collection' %}
{% assign mf = collection.metafields.meta_data %}
{% unless mf == empty or mf == nil %}
{% for mf in collection.metafields.meta_data %}
{% capture key%}{{ mf | first }}{%endcapture%}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
<title>{{ shop.name }} - {{ page_title | escape }}</title>
<meta name="description" content="{{collection.description | strip_html | strip_newlines | truncate: maxmeta | escape}}" />
{% endunless %}
{% elsif template contains 'index' %}
{% if settings.seo_tagline != '' %}
<title>{{ settings.seo_tagline }} - {{ shop.name }}</title>
{% else %}
<title>{{ shop.name }}</title>
{% endif %}
{% if settings.shop_description != '' %}
<meta name="description" content="{{ settings.shop_description | escape }}" />
{% endif %}
{% elsif template contains 'blog' %}
{% assign mf = blog.metafields.meta_data %}
{% unless mf == empty %}
{% for mf in blog.metafields.meta_data %}
{% capture key %}{{ mf | first }}{% endcapture %}
{% if key == 'title' %}
<title>{{mf | last}}</title>
<meta name="title" content="{{mf | last}}" />
{% endif %}
{% if key == 'meta_description' %}
<meta name="description" content="{{mf | last}}" />
{% endif %}
{% if key == 'keywords' %}
<meta name="keywords" content="{{mf | last}}" />
{% endif %}
{% endfor %}
{% else %}
<title>{{ page_title | escape }} {{ shop.name }}</title>
<meta name="description" content="{{ shop.name }} {{ blog.title | escape }} blog" />
{% endunless %}
{% elsif template contains 'article' %}
<title>{{ page_title | escape }}</title>
<meta name="description" content=" {{ article.excerpt_or_content | strip_html | strip_newlines | escape | truncate: maxmeta }}" />
{% else %}
<title>{{ shop.name }} - Welcome {% if template == "404" %} ==> Page Not Found <== {% endif %}</title>
{% if shop.description != '' %}
<meta name="description" content="{{shop.description | escape }}" />
{% endif %}
{% endif %}
@MatthewKennedy
Copy link

There are several instances of {{ page_title }} that should be {{ page.title }}.

@resistorsoftware
Copy link
Author

resistorsoftware commented Feb 24, 2012 via email

@MatthewKennedy
Copy link

MatthewKennedy commented Feb 24, 2012 via email

@resistorsoftware
Copy link
Author

resistorsoftware commented Feb 24, 2012 via email

@MatthewKennedy
Copy link

Where in the Shopify admin do you set the custom title info? This could be where I went wrong, either way what you suggested above is fool proof I like it, I'll amend my file shortly.

@resistorsoftware
Copy link
Author

resistorsoftware commented Feb 24, 2012 via email

@MatthewKennedy
Copy link

Hi,

I was looking all over the wiki and Shopify cheat sheet for the variable to pull up that new page meta title, so thats page_title, haha so sorry I should of realised this!

I'll make an addition to my file to use the Shopify page meta like you suggested

if page_title != ''
page_title
else
resource.title
endif

Any other ideas for the file?

@resistorsoftware
Copy link
Author

resistorsoftware commented Feb 24, 2012 via email

@azedan
Copy link

azedan commented Mar 31, 2012

You forgot to include the blog and article templates. Based on the current code it displays the {{ shop.name }} - Welcome title.

@resistorsoftware
Copy link
Author

resistorsoftware commented Mar 31, 2012 via email

@azedan
Copy link

azedan commented Apr 1, 2012

I completely agree with you in principal. But if you're the developer of the Shopify app Meta-Tagger then you should review the code. If you apply the app to a shopify store and you happen to have a blog included in it, you get a whole series of duplicate title tags for your blog and articles. As you know, this is very bad for SEO. Thanks.

@resistorsoftware
Copy link
Author

resistorsoftware commented Apr 1, 2012 via email

@rickydazla
Copy link

Davo!

Towards the end of lines 8, 28, & 48 you have a stray apostrophe:
metafields.meta_data' <==

Ricko

@resistorsoftware
Copy link
Author

resistorsoftware commented Apr 10, 2012 via email

@rickydazla
Copy link

No worries. Out of interest - why the limiting number of characters on title and description?

@resistorsoftware
Copy link
Author

resistorsoftware commented Apr 10, 2012 via email

@rickydazla
Copy link

Dave ~ you have: {% if page_title != '' %} but page_title is never empty. It's annoying that you can't tell (I don't think) whether the page_title has been specifically set or is just e.g. product.title but the if-else you have now is irrelevant (I believe)...

@resistorsoftware
Copy link
Author

True enough.. page_title represents whatever is set, either the special SEO title of the resource, or the ordinary title of the resource, and that is set by the server, so it seems that is correct, it is never empty... changed the code to reflect that... will have to check it out... thanks

@aforonda
Copy link

Hi Dave,

I installed the Meta-Tagger App on our theme in Shopify and it is not showing in the admin dashboard.
Can you help me with this? Thanks

@abes13
Copy link

abes13 commented Dec 15, 2014

Dave,

Im in the same boat as aforonda. im not sure how to get started. when i click get started in the app it just brings me to the dashboard... then i discovered your email.. i went through the installation procedure and the app still doesnt come up. what do i do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment