Setting the identity with Javascript

A fundamental part of Triggerbee is the Identity Layer. By identifying visitors you are able to create 1-to-1 communication and create very specific segments for targeting Onsite Campaigns. This article will take you through how to set the identity of a visitor using JavaScript, as well as passing other identity type data, such as name or telephone number.

As read in this article about identifying visitors, identification of your visitors can be done in several ways. If you have existing forms on your websites, such as a contact form, login, or checkout - it can be scripted to log the submitted data to Triggerbee. Depending on the desired outcome, scripting can be composed to identify the visitors' contact details, what action they performed, and/or where they are on the website.

Basic identification script

The identification script will collect the current Triggerbee user object, add the new identification data, and finally submit the data to Triggerbee via a goal. Only the adding of data and the log submission have to be customized. 

/* get och create current triggerbee user object */

var tbContact = window.mtr_custom || {};

tbContact.session = tbContact.session || {};


/* add identification to user object */

tbContact.session.email = "erlich@piedpiper.com";


/* set triggerbee user object */

window.mtr_custom = tbContact;


/* log goal to submit user object to triggerbee */

mtr.goal("Logged in");


Script examples

Adding identification from a login form

A simple example of how to pick up data from a form and log the collected email to Triggerbee.

<script type="text/javascript">

var form = document.querySelector('.login-form');

form.addEventListener('submit', function () {

var tbContact = window.mtr_custom || {};

tbContact.session = tbContact.session || {};

tbContact.session.email = document.querySelector('input[type="Email"]').value;

window.mtr_custom = tbContact;

mtr.goal('Logged in');

});

</script>

Adding identification on account creation

Apart from logging the email of the user (which is the unique identifier in Triggerbee), you can also add other data to the user - such as name, phone number, gender, etc. Any data you like! Simply build your user object like this:

<script type="text/javascript">

var form = document.querySelector('.login-form');

form.addEventListener('submit', function () {

var tbContact = window.mtr_custom || {};

tbContact.session = tbContact.session || {};

tbContact.session = {

email: document.querySelector('input[type="Email"]').value,

name: document.querySelector('input[id="Name"]').value

}

window.mtr_custom = tbContact;

mtr.goal('Created account');

});

</script>

Adding identification on completed purchase

When logging purchases, or any goal that contains revenue, you can also add this to the goal.

<script type="text/javascript">

if (window.location.pathname=="/checkout/thankyou"){

var tbContact = window.mtr_custom || {};

tbContact.session = tbContact.session || {};

tbContact.session.email = document.querySelector('customerEmail').value

var revenue = parseInt(document.querySelector('totalValue'));

window.mtr_custom = tbContact;

mtr.goal('Completed Purchase',revenue);

}

</script>

Log other form data

In the same basic script as above, it is also possible to add other custom data which you collect from forms on your site. Simply add any parameter you wish to add and Triggerbee will automatically create a corresponding data field on the profile. This is an example from a signup page:

<script type="text/javascript">

signupForm.addEventListener("submit", function () {

var tbContact = window.mtr_custom || {};

tbContact.session = tbContact.session || {};


tbContact.session = {

email: document.querySelector('input[type="Email"]').value,

name: document.querySelector('input[id="Name"]').value,

age: document.querySelector('input[id="Age"]').value,

gender: document.querySelector('input[id="Gender"]').value

favoriteShop: document.querySelector('input[id="Gender"]').value

}


window.mtr_custom = tbContact;

mtr.goal('Created Account');

})

</script>


Creating a visitor Object in the browser

Apart from adding events and identification to the visitor, you can also add a visitor object to the browser in order to target current settings for that particular visitor and/or session - such as logged-in status or page type.


Example script:

triggerbeeCurrentVisitor = {

name: "Anna",

isLoggedin: true,

memberType: "Premium"

}

This will allow you to target campaigns on those attributes, like targeting only logged-in visitors or visitors on specific page types.

Still need help? Contact Us Contact Us