Event Handlers (Optional)

Use these event handlers, which are JavaScript functions that you can register with a browser and which the browser will invoke when some specified type of event occurs.

Note that the following event handlers are optional.

Global Passback Handler

This handler enables you to run a basic client-side passback with the readmoPassbackHandler global method. The readmoPassbackHandler method only executes when the total number of ads returned is 0.

To use the global passback handler, add the following lines to the ad tag at the bottom before the closing </script> tag.

Example

window.readmoPassbackHandler = function({ section, error }){
  // check which section failed
  if (section === '12345') {
    var container = document.getElementById('readmo-12345');
    // remove the container from the DOM
    if (container) {
      container.parentNode.removeChild(container);
    }
  }
};

Properties

Property

Description

Type

Value

section

Section code that failed.

string

error

Request error message

string

NO_RECOMMENDATIONS

Global Click Handler

The readmoClickHandler is fired whenever a site or sponsored item is clicked. It is useful for publishers who want to control the navigation experience after a content site click occurs.

The callback returns the clickUrl and isSponsored flags.

window.readmoClickHandler = function(event, clickUrl, isSponsored){
       console.log(event, clickUrl, isSponsored);
     };

Properties

Property

Description

Type

Value

event

Original event object

object

clickUrl

Destination Url of site or sponsor

string

isSponsored

If true, the clicked item is sponsored

boolean

True or False

Example

(window.readmo = window.readmo || []).push({
  section: 'test-section',
  container: '#readmo-test-section',
  clickHandler: function(e, clickUrl, isSponsored){
    if (!isSponsored) {
      e.preventDefault();
      e.stopPropagation();

      console.log('prevent redirection on site click');
    }
  }
});

!(function(d) {
  var script = d.createElement('script');
  script.async = true;
  script.src = 'https://s.yimg.com/dy/ads/readmo.js';
  d.body.appendChild(script);
})(document);

Global Error Handler

You can control error handling with the readmoErrorHandler global method. The readmoErrorHandler method only executes if the Recommends gateway request fails.

To use the global error handler, add the following lines to the ad tag at the bottom before the closing </script> tag.

The callback function returns the section code and error type.

window.readmoErrorHandler = function({ section, error }){
  if (section === 'test-section') {
    console.log('do something when "test-section" fails', error);
  }
};

Properties

Property

Description

Type

section

Section code that failed.

string

error

Request errors: REQUEST_FAILED, TEMPLATE_REQUEST_FAILED.

string

Global Success Handler

Use the readmoSuccessHandler global method to perform actions after a successful gateway request has been made. The readmoSuccessHandler method only executes if the gateway request succeeded and returned ads.

The callback function returns the section config and message type.

successHandler: function({ section, message }) {
       console.log(section.container, section.rads, section.code, message);
     };

Properties

Property

Description

Type

section.container

Section DOM element.

element

section.code

Section code that succeeded.

string

section.rads

Array of site or sponsored content in that section.

array

section.totalSiteAndSponsored

Total count of site or sponsored items in the section.

number

message

Request response message: REQUEST_SUCCEEDED.

string

Global Render Complete Handler

The readmoRenderCompleteHandler is fired once the Recommends module completes rendering on the page.

The callback returns the section code and container element on the page.

window.readmoRenderCompleteHandler = function({ container, section }){
       console.log(container, section);
     };

Properties

Property

Description

Type

section

Section code

string

container

Section DOM element

element