December 11, 2020

Blogger: Analytics issue because of mobile views

I've noticed following trouble with Blogger and Google Analytics:
A same page will have different analytics wether it is opened from mobile or not.
I present here a solution to fix this.

Problem description

In Google Analytics the same page has different views whether it was opened from a mobile or a desktop.
See in the screenshot below, pages opened on mobile have a different URL with ?m=1 at the end.


This problem is well described also here but the proposed solution is wrong.

Solution

To fix this I have added in my HTML Template, just above the </head> tag:

<script type='text/javascript'>
//<![CDATA[
//** TD fix see https://tdalon.blogspot.com/2020/12/blogger-analytics-mobile-issue.html
var uri = window.location.toString();
if (uri.indexOf("?m=1") > 0) {
var clean_uri = uri.replace("?m=1","");
window.history.replaceState({}, document.title, clean_uri);
}
//]]>
</script>

Credit: idea for this fix came from https://www.techforftcp.com/2020/06/how-to-remove-m1-from-url-in-blogger-in-hindi.html but I think I have cleaned-up the proposed code a bit.

No comments:

Post a Comment