OGメタタグ(商品・記事・ページ別)
商品ページ、ブログ記事、その他ページの種類に応じて、Facebook / Twitter などの SNS で正しく共有されるための OG メタタグを自動生成する。商品は画像・価格・在庫、記事はアイキャッチ画像、その他ページはロゴを出し分ける。
コード
40 行 / liquid{%- comment -%}
You have to change the logo asset on line 32 & 33.
{%- endcomment -%}
{%- if template contains 'product' -%}
<meta property="og:type" content="product">
<meta property="og:title" content="{{ product.title | strip_html | escape }}">
<meta property="og:category" content="{{ product.type }}" />
{%- for image in product.images limit:3 -%}
<meta property="og:image" content="http:{{ image.src | product_img_url: 'master' }}">
<meta property="og:image:secure_url" content="https:{{ image.src | product_img_url: 'master' }}">
{%- endfor -%}
<meta property="og:price:amount" content="{{ product.price | money_without_currency | stip_html | escape | remove: ',' }}">
<meta property="og:price:currency" content="{{ shop.currency }}">
<meta property="og:availability" content="{%- if product.available -%}instock{%- else -%}oos{%- endif -%}" />
<meta property="og:description" content="{{ product.description | strip_newlines | strip_html | truncate: 300 | escape }}">
{%- elsif template contains 'article' -%}
<meta property="og:type" content="article">
<meta property="og:title" content="{{ article.title | strip_html | escape }}">
{%- assign img_tag = '<' | append: 'img' -%}
{%- if article.content contains img_tag -%}
{%- assign src = article.content | split: 'src="' -%}
{%- assign src = src[1] | split: '"' | first | remove: 'https:' | remove: 'http:' -%}
{%- if src -%}
<meta property="og:image" content="http:{{ src }}">
<meta property="og:image:secure_url" content="https:{{ src }}">
{%- endif -%}
{%- endif -%}
{%- else -%}
<meta property="og:type" content="website">
<meta property="og:title" content="{{ page_title | escape }}">
<meta property="og:image" content="http:{{ 'logo.png' | asset_url }}">
<meta property="og:image:secure_url" content="https:{{ 'logo.png' | asset_url }}">
{%- endif -%}
{%- if page_description and template != 'product' -%}
<meta property="og:description" content="{{ page_description | escape }}">
{%- endif -%}
<meta property="og:url" content="{{ canonical_url }}">
<meta property="og:site_name" content="{{ shop.name }}">
出典・ライセンス
- Repository:
- https://github.com/pgrimaud/shopify-snippets
- License:
- MIT
このコードは pgrimaud 著作の MIT ライセンスソースです。 原本の著作権は pgrimaud が保有します。日本語訳は ALSEL によるものです。
関連項目
サイト検索のWebSite構造化データ
ストアの検索機能を schema.org の WebSite / SearchAction 型として JSON-LD で埋め込む。Google 検索ボックス機能をサポートし、検索結果ページのURL形式を指定する。
ストア情報のOrganization構造化データ
ストア全体の基本情報(URL、ロゴ、SNS リンク)を schema.org の Organization 型 JSON-LD として埋め込む。Google 検索結果やナレッジパネルにストア情報を表示させるための構造化データ。
Twitter Card メタタグ
商品ページ・ブログ記事・その他ページに対応した Twitter Card メタタグを <head> に出力するスニペット。ページタイプごとに card 型を切り替え、価格・在庫・ブランドなどを表示。
ページのOGP・Twitterカードメタタグ
ページタイトル、説明、画像、URL などから Open Graph Protocol と Twitter Card のメタタグを自動生成する。商品ページでは価格情報も追加される。
Organization・WebSite・Product・Article構造化データ
ストア全体と個別ページ(商品・記事)の構造化データを schema.org 形式の JSON-LD で出力するスニペット。Organization タイプでショップ情報とソーシャルリンク、WebSite タイプでサイト検索、Product タイプで商品情報(価格・在庫・SKU・GS1 バーコード)、Article タイプでブログ記事メタデータを Google 検索のリッチリザルト表示に対応させる。
商品ページのProduct構造化データ
商品詳細ページに schema.org の Product 型 JSON-LD を埋め込み、Google 検索のリッチリザルト(価格・在庫・バリアント)に対応させる。SKU・バーコード(GTIN12/13/14)・ブランド・説明文も含め、検索結果での表示品質を高める。