Skip to content

Javascript API Reference

The CustomerLabs Javascript API empowers you to capture user interactions, manage customer identities, and organize group or account-level data in your website or app and send it to CustomerLabs. This reference documentation covers the primary methods available for integrating CustomerLabs into your web application.

To leverage the JavaScript API, the CustomerLabs tracking code must be initialized within your application. This setup instantiates the global _cl object, which serves as the primary entry point for all tracking operations.

The tracking snippet can be retrieved from the CustomerLabs dashboard via Home → Connect Website → Default Tracking Code

Installation

For optimal performance and data integrity, embed the snippet within the <head> section of every page. The following snippet demonstrates the standard implementation:

<script>
!function(t,e,r,c,a,n,s){t.ClAnalyticsObject=a,t[a]=t[a]||[],t[a].methods=["trackSubmit","trackClick","pageview","identify","track", "trackConsent"],t[a].factory=function(e){return function(){var r=Array.prototype.slice.call(arguments);return r.unshift(e),t[a].push(r),t[a]}};for(var i=0;i<t[a].methods.length;i++){var o=t[a].methods[i];t[a][o]=t[a].factory(o)};n=e.createElement(r),s=e.getElementsByTagName(r)[0],n.async=1,n.crossOrigin="anonymous",n.src=c,s.parentNode.insertBefore(n,s)}(window,document,"script","https://cdn.js.customerlabs.co/<ACCOUNT-ID>.js","_cl");_cl.SNIPPET_VERSION="2.0.0"
</script>

Once the tracking code is installed, the global _cl object becomes available. This object exposes the following methods:

  • trackSubmit
  • trackClick
  • pageview
  • identify
  • track
  • trackConsent
  • group
_cl.<method>(<parameters>)

The parameters includes Event Name and Properties.

_cl.pageview(eventName, properties)

properties is a object in CustomerLabs that is triggered with the event. It is a nested object that contains the properties of the event.

var properties = {
"customProperties": {
"property_name": {
"t": "string",
"v": "property_value"
}
}
}

The structure has two main parts with property_name

  1. t: This is the type of the property.
  2. v: This is the value of the property.

The properties object usually contains,

  1. Custom Properties
  2. Product Properties

To maintain continuity of user profiles across multiple domains, you can retrieve the CustomerLabs user ID and pass it to the destination domain.

window.CLabsgbVar.generalProps.uid
Cross-Domain Tracking

Appending the current URL with the user ID

Section titled “Appending the current URL with the user ID”

This customerlabs_user_id can be appended to the current URL using a simple function.

(function() {
if (window.location.host === "<your-domain.com>" || window.location.host === "<your-sub-domains.com>") {
var cluid = window?.CLabsgbVar?.generalProps?.uid;
var queryParams = new URLSearchParams(window.location.search);
if (!queryParams.has('cluid') && cluid) {
queryParams.append('cluid', cluid);
window.location.search = queryParams.toString();
}
}
})();

This customerlabs_user_id can be appended to a <a> or <button> in href after retrieving the user ID.

In the below screenshot, the customerlabs_user_id is appended as cluid to the href of the <a> tag of “Continue” button.

So, when the user clicks the “Continue” button in the website, the customerlabs_user_id appended to the URL of href is passed on the other domain.

Cross-Domain Tracking

Use cases with appending the CLUID to a button URL

Section titled “Use cases with appending the CLUID to a button URL”
  • There will be continuity of user profiles across your related domains, where tracking code can be placed.
  • If the redirection is to a third-party domain, this CLUID can be captured in the form field as a hidden field in third-party domain and passed back to CustomerLabs via webhook. This event can be unified using cluid parameter.

Callback functions allow for real-time data transformation before events are dispatched to your enabled destinations. This flexibility ensures that data formats align with the requirements of various third-party tools.

function(p1, p2, p3, p4, p5, p6, mid) {
// function logic
}
ParameterDescriptionJavascript Type
p1Event nameString
p2Event attributesObject
p3ProductsArray
p4identifyBoolean
p5user external_idString
p6group external_idString
midmessage_id
(Default id set by CustomerLabs)
String

Manage these transformations in the CustomerLabs application under

  1. Destinations on the left side menu → Click on the Destination that you wish to edit.
Callback Function
  1. Setup Event Workflow → Identify the event you wish to edit.
Callback Function
  1. Click the Edit Code Icon </>.
Callback Function
Callback Function
function(p1, p2, p3, p4, p5, p6, mid) {
// Custom logic and data transformation
}

Modifying the event name and URL while sending to ad platforms

Section titled “Modifying the event name and URL while sending to ad platforms”

Here we are modifying the event name from “purchase” to “Purchased” and adding a URL to the event.

function(p1,p2,p3){
reqData.data.p1='App Purchase';
p2.link="https://example.com";
p2.products=p3;
return p2;
}
Callback Function

Sending the data if acquisition is from a specific source

Section titled “Sending the data if acquisition is from a specific source”

Lets consider if the source is “google” or “youtube”, then the data should be sent to the destination. Otherwise, the data should not be sent to the destination.

function (p1, p2, p3, p4, p5, p6) {
p2.products = p3;
if (p2.acquisition && p2.acquisition.source) {
const source = p2.acquisition.source.toLowerCase();
if (source.includes("google") || source.includes("youtube")) {
return p2;
}
}
return {};
}