Skip to main content

JavaScript Reference

Whether you used HTML snippet, JavaScript library, or React you'll have access to the same API to send events. The API is based on Analytics.js, and is compatible with it.

Configuration

Basic Configuration

NameScript AttributeDescription
writeKeydata-write-keyBrowser Write Key configured on Jitsu Site entity. If no Browser Write Key is added for Site entity, Site ID value can be used a Write Key. On Jitsu.Cloud can be omitted if Site has explicitly mapped domain name
host-Jitsu installation domain, e.g. your-jitsu-domain.com. For HTML snippet value is assumed from script URL.
debugdata-debugEnables debug log messages in Browser: true or false. Default false
-data-onloadFunction to call after the script has loaded. Function should be previously defined in window
-data-init-onlyBy default, the script will send a page event. Set this to true to just initialize the library. You still will be able to send events manually by setting data-onload hook

Advanced Configuration

NameScript AttributeDescription
idEndpointdata-id-endpointEndpoint that makes sure that Jitsu anonymousId cookie is set as server (httpOnly) cookie. Endpoint must be hosted on the same domain as the site where Jitsu code is installed. Required to overcome Safari ITP restrictions.
cookieDomaindata-cookie-domainExplicitly specify cookie domain. If not set, cookie domain will be set to top level of the current domain. Example: if JS lives on "app.example.com", cookie domain will be set to ".example.com". If it lives on "example.com", cookie domain will be set to ".example.com" too
fetch-Custom implementation of Fetch API fetch method
fetchTimeoutMsdata-fetch-timeout-msTimeout for fetch requests. Default value: 5000
echoEventsdata-echo-eventsIf true, Jitsu will output events in console. In this case you don't need to set writeKey / host. It's useful for debugging development environment
defaultPayloadContextdata-default-payload-context
expects stringified json value
Default context object that will be merged with the context of every event.
cookieNamesdata-cookie-names
expects stringified json value
Map of alternative names for standard cookies. Format: {"anonymousId":"my_eventn_id"}
Default values:
anonymousId: __eventn_id
userId: __eventn_uid
userTraits: __eventn_id_usr
groupId: __group_id
groupTraits: __group_traits
cookieCapturedata-cookie-capture
expects stringified json value
Map of cookies to capture in addition to Facebook's _fbc,_fbp and Google's _ga ids. Format: {"id":"cookie-name"}. Captured cookies will be added to context.clientIds object of event payload.

Privacy Settings

info

Available since Jitsu v2.8.0 and npm packages v1.9.7

Privacy settings are nested under privacy object of Jitsu Options:

const analytics = jitsuAnalytics({
host: "https://your-jitsu-domain.com",
privacy: {
dontSend: false,
disableUserIds: true,
ipPolicy: "stripLastOctet",
consentCategories: {
"Analytics": true,
"Marketing": false
}
}
});
NameScript AttributeDescription
dontSenddata-privacy-dont-sendIf true, disables storing anything in cookies and sending events to Jitsu servers.
disableUserIdsdata-privacy-user-idsIf true, disables storing in cookies and sending to Jitsu servers any user identifiers (including anonymousId). In this mode identify and group calls will have no effect.
ipPolicydata-privacy-ip-policyControls how Jitsu collects information about user's IP: keep - collect full IP, stripLastOctet - collect only first 3 octets, e.g: 104.154.19.0 , remove - do not collect user's IP
consentCategories-Object containing user's consent state by category name. Provided value will be passed to Jitsu in every event in context.categoryPreferences object.

Methods

.page()

Trigger page view.

//trigger page view with a custom name
jitsu.page("Page Name");

//trigger page view with a custom properties
jitsu.page({ propName: "propVal" });

//trigger page view with a name AND custom properties
jitsu.page("Page Name", { propName: "propVal" });
tip

For HTML Snippet page view is triggered automatically, unless data-init-only attribute is set to true.

.identify()

Identify a user.


// Identify user: `xyz` as a userId, and additional properties (traits)
jitsu.identify('xyz', {
name: 'Michael Scott',
company: 'Dunder Mifflin',
})
//or just set a userId
jitsu.identify('xyz')

$doNotSend is a special property that tells Jitsu to save user and it's properties to local storage, but not to send it to the server.

This is useful to avoid sending excessive volumes of events to Jitsu

jitsu.identify('xyz', {
name: 'Michael Scott',
company: 'Dunder Mifflin',
$doNotSend: true
})
tip

Use .identify() for permanent user identifiers such as a database id of registered user. Use .setAnonymousId() for temporary identifiers such as cookies

.track()

Send a custom event.


// Event name only
jitsu.track("buttonClick");

// Event with properties
jitsu.track("itemPurchased", { price: 99 });

.group()

Assign user to a group, usually a company or organization.


// Group ID. Use permanent identifiers such as a database id of company
jitsu.group("g-xyz");

//Group with properties
jitsu.group("g-xyz", { name: "Dunder Mifflin" });

.setAnonymousId()

Jitsu automatically detects anonymous id for each visitor. You can override it with .setAnonymousId()

//Set anonymous id of a user, such as ID of the visitor based on cookie
jitsu.setAnonymousId("xyz");
warning

Jitsu automatically detects anonymous id for each visitor, you rarely need to use .setAnonymousId(). Use at your own risk.

.setContextProperty()

Allows to set a context property that will be sent with every event. Effectively, it sets property of defaultPayloadContext object that Jitsu merges with a standard context for every event.

jitsu.setContextProperty("pageViewId", "12345");

.getContextProperty()

Get a property of defaultPayloadContext object if available.

const pageViewId = jitsu.getContextProperty("pageViewId");  

.configure()

info

Available since Jitsu v2.8.0 and npm packages v1.9.7

Change Jitsu configuration on the fly. Only privacy, debug and echoEvents settings can be changed on the fly.

//Change Jitsu configuration on the fly
jitsu.configure({
debug: true,
privacy: {
dontSend: true,
disableUserIds: true,
ipPolicy: "stripLastOctet"
}
});