Include dynamically generated content in email messages

Fetch dynamic content from an RSS/XML/JSON content feed

If you have a content feed with news stories or other forms of content such as individual product recommendations or time-sensitive data, you can make this available in your messages when you enable the Fetch Dynamic Content option from the Setup tab of the campaign builder.

📘

Can't see this option?

The Fetch Dynamic Content option not enabled by default in accounts. If you wish to use this feature please contact us.

This functionality is often used to generate a daily news digest email, for instance in industries such as publishing, where content refreshes frequently; or travel, where flight price volatility can make once-per-day data too unreliable.

Other use cases for this feature are to add blocks of personalised content to other email types, for example, to show personalised product recommendations in a weekly newsletter.

Configure the content source

Once you click on Fetch Dynamic Content, a new page section is presented to set the source and the format of the content, and to add more sources if required (Add Source).

2204

There are two format options: XML or JSON. If you are working with RSS/XML choose XML and if you are working with a JSON feed choose JSON.

Once you have selected the format, the next step is to enter a URL which will return the desired content from your feed. You should add filtration options to your URL so you only return the minimum data required.

👍

Namespace

To avoid the possibility of the imported data conflicting with existing data in Xtremepush, we recommend setting the Namespace value to a simple word so that you can easily identify this data when referencing it in your content. Avoid dots and spaces.

Using a unique namespace value also helps avoid any potential conflict if you are importing more than one source.

Examples of working with JSON and RSS sources are provided in the following sections.

User attributes can be used in the source URL as described in this guide, so for example, if you have a recommender feed that can return recommended products based on a customer ID then you could use the following syntax:

https://recommenderfeed.com/reccs?per_page=5&page=1&user_id={{user.id}}

Working with a JSON content source

Before adding your JSON source URL, test it to see that it returns only the required data. If, for example, we wanted to create a newsletter from the first five articles of the Xtremepush blog using the title, link, excerpt and featured media from each blog post, we would construct the URL as follows:

https://xtremepush.com/wp-json/wp/v2/posts?per_page=5&page=1&_envelope&_fields=title,link,excerpt,_fields=title,link,featured_media

In the above content feed URL we have added parameters to only return the first five articles and the four data fields we need. Next, we will look at the format of the returned data so we can use it in an email.

{
  "body": [
    {
      "link": "https://xtremepush.com/customer-data-warfare-how-banks-can-create-better-experiences-and-fire-back-against-the-challengers/",
      "title": {
        "rendered": "Customer-Data Warfare: How banks can create better experiences and fire back against the challengers"
      },
      "excerpt": {
        "rendered": "<p>At a recent European Digital Banking Summit, our CTO Kevin Collins told an anecdote from his personal experience that underlines the threat to incumbent banks posed by the challengers. As he was travelling so often to overseas conferences and events, he had set up a Revolut account (free foreign exchange transactions were a compelling draw [&hellip;]<\/p>\n",
        "protected": false
      },
      "featured_media": 20735
    },
    {
      "link": "https://xtremepush.com/why-banks-are-in-danger-of-becoming-irrelevant-and-what-they-can-do-about-it/",
      "title": {
        "rendered": "Why banks are in danger of becoming irrelevant. (And what they can do about it.)"
      },
      "excerpt": {
        "rendered": "<p>Traditional banks are at a crossroads. The digital disruption that we\u2019ve seen sweep across other verticals, from ride-hailing in transport to streaming in media, has finally come for the banking industry.&nbsp; Trust in the established banks has been eroded over the past decade, though work has been done to repair their public image. Of greater [&hellip;]<\/p>\n",
        "protected": false
      },
      "featured_media": 20710
    },

...

Our data is returned as an array of objects so we can use this in our email. The array of objects is called body so we can loop through this array using a for loop like below:

//To loop through all items
{% for item in body %}
//use data from items in content
{% endfor %}

//To get the first item
{% for item in body[0:1] %}
//use data from items in content
{% endfor %}

//And then the next four
{% for item in body[1:4] %}
//use data from items in content
{% endfor %}

In the email editor the loops have been added by using HTML blocks.

1950

The easiest way to add a loop around a row of content is to select the row and add the loop by using the Display condition option in the settings pane (click on Add condition). Name the condition and add the for loop using the before and after inputs. Click on Confirm once you are done.

1022
2838

We can edit or delete the display condition for the row once it has been created.

Inside the row you can then add references to variable data that will be pulled in from the content feed. In our example we are accessing data inside the loop by referencing the following variables:

{{item.title.rendered}}

{{item.excerpt.rendered|striptags|slice(0,200)}}...

As you can see above you can use other programmatic options such as striptags, slice, capitalize etc., to perform other actions on the returned data.

If you want to use an image URL returned in your data you can place a reference to the URL from the data feed in the Dynamic Url option of an image content block.

820

Dynamic URL pulled from the JSON content.

If all the fields are configured correctly you would see something like this:

940

Working with an RSS content source

If working with RRS, set the source format type to XML. Before adding your RSS source URL first test it to see that it is returning only the required data. If, for example, we wanted to create a newsletter including the first five articles from a news feed, you might have a URL with some filtration parameters like below:

https://www.news.com/feeds/rss/?index=/news/&limit=5/first=0/img-width=600

And if you view this source content you should see something like this:

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/shared/bsp/xsl/rss/nolsol.xsl"?>

<rss

 xmlns:dc="http://purl.org/dc/elements/1.1/"

 xmlns:content="http://purl.org/rss/1.0/modules/content/"

 xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"

 xmlns:media="http://search.yahoo.com/mrss/">

 <channel>

 <title>

 <![CDATA[BBC News - Home]]>

 </title>

 <description>

 <![CDATA[BBC News - Home]]>

 </description>

 <link>https://www.bbc.co.uk/news/</link>

 <image>

 <url>https://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif</url>

 <title>BBC News - Home</title>

 <link>https://www.bbc.co.uk/news/</link>

 </image>

 <generator>RSS for Node</generator>

 <lastBuildDate>Mon, 09 Dec 2019 11:34:52 GMT</lastBuildDate>

 <copyright>

 <![CDATA[Copyright: (C) British Broadcasting Corporation, see http://news.bbc.co.uk/2/hi/help/rss/4498287.stm for terms and conditions of reuse.]]>

 </copyright>

 <language>

 <![CDATA[en-gb]]>

 </language>

 <ttl>15</ttl>

 <item>

 <title>

 <![CDATA[New Zealand volcano: At least five dead after White Island eruption]]>

 </title>

 <description>

 <![CDATA[About 50 visitors were near White Island volcano, with some walking inside the rim, when it erupted.]]>

 </description>

 <link>https://www.bbc.co.uk/news/world-asia-50708727</link>

 <guid isPermaLink="true">https://www.bbc.co.uk/news/world-asia-50708727</guid>

 <pubDate>Mon, 09 Dec 2019 11:33:27 GMT</pubDate>

 </item>

 <item>

 <title>

 <![CDATA[Russia banned for four years to include 2020 Olympics and 2022 World Cup]]>

 </title>

 <description>

 <![CDATA[Russia is handed a four-year ban from all major sporting events - including the Tokyo 2020 Olympics - by the World Anti-Doping Agency.]]>

 </description>

 <link>https://www.bbc.co.uk/sport/olympics/50710598</link>

 <guid isPermaLink="true">https://www.bbc.co.uk/sport/olympics/50710598</guid>

 <pubDate>Mon, 09 Dec 2019 11:26:04 GMT</pubDate>

 </item>

 <item>

 <title>

 <![CDATA[General election 2019: Boris Johnson's Brexit plan 'presents major challenge']]>

 </title>

 <description>

 <![CDATA[A leaked government document says customs plans for Northern Ireland may not be ready in time.]]>

 </description>

 <link>https://www.bbc.co.uk/news/uk-politics-50711868</link>

 <guid isPermaLink="true">https://www.bbc.co.uk/news/uk-politics-50711868</guid>

 <pubDate>Mon, 09 Dec 2019 11:14:04 GMT</pubDate>

 </item>

Similar to the JSON example, our data is returned as an array of objects. The format is just a little different but we can use this as follows in our email. The array of objects is found in rss.channel.item so we can loop through this array using a for loop as in this example:

//To loop through all items
{% for item in rss.channel.item %}
//use data from items in content
{% endfor %}

//To get the first item
{% for item in rss.channel.item[0:1] %}
//use data from items in content
{% endfor %}

//And then the next four
{% for item in rss.channel.item[1:4] %}
//use data from items in content
{% endfor %}

Once you are referencing the data correctly you can use the data shown in the JSON example above.

Troubleshoot issues

👍

Testing, testing...

When preparing a dynamically generated email, it is important to try multiple test sends to make sure that data is populating correctly.

If there are any issues you can navigate to Campaigns > Notification Log > Options menu > Details to drill into this campaign and review any error messages.

In our example below, the array was incorrectly referenced as JSON instead of body in the for loop. The Individual Messages section of the Notifications log has an error: the Variable ''json" does not exist. You could then scroll down in the Message section where the data pulled in is stored in a field called params, near the bottom of the message, and see what the dynamically pulled content data actually looks like and then correct the error.

2780