Coder Social home page Coder Social logo

teebot / kissmetrics-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evansolomon/kissmetrics-js

0.0 0.0 0.0 1.2 MB

Server (Node.js) + client JavaScript library for Kissmetrics

Home Page: http://evansolomon.github.com/kissmetrics-js/

License: MIT License

kissmetrics-js's Introduction

Build Status

Kissmetrics JS

A small library to interact with Kissmetrics that can be shared across a client (browser) and server (Node.js).

The minified source is about 3k, compared to about 20k for the version served by Kissmetrics. If you gzip the minified source, it is about 1k (Kissmetrics does not gzip their JavaScript for backward compatability reasons with older browsers). The only feature missing is the "automatic" events that Kissmetrics will record for you. These are things like page views or site visits. Since only Kissmetrics has access to the options you've sent, it's not possible to know which events you want to record automatically without using their JavaScript. For that trade off you save 95% of the file size and can use the same library on the client and server.

Annotated Source

In place of a more detailed readme, the annotated source is very thorough.

Installation

Install from NPM with: npm install kissmetrics-js

In a browser, load the compiled JavaScript.

<script src="min/kissmetrics.min.js"></script>
<script>
km = new KissmetricsClient(API_KEY, user.name);
// ...
</script>

In Node.js, you probably just want to require the module.

KM = require('kissmetrics-js');
km = new KM(API_KEY, user.name);
// ...

Usage Examples

Record data about a logged in user.

// General activity
km = new KissmetricsClient(API_KEY, user.name);
km.record('Published post');
km.set({last_seen: new Date()});

// Change username
km.record('Change username');
km.alias(user.name);

Record data about a logged out user.

// New visitor
km = new AnonKissmetricsClient(API_KEY);
km.record('Visited front page');
km.record('Visited signup form');

// Signs up
km.record('Signed up');
km.alias(user.name);

// Record more data as the new logged in user
km = new KissmetricsClient(API_KEY, user.name);
km.record('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 false.

km = new AnonKissmetricsClient(API_KEY);
km.record('Signed up').alias(user.name);
console.log(km._storage.get());
// null

km = new AnonKissmetricsClient(API_KEY);
km.record('Signed up').alias(user.name, false);
console.log(km._storage.get());
// "56a44b65ddad8a4ab00885ec42e7d2f7db46dcd69c3f"

Data methods can be chained.

km = new AnonKissmetricsClient(API_KEY);
km.record('Visited front page')
	.record('Visited signup form')
	.record('Signed up')
	.alias(user.name)
	.record('Published post');

Calling alias() updates the instance's person attribute, so future data is recorded using the new identity.

km = new KissmetricsClient(API_KEY, 'evan');
console.log(km.person);
// evan

km.alias('evansolomon');
console.log(km.person);
// evansolomon

km.record('foobar');
// Recorded as "evansolomon" doing "foobar"

Batching

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 = {
  add: function(data) {
    someQueue.add('kissmetrics', data);
  },
  get: function() {
    return someQueue.get('kissmetrics');
  }
};

KM = require('kissmetrics');
client = new KM(null, 'Evan', {queue: myQueueObject});
client.record('This event will be batched').set({addedToQueue: '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');
KM.batchProcess(myQueueObject, '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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.