Skip to content

plan3/sdk-js-event-tracking

Repository files navigation

sdk-js-event-tracking

Get up and running:

npm install

grunt

When developing:

grunt watch

Documentation

Example usage

Snippet

Create and send an event

<head>
    <script src="dist/tracker.js"></script>
</head>
<body>
    <script type="text/javascript">
        // Set your options
        var opt = {
            clientId:   '1234asdf',
            pageType:   'Article',
            pageId:     '98798342'
        }

        // Create your activity. It needs your options as a parameter.
        var activity = new Activity(opt);

        // When you have your activity, you can track an event
        activity.events.trackPageLoad('Test article', 'Read').send();

    </script>
</body>

Queue events and send a batch send

// Set your options
var opt = {
    clientId:   '1234asdf',
    pageType:   'Article',
    pageId:     '98798342'
}

// Create your activity. It needs your options as a parameter.
var activity = new Activity(opt);

// Use .queue() to add events to the queue.
activity.events.trackComment('843223', 'Post').queue();
activity.events.trackPoll('843223', 'Post').queue();

// And manually send the queue.
activity.sendQueue();

Add parameters and customData

// Set your options
var opt = {
    clientId:   '1234asdf',
    pageType:   'Article',
    pageId:     '98798342'
}

// Create your activity. It needs your options as a parameter.
var activity = new Activity(opt);

// Use .queue() to add events to the queue.
activity.events.trackComment('843223', 'Post').addCustomData('primary', {words: 32, lines: 3}).send();
activity.events.trackPoll('843223', 'Post').addProperty('secondary', 'displayName', 'Is this a question?').send();

primary, secondary and tertiary is used to tell which object you want to add data to. Each event function described below has specified the objects being generated and which reference the object coresponds with.

Activity(opts)

opts = {
    pageId: '',     // The Id of the current page/article (required)
    clientId: '',   // The providers ID, supplied by SPT. (required)
    pageType: '',   // The type of page where the Activity is created. Default 'Page'
    url: '',        // The URL that data will be sent to.
    transport: f()  // To specify custom transport function.
    provider: {}    // Add custom provider data
}

Activity.refreshUserIds(userId)

Used for login / logout events. When a user logs in, call this function with a user ID in the following format: urn:<your domain>:user:<your userId>. When the user logs back out, call the same function with undefined.

Activity.Event(eventType)

Activity.Event.trackPageLoad(title, activityType)

Required parameters

title - The title of the page. Will default to document.title

activityType - The type of activity that is being tracked. Will default to 'Read'

Generated objects

object as 'primary' - Where the event originated.

Extra parameter suggestions

content store the content of the submitted form.

Activity.Events.trackForm(formId, contentType, activityType)

Required parameters

formId - The ID of the form element, must be unique per page.

contentType - The type of content generated by the form (note, spt:email, other? )

activityType - The type of activity that is being tracked. Will default to 'Post'

Generated objects

object as 'primary' - The object of the Activity. In this case, the form.

result as 'secondary' - The result of the event. In this case, some new content from the form.

origin as 'tertiary' - Where the Activity originated (e.g the page)

Extra parameter suggestions

displayName - The title of the form in a human readable format.

content store the content of the submitted form.

inReplyTo a string or a object that links the this form submission to a some other entity.

url A URL will be set automatically using document.URL, but if you are running a single page application or for some other reason want to change the URL, feel free to do so.

Activity.Events.trackComment(formId, activityType)

Required parameters

formId - The ID of the form element, must be unique per page.

activityType - The type of activity that is being tracked. Will default to 'Post'

Generated objects

object as 'primary' - The object of the Activity. In this case, the form.

result as 'secondary' - The result of the event. In this case, some new content from the form.

origin as 'tertiary' - Where the Activity originated (e.g the page)

Extra parameter suggestions

content - store the content of the comment.

inReplyTo - a string or a link object that links this comment to a page/article or another comment.

Custom data suggestion

spt:commentId - The ID of the comment being generated by the comment form

Activity.Events.trackPoll(formId, activityType)

Required parameters

formId - The ID of the form element, must be unique per page.

activityType - The type of activity that is being tracked. Will default to 'Post'

Generated objects

object as 'primary' - The object of the Activity. In this case, the form.

result as 'secondary' - The result of the event. In this case, some new content from the form.

origin as 'tertiary' - Where the Activity originated (e.g the page)

Extra parameter suggestions

question - The question being asked, will be stored in displayName in primary object.

oneOf - A object in the object with @type 'collection' and items property with a array of possible answers.

replies - A object in the result with @type 'collection' and items property with a array of answers.

Activity.Events.trackClick(elementId, displayName, targetType, targetId, activityType)

Required parameters

elementId - The ID of the clicked element.

displayName - The name/title of the clicked element.

targetType - The type of entity that is the target for the action (page, application, process, article)

targetId - The ID of the target.

activityType - The type of activity that is being tracked. Will default to 'Accept'

Generated objects

object as 'primary' - Where the event originated. In this case, the clicked element.

target as 'secondary' - The target of the action. Normally the current page or where a users ends up after the action.

Extra parameter suggestions

displayName To get a good textual representation. Can be used for both primary and secondary object.

Activity.Events.trackSocial(elementId, networkName, activityType)

Required parameters

elementId - The ID of the button or link the user clicks to achieve a social action (e.g Facebook like button)

networkName - The name of the social network. (Facebook, Twitter, Pintrest etc.)

activityType - The type of activity that is being tracked. Will default to 'Like'

Generated objects

object as 'primary' - The page/article/entity being shared/liked.

origin as 'secondary' - Where the event happened, such as a button or link.

target as 'tertiary' - The target of the action. The socialmedia site receiving the action.

Extra parameter suggestions

displayName To get a good textual representation. (for all objects)

Activity.Events.trackMediaState(mediaId, mediaType, activityType)

Required parameters

mediaId - The ID of the media entity.

mediaType - The type of media (video, audio, album)

activityType - The type of activity that is being tracked. Will default to 'Watch'

Generated objects

object as 'primary' - The media entity where a state change happened.

origin as 'secondary' - The page where the event was generated.

Extra parameter suggestions

displayName To get a good textual representation.

url The URL to the page.

Activity.Events.trackScroll(scrollDepth, activityType)

Required parameters

scrollDepth - A value representing the relative scroll distance. Will be stored as a content parameter in the target.

activityType - The type of activity that is being tracked. Will default to 'Arrive'

Generated objects

object as 'primary' - The page or entity where scroll happened.

target as 'secondary' - The scroll depth.

Extra parameter suggestions

displayName - A human readable text for the scroll distance (e.g "Under header")(for target)

Activity.Events.trackVisibility(elementId, time, activityType)

Required parameters

elementId - The ID of the element that are tracked.

elementId - Either a string with start time as a AS 2.0 compatible timestamp, or a object {start: , end: <timestamp}

activityType - The type of activity that is being tracked. Will default to 'Arrive'

Generated objects

origin as 'primary' - The page or entity where the element is located.

object as 'secondary' - The element that is tracket.

Extra parameter suggestions

displayName - A human readable text for the element.

Activity.Events.trackExit(targetId, targetType, activityType)

If no target is available, set targetId and targetType to undefined.

Required parameters

targetId - The ID of where the user goes next.

targetType - The type of the target for the users exit (page, article, application)

activityType - The type of activity that is being tracked. Will default to 'Leave'

Generated objects

object as 'primary' - The page or entity where exit happened.

target as 'secondary' - Target action for the exit, for example the next page.

Extra parameter suggestions

displayName - A human readable text such as a title or description.

Activity.Events.trackEngagementTime(duration, activityType)

Required parameters

duration - A integer representing the number of milliseconds that the engagement has lasted.

activityType - The type of activity that is being tracked. Will default to 'View'

Generated objects

object as 'primary' - The page or entity the engagement is taking place.

Extra parameter suggestions

displayName - A human readable text for the scroll distance (e.g "Under header")(for target)

Activity.Events.trackCustomEvent(obj, activityType)

A function for tracking any other events. Takes an object with Activitystream 2.0 compatible objects. Actor and header info will be added by the function.

Required parameters

obj - A object of Activitystream 2.0 objects (like object, target and origin).

activityType - The type of activity that is being tracked. No default

Activity.Event.addProperty(obj, property, value)

Required parameters

object - The target object in the activity (object, target, result, origin etc).

property - The property that should be added. Allowed properties: http://www.w3.org/TR/2015/WD-activitystreams-vocabulary-20150129/#properties '4. Properties'

value - Preferably a string, but an objects will work as well. If a raw Activitystream object is inputed, it can compromise future compatibility.

Activity.Event.addCumstomData(obj, data)

Required parameters

object - The target object in the activity (object, target, result, origin etc).

data - An object. All data will be a sub-object of the property spt:custom. All property values in the data object should be prefixed with spt:

Activity.Event.send()

Will ship the built event objects to the Activity object to get providor and actor, before it gets sent to the server.

Activity.Event.queue()

Will add the created activity to a queue to be sent later.