Open Toruide opened 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
@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
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 :
But i think the correct way is (according to the sample in readme) :
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 :
works
and
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 !