Node JS Agent
Use the Node JS Agent to post events to Loggr from your web application.
InstallingHow To UseReading EventsPosting EventsTracking Users
Installing
Installation is simple.
npm install loggr
top
How To Use
This library can read and post events. The first thing you need to do is load the loggr module.
var loggr = require("loggr");Next you need to get a reference to a log using the
LOGKEY and
APIKEY which can be found thru Loggr.net.
var log = loggr.logs.get("YOUR-LOGKEY", "YOUR-APIKEY");Once you have a reference to your log, you can follow the instructions below to read and write events to your log.
top
Reading Events
There are 3 methods for reading events: get(), query() and getData().
events.get() returns a single event given an event id:
log.events.get(id, function (err, ev) {
console.log(ev.text);
}events.query() executes a
Loggr Query Language (LQL) statement and returns the results:
log.events.query("GET events TAKE 10 SORT created DESC", function (err, evs) {
console.log(evs.length);
}Check out the
Events Resource page to see an example of what is returned in the previous two calls.
events.getData() returns the data for a given event id:
log.events.getData(id, function (err, data) {
console.log(data);
}In the getData() call, the data returned is a string.
top
Posting Events
With a log reference you can create and post events using a fluent event wrapper.
log.events.createEvent().text("this is text").post()
log.events.createEvent()
.text("my first event")
.link("http://loggr.net")
.tags("tag1 tag2")
.source("web1")
.user(sUsername)
.value(35.50)
.data("<b>user-agent:</b> {0}<br/><b>on:</b> {1}", navigator.userAgent, new Date())
.dataType(Loggr.dataType.html)
.geo(40.1203, -76.2944)
.post();The text, link, source and data methods can work like the C sprintf function:
log.events.createEvent().text("the date is {0} and the time is {1}", new Date().toDateString(), new Date().toTimeString());Those methods will also replace $$ with the previous value:
log.events.createEvent().text("foo").text("$$bar") // will output "foobar" for the textThe tags method can accept an array of string, a space-delimited string, or multiple string arguments:
.tags(new Array("tag1", "tag2"), "tag3")
.tags("tag1 tag2", "tag3")The text, tags and data methods also have corresponding append methods, which append values. addText(), addTags(), addData() take the same arguments that their setters do.
When settings data, you can specify if the data is to be displayed as HTML or Plain Text using the .dataType() method (plaintext is default)
.dataType(Loggr.dataType.html) or .dataType(Loggr.dataType.plaintext)
When setting geo, you can specify a latitude and longitude, or a prefixed value like:
.geo(40.1203, -76.2944)
.geo("40.1203, -76.2944")
.geo("ip:274.65.485.231")top
Tracking Users
It's really helpful to be able to see details and activity history for the users being assigned to an event. Use trackUser(...) to register a user in your app. Once the user is registered, you'll be able to see their information when examinging an event on Loggr.
Note
We actually recommend putting a call like this on every web page in your website, within the JavaScript of your pages, but in some cases you'll want to do it on the server-side. If that's the case, use the following example. To see how to put in in your web pages, see our Javascript Agent
Each call to trackUser(...) will register an action for a user.
var username = "USERNAME";
var emailAddress = "EMAILADDRESS";
log.trackUser(username, emailAddress);
Make sure you set the 'username' and 'emailAddress' variables to the current user's actual values.
You can also send a 3rd argument to the trackUser() method which would be the "action" the user is performing.
var username = "USERNAME";
var emailAddress = "EMAILADDRESS";
log.trackUser(username, emailAddress, "some action");
top