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:
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 trackingID3 accountID?: string4 5 // Your websiteID. Automatically extracted from trackingID6 websiteID?: string7 8 // Enable global keystroke tracking9 keystrokes?: boolean10 11 // Set this to the highest-level hostname to record session across different subdomains on your site12 // (e.g. `.your-domain.com`)13 rootHostname?: string14 }
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:
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 app3 // Maximum length: 128 characters4 name?: string5 6 // Displays user email7 // Maximum length: 128 characters8 email?: string9 10 // Displays user context data11 // Maximum length: 50 items12 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:
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):
1 import ls from "@livesession/browser";2 3 ls.track("User Subscribed", {4 plan: "premium",5 seats: 1,6 total: 255.50,7 isPatron: true8 });
properties signature:
1 // Maximum length: 50 items2 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 characters3 type EventPropertyString = {[key: `${string}_str`]: string }4 5 // Int property value, eg. {seats_int: 2}6 // Maximum length: int max. value7 type EventPropertyInt = {[key: `${string}_int`]: number }8 9 // Float property value, eg. {total_float: 255.50}10 // Maximum length: float max. value11 type EventPropertyFloat = {[key: `${string}_float`]: number }12 13 // Bool property value, eg. {isPatron_bool: true}14 type EventPropertyBool = {[key: `${string}_bool`]: boolean }
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: true8 });
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:
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:
1 import ls from "@livesession/browser";2 3 ls.newPageView({4 baseURL: "https://example.com",5 });6 7 // or8 9 ls.newPageView({10 baseURL: function(base) {11 return base12 },13 });
options signature:
1 interface NewPageViewOptions {2 // Overwrite page title3 title?: string4 // Conditions for starting new pageView5 conditions?: NewPageViewCondition[]6 // Overwrite base URL on player7 baseURL?: string | ((base: string) => string)8 }
1 interface NewPageViewCondition {2 // Use to specify condition based on user interaction (event).3 type: "event";4 // Event type5 name: EventType;6 // Operator for condition7 operator: Operator;8 // Event key9 key: EventKey;10 // Event value11 value: string;12 }
1 type EventType =2 | "MouseClick" // Click on element3 | "RageClick" // Click on element multiple times4 | "ErrorClick" // Click on element with error5 | "Scroll" // Scroll on page6 | "WindowScroll" // Scroll on window7 | "MouseMove" // Move mouse on page8 | "TouchMove"; // Move touch on page9 10 type Operator =11 "start" | // base.indexOf(value) == 012 "contain" | // base.indexOf(value) > -113 "end" | // base.indexOf(value) == base.length - value.length14 "eq" | // base == value15 "neq" | // base !== value16 "gt" | // base > value17 "gte" | // base >= value18 "lt" | // base < value19 "lte"; // base <= value20 21 type EventKey =22 // Full DOM path to element23 // e.g `body > #header > .link.add-cart[href="/cart"]`24 "path" |25 // Element identificator26 // `.link.add-cart[href="/cart"]`27 "el" |28 // Text content of element29 // 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 items3 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 session5 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: boolean5 ) => 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:
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();