peaksandpies / universal-analytics

A node module for Google's Universal Analytics and Measurement Protocol
961 stars 146 forks source link

Express & universal-analytics #52

Open Toruide opened 8 years ago

Toruide commented 8 years ago

Hi,

I found a few "wierd thing" when trying to use universal analytics with express. Please be aware that i'm am not a pro dev, just a student.

First doc :

In the doc you say :

express.use(ua.middleware("UA-XXXX-Y", {cookieName: '_ga'}));

But i think the correct way is (according to the sample in readme) :

app.use(ua.middleware("UA-XXXX-Y", {cookieName: '_ga'}));

Second :

I had a very wierd behavior where the CID was not taken from the cookie (_ga). In the end i realized that it was working only when the initialization line was just before my routes

ex :

var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/*Tracking*/
app.use(ua.middleware(config.ga.key, {
  cookieName: '_ga'
}));
app.use('/', routes);

works

var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
/*Tracking*/
app.use(ua.middleware(config.ga.key, {
  cookieName: '_ga'
}));

and

var app = express();
/*Tracking*/
app.use(ua.middleware(config.ga.key, {
  cookieName: '_ga'
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

do not work.

In this 2 cases the function is unable to find the req.cookies and because of that a new cid is created everytime which means you will get a new session everytime a user refresh or change page.

I have no idea why and maybe it's because i have done something wrong but i just wanted to let you know about it.

Thanks for the great work :)

ps : Your code is really easy to read and understand GJ !

sandromartis commented 8 years ago

Hey

I just tested that and I think you're right.

If

app.use(ua.middleware(config.ga.key, {
  cookieName: '_ga'
}));

is BEFORE app.use(cookieParser()) then Universal Analytics does not seem to find the cid that is stored in the cookie and it creates a new one (something in the form of 53816753-23f6-4839-8eed-c99bd6bf33d6).

But if Universal Analytics is AFTER cookieParser then the cid is the same as in the cookie that is stored in the browser (something like 2114748171.1452681493).

I'm not sure if this is a bug of Universal Analytics or intended behaviour. Of course it makes sense that cookies only work properly if cookieParser() is before Universal Analytics. The format 53816753-23f6-4839-8eed-c99bd6bf33d6 still looks odd though.

Anyway, what I noticed is that I had a lot of (not set) values in my Google Analytics Dashboard and now that I looked at it again, I realized, that I actually had the Universal Analytics code BEFORE the cookieParser() in my Express.js config. (Like you described) So I guess a new cid was generated every time I sent a custom tracking event. Thus Google Analytics was not able to match the back-end cid to the font-end cid resulting in (not set) values.

Is this assumption correct? Can somebody confirms this?

Best, Sandro

mebibou commented 8 years ago

@SandroMartis yes, middlewares are used in the order they are defined in your code, if cookieParser is used after then it only parses the cookies after this middleware. Order of use is important for every middleware, nothing to do with this one.

I guess it would be worth mentioning in the README