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

  1. Go to Campaigns and create a new campaign
  2. 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

  1. On the Leanplum dashboard, navigate to Content -> Linked Data and click Add New Source
  2. Fill-in the requested source properties
    • Set URL to https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json
  3. Preview the setup to ensure that the data source queries the URL for its associated web service properly
  4. Click Save Source. Name the source stockquote and save it

4. Compose campaign content

  1. Go to Campaigns and create a new campaign
  2. 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: