splunk / splunk-demo-collector-for-analyticsjs

Example Node.js based backend collector for client-side data
93 stars 31 forks source link

Backend Collector for Client-Side Data

This is a Node.js backend collector for client-side data that is tracked by sp.js Analytics JavaScript library. All tracked events are collected in events.log following logging best practices for Splunk log ingestion.

Refer to appendix below on how to use sp.js simple API for tracking.

Getting Started

That's it! After pointing sp.js library to your collector server address using sp.load(<YOUR_COLLECTOR_URL>), watch the tracked events being collected in newly created local file events.log

Additional Resources

Appendix: How to use sp.js Analytics JavaScript Library

Setup

To use sp.js, simply paste the following snippet of code before the closing </head> tag on your page:

<script type="text/javascript">
    var sp=sp||[];(function(){var e=["init","identify","track","trackLink","pageview"],t=function(e){return function(){sp.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var n=0;n<e.length;n++)sp[e[n]]=t(e[n])})(),sp.load=function(e,o){sp._endpoint=e;if(o){sp.init(o)};var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d21ey8j28ejz92.cloudfront.net/analytics/v1/sp.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)};
    sp.load("https://www.example.com"); // Replace with your own Collector URL
</script>

Make sure to replace https://www.example.com with your own collector server URL to send the data to.

API

sp.js provides a common set of tracking methods similar to leading web analytics providers & exemplified by the clean API provided by segment.io.

Here’s the list of tracking methods provided by sp.js:

Full Definition:

sp.track(event, properties, fn)

Track a custom event (i.e. user action) along with a set of associated event properties.

sp.track('Preview Movie', {
    title: 'World War Z',
    category: 'Action',
    loggedIn: false
});

Parameters:

sp.trackLink(links, event, properties)

Track link clicks, including outbound links, with a custom event and custom properties. Tracking occurs before page changes. This automatically records properties such as the anchor (a) tag's href and text.

sp.trackLink($('a.free-download'), 'Click Free Download Link', {
    linkColor: 'Green'
});

Parameters:

sp.pageview(url)

Tracks a 'pageview' event including document title and referrer. This is automatically called by default.

sp.pageview();

Parameters:

sp.identify(userId, userTraits)

Associate a user with an ID, and record user-specific traits or persistent properties. These persistent properties will be automatically added as properties to any subsequent tracked event.

sp.identify("power-user-3961", {
    email: "abc@example.com",
    age: 30,
    gender: "male"
});

Parameters:

sp.init(settings)

Advanced Usage: method to configure library parameters. Typically sp.load(<YOUR_COLLECTOR_URL>) is all you need, and it's already called in the JavaScript snippet that you included in your page header. See Setup section above.

sp.init({
    api_host: <YOUR_COLLECTOR_URL>  // typically set via sp.load(YOUR_COLLECTOR_URL)
    tracking_pageview: true,        // default to tracking all page views
    track_links_timeout: 300,       // default to 300ms
    cookie_name: "_sp",             // defaults to "_sp"
    cookie_expiration: 365,         // defaults to 365 days
    cookie_domain: "example.com"    // defaults to your website domain
});

Parameters: