In the Blogger dashboard, when you click Stats
-> Overview
->Manage tracking your own pageviews
and check Don't track my views for this blog.
, your pageviews are expected to be excluded from the stats.
But your pageviews might be counted, and the checkbox is unchecked each time you restart the browser.
Causes
When Don't track my views for this blog.
is checked, it invokes the following JavaScript setting the Cookie.
var COOKIE_NAME = '_ns';
var COOKIE_SET_VALUE = '2';
document.cookie = COOKIE_NAME + '=' + COOKIE_SET_VALUE;
But it has some issues as follows.
Domain attribute
It doesn’t specify the domain attribute, so current sub domain, blogname.blogspot.com, is set to the Cookie. If you are redirected to a country-specific URL (ccTLD), blogname.blogspot.ccTLD, the cookie is not sent because it has the different domain.(Redirect to the ccTLD has been expired. See Official Blogger Blog: It’s spring cleaning time for Blogger.)
Note: The domain attribute is supposed to be specified to apply the cookie to all sub domains under the domain. If you want the cookie for the specific sub domain like this case, you need not specify the domain attribute. And you can not specify the other domain you are requesting.
Path attribute
It doesn’t specify the path attribute, so the current page path /b
is set for the path attribute. As a result, the cookie is sent only when requested pages are under /b/
.
Expires attribute
As the expires attribute is also not specified, the cookie is deleted when the browser is closed (aka Session Cookie).
FYI
Workaround
The script modified the above issues is as follows.
document.cookie = "_ns=2; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
If you are using Chrome, after visiting your blog, start Developer Tools (press F12
). Paste this script in Console
and run (press Enter
).
(In Chrome Developer Tool, you can see and edit the Cookies at Application
-> Cookies
.)
- You can’t set a cookie to never expire. So I set 03:14:07 UTC on Tuesday, 19 January 2038 for the expires attribute, the latest time avoiding the Year 2038 problem.
I don’t usemax-age
attribute because IE11 doesn’t support.
If your access to your blog is redirected to blogname.blogspot.ccTLD and you want to keep Don't track my views for this blog.
unchecked (which has no practical sense), execute the above script after visiting blogger’s dashboard as well.
Here is another version, which expires
date is in a plain way.
var d = new Date("2038-01-19"); document.cookie = "_ns=2; expires=" + d.toGMTString() + "; path=/";
You can modify "2038-01-19"
as you like (not to exceed “2038-01-19”).
If you specify the date in the past, you can delete the cookie.
Setup with Smartphones
You may want not to count the pageviews from smartphones, which browser doesn’t have Developer Tools. In such case, after visiting your blog with your smartphone’s browser, clear the address bar and type javascript:
and paste the above script just after it, then press Enter
.
And, if you input javascript:document.cookie;
in the address bar (the last semicolon is probably not necessary), the current page’s cookie can be shown. If _ns=2
is displayed, the cookie is set properly.
Excellent info, thanks!
ReplyDeleteCan I use the javascript above directly in my theme so I don't have to play around at developer tool?
ReplyDeleteI'm afraid that pageviews from visitors other than you will not be counted either.
Delete