Poll

Edit on Github
Open in Playground View Demo

Experimental Mode

This example uses the following experimental feature: [amp-form-var-sub]. Enable the experiment via the button below. Some components require the AMP Dev Channel to be enabled as well. Learn more here.

Enable Dev Channel

Introduction

This is a sample template for a poll in AMP. After selecting one of the answers, you will see the percentage of chosen answers.

Setup

Import the additional AMP components. This sample uses a amp-form and mustache templates.

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

Poll

We use amp-form to implement a poll. To avoid multiple votes by the same user, we need to identify which users have already voted. We use a cookie called POLL_USER_ID to identify the user. Given that in Safari the default behavior is to block third-party cookies from sites that have not been previously visited, this approach may not work if the page is visited via the AMP Cache. In this case we fallback to checking for duplicate entries via the CLIENT_ID.

The CLIENT_ID is a unique identifier for the user when accessing a specific origin, for example the original page or the cached version of it. This means that the same user will generate two different values when accessing different origins. That's why we use a custom cookie to track cross origin access to the poll page.

The CLIENT_ID variable can be passed via an amp-form by declaring a hidden input value with default-value="CLIENT_ID(POLL_USER_ID). In our implementation, the backend sets POLL_USER_ID to the value of CLIENT_ID, but the value of this cookie is not relevant and you could re-use your own session id.

Read more about variable substitution. Currently, HTML tables are not supported in form responses, so we'll use CSS to layout the result data.

In order to submit the form as soon as the user picks an option, we will add an on attribute to each <input>, that will submit the poll on change.

Example

What is your favorite flightless bird?

Choose your favourite flightless bird and you will discover what other users have chosen. If you have already voted, your answer will be overwritten.

<form method="post"
  action-xhr="/samples_templates/poll/submit"
  target="_blank"
  id="poll1"
  custom-validation-reporting="as-you-go">
  <input name="clientId"
    type="hidden"
    value="CLIENT_ID(POLL_USER_ID)"
    data-amp-replace="CLIENT_ID">
  <fieldset class="m1 p1 ampstart-card">
    <h3 class="pb1">What is your favorite flightless bird?</h3>
    <p>Choose your favourite flightless bird and you will discover what other users have chosen. If you have already voted, your answer will be overwritten.</p>

    
    <div class="ampstart-input ampstart-input-radio block relative m0 p0 mt1 ">
      <input type="radio"
        value="0"
        name="answer"
        id="0"
        class="relative"
        on="change:poll1.submit">
      <label for="0"
        class="">Penguins</label>
    </div>
    
    <div class="ampstart-input ampstart-input-radio block relative m0 p0 mt1 ">
      <input type="radio"
        value="1"
        name="answer"
        id="1"
        class="relative"
        on="change:poll1.submit">
      <label for="1"
        class="">Ostriches</label>
    </div>
    
    <div class="ampstart-input ampstart-input-radio block relative m0 p0 mt1 ">
      <input type="radio"
        value="2"
        name="answer"
        id="2"
        class="relative"
        on="change:poll1.submit">
      <label for="2"
        class="">Kiwis</label>
    </div>
    
    <div class="ampstart-input ampstart-input-radio block relative m0 p0 mt1 ">
      <input type="radio"
        value="3"
        name="answer"
        id="3"
        class="relative"
        on="change:poll1.submit">
      <label for="3"
        class="">Wekas</label>
    </div>
    
  </fieldset>
  <div submit-success
    class="ml1">
    <template type="amp-mustache">
      <p class="pt1">{{Message}}</p>
      <p class="pt1">Here are the results:</p>
      <div class="ampstart-card mt1 mr1 p1">
        <span class="fixed-sixe-cell">Answer</span>
        <span class="fixed-size-cell">&nbsp;</span>
        <span>&nbsp;</span>
        <span class="fixed-size-cell votes-cell">Votes</span>
        {{#PollEntryResults}}
        <div class="mt1 relative">
          <span class="fixed-size-cell">
            {{Answer}}
          </span>
          <span class="fixed-size-cell">
            {{Percentage.length}}%
          </span>

          <span class="percentage-container-fixed">
            {{#Percentage}}
            <span class="one-pc-fixed"></span>{{/Percentage}}
          </span>

          <span class="fixed-size-cell votes-cell">
            {{Votes}}
          </span>
        </div>
        {{/PollEntryResults}}

      </div>
    </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">
      Error! Looks like something went wrong with your vote, please try to submit it again. {{error}}
    </template>
  </div>
</form>