matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.91k stars 2.65k forks source link

Wrong display resolution (Android) #9371

Closed ghost closed 8 years ago

ghost commented 8 years ago

I've noticed many resolutions are reported wrongly, mainly for Android devices. I'm not sure if this is due to a bug in piwik, or if its due to a fake browser on the client side.

For example, this displays the correct resolution (1280x800):

(user agent)

"Mozilla/5.0 (Linux; Android 4.4.2; IQ1010 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.76 Safari/537.36"

(piwik.php parameter)

&res=1280x800

But...

This displays an invalid resolution (800.08129 ?!?!):

(user agent)

"Mozilla/5.0 (Linux; Android 4.4.2; Lenovo A5500-H Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36"

(piwik.php parameter)

&res=800.0812929868698x1216.7625653743744

Looks like there is a rounding error?

tsteur commented 8 years ago

Is it maybe somehow related to https://github.com/piwik/piwik/issues/8643 or https://github.com/piwik/piwik/issues/4663 ?

Is this using one of our Android or Java Tracking SDKs or via JavaScript in the browser?

ghost commented 8 years ago

Thank you for your quick reply.

I don't think its related to #8643, but #4663 could be related, unfortunately I can't tell for sure because they don't list any examples.

This is via javascript in the browser.

ghost commented 8 years ago

I'm not familiar with piwik, but I had some free time so I took a look at piwik.js. It seems the line 4775 is one culprit:

                browserFeatures.res = screenAlias.width * devicePixelRatio + 'x' + screenAlias.height * devicePixelRatio;

If the devicePixelRatio is... a strange float number, like 1.0001 then multiplying 800 with that, we end up with the wrong number as seen in my real example above.

Maybe the devicePixelRatio is either incorrectly reported by the browser, or the calculation should round down the numbers when the ratio is a float and not 1,2 or 3.

Or maybe I've got it all wrong :)

tsteur commented 8 years ago

We could definitely do something there as it is easy to do. I'd suggest something like

devicePixelRatio = parseFloat(devicePixelRatio).toFixed(3); // I think we could even do "devicePixelRatio.toFixed(2)" but better we make sure to work with int or float. Maybe we would do it only if it already is a float and not an "int"
var width = parseInt(screenAlias.width,10) * devicePixelRatio;
var height = parseInt(screenAlias.height, 10) * devicePixelRatio;
browserFeatures.res = parseInt(width, 10) + 'x' + parseInt(height, 10);
tsteur commented 8 years ago

Moving it into 2.15.1 for now as it might lead to invalid tracking data

mattab commented 8 years ago

Fixing it in piwik.js is useful but it looks like we need to also sanitise the data in the Tracking API, see the similar new issue: https://github.com/piwik/piwik/issues/9911