Why does Google Analytics/Mixpanel/etc. send cookies to my server?

3.2k views Asked by At

Let's say I have a website at http://domain.org on which I also have Google Analytics (GA) and Mixpanel (MP) Javascript tracking codes. Both MP and GA store cookies on the user's browser for my entire domain including sub domains (.domain.org).

Because of this every time I do a request to any URL in this entire domain, the cookies for GA and MP are sent along.

New Relic on the other hand store their cookie on the bam.nr-data.net domain.

Why do GA and MP do this? Only in a situation of strange backend hackings one would use the values in these cookies on the backend.

2

There are 2 answers

1
Eike Pierstorff On

Google Analytics uses first party cookies (by injecting the javascript code that's setting the cookie into the web site source) because 3rd party cookies are often blocked (in many browsers they are blocked by default).

Since they are set under your domain name they are sent to your server. That's pretty much a side effect of the original intent (which is to make the tracking more reliable), your are not supposed to use them in your backend.

When setting up Google Analytics you can use the cookie domain parameter to limit ga cookies to a specific part of your domain/subdomain. You cannot, to the best of my knowledge, make GA use a third party cookie.

0
Philip Walton On

On a related note, if your goal is to avoid sending the cookie data with each request, you can tell Google Analytics to use localStorage (instead of cookies) to store the client ID.

Here's a thread on the HTML5Boilerplate repo discussing implementation and the pros and cons of such an approach.

The TL;DR is if you don't have to support IE7 and older, and if you're not tracking across multiple sub-domains, you can use localStorage with no problems.