Keen IO JavaScript SDK (v5)
This is v5 of the Keen IO JS SDK. Previous versions and their documentation are available as branches of this repo.
Get Project ID & API Keys
If you haven’t done so already, login to Keen IO to create a project. The Project ID and API Keys are available on the Access page of the Project Console. You will need these for the next steps.
Installation
Install this package from NPM:
npm install keen-js --save
Under the hood, this is simply a bundled release of the following packages:
Stream - Keen Tracking JS
What is an event? An event is a record of something important happening in the life of your app or service: like a click, a purchase, or a device activation.
Full documentation is available in the keen-tracking.js repo.
Automated Event Tracking (Browser-only)
Automatically record pageviews
, clicks
, and form_submissions
events with robust data models.
Learn how to configure and customize this functionality here
Pageview Tracking (Browser/Front-end)
First, let's create a new client
instance with your Project ID and Write Key, and use the .extendEvents()
method to define a solid baseline data model that will be applied to every single event that is recorded. Consistent data models and property names make life much easier later on, when analyzing and managing several event streams. This setup also includes our data enrichment add-ons, which will populate additional information when an event is received on our end.
import KeenTracking from 'keen-tracking';
const client = new KeenTracking({
projectId: 'PROJECT_ID',
writeKey: 'WRITE_KEY'
});
const helpers = KeenTracking.helpers;
const utils = KeenTracking.utils;
const sessionCookie = utils.cookie('rename-this-example-cookie');
if (!sessionCookie.get('guest_id')) {
sessionCookie.set('guest_id', helpers.getUniqueId());
}
client.extendEvents(() => {
return {
geo: {
info: { },
ip_address: '${keen.ip}',
},
page: {
info: { },
title: document.title,
url: document.location.href
},
referrer: {
info: { },
url: document.referrer
},
tech: {
browser: helpers.getBrowserProfile(),
info: { },
user_agent: '${keen.user_agent}'
},
time: helpers.getDatetimeIndex(),
visitor: {
guest_id: sessionCookie.get('guest_id')
},
keen: {
addons: [
{
name: 'keen:ip_to_geo',
input: {
ip: 'geo.ip_address'
},
output : 'geo.info'
},
{
name: 'keen:ua_parser',
input: {
ua_string: 'tech.user_agent'
},
output: 'tech.info'
},
{
name: 'keen:url_parser',
input: {
url: 'page.url'
},
output: 'page.info'
},
{
name: 'keen:referrer_parser',
input: {
referrer_url: 'referrer.url',
page_url: 'page.url'
},
output: 'referrer.info'
}
]
}
}
});
client.recordEvent('pageviews', {});
Every event that is recorded will inherit this baseline data model. Additional properties defined in client.recordEvent()
will be applied before the event is finally recorded.
Want to get up and running faster? This can also be achieved in the browser with automated event tracking.
Node.js Event Recording (Back-end)
const KeenTracking = require('keen-tracking');
const client = new KeenTracking({
projectId: 'PROJECT_ID',
writeKey: 'WRITE_KEY'
});
client.recordEvent('purchases', {
item: 'Avocado',
price: 123
});
More examples:
What else can this SDK do?
React Examples
Documentation: Full documentation is available in the keen-tracking.js repo.
Compute - Keen Analysis JS
Keen's powerful Compute API gives you fast answers to the questions that matter.
Full documentation is available in the keen-analysis.js repo.
Running a Query
Create a new client
instance with your Project ID and Read Key, and use the .query()
method to execute an ad-hoc query. This client instance is the core of the library and will be required for all API-related functionality.
import KeenAnalysis from 'keen-analysis';
const client = new KeenAnalysis({
projectId: 'YOUR_PROJECT_ID',
readKey: 'YOUR_READ_KEY'
});
client
.query('count', {
event_collection: 'pageviews',
group_by: 'device_type',
interval: 'daily',
timeframe: 'this_14_days'
})
.then(res => {
})
.catch(err => {
});
What else can this SDK do?
Documentation: Full documentation is available in the keen-analysis.js repo.
Visualize - Keen Dataviz JS
Documentation: Full documentation is available in the keen-dataviz.js repo.
Examples: keen.github.io/keen-dataviz.js.
Rendering a Chart
<html>
<head>
<meta charset="utf-8">
<script src="https://d26b395fwzu5fz.cloudfront.net/keen-analysis-2.0.0.min.js"></script>
<link href="https://d26b395fwzu5fz.cloudfront.net/keen-dataviz-2.0.4.min.css" rel="stylesheet" />
<script src="https://d26b395fwzu5fz.cloudfront.net/keen-dataviz-2.0.4.min.js"></script>
</head>
<body>
<div id="my-chart-div"></div>
<script>
const chart = new Keen.Dataviz()
.el('#my-chart-div')
.colors(['red', 'orange', 'green'])
.height(500)
.title('New Customers per Week')
.type('metric')
.prepare();
const client = new Keen({
projectId: 'YOUR_PROJECT_ID',
readKey: 'YOUR_READ_KEY'
});
client
.query('count', {
event_collection: 'pageviews',
timeframe: 'this_14_days',
interval: 'daily'
})
.then(function(res){
chart
.data(res)
.render();
})
.catch(function(err){
chart
.message(err.message);
});
</script>
</body>
</html>
Contributing
This is an open source project and we love involvement from the community! Hit us up with pull requests and issues.
Learn more about contributing to this project.
Support
Need a hand with something? Shoot us an email at team@keen.io. We're always happy to help, or just hear what you're building! Here are a few other resources worth checking out: