News Article

Edit on Github
Open in Playground View Demo

Introduction

This is a sample template for a news article in AMP. It demonstrates the usage of AMP components which works well in news articles. Examples include social sharing, image galleries, personalized content, ads, and more.

Setup

All additionally used AMP components must be imported in the header.

<script async custom-element="amp-social-share" src="https://cdn.ampproject.org/v0/amp-social-share-0.1.js"></script>

Metadata

The Top Stories carousel requires schema.org markup for one of the following types: Article, NewsArticle, BlogPosting, or VideoObject. Learn more.

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"mainEntityOfPage": "https://ampbyexample.com/samples_templates/news_article/",
"headline": "Lorem Ipsum",
"datePublished": "2016-04-21T11:55:02Z",
"dateModified": "2016-04-21T11:55:02Z",
"description": "A sample news article build with AMP.",
"author": {
  "@type": "Person",
  "name": "Sebastian Benz"
},
"publisher": {
  "@type": "Organization",
  "name": "Google",
  "logo": {
    "@type": "ImageObject",
    "url": "http://cdn.ampproject.org/logo.jpg",
    "width": 600,
    "height": 60
  }
},
"image": {
  "@type": "ImageObject",
  "url": "https://ampbyexample.com/img/landscape_lake_1280x857.jpg",
  "height": 1280,
  "width": 857
}
}
</script>

Social Sharing

The Social Share extension provides a common interface for share buttons. Learn more about amp-social-share here.

Example

<p class="heading">
  <amp-social-share type="twitter"
    width="45"
    height="33"></amp-social-share>
  <amp-social-share type="facebook"
    width="45"
    height="33"
    data-attribution="254325784911610"></amp-social-share>
  <amp-social-share type="gplus"
    width="45"
    height="33"></amp-social-share>
  <amp-social-share type="email"
    width="45"
    height="33"></amp-social-share>
  <amp-social-share type="pinterest"
    width="45"
    height="33"></amp-social-share>
</p>

AD Support

There are no limitations regarding the number or placement of ads in an AMP. Learn more about amp-ad here.

Example

<div class="amp-ad-container">
  <amp-ad width="300"
    height="250"
    type="a9"
    data-aax_size="300x250"
    data-aax_pubname="test123"
    data-aax_src="302">
  </amp-ad>
</div>

Image Galleries

Use amp-carousel for image galleries. Learn more about creating image galleries in AMP here.

Example

Each image has a different caption.
This caption is different.
The third image has its caption.
<amp-carousel width="1280"
  height="1000"
  layout="responsive"
  type="slides">
  <figure>
    <amp-img src="/img/landscape_lake_1280x857.jpg"
      width="1280"
      height="857"
      layout="responsive"></amp-img>
    <figcaption>Each image has a different caption.</figcaption>
  </figure>
  <figure>
    <amp-img src="/img/landscape_village_1280x853.jpg"
      width="1280"
      height="853"
      layout="responsive"></amp-img>
    <figcaption>This caption is different.</figcaption>
  </figure>
  <figure>
    <amp-img src="/img/landscape_desert_1280x853.jpg"
      width="1280"
      height="853"
      layout="responsive"></amp-img>
    <figcaption>The third image has its caption.</figcaption>
  </figure>
</amp-carousel>

Interactive Content

To achieve its performance, AMP HTML doesn't allow custom Javascript. This doesn't mean that you can't created visually rich and interactive websites with AMP. Embed interactive content via amp-iframe. Learn more about amp-iframe here.

Example

An interactive chart - in AMP!
<figure>
  <amp-iframe src="https://www.google.us/trends/embed/US_cu_82I1CVMBAACV6M_en/horserace_chart_3c6cdb21-e1eb-4412-9c28-707c3638ea35?hl=en&template=fe"
    height="360"
    layout="fixed-height"
    frameborder="0"
    sandbox="allow-scripts allow-same-origin">
    <amp-img src="/img/oscars_placeholder_1.png"
      layout="fixed-height"
      height="360"
      width="auto"
      placeholder>
    </amp-img>
  </amp-iframe>
  <figcaption>An interactive chart - in AMP!</figcaption>
</figure>

It's a good idea to recommend related content to your readers. With amp-list it is possible to dynamically include personalized content into your AMPs. It's a good idea to link to the AMP version of related content in order to keep the user in the fast AMP world. Learn more about amp-list here

Example

<amp-list width="300"
  height="75"
  layout="responsive"
  src="/json/related_articles.json">
  <template type="amp-mustache">
    <a class="card related"
      href="{{url}}">
      <amp-img width="101"
        height="75"
        src="{{thumbnail}}"></amp-img>
      <span>{{title}}</span>
    </a>
  </template>
</amp-list>

Use amp-user-notification to implement a cookie consent form (if needed). By default, the AMP runtime doesn't use cookies. Some analytics vendors might use require cookies though. Learn more about amp-user-notification here

Example

This page might use cookies if your analytics vendor requires them.
<amp-user-notification layout="nodisplay"
  id="amp-user-notification1">
  This page might use cookies if your analytics vendor requires them.
  <button on="tap:amp-user-notification1.dismiss">Accept</button>
</amp-user-notification>

User Analytics

Analytics must be configured in the body. Here we use Google Analytics to track pageviews.

Example

<amp-analytics type="googleanalytics">
  <script type="application/json">
    {
      "vars": {
        "account": "UA-73836974-1"
      },
      "triggers": {
        "default pageview": {
          "on": "visible",
          "request": "pageview",
          "vars": {
            "title": "{{title}}"
          }
        }
      }
    }
  </script>
</amp-analytics>