Use external data in a campaign
Learn how to use Linked Data Sources in Campaigns
For this tutorial, we'll use the Quandl API for stock quotes, because it is free to use and does not require any API keys. For the purpose of this tutorial, we also assume you have a test app on Leanplum configured with either in-app messages or push notifications.
1. Confirm that in-app messages or push notifications are working
- Go to Campaigns and create a new campaign
- Compose a message (e.g. an in-app or a push notification), click the Preview & Test button, and send a preview to verify that it appears on your test device
2. Set a favorite stock as a User Attribute
Let's assume that your users have a User Attribute that contains their favorite stock. For example:
FavoriteStock=AAPL
You can set this yourself by calling our REST API. Do this for your own test device (you can verify your own User ID by going to Test Devices):
https://www.leanplum.com/api?action=setUserAttributes&appId=APP_ID&clientKey=CLIENT_KEY& userId=USER_ID&userAttributes={"FavoriteStock":"AAPL"}
3. Set up a new Linked Data source
- On the Leanplum dashboard, navigate to Content -> Linked Data and click Add New Source
- Fill-in the requested source properties
- Set URL to
https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json
- Set URL to
- Preview the setup to ensure that the data source queries the URL for its associated web service properly
- Click Save Source. Name the source
stockquote
and save it
4. Compose campaign content
- Go to Campaigns and create a new campaign
- Compose a message (e.g. an in-app or a push notification) that references the Linked Data
Example
Your stock quote for {{userAttribute['FavoriteStock']}} is
{{ linkedData.stockquote[userAttribute['FavoriteStock']] }}
5. Preview the message
Click Preview & Test button to launch preview settings.
The message is now echoing the entire contents of the JSON response from the Linked Data URL.
To get the desired field, modify the message to:
Your stock quote for {{userAttribute['FavoriteStock']}} is
{{ linkedData.stockquote[userAttribute['FavoriteStock']]['dataset']['data'][0][4] }}
To round the stock quote to 2 decimal places, use the round filter:
{{ linkedData.stockquote[userAttribute['FavoriteStock']]['dataset']['data'][0][4] | round(2) }}
See also:
- How to use message templates to select and format data.
- How to add authentication and configure linked data caching.
Updated almost 3 years ago