Create Interactive Emails with AMP and Marketing Cloud | Salesforce Developers Blog

Most of the everyday emails you may get as a consumer are the same. They’re mostly static information that won’t let you interact, and will send you to a website to do so. By implementing Accelerated Mobile Pages (AMP) in your emails, you can change this for your customers. This blog post will introduce you to the basics of AMP and how to implement data-driven, interactive email experiences in Marketing Cloud.

AMP

AMP, in a nutshell, is a web component framework designed for mobile B2C end-user experiences. It was developed by Google and is now part of the OpenJS foundation. At this point, it’s more than likely that you have experienced AMP from the user-land perspective. For example, when you search Google for your favorite location or newspaper, you might experience AMP. The following screenshot shows such a search result. The lightning symbol in the top right corner indicates that this content is served via AMP.

You can learn more about AMP in general, as well as its usage scenarios on the AMP project site. For this blog post though, we’ll look at the ability to use AMP, and here the subset AMP Email, within your Marketing Cloud emails.

Interactive emails with AMP

Let’s take a look at an interactive email example in the following GIF. Envision that you ordered a new bike, and received the usual confirmation email. With AMP components, this email is no longer static. The email provides an interactive experience for scheduling a setup appointment. You won’t have to leave the email to select the date, time, your nearest location, or to complete the booking itself.

This is just one example of how you can leverage AMP components in email. See more examples here. The next GIF shows another example, where the user can switch between different images in a carousel — right from within their email.

Let’s take a look at what it takes to build such a carousel experience.

Implementing interactive email frontends

As AMP components are web components, they are used like standard HTML markup. The following two code snippets show the markup to provide such a carousel experience as shown previously.

When you look at the head markup in the first code block, you’ll see that:

  • The first action is to load the core framework.
  • Then you load the AMP component(s) asynchronously. In our case, it’s one component, amp-carousel.
  • Within the style tag you can then define any custom styling for the used AMP components.
<!doctype html>
<html amp4email data-css-strict lang="en">
<head>
  ...
  <meta charset="utf-8">
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
  <style amp4email-boilerplate>body{visibility:hidden}</style>
  <style amp-custom>
    ...
    amp-carousel .slide{
      display:grid;
      transition: transform 0.2s
    }
    ...
  </style>
</head>
<body>
   ...
</body>
</html>

In the second code block, we’ll take a look at the markup to integrate an AMP component into the HTML body. And no worries, it’s markup only.

  • The amp-carousel gets integrated like every other HTML tag. The tag attributes are specific to the carousel and will vary for other AMP components.
  • Within the amp-carousel tag you can use any HTML, like we used to implement the links and the images. Note, the images are also AMP components, as you can identify them via the amp-img tag. You don’t have to load these separately in the HTML header though, as they ship as part of the base AMP framework.
!doctype html>
<html amp4email data-css-strict lang="en">
<head>
  ...
</head>
<body>
  ...
    <div>
      <amp-carousel width="700" height="450" layout="responsive" type="slides" autoplay loop>
        ...
        <a href="https://ntoretail.com/shop/mens">
          <h2><span>Mens</span></h2>
          <amp-img src="https://s3.amazonaws.com/nto/images/home-mens.png" width="450" height="300" layout="responsive" alt="Mens wear"></amp-img>
        </a>
        <a href="https://ntoretail.com/shop/womens">
          <h2><span>Womens</span></h2>
          <amp-img src="https://s3.amazonaws.com/nto/images/home-womens.png" width="450" height="300" layout="responsive" alt="Womens wear"></amp-img>
        </a>
        ...
      </amp-carousel>
    </div>
  ...
</body>
</html>

That’s pretty much it on how to build the front-end experience for interactive emails using AMP. But wait, what needs to be done when you want to drive a dynamic experience at the time of email open? This is what we’ll look at next.

Providing dynamic data using CloudPage code resources

A core capability of Marketing Cloud is to provide programmatic marketing content using server-side JavaScript via Code Resources. And some AMP components, namely amp-form and amp-list, provide the capability to consume data on email open from a data source like a Code Resource. This is a win-win.

The following abbreviated code example shows what is needed to build a JSON Code Resource for AMP components. The key requirement for your data is that you construct it within a single array, and return that array as the value for the object key items.

<script runat="server" language="JavaScript">

  ...  
  
  // For returning data, AMP requires the data to be returned in an object with a single array called items.
  var obj = {"items": [
    { "key": "value" }
  ]};
  
  // In order for the Code Resource to render the values of that array you must Stringify and Write the value to render it.
  Write(Stringify(obj));
  
  ...
  
</script>

That’s it. Well, not completely. There are a couple more things to consider, like adding security measures to your server-side JavaScript. You can find an exhaustive example on the AMP Demos GitHub repo.

Your next steps

AMP Email is an exciting addition to the Marketing Cloud. It builds upon standard web development principles, and provides out-of-the-box interactivity for your emails. To get started, we prepared a brand new demo app with lots of source code for you — which you can find here on the Sample Gallery.

To bring this to production, register at a supported email provider, and submit a sender verification form:

Give it a try, and experience the move from static to engaging email experiences.

About the author

René Winkelmeyer works as Architect, Developer Relations, at Salesforce. He focuses on enterprise integrations, JavaScript, node, and all the other cool stuff that you can do with Salesforce technologies. You can follow him on Twitter @muenzpraeger or on GitHub @muenzpraeger.

Stay up to date with the latest news from the Salesforce Developers Blog

Subscribe