Friday, 22 February, 2019 UTC


Summary

As a young developer, I would often make decisions based on what I considered to be common sense or what I would envision appealed to users. What a mistake. As I’ve become more experienced as a developer and a businessman, I’ve realized that analytics are the best way to make decisions….and money. There are numerous services that allow you to track analytics but many are difficult to implement, are too costly, or don’t allow you to send and use data appropriately.
My experience with Keen.io, however, was the opposite. Keen is an analytics platform that helps developers build and ship customer-facing metrics easily: collect, store, query, and present data. Keen is available in a host of languages, and allows the developer to access and display information beautifully.
Quick Hits
  • Keen.io allows you to quickly collect, store, query, and present data
  • Perfect for prototyping and being agile
  • Provides helpful code samples in a variety of languages and platforms
  • Low maintenance and low overhead
  • Keen.io allows you to restrict access to data
  • Sign up for free!
Getting Started
Start by signing up for a free account on Keen.io. After a few easy questions, you’re provided your first “stream” and an easy snippet you can add to your site or app to send data. The code snippet is really small; for example, the HTML is:
<!-- HTML: In page -->
<script>
(function(name,path,ctx){ctx[name]=ctx[name]||{ready:function(fn){var h=document.getElementsByTagName('head')[0],s=document.createElement('script'),w=window,loaded;s.onload=s.onerror=s.onreadystatechange=function(){if((s.readyState&&!(/^c|loade/.test(s.readyState)))||loaded){return}s.onload=s.onreadystatechange=null;loaded=1;ctx[name].ready(fn)};s.async=1;s.src=path;h.parentNode.insertBefore(s,h)}}})
('KeenTracking', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/keen-tracking.min.js', this);

KeenTracking.ready(function(){
  const client = new KeenTracking({
    projectId: 'XXX',
    writeKey: 'XXX'
  });

  // Record an event
  client.recordEvent('purchases', {
    item: 'Avocado'
  });
});
</script>
If you prefer Node.js, you can use:
// npm install keen-tracking --save

const KeenTracking = require('keen-tracking');

// This is your actual Project ID and Write Key
const client = new KeenTracking({
    projectId: 'XXX',
    writeKey: 'XXX'
});

// Record an event
client.recordEvent('purchases', {
  item: 'Avocado'
});
Note how easy it is to send the basic metric with recordEvent:
// Record an event
client.recordEvent('purchases', {
  item: 'Avocado'
});
That’s all you need to do to get started collecting data with Keen.io!
Querying
Once you’ve started collecting data, the next step is querying the data for customer-facing visualizations or simple export. The same Keen.io client you use to record event also lets you query collected data. Let’s look at a few examples:
This examples queries purchases and groups them by day:
import KeenAnalysis from 'keen-analysis';

const client = new KeenAnalysis({
  projectId: 'XXX',
  readKey: 'XXX' //here we'll use our read-only key
});

client
  .query({
    analysis_type: 'count', // we'll do a simple count of events
    event_collection: 'purchases', // in the "purchases" collection
    interval: 'daily', // using a daily interval i.e. how many per day
    timeframe: 'this_31_days' // over the last 31 days
  })
  .then(res => {
    // as you might expect, this time res.result is an array of objects matching the daily interval we asked for
    // [{"value": 42, "timeframe": {"start": "2019-01-22T06:00:00.000Z", "end": "2019-01-23T06:00:00.000Z"}},...]
    console.log(res.result) 
    // Handle results, e.g. visualise them with https://github.com/keen/keen-dataviz.js
  })
  .catch(err => {
    // Handle errors
  });
This example queries all purchases within the last 31 days:
import KeenAnalysis from 'keen-analysis';

const client = new KeenAnalysis({
  projectId: 'YOUR_PROJECT_ID',
  readKey: 'YOUR_READ_KEY' //here we'll use our read-only key
});

client
  .query({
    analysis_type: 'count', // we'll do a simple count of events
    event_collection: 'purchases', // in the "purchases" collection
    timeframe: 'this_31_days' // over the last 31 days
  })
  .then(res => {
    console.log(res.result) // the int result of the count of how many purchase events occurred in the last 31 days.
    // Handle results, e.g. visualise them with https://github.com/keen/keen-dataviz.js
  })
  .catch(err => {
    // Handle errors
  });
Visualization
Keen.io provides an awesome querying functionality that provides data in simple format so that you can implement your visualizations with any library you choose, from C3.js to D3.js and more. Check out these examples:
Third Party Visualization Library Keen Visualization Wrapping Up
With many of the analytics apps I’ve tried, I’ve had a hard time figuring out what the next step was and how to access and use the data recorded. Keen.io changed my perception of what I could expect from an analytics service: easy to record, easy to consume, and easy to trust. Try Keen.io for free and you may not look at analytics services the same again!
The post Awesome Analytics with Keen.io (Sponsored) appeared first on David Walsh Blog.