Express GA Middleware
This is an express middleware to enable Google Universal Analytics page tracking on your server. You can use this with server-served pages, or any custom route events.
Install
npm install --save express-ga-middleware
Usage
To simply track page views use this -
const app = ;const expressGa = ; app; app; app;
If you also want to generate events, we have a .event() middleware too.
var expGa = ; //Use globally for all pageviewsapp; //Use event on a pathapp;
The .event() function is available in the req
object too,
inside ga
object.
app; app
What it tracks
The middleware automatically tracks the following
Tracked parameter | Description |
---|---|
document path | Part of the URL, after the domain name, i.e. /b/c in http://a.com/b/c |
document referer | The website from which the user came from, if any |
user agent | The device/browser/OS used to browse the page |
ip address | The user's ip Address |
All of this is fetched from the request object. Here is the code basically -
dp: reqoriginalUrl dr: req ua: reqheaders'user-agent' uip: reqheaders'x-forwarded-for' || reqconnectionremoteAddress || reqsocketremoteAddress || reqconnectionsocketremoteAddress
Thanks
This is a wrapper over the very useful node module universal-analytics
which in turn used the http://www.google-analytics.com/collect
REST API.