@cxsense/analytics v1.0.3
CxSense Analytics
Analytics is a client web SDK that collects customer interactions and listens to your customer journey updates.
CxSense empowers engineers to build personalised and hyper-personalised customer expriences in days instead of month. Sign up with CxSense today.
Before you start
This guide assumes you have created a free project account on
CxSense.
If you don't already have an account you can start by creating a free account and registering a web app to access your api_key.
Getting started
Using the Analytics package is as simple as adding this JavaScript code to your page.
import analytics from '@cxsense/analytics'
analytics.load('your_api_key');
analytics.identify("your_user_identifier");
analytics.track('plan-upgrade', { metadata: { plan: 'premium' } });Installation
npm install @cxsense/analytics
# or
yarn add @cxsense/analyticsAnonymous users
Anonymous users can be identified by providing null as their identifier.
Analytics generates an ID and store it in users browser localstorage.
analytics.identify(null);By default, Analytics uses the same generated identifier whenever the above function is called. In case you need a new identifier you have to reset it.
analytics.reset();
analytics.identify(null);API
Table of Contents
- track
- identify
- onJourneyCompletion
- onDestinationResponse
- journeyCompletionCallback
- destinationResponseCallback
- load
track
Track user interactions
Parameters
eventstring the name of the event to track.propertiesObject event properties. (optional, default{})properties.metadataObject the metadata that describes the event. (optional, default{})properties.variation_settingsObject additional tags and attributes that define the event. (optional, default{})properties.itemObject? user interactions with items in catalogues. Ifitemprovided,variation_settingswill be ignored.
Examples
Track events with metadata
analytics.track('subscribed', {
metadata: { email: 'foo@bar.com' }
});Track events that are defined with attributes
analytics.track('product_view', {
variation_settings: { plan: 'premium', rate: '5star' }
});Track events for items in a catalogue
analytics.track('article_read', {
item: { catalogue: 'articles', id: 'your_article_id' }
});identify
To get started with our libraries your users needs to be identified. Users in segments are identified by a unique profile identifier in CxSense.
If the user_id is not provided, the user is considered anonymous. In this case, a user identifier will be generated and stored in the local storage.
Parameters
user_idstring The unique user identifier in your system. If not provided, a random identifier will be generated.segmentObject Segment object that describes the segment the user belongs to. (optional, default{})user_properties(optional, default{})propertiesObject User properties. (optional, default{})
Examples
Identify a known user
analytics.identify('your_user_id', {
title: 'my_segment_name',
settings: {
tenant: "customer1",
}
}, {
name: 'John Doe',
age: 30
});Identify an anonymous user
analytics.identify(null)Returns string profileId - User unique identifier in CxSense
onJourneyCompletion
Parameters
handlerjourneyCompletionCallback The function to be called when a journey has completed for a user in a segment.
Examples
Listen to journey completion events
analytics.onJourneyCompletion((data) => console.log(data)))onDestinationResponse
Parameters
handlerdestinationResponseCallback The function to be called after a destination executes.
Examples
Listen to destination success responses
analytics.onDestinationResponse((data) => console.log(data)))journeyCompletionCallback
Type: Function
Parameters
dataObject The event object.
destinationResponseCallback
Type: Function
Parameters
dataObject The event object.
load
Parameters
apiKeystring The API key for your CxSense account.optionsObject Options object. (optional, default{})options.observerstring The websocket observer. (optional, defaultfalse)
Examples
Load the library with in-app notification
analytics.load('your_api_key', { observer: true })License
@cxsense/analytics is MIT licensed.