If you have a content feed with news stories or other forms of content such as individual product recommendations you can make this available in your messages using the fetch dynamic content option of campaigns.
In publishing where content refreshes frequently enough this functionality is often used to to generate a daily news digest email. The dynamic content is configured and the mail is scheduled to send daily at a particular time. It can also be used for use cases where you want to add blocks of personalised content to other emails for example putting three personalised product recommendations in a weekly newsletter.
Configure your content source
Go to create campaign and select Fetch Dynamic Content. 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.
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. We will give an examples of working with JSON and RSS sources in the next sections.
User attributes can be used in the source url using our merge tag syntax so for example if you have a recommender feed that can return recommended products based on a customer id then you could do something like:
https://your.recommenderfeed.com/xp/v2/reccomendations?per_page=5&page=1&user_id={{user.id}}
Working with a JSON content source
Before adding your JSON source url first test the url to see that it is returning only the data you need. If for example we wanted to create a newsletter from the first five articles of the Xtremepush blog using; 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 params 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 […]<\/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. Trust in the established banks has been eroded over the past decade, though work has been done to repair their public image. Of greater […]<\/p>\n", "protected": false }, "featured_media": 20710 },
...
Our data is returned as an array of objects so we can use this as follows 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 you can see how we have added loops using HTML blocks:
The easiest way to add a loop around a row of content is to select the row. And add the loop using the add condition feature in the "Display condition" option in the settings pane to the right:
Inside the row you can then add references to variable data that will be pulled in from the content feed. In our example Inside the loop we are accessing data by referencing the variables in text
{{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 as shown below.
When you send your email if configured correctly you should see data pulled in dynamically like in our example below.
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 the url to see that it is returning only the data you need. If for example we wanted to create a newsletter from a news feeds first five articles you might have a url with some filtration params like below:
And if you view this source content you should see something like:
<?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>
...
Like in 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 like below
//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 as in the JSON example above.
Troubleshooting Issues
It is important to try multiple test sends when preparing a dynamically generated email to make sure data is populating correctly. If there are any issues you can visit Campaigns > Notification Log and drill into your failed send to see why it failed.
In the example below I incorrectly referenced the array as json instead of body in my for loop. To identify this I can click the failed tab in a log see the reason in this case - Variable ''json" does not exist. I can then scroll down in Message section where the data pulled in is stored in a field called params near the bottom of the message and look at what my dynamically pulled content data actually looks like and correct this error.
You can see another failure example below. In this example I did not add the excerpt field to the list of returned fields in my json source url. Consequently there is an error - Key "excerpt" for array with keys "link, title" does not exist. And if I scroll down in the messages pane I can see that excerpt is not in the data.
Comments
0 comments
Article is closed for comments.