API

Methods

init

This method is an entry point for LiveSession tracking script used to initialize tracking script on your website. You can find your trackingID in LiveSession -> Settings -> Websites.

Type signture:

1
function ("init", trackingID: string, options?: InitOptions)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.init("accountID.websiteID");

with options:

Init and track every input
1
import ls from "@livesession/browser";
2
3
ls.init("accountID.websiteID", { keystrokes: true });

options signature:

1
interface InitOptions {
2
// Your accountID. Automatically extracted from trackingID
3
accountID?: string
4
5
// Your websiteID. Automatically extracted from trackingID
6
websiteID?: string
7
8
// Enable global keystroke tracking
9
keystrokes?: boolean
10
11
// Set this to the highest-level hostname to record session across different subdomains on your site
12
// (e.g. `.your-domain.com`)
13
rootHostname?: string
14
}

More info about keystrokes you can find here.

 

identify

Identify user and custom data to session.

Type signture:

1
function("identify", data: IdentifyData)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.identify({ name: "John Doe", email: "john.doe@example.com" });

with params:

Identify with parameters
1
import ls from "@livesession/browser";
2
3
ls.identify({
4
name: "John Doe",
5
email: "john.doe@example.com",
6
params: {
7
order_id: "123-abc-def",
8
plan: "premium",
9
},
10
});

data signature:

1
interface IdentifyData {
2
// Displays usernames in app
3
// Maximum length: 128 characters
4
name?: string
5
6
// Displays user email
7
// Maximum length: 128 characters
8
email?: string
9
10
// Displays user context data
11
// Maximum length: 50 items
12
params?: {[key: string]: string | number | boolean}
13
}

User's name and email will be the same across all of their sessions.

 

track

This method allows you to send custom event that your users perform along with custom properties.

Type signture:

1
function("track", event: string, properties?: EventProperties)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.track("User Subscribed");

with properties:

Track with properties
1
import ls from "@livesession/browser";
2
3
ls.track("User Subscribed", {
4
plan_str: "premium",
5
seats_int: 1,
6
total_float: 255.50,
7
isPatron_bool: true,
8
});

without properties type suffix (not recommended):

Track with properties without suffix
1
import ls from "@livesession/browser";
2
3
ls.track("User Subscribed", {
4
plan: "premium",
5
seats: 1,
6
total: 255.50,
7
isPatron: true
8
});

properties signature:

1
// Maximum length: 50 items
2
type EventProperties = TypedEventProperty | {[key: string]: string | number | boolean}
3
4
type TypedEventProperty =
5
EventPropertyString |
6
EventPropertyInt |
7
EventPropertyFloat |
8
EventPropertyBool
1
// String property value, eg. {plan_str: "premium"}
2
// Maximum length: 256 characters
3
type EventPropertyString = {[key: `${string}_str`]: string }
4
5
// Int property value, eg. {seats_int: 2}
6
// Maximum length: int max. value
7
type EventPropertyInt = {[key: `${string}_int`]: number }
8
9
// Float property value, eg. {total_float: 255.50}
10
// Maximum length: float max. value
11
type EventPropertyFloat = {[key: `${string}_float`]: number }
12
13
// Bool property value, eg. {isPatron_bool: true}
14
type EventPropertyBool = {[key: `${string}_bool`]: boolean }
Example usage
1
import ls from "@livesession/browser";
2
3
ls.track("User Subscribed", {
4
plan_str: "premium",
5
price: 1,
6
total_float: 255.50,
7
pro: true
8
});

 

newPageView

Start recording user's visit and add it to session when conditions fulfilled. If session doesn't exists it also create new session.

Type signture:

1
function("newPageView", options?: NewPageViewOptions)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.newPageView();

with options:

1
import ls from "@livesession/browser";
2
3
ls.newPageView({ title: "Anonimized page title" });

with conditions:

New page view with conditions
1
import ls from "@livesession/browser";
2
3
ls.newPageView({
4
conditions: [{
5
type: "event",
6
name: "MouseClick",
7
operator: "contain",
8
key: "path",
9
value: ".add-cart"
10
}],
11
});

with base URL:

New page view with base URL
1
import ls from "@livesession/browser";
2
3
ls.newPageView({
4
baseURL: "https://example.com",
5
});
6
7
// or
8
9
ls.newPageView({
10
baseURL: function(base) {
11
return base
12
},
13
});

options signature:

1
interface NewPageViewOptions {
2
// Overwrite page title
3
title?: string
4
// Conditions for starting new pageView
5
conditions?: NewPageViewCondition[]
6
// Overwrite base URL on player
7
baseURL?: string | ((base: string) => string)
8
}
1
interface NewPageViewCondition {
2
// Use to specify condition based on user interaction (event).
3
type: "event";
4
// Event type
5
name: EventType;
6
// Operator for condition
7
operator: Operator;
8
// Event key
9
key: EventKey;
10
// Event value
11
value: string;
12
}
1
type EventType =
2
| "MouseClick" // Click on element
3
| "RageClick" // Click on element multiple times
4
| "ErrorClick" // Click on element with error
5
| "Scroll" // Scroll on page
6
| "WindowScroll" // Scroll on window
7
| "MouseMove" // Move mouse on page
8
| "TouchMove"; // Move touch on page
9
10
type Operator =
11
"start" | // base.indexOf(value) == 0
12
"contain" | // base.indexOf(value) > -1
13
"end" | // base.indexOf(value) == base.length - value.length
14
"eq" | // base == value
15
"neq" | // base !== value
16
"gt" | // base > value
17
"gte" | // base >= value
18
"lt" | // base < value
19
"lte"; // base <= value
20
21
type EventKey =
22
// Full DOM path to element
23
// e.g `body > #header > .link.add-cart[href="/cart"]`
24
"path" |
25
// Element identificator
26
// `.link.add-cart[href="/cart"]`
27
"el" |
28
// Text content of element
29
// e.g `Add to cart`
30
"txt";

 

setCustomParams

Set custom properties to session.

Type signture:

1
function("setCustomParams", options: CustomParamsOptions)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.setCustomParams({
4
params: {
5
order_id: "123-abc-def",
6
plan: "premium",
7
},
8
});

options signature:

1
type CustomParamsOptions = {
2
// Maximum length: 50 items
3
params: {[key: string]: string | number | boolean}
4
}

 

getSessionURL

Get URL to current session.

Type signture:

1
function("getSessionURL", callback: GetSessionURLCallback)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.getSessionURL(function(url, isNewSession) {
4
// do only if it's a new session
5
if (isNewSession) {
6
YOUR_API.addSessionURL(url);
7
}
8
});

integration with other software:

1
ls.getSessionURL(function(url, isNewSession) {
2
if (isNewSession) drift.track("LiveSession recording URL", { sessionURL: url });
3
});

callback signature:

1
type GetSessionURLCallback = (
2
// https://app.livesession.io/app/session/{visitor_id}/{session_id}
3
url: string,
4
isNewSession: boolean
5
) => void

 

setOptions

Set options and init LiveSession tracking stript (if it's not inited). You can find your website ID and account ID in LiveSession -> Settings -> Websites.

Type signture:

1
function ("setOptions", options: InitOptions)

Exmaples:

1
import ls from "@livesession/browser";
2
3
ls.setOptions({ accountID: "abc", websiteID: "cdef" });

with keystrokes:

Set options with keystrokes
1
import ls from "@livesession/browser";
2
3
ls.setOptions({ keystrokes: true });

 

invalidateSession

Close curent session.

Type signture:

1
function ("invalidateSession")

Examples:

1
import ls from "@livesession/browser";
2
3
ls.invalidateSession();

 

debug

Set debug logging mode.

Type signture:

1
function ("debug")

Examples:

1
import ls from "@livesession/browser";
2
3
ls.debug();

 

log

Standard console.log() statements will be recorded by LiveSession, but you have the option to log messages without adding additional noise to your users browser consoles.

Type signture:

1
function ("log", logLevel?: LogLevel, data?: object)

Examples:

1
import ls from "@livesession/browser";
2
3
ls.log("info", "demo info message");
4
ls.log("warn", "demo warn message");
5
ls.log("error", { id: 2, message: "demo error message" });

logLevel signature:

1
type LogLevel = "info" | "warn" | "error"

 

off

Turn LiveSession script off.

Type signture:

1
function ("off")

Examples:

1
import ls from "@livesession/browser";
2
3
ls.off();
Recording conditions