Skip to content
  • There are no suggestions because the search field is empty.

Integrating with Marketo Personalization Javascript API

 Target Convert Experiences Using Marketo Personalization API Data

 THIS ARTICLE WILL HELP YOU:


Overview

Marketo Engage Web Personalization can share data about each visitor with the page through its User Context API. Once Marketo enables it, a userContext object is available under the RTP global object, as rtp.userContext.

Convert can read that object in an audience condition and target experiences on it, for example by the visitor's state or by the Marketo campaigns they have viewed.

Marketo data may not be ready when Convert first checks the page, so the experience has to wait for it. The When a callback is called Location trigger starts the experience once the data is there. It replaces the legacy convert_recheck_experiment() token, which is being phased out. The developer docs cover the same switch in Migrate to Dynamic Location Triggers.

Before you start

  • Your project must use the current Convert tracking script. The legacy script does not support Dynamic Website Triggers. To switch, see Migrate from the Legacy Tracking Script to the New Tracking Script.
  • When a callback is called is one of the Dynamic Website Triggers. If your plan includes Dynamic Website Triggers, follow the steps below. If it doesn't, use the legacy method at the end of this article. The Convert pricing page shows what each plan includes.
  • You need to be a Marketo Web Personalization customer with the RTP tag on your site.
  • Ask Marketo Support to enable the User Context API for your account. Marketo's documentation says it has to be enabled before you can use it.

Install the Web Personalization (RTP) tag

Install the RTP tag on the pages you want to personalize, as described in Marketo's Web Personalization JavaScript API documentation. Load it before the Convert tracking code, so the Marketo data is available as early as possible.

Explore the Marketo data

To check that the User Context API works and to see the data you can target, open the browser console on one of your pages and run:

rtp.userContext;

If the console shows rtp is not defined, the RTP tag is missing from the page. If it returns undefined after the page has finished loading, the User Context API is not enabled for your account. Ask Marketo Support to enable it. Otherwise you see the visitor's context, for example location.state.

To target a campaign, you need its ID. Marketo shows the campaign ID in the RTP interface when you hover over a campaign name.

Add the wait helper to your Project Global JS

In your project, go to Configuration > Global Javascript and add this helper. It checks every 100 ms, up to 50 times (about 5 seconds), for rtp.userContext. If it never appears, the helper stops, and the experience does not run for that visitor. You only need to add it once per project.

Save it under All Environments. If one of your environments has its own Global JS, add the helper there too, because that script replaces the All Environments code on that environment.

window.waitForMarketo = ({
nextRetry = 100, // ms
maxRetries = 50,
} = {}) => {
let retries = 0;
const check = (resolve) => {
const context = window.rtp && window.rtp.userContext;
if (context) {
resolve(context);
} else if (retries < maxRetries) {
retries++;
setTimeout(() => check(resolve), nextRetry);
} else {
resolve();
}
};
return new Promise((resolve) => check(resolve));
};

Set the Location trigger to wait for Marketo data

First, create the experience you want to target, if you have not done so.

Edit the experience's Location settings, choose When a callback is called as the trigger, and paste this code:

if (options.isActive) return;

window.waitForMarketo().then((context) => {
if (context) {
activate();
}
});

Convert passes two arguments to the callback: activate, which you call when the experience should start, and options, which includes locationId and isActive. The isActive check stops the callback from running twice. If the Marketo data never appears, the experience does not start. For more on this trigger, see Targeting Experiments with Dynamic Location Triggers (JS Callbacks).

Create the audience

Create an Advanced Audience: edit the experience's audience settings, select New Audience, open the Advanced tab, and add a JS Condition. The condition reads the Marketo data and returns true or false. Here are two examples, based on Marketo's User Context documentation.

Target visitors by state. This condition matches visitors whose Marketo location is the state of California (CA):

(function () {
var context = window.rtp && window.rtp.userContext;
var place = context && context.location;
return !!place && place.state === "CA";
})()

Target visitors who viewed a campaign. This condition matches visitors who viewed the Marketo campaign with ID 45. Replace "45" with your campaign ID:

(function () {
var context = window.rtp && window.rtp.userContext;
if (!context || typeof context.viewedCampaign !== "function") return false;
return !!context.viewedCampaign("45");
})()

Both conditions check that the Marketo data exists before they read it, so they return false instead of failing when it is missing. Add the audience to your experience and save.

Run a QA in your experience

On a page with the RTP tag, paste the condition into the browser console and confirm it returns true for a visitor who should see the experience. Then follow the QA guide to check that the experience runs when it should and stays off when it should not.

If your plan doesn't include Dynamic Website Triggers (legacy)

Without Dynamic Website Triggers, you can use the legacy convert_recheck_experiment() token in the audience condition instead. The token is being phased out. It still works today, but use the callback trigger above if your plan includes Dynamic Website Triggers.

When the condition calls the token, Convert checks the experience again every 50 ms for about 2 minutes, then stops. If the Marketo data arrives after that, the experience does not run on that page view.

  1. Install the RTP tag as described above. Skip the wait helper and the callback.
  2. Keep the Location trigger set to Upon Run.
  3. Add one of these JS Conditions to the audience.

By state:

(function () {
var context = window.rtp && window.rtp.userContext;
if (!context) {
convert_recheck_experiment();
return false;
}
var place = context.location;
return !!place && place.state === "CA";
})()

By viewed campaign:

(function () {
var context = window.rtp && window.rtp.userContext;
if (!context || typeof context.viewedCampaign !== "function") {
convert_recheck_experiment();
return false;
}
return !!context.viewedCampaign("45");
})()

Keep convert_recheck_experiment(); on its own line, as shown. The condition calls it only while the Marketo data is missing. Once the data is there, the condition returns true or false and the re-checks stop.

If your plan later includes Dynamic Website Triggers, move to the callback: add the wait helper, change the Location trigger to When a callback is called, replace the condition with one from Create the audience, and run a QA before you publish the change.