Callbacks

Callbacks in ReferralHero are functions that get executed when specific events occur during the tracking and referral process. You define these callbacks within the window.RHConfig object. Each callback serves a particular purpose, allowing you to customize the behavior of your referral system.

Defining Callbacks

Callbacks are properties of the global variable window.RHConfig. Here's a general structure:

<script type="text/javascript">
  window.RHConfig = {
    callbacks: {
      // Define your callbacks here
    }
  }
</script>

IMPORTANT: Callbacks must be defined BEFORE the Tracking Pixel.

CallbacksDescription

onLoad

This callback is triggered before everything else. It’s useful for initializing variables or performing actions that need to happen as soon as the tracking script is loaded.

ready

Triggered when the Tracking Code is fully initialized, all widgets are generated, and all required libraries are loaded. This is usually the place to put logic that should run as soon as your tracking is ready to go.

beforeSubmit

This callback is triggered right before the sign-up form is submitted. It’s useful for modifying the data that will be sent to the server. It receives an object containing form data like name and email.

success

Triggered after the sign-up form has been successfully submitted. This callback receives an object with the response of the submission. Initializing this callback will prevent the default sharing screen from appearing.

afterSuccess

Triggered after the form has been successfully submitted. Like success, it receives the submission response. However, initializing this callback will NOT prevent the sharing screen from appearing.

error

This callback is triggered if there is an error during the form submission. It’s useful for handling errors gracefully.

popupOpen

Triggered when a popup is opened. This can be used to track popup usage or to customize what happens when a popup is displayed.

popupClose

Triggered when a popup is closed. Use this to track or manage actions after the user closes a popup

subscriberNotFound

Triggered when an email that doesn’t exist in the system is used to check the status of a subscriber.

subscriberLoaded

Triggered when a subscriber is identified. This is useful for loading subscriber-specific data or customizing the experience based on the subscriber’s information.

emailNotValid

Triggered when the email entered is not valid. It receives a reason parameter explaining why the email is considered invalid.

serverProblem

Triggered when the server returns a 500 or 4XX error, indicating a server-side issue.

Syntax for defining callbacks

<script type="text/javascript">
  window.RHConfig = {
    callbacks: {
      onLoad: function() {
        // Triggered before everything else
        // Example: console.log("ReferralHero script is loading...");
      },
      ready: function() {
        // Triggered when Tracking Code is initialized and all libraries are loaded
        // Example: console.log("ReferralHero is ready!");
      },
      beforeSubmit: function(data) {
        // Triggered right before the sign-up form is submitted
        // Modify form data here before submission
        RH.form.data = {
          name: "Mr " + data.name,
          email: data.email || "john.smith@email.com", // Example to modify email
          extra_field: data.extra_field || "Some Default",
          extra_field_2: data.extra_field_2 || null
        };
        // Example: console.log("Form data before submission:", RH.form.data);
      },
      success: function(output) {
        // Triggered after the form is successfully submitted
        // Example: console.log("Form submitted successfully:", output);
      },
      afterSuccess: function(output) {
        // Triggered after the form is successfully submitted and the success screen appears
        // Example: console.log("After success callback executed:", output);
      },
      error: function() {
        // Triggered if there was an error with form submission
        // Example: console.error("There was an error submitting the form.");
      },
      popupOpen: function() {
        // Triggered when the popup is opened
        // Example: console.log("ReferralHero popup opened.");
      },
      popupClose: function() {
        // Triggered when the popup is closed
        // Example: console.log("ReferralHero popup closed.");
      },
      subscriberNotFound: function() {
        // Triggered when a non-existent email is used to check status
        // Example: console.warn("Subscriber not found.");
      },
      emailNotValid: function(reason) {
        // Triggered when an invalid email is detected
        // Example: console.error("Invalid email:", reason);
      },
      serverProblem: function(reason) {
        // Triggered when a server error (500 or 4XX) occurs
        // Example: console.error("Server problem encountered:", reason);
      },
      subscriberLoaded: function(response, data) {
        // Triggered when a subscriber is identified
        // Example: console.log("Subscriber loaded:", response, data);
      }
    }
  }
</script>

Last updated