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 has 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>

Still need help? Contact Us Contact Us