The following section describes MOCA JavaScript SDK public interface. Here you can find the SDK initialization and other important methods, all with working examples.
Initialization
This function initializes a new instance of the MOCA SDK. All new instances are added to the main moca object as sub properties (such as moca.library_name
) and also returned by this function.
moca.init({ api_token: <apiToken>,
user_id : <userId>,
track_pageload : true, // optional
disable_cookie : false, // optional
http_post : false // optional
});
Available arguments:
api_token
(string, required) - your MOCA API tokenuser_id
(string, optional) - logged-in user identifier (usually CRM ID)track_pageload
(boolean, optional, defaults true) - indicates if the pageview event should be automatically trackeddisable_cookie
(boolean, optional, defaults false) - disables cookies in the SDKhttp_post
(boolean, optional, defaults false) - forces tracker to use POST request to contact MOCA backend.
The user_id
parameter is an optional string that uniquely identifies the logged-in user. If the user is not logged in or browses a public page, do not use the user_id
parameter.
By default, the execution of this code snippet will initialize MOCA SDK. The snippet will initialize an instance of the minified library to keep your website loading quickly even if placed in the of the page. Once loaded, it will send a single event to indicate a particular web page has been viewed. (_pageview
event).
The snippet provides a global variable named window.moca
that you can use to send data to the MOCA backend API.
Tracking custom events
This method tracks a single custom event triggered by a user in your Web Application. This is the most important and frequently used MOCA function. Note that this API call is asynchronous.
Usage:
moca.trackEvent ("eventName", {parameters});
Arguments:
- eventName` (string, required) - the name of the event. This can be anything the user does, “view”, “search”, “buy”, etc.
parameters
(object, required) - a set of properties to include with the event you are sending. These describe the details about the event itself.callback
(no-parameters callback function, optional) - if provided, the callback function will be called after tracking the event.
Simple example:
moca.trackEvent ("search_view", {
category: "exhibitor",
item: "394875"
});
Example with call-back:
moca.trackEvent ("search_view", {
category: "exhibitor",
item: "394875"
},
function () {
console.log ("Event submitted to MOCA");
}
);
Recommend content
Provides personalized recommendations for a logged-in user. This API call is asynchronous.
Usage:
moca.recommend ({
item_type : <string>,
limit : <integer>,
success : function (items) { … },
error : function (errorMessage) { … }
});
Arguments:
- item_type (string, required): requested item type to be recommended. Valid item types include “exhibitor”, “session”, “speaker” and “visitor”.
- limit (integer, required): maximum number of items to recommend.
- success (single parameter callback function, obligatory): the callback function will be called after successfully receiving the recommendations. The callback receives a single parameter (items) that provides access to an array of recommended items.
- items: returned array of recommended items. Each item is a JavaScript object and contains two fields:
- itemId: recommended item identifier
- score: numeric scores (highest first)
- error (single parameter callback function, optional) - the callback function will be called when an error occurs. The callback receives a single parameter with the errorMessage.
Example: Get top 10 sessions recommendations for currently logged-in user:
moca.recommend ({
item_type : "sessions", // recommend sessions
limit : 20, // maximum 20 recommendations
success : function (items) {
for (int i=0; i<items.length; ++i) {
console.log (items[i].itemId + “ “ + items[i].score);
}
// store and display recommendations
},
error : function (errorMessage) {
// cannot obtain recommendations
console.log (“Cannot obtain recommendations: “ + errorMessage);
// retry later
}
});
Variables captured by the MOCA JS SDK Automatically
The MOCA JS SDK captures the following variables automatically. These enable in-depth analytics of your Web App’s audience. Here’s the complete list:
Variable | Description |
---|---|
_id | This is the internal, auto-generated event identifier (UUID). |
type | This is the type of event. The MOCA JS SDK sends the type as _webevent. |
verb | This is the eventName for the recorded event. |
t | This is the timestamp when the event happened, expressed in milliseconds since 1/1/1970 UTC. |
url | This is the full URL where the event happened. |
lang | This is the locale reported by the Window Navigator Object. |
screen_width | This is the reported screen width of the browser window, in logical pixels. |
screen_height | This is the reported screen height of the browser window, in logical pixels. |
user_agent | This is the browser’s user agent string reported by the Window Navigator Object. |
domain | This is the domain where the event took place extracted from the page url. |
online | Returns true if the browser is online |
cookie_enabled | This returns true if cookies are enabled in the browser. |
referring_url | This is the URL that referred to your WebApp |
referring_domain | This is the URL’s domain that referred to your WebApp |
instance | This is an internal unique user identifier, used primarily to identify guest users on their device. |
app_key | This is the API Token you use to initialize the SDK. |
user | This is the ID you assign to a logged in user. |
moca_version | This is the library version for the MOCA JS SDK present |