A small library to interact with Kissmetrics that can be shared across a client (browser) and server (Node.js).
In place of a more detailed readme, the annotated source is very thorough.
Install from NPM with:
npm install kissmetrics-js
In Node.js, you probably just want to require the module.
KM = require'kissmetrics-js';km = API_KEY username;// ...
Record data about a logged in user.
// General activitykm = API_KEY username;kmrecord'Published post';kmsetlast_seen: ;// Change usernamekmrecord'Change username';kmaliasusername;
Record data about a logged out user.
// New visitorkm = API_KEY;kmrecord'Visited front page';kmrecord'Visited signup form';// Signs upkmrecord'Signed up';kmaliasusername;// Record more data as the new logged in userkm = API_KEY username;kmrecord'Publish post';
Automatically-generated ID's are deleted from storage by default when
alias() is called, but you have the option to save them by passing a second argument of
km = API_KEY;kmrecord'Signed up'aliasusername;console.logkm_storageget;// nullkm = API_KEY;kmrecord'Signed up'aliasusername false;console.logkm_storageget;// "56a44b65ddad8a4ab00885ec42e7d2f7db46dcd69c3f"
Data methods can be chained.
km = API_KEY;kmrecord'Visited front page'record'Visited signup form'record'Signed up'aliasusernamerecord'Published post';
alias() updates the instance's
person attribute, so future data is recorded using the new identity.
km = API_KEY 'evan';console.logkmperson;// evankmalias'evansolomon';console.logkmperson;// evansolomonkmrecord'foobar';// Recorded as "evansolomon" doing "foobar"
The library support's Kissmetrics' batch API in Node.js (not in browsers). Batching queries works in two parts, adding to the batch queue and processing the batch queue.
To add queries to the queue, you need to pass in a queue object to the
KissmetricsClient constructor when you create your instance.
myQueueObject =someQueueadd'kissmetrics' data;return someQueueget'kissmetrics';;KM = require'kissmetrics';client = null 'Evan' queue: myQueueObject;clientrecord'This event will be batched'setaddedToQueue: 'yup';
The queue object you provide to
KissmetricsClient must expose a method called
add() that accepts an object and adds it to your queue. Once your instance of
KissmetricsClient is created, you can use it normally to record events, properties and aliases. The difference is that those queries will not be sent to Kissmetrics immediately, they'll be formed into objects and added to your queue. You'll also notice that I didn't pass in an API key when I created my client instance. Batch requests send the API key when the batch is processed, and all queries in a batch must use the same API key. You can pass in an API key if you want, like a normal client, it will just be silently ignored.
When you're ready to process the queue, you need to use the
batchProcess() method. You'll need to pass in your a queue object, API key, API secret, and product GUID. Note that these credential are all different than your normal API key, and all come from Kissmetrics.
KM = require'kissmetrics';KMbatchProcessmyQueueObject 'clientKey' 'clientApiSecret' 'product-guid';
The queue object you provid must expose a
get() method. It's possible to provide an entirely different queue object to
Batch.process() than you do to
KissmetricsClient, though for simplicity's sake you may use the same one. The
get() method should return all of the objects that were added to the queue by
KissmetricsClient. Note that managing race conditions is your responsibility and will not be done by the library. It's a good idea to keep track of this in the queue object that you provide.