Get the library
$ npm install stream-analytics
$ bower install stream-analytics
Or load the library from our CDN:
<script src="https://xxx.cloudfront.net/..." type="text/javascript"></script>
Configure an instance for your project
var client = new StreamAnalytics({
projectId: "YOUR_PROJECT_ID",
writeKey: "YOUR_WRITE_KEY"
});
Track impressions
Every activity (eg. messages) shown to the user should be tracked as an impression
var impression = {
viewerId: 'user:Thierry',
activityId: 'message:34349698',
feedId: 'user:ChartMill',
timestamp: new Date().toISOString()
};
client.trackImpression(impression);
client.trackImpressions(impressions);
Engagement tracking
Every meaningful user interactions should be tracked with content (activity_id) and source (sourceFeedId) information and provide a label and a score (the modifier) for the engagement. modifier is a signed integer. Positive values boost source and content for the user that trigger the engagement, negative values work as a discount for the same context. The value of the modifier can be used to differentiate engagements according to their importants (eg. sharing a message is probably more important than just clicking it).
engagement = {...}
client.trackEngagementEvent(engagement);
client.trackEngagementEvents(engagements);
var engagement = {
engagement: {
label: 'click',
modifier: 10
},
userId: 'user:Thierry',
activityId: 'message:34349698',
sourceFeedId: 'user:ChartMill',
timestamp: new Date().toISOString()
};
client.trackEngagementEvent(engagement);
var engagement = {
engagement: {
label: 'share',
modifier: 100
},
userId: 'user:Thierry',
activityId: 'message:34349698',
feedId: 'user:ChartMill',
timestamp: new Date().toISOString()
};
client.trackEngagementEvent(engagement);
Update user data
this allows for storing extra information about users such as public profile IDs
var userData = {
userId: 'user:Thierry',
twitterId: '15875029',
facebookId: '784785430'
};
client.updateUserData(userData);