@incrowd/track-util v2.0.0
Generic Event Tracking Module
A collection of JavaScript functions to track events such as page view and social sharing using Snowplow.
Motivation
To reuse the same collection of event tracking functions across all projects powered by FanScore.
Requirements
Snowplow must be set up using the following script:
// Snowplow script
;(function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[];
p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments)
};p[i].q=p[i].q||[];n=l.createElement(o);g=l.getElementsByTagName(o)[0];n.async=1;
n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","//d1fc8wv8zag5ca.cloudfront.net/2.10.0/sp.js","snowplow"));
// Create new tracker
snowplow('newTracker', 'TRACKER_NAME', 'COLLECTOR_ENDPOINT', {
appId: 'com.example.app',
platform: 'web',
contexts: {webPage: true, geolocation: false}
})
Read more about loading Snowplow.
Getting Started
npm i @incrowd/track-util
or yarn add @incrowd/track-util
then in JavaScript:
import trackUtil from '@incrowd/track-util'
const config = {
// Optional, if omitted, calling trackPageView will not store view time
setPageViewStartTime: time => {
// Logic to store time
},
// Optional, if omitted, cannot call trackPageViewTiming
pageViewStartTime: () => {
// return pageViewStartTime
},
// Optional, if omitted, cannot call trackLogin, getUser will always return null and auth context will always be anonymous
authObj: () => {
// return authObj
},
// Optional, default is 'INCROWD'
clientId: () => {
// return clientId
}
}
const icTrack = trackUtil(config)
API
trackPageView (pageName)
icTrack.trackPageView(pageName)
Track page view of given pageName
and setPageViewStartTime
for trackPageViewTiming
.
trackPageViewTiming (pageName, pageDesc, contentId)
icTrack.trackPageViewTiming(pageName, pageDesc, contentId)
Track duration and start time of page view. pageDesc
and contentId
are optional.
trackLogin (source)
icTrack.trackLogin(source)
Track user login. source
is the name given to indicate which application the user is logging into.
trackShare (name, target, contentId, contentDescription)
icTrack.trackShare(name, target, contentId, contentDescription)
Track social sharing of a particular social platform.
trackWebLink (url, name, contentId, contentDescription)
icTrack.trackWebLink(url, name, contentId, contentDescription)
Track navigation to external websites.
trackPollParticipation (data)
icTrack.trackPollParticipation(data)
Track poll participation. Read more about poll participation context
trackPredictorParticipation (data)
icTrack.trackPredictorParticipation(data)
Track predictor participation. Read more about predictor participation context
trackSelfDescribingEvent (schema, data)
icTrack.trackSelfDescribingEvent(schema, data)
Track self-describing event, mostly used by other methods within this library. Read more about self-describing event.
trackStructEvent (category, action, label, property, value)
icTrack.trackStructEvent(category, action, label, property, value)
Read more about structured event.
authContext ()
const context = icTrack.authContext()
Return authentication context base on authObj
, mostly used by other methods within this library.
appContext ()
const context = icTrack.appContext()
Currently return clientId
as versionName
, mostly used by other methods within this library.
getUser ()
const user = icTrack.getUser()
Decode JWT token and return user object. null
is returned if authentication object does not exist. Mostly used by other methods within this library.