Skip to main content

JavaScript API for Fresh Relevance

Pass data to Fresh Relevance with JavaScript.

Updated over a week ago

The JavaScript API enables you to pass data from your website to Fresh Relevance. You can implement this yourself or request our team to add it as part of our services.

Common use cases include:

  • Raising custom signals to trigger personalized programs

  • Capturing customer actions on your site (such as brochure requests or page views)

  • Passing visitor data when customers log in to enable personalization

  • Sending customer information for use in triggered messages

Data passed through signals is available through the entire process, including at email send time for personalized content.

Core JavaScript functions

Use these functions in your website code to send data to Fresh Relevance servers.

Name

Description

$TB.setEmail(emailAddress)

Set the email address for this person, for example, when they've logged in.

You need to call $TB.sendBeacon() directly afterward, to ensure the data is sent to the Fresh Relevance server.

$TB.setMobile(mobile, code)

Set the mobile number (and optional mobile code) for this person.

You need to call $TB.sendBeacon() directly afterward, to ensure the data is sent to the Fresh Relevance server.

$TB.setDetails(emailAddress, firstName, lastName, fullName, options)

Set details for this person, for example, when they've logged in. Any parameters passed as null are ignored. Set options.emailType to

  • 'e' for plain email address

  • 'b64e' for base64-encoded email address

  • 'rc4e' for RC4 encrypted email address

You don't need to call $TB.sendBeacon() separately, as this is called part of $TB.setDetails().

$TB.setData(rootData, extraData)

Set data for this person; where rootData and extraData are JSON objects. rootData can contain the data from the Person fields table below.

extraData contain any other data that you may wish to submit to be added to the person's record.

Example (gender).

$TB.setData({'e': emailAddress, 'fn': firstName, 'ln': lastName}, {'gender': customerGender}

You don't need to call $TB.sendBeacon() separately, as this is called part of $TB.setData().

$TB.raiseSignal(...)


JavaScript hooks

These hooks allow you to run custom code at specific points in the personalization lifecycle.

You can create functions within these hooks and the code is called at the appropriate time, such as, when a slot has loaded.

Function name

Called

Description

window.frBeforeSlotLoad(slotID, data)

Once for each Slot, after the data has been retrieved, before the page is updated.

Parameters:

  • slotID (string): the ID of the Slot currently being processed

  • data (object): the data generated by the server for the current Slot

{
"html": "<h2>Hello World</h2>",
"css": null,
"js": null,
"var": {}, // Optional variables passed back from the server

meta: {
"tmsb": "SmartBlock ID",
"tmsl": "Slot ID"
"tmrl": "Rule ID"
"tmcs": "Content ID"
}
}

Return value: true | false. If true, the slot is added to the page as normal. If false, the slot is not added to the page.

This hook can also be used to alter the html before it is added to the document by replacing the html attribute.

window.frOnSlotLoad(slotLoaded, slotID, data)

Once for each slot, after the page has updated.

Parameters:

  • slotLoaded (Boolean): indicating whether the Slot was added to the page or not

  • slotID (string): the ID of the Slot currently being processed

  • data (object): the data generated by the server for the current Slot

{
"html": "<h2>Hello World</h2>",
"css": null,
"js": null,
"var": {}, // Optional variables passed back from the server

meta: {
"tmsb": "SmartBlock ID",
"tmsl": "Slot ID"
"tmrl": "Rule ID"
"tmcs": "Content ID"
}
}

Return value: None

A wider range of hooks are available.


Popover reporting functions

  • Before you start
    Popover reporting only works in SmartBlocks displayed on web pages containing our script.

These functions report key popover events back to Fresh Relevance for analytics:

Function

Purpose

Example

$TB.popoverShown

Reports that a popover was displayed

$TB.popoverShown('{{ slotid }}')

$TB.popoverDismissed

Reports that a popover was closed

$TB.popoverDismissed('{{ slotid }}')

$TB.popoverSubmitted

Reports that a popover form was submitted

$TB.popoverSubmitted('{{ slotid }}', emailAddress)

You can include additional data with the popover submission using the extended format:

javascript

$TB.popoverSubmitted('{{ slotid }}', emailAddress, rootData, extendData)

Person fields

The rootData object can contain these core person fields:

Parameter

Description

e

Plain email address

b64e

Base64-encoded email address

rc4e

RC4-encrypted email address

cid

Customer ID, usually a username or user ID used on your website

curr

Currency

eid

External ID, usually to identify users in an ESP

fn

First name

lang

Site language

ln

Last name

mobile

Mobile/Cell/SMS number

perm

Permission to send emails to the person

sbr

Site brand

extendData contains any other data that you want to submit with this popover.

Example with extended data

This example shows how to submit a popover with the customer's name and gender:

$TB.popoverSubmitted({{ slotid }}, emailAddress, {'fn': firstName, 'ln': lastName}, {'gender': customerGender})

Did this answer your question?