
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This is v4 of the Keen IO JS SDK. Previous versions and their documentation are available as [branches](https://github.com/keen/keen-js/branches) of this repo.
This is v4 of the Keen IO JS SDK. Previous versions and their documentation are available as branches of this repo.
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.
Install this package from NPM:
npm install keen-js --save
Or load it synchronously from the CDN:
<link href="https://d26b395fwzu5fz.cloudfront.net/4.3.0/keen.min.css" rel="stylesheet" />
<script src="https://d26b395fwzu5fz.cloudfront.net/4.3.0/keen.min.js"></script>
Under the hood, this is simply a bundled release of the following packages:
When keen-js is loaded as a global, these packages will be assigned to a shared Keen
namespace. We recommend using the standalone packages that best suit your needs.
The following examples are intended to help you get up and running quickly with keen-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.
This package contains keen-tracking.js as-is, and can be used interchangeably. If you only need tracking functionality, we recommend using the standalone package.
Full documentation is available in the keen-tracking.js repo.
Automatically record pageviews
, clicks
, and form_submissions
events with robust data models:
<link href="https://d26b395fwzu5fz.cloudfront.net/4.3.0/keen.min.css" rel="stylesheet" />
<script src="https://d26b395fwzu5fz.cloudfront.net/4.3.0/keen.min.js"></script>
<script>
Keen.ready(function(){
var client = new Keen({
projectId: 'YOUR_PROJECT_ID',
writeKey: 'YOUR_WRITE_KEY'
});
client.initAutoTracking();
});
</script>
Learn how to configure and customize this functionality here
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 Keen from 'keen-js';
// import Keen from 'keen-tracking';
const client = new Keen({
projectId: 'PROJECT_ID',
writeKey: 'WRITE_KEY'
});
const helpers = Keen.helpers;
const utils = Keen.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: { /* Enriched */ },
ip_address: '${keen.ip}',
},
page: {
info: { /* Enriched */ },
title: document.title,
url: document.location.href
},
referrer: {
info: { /* Enriched */ },
url: document.referrer
},
tech: {
browser: getBrowserProfile(),
info: { /* Enriched */ },
user_agent: '${keen.user_agent}'
},
time: helpers.getDatetimeIndex(),
visitor: {
guest_id: sessionCookie.get('guest_id')
/* Include additional visitor info here */
},
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('pageview', {});
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.
More examples:
What else can this SDK do?
React Examples
Documentation: Full documentation is available in the keen-tracking.js repo.
Keen's powerful Compute API gives you fast answers to the questions that matter.
This package contains keen-analysis.js as-is, and can be used interchangeably. If you only need compute functionality, we recommend using the standalone package.
Full documentation is available in the keen-analysis.js repo.
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 Keen from 'keen-js';
// import Keen from 'keen-analysis';
const client = new Keen({
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 => {
// Handle results
})
.catch(err => {
// Handle errors
});
What else can this SDK do?
Documentation: Full documentation is available in the keen-analysis.js repo.
This package contains keen-dataviz.js as-is, and can be used interchangeably.
Examples: keen.github.io/keen-dataviz.js.
Documentation: Full documentation is available in the keen-dataviz.js repo.
<html>
<head>
<link href="https://d26b395fwzu5fz.cloudfront.net/4.3.0/keen.min.css" rel="stylesheet" />
<script src="https://d26b395fwzu5fz.cloudfront.net/4.3.0/keen.min.js"></script>
</head>
<body>
<!-- DOM Element -->
<div id='my-chart-div'></div>
<!-- Create and Render -->
<script>
var client = new Keen({
projectId: 'YOUR_PROJECT_ID',
readKey: 'YOUR_READ_KEY'
});
var chart = new Keen.Dataviz()
.el('#my-chart-div')
.height(180)
.title('Pageviews (14d)')
.type('area')
.prepare();
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>
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.
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:
4.3.0 Module Updates
Stream: keen-tracking.js Release: Protocol Handling and Dev Setup (v1.4.0)
Visualize: keen-dataviz.js Release: Bug Fix and PR Template (v1.2.1)
<a name="4.2.0"></a>
FAQs
### Get API Keys
The npm package keen-js receives a total of 535 weekly downloads. As such, keen-js popularity was classified as not popular.
We found that keen-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.