Getting Started with the Web SDK

The Moonsense Web SDK is designed to make integrating Moonsense into your web application as easy as possible. The SDK is managed through NPM and only requires a Public Token for your app from the Moonsense Console to get started.

The sections below will walk you through configuring NPM to pull the SDK, initializing the SDK, and recording a session.

Setup NPM

First, NPM needs to be configured to fetch Moonsense packages from the Moonsense repository. To do this, you will need the repository credentials for your project listed on the Moonsense Console.

Add the following to .npmrc within your project:

@moonsense:registry=https://npm.moonsense.io/sdk/ //npm.moonsense.io/sdk/:_authToken=<repo_access_token>

Integration

The SDK is setup using a private repository and can be included in your project by running the following command:

npm install --save @moonsense/moonsense-web-sdk

Usage

The SDK requires some configuration information to be setup and associate correctly with your app. To continue, you will need to create a publicToken for you app on the Moonsense Console.

The SDK supports integration in a number of ways including as an ES6 module or via CommonJS

Instantiation

The SDK can be instantiated either via the init(...) method or by creating a new instance of the Moonsense(...) object. Both can be setup with an init object as such:

/** * Setup a callback to use with the SDK. * * The callback allows the SDK to communicate easily to * your app and allows you to trigger specific events * within your app based on Moonsense SDK events. */ const myCallback = { /** * Triggered when a Moonsense Session is started */ onSessionStarted: (session) => { console.log('Session start callback called'); }, /** * Triggered when a Moonsense Session ends */ onSessionStopped: (session) => { console.log('Session stop callback called'); }, /** * Triggered when the Moonsense Session * experiences an error */ onSessionError: (msg) => { console.warn('Session error occurred', msg); } } // The config to pass to new Moonsense(...) or init(...) const sdkConfig = { // the PublicToken for this App publicToken: '<your_public_token>', // required // The callback created above moonsenseCallback: myCallback, // optional // The minimum level of logs that should // be output to the console logLevel: 'error', // optional }

ES6

// Import the Moonsense SDK import { Moonsense } from '@moonsense/moonsense-web-sdk'; // Create an instance of the SDK const moonsenseSdk = new Moonsense(sdkConfig);

CommonJS with Require

const moonsenseWebSdk = require('./moonsense-web-sdk.js'); const moonsenseSdk = moonsenseWebSdk.init(sdkConfig);

CommonJS via Script Tag

<!-- This assumes you are hosting the sdk at root --> <script src="/moonsense-web-sdk.js"></script> <script> const moonsenseSdk = moonsenseWebSdk.init(sdkConfig); </script>

Recording

The SDK records in Sessions. A Session is defined as a single start and stop point in time. A Session is started in the following way:

// Creates a session that will record for up to 20 seconds const session = moonsenseSdk.startSession(20000);

A session can also be terminated early using the following:

// Stops a specific session session.stopSession();

Or all sessions can be stopped using:

// Stops all sessions moonsenseSdk.stopAllSessions();

Sample React App Implementation

The React App implementation shows how to incorporate the Moonsense SDK into a React Application. Details on the application can be on github.