Netlify User Activity Aggregation
To keep track of activity across multiple Netlify properties, we need a centralized place where we can store activity.
This activity is used to track progress (e.g. how much of an educational resource has been completed), determine whether someone has completed the steps to earn a community badge or other bonuses, benefits, or bragging rights.
This module is a write-only helper that makes sending activity data to the central service as painless as possible.
Installation
npm i @netlify/activity-hub
Usage
This app assumes you’ve already authenticated the user and that you have their Netlify user ID. If you need to get this, you can use Netlify OAuth and the Netlify API to retrieve it.
import netlifyActivityHub from '@netlify/activity-hub';
// this is pseudo-code; your app needs to provide the user object
const user = getNetlifyUser()
const sendActivity = netlifyActivityHub({
userId: user.id,
app: "your-app-name", // use the same app name everywhere in your app
});
// whenever you want to send activity, call `sendActivity`
document.querySelector('button').addEventListener('click', () => {
sendActivity('button-click', {
page: window.location.pathname,
})
});
API
netlifyActivityHub
The main function exported by @netlify/activity-hub
accepts global config and returns a function that will send activity to the Activity Hub.
import netlifyActivityHub from '@netlify/activity-hub';
// see the next section for the config object’s shape
const sendActivity = netlifyActivityHub(config);
property | type | description |
---|---|---|
userId |
string |
a Netlify user ID, as returned from the Netlify API |
app |
string |
a unique identifier for your app. this can be used to group activity later |
sendActivity
The function returned from netlifyActivityHub
accepts an event type as a string, and an object full of arbitrary data to track whatever details you want about the activity.
sendActivity(type, data);
property | type | description |
---|---|---|
type |
string |
any string to identify the activity type (e.g. start-playback ) |
data |
object |
any JSON.stringify -able data in an object that can be attached to the activity |
NOTE: don’t store any personally identifying data in the data
object