Custom JS snippet
Override default identification, trait syncing, consent, promotion display, and analytics behaviors in the Recurly Engage JavaScript SDK by supplying custom Settings functions.
Settings class. This covers user identification, trait syncing, consent, promotion gating, analytics, and language.Prerequisites:
- Company or App Administrator permissions in Recurly Engage
- Familiarity with your site's user identification methods (localStorage, cookies, dataLayer)
Definition
Key benefits
Key details
Admin users may override the following functions on the Settings class. Upon deploying Recurly Engage for the first time, review and configure the functions relevant to your app to ensure users are recognized, tracked, and prompted correctly. You may need to work with your developers to determine the best method for each. Reach out to [email protected] if you need assistance.
fetchUserId() (required)
fetchUserId() (required)Returns the authenticated user's unique ID. This string can come from cookies, local storage, a database, or an async endpoint fetch. This function runs when the Recurly Engage JS tag loads.
static async fetchUserId() {
const user = JSON.parse(localStorage.getItem("user_object"));
return user.id;
}null.fetchUserJwt()
fetchUserJwt()Returns a JSON Web Token (JWT) for the current user, if your app issues one. Recurly Engage uses this to verify the user's identity for secure operations.
static async fetchUserJwt() {
return window.userJwt;
}fetchAnonUserId() (optional)
fetchAnonUserId() (optional)Returns a stable anonymous ID for unauthenticated users. If omitted, Recurly Engage automatically generates and assigns one.
static async fetchAnonUserId() {
return JSON.parse(localStorage.getItem("ajs_anonymous_id"));
}fetchUserTraits() (optional)
fetchUserTraits() (optional)Returns an object of user traits to sync to Recurly Engage. Traits can come from cookies, local storage, a database, or an async endpoint fetch, and are used for segmentation, personalization, and reporting.
static async fetchUserTraits() {
const user = JSON.parse(localStorage.getItem("user_object"));
return {
is_registered: !!user.token,
member_since: user.registrationDate,
accepted_tos: localStorage.getItem("tos_acceptance_date")
};
}canShowPromotion()
canShowPromotion()Specifies app-wide conditions under which prompts should never be shown. Returns a boolean — when false, no overlay prompts are shown. Unlike the other functions, this one is not async.
static canShowPromotion() {
return !document.querySelector(".full-screen-video-player");
}onPromptInteraction(eventName, payload)
onPromptInteraction(eventName, payload)Called whenever a prompt event occurs, for custom analytics purposes.
| Parameter | Description |
eventName | The event type: impression, click, click2, decline, dismiss, timeout, or holdout. |
payload | An object describing the interaction — see fields below. |
static onPromptInteraction(eventName, payload) {
analytics.track(payload.activity, payload);
}The payload object includes:
| Field | Description |
activity | The activity name, e.g. "Redfast Prompt Click" |
cta | The CTA button text |
el | The interacted HTML element |
event_timestamp | ISO 8601 timestamp of the event |
promo_id | The prompt's unique ID |
promo_name | The prompt's name |
user_id | The identified user's ID |
variation_id | The variation's unique ID |
variation_name | The variation's name |
redirect_url | The website action's redirect URL, if applicable |
rf_metadata | Custom key-value metadata attached to the prompt |
experiment_name | The experiment's name, if applicable |
experiment_id | The experiment's unique ID, if applicable |
promo_input_1_value – promo_input_3_value | Values entered into form inputs on the prompt |
survey_input_value | The value entered into a survey input |
canPing()
canPing()Specifies whether the user has opted into being identified by Recurly Engage. Returns a boolean — use this to honor CCPA/GDPR opt-outs. When false, user information is not collected.
static canPing() {
return true;
}setLanguage()
setLanguage()Specifies the user's language, controlling the display language of prompts that have languages configured. Returns a 2-letter or 4-letter language code, or null to use the default.
static setLanguage() {
return null;
}Updated 19 days ago