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.

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