Javascript integration of segment.com HTTP API
segment.com provides a javascript library, Analytics.js, so why osjs ? The main advantage of osjs is to have a queue system that allows to register events even if there is no internet connection : the events are sent to segment.com when the connection is back.
-
Put osjs.js or osjs.min.js in your project.
-
Initialize the osjs object with your segment.com write key
osjs.setOptions({ "write_key" : "mysegmentwritekey" });
-
Use the functions available in the HTTP segment API
-
Identify
osjs.identify({ "userId": "019mr8mf4r", "traits": { "email": "[email protected]", "name": "Peter Gibbons", "industry": "Technology" }, "context": { "ip": "24.5.68.47" }, "timestamp": "2012-12-02T00:30:08.276Z" });
-
Group
osjs.group({ "userId": "019mr8mf4r", "groupId": "8e9df332ac", "traits": { "name": "Initech", "industry": "Technology", "employees": 420 } });
-
Track
osjs.track({ "userId": "019mr8mf4r", "event": "Purchased Item", "properties": { "name": "Leap to Conclusions Mat", "revenue": 14.99 }, "context": { "ip": "24.5.68.47" }, "timestamp": "2012-12-02T00:30:12.984Z" });
-
Page
osjs.page({ "userId": "019mr8mf4r", "name": "Tracking HTTP API" });
-
Screen
osjs.screen({ "userId": "019mr8mf4r", "name": "Tracking HTTP API" });
-
Alias
osjs.alias({ "userId": "019mr8mf4r", "name": "Tracking HTTP API" });
-
Import is not yet supported
-
You can have more details here.
- You don't have to specify the timestamp. If you have not specified one, it will set the timestamp to the time the event was triggered. So it doesn't matter if events are triggered when there is no Internet connection.