2

How does Google Analytics calculate the Browser Size metric that it reports for user sessions?

Another thing that would be helpful would be a way to modify a browser to replicate the "(not set)" reporting in Google Analytics.

There is no documentation on the logic they are using that we can find.
This is roughly their algorithm as far as we can tell:

  1. Get the window.innerWidth or the document.body.clientWidth or the window.outerWidth...
  2. Round that to the nearest 10.
  3. Do basically the same for height.
  4. If either can't be calculated for whatever reason, return "(not set)".
  5. But if we can get both values, return "widthxheight" i.e. "680x800".

The thing is, step 1 could be done in a variety of ways, and so far in our attempts to equally replicate, we have been unsuccessful.

Background: Long story short, we have a lot of bot traffic, and we've been able to filter them out with GA's Browser Size metric.
These bots all report in GA a Browser Size of "(not set)".
We intend to block all of these bots on our caching service (which can't run any client-side js).
So to do this, we are setting a cookie in a user's session when they visit if they have that browser size and then the caching service can see that cookie and block.
But our attempts to create this cookie have created too many false positives, so we're missing something from GA's algorithm.

2
  • Update: After running more tests and looking at the "vp" parameter that GA sends up, it looks like Google Analytics is using at first document.body.clientWidth for the width and window.innerHeight for the height. And if window.innerHeight is 0, it instead uses document.body.clientHeight. And it limits the height to 1610. I haven't been able so far to consistently recreate conditions for (not set) to appear. Commented Mar 16, 2022 at 18:38

1 Answer 1

2

Internet Explorer ended up helping me out here.
It let me shrink its window to the point that the height of the window was less than 0, and I started seeing "(not set)" values in Google Analytics.

The following is definitely not a complete description of how Google Analytics determines Browser Size, but it's close enough for me to move forward with what I am working on.

First, Google Analytics runs some javascript to get the browser dimensions:

  1. Get the document.documentElement.clientHeight.
  2. If that’s 0, get the document.body.clientHeight instead, but if it’s less than 0, fail out and return an empty string. (if I had to guess, anything other than a 0 or positive int causes this to fail)
  3. Do the same for width. (I cannot get the width to below 0 in my testing like I can with height, but it makes sense that it would fail in the same way)
  4. Return "widthxheight" i.e. "682x795".

Whatever is returned, Google Analytics sends that up to the server with the “vp” parameter. (presumably standing for ViewPort) On the server, Google Analytics then does the following:

  1. If the session for this user is already set, don’t update Browser Size and stop here.
  2. Otherwise, if the value is an empty string, set Browser Size to “(not set)” and stop here.
  3. Otherwise, round this widthxheight value to the nearest 10. (“682x795” -> “680x800”)
  4. If the height value is higher than 1610, it’s set to 1610. (“680x2100” -> “680x1610”)
  5. There’s probably also a maximum width value, but I wasn’t able to reach it with the browsers I used. If someone knows this or finds out, let’s update this.
  6. Set Browser Size to this value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.