Logging Goals with JavaScript

Note: If you haven't read An intro to Goals in Triggerbee, we suggest you do that first. This article is written for developers, and only covers JavaScript methods of logging Goals.

As a developer, you have complete freedom when it comes to keeping track of important events contributing towards your online success.

Events

Goals: mtr.goal()

These logs goals in Triggerbee. A goal is an important event that can be used to trigger automation events and build audiences. Achieved goals are added to the contact, and can also be added to the subscriber in the email software and CRM. The method accepts the name of the goal and any revenue.

Examples
mtr.goal("Logged a form"); 
mtr.goal("Added to cart"); 
mtr.goal("Completed Purchase", 310); // 310 is the revenue
Logging a goal on page load with some logic
  window.addEventListener('afterTriggerbeeReady', () => {
    if(window.location.pathname.endsWith('success-invoice-payment/') && (getCookie("Is Premium member")==true)){
         mtr.goal('Successful payment from premium member'); 
    }
 });

Scripting Guidelines

AfterTriggerbeeReady

The above events can only be used  after the entire Triggerbee tracking script has loaded making the mtr-object available. Below is an example script that utilizes the afterTriggerbeeReady function.

afterTriggerbeeReady(function () {
  // variable exists, do what you want
  // for example call mtr.goal("Purchase complete!", "995");
});

function afterTriggerbeeReady(callback) {
 var maxAttempts = 10;
 var counter = 0;
 checkIsInitialized();

 function checkIsInitialized() {
   if (typeof (window.mtr) !== "undefined") {
     callback();
   } else {
     if (counter < maxAttempts) {
       setTimeout(function () {
         counter++;
         checkIsInitialized();
       }, 250);
     }
   }
 }
}<br>

BeforeTriggerbeeReady

If you want to run some code before the Triggerbee script has finished loading, you can use the beforeTriggerbeeReady function, like this:
document.addEventListener('beforeTriggerbeeReady', function() { 
    // Do something before Triggerbee is initialized
}

Namespaces

Sometimes you might want to customize the logging using the mtr_custom object in a namespace of your own. Then the Triggerbee script can't read those customizations. To avoid this use the following trick that makes sure that the mtr_custom object is accessible in the global space accessed the Triggerbee mtr-logger:

window.mtr_custom = mtr_custom;

Goal Callback

If you want to wait for the callback of you goal, use this function.

mtr.goal('Callback goal', null, function() {
    //your code
});

Still need help? Contact Us Contact Us