Exclude my own visits
-
Hello,
I would like to exclude my own visits from the statistics.
As I do many tests, logged in as admin or not, and with different browsers, I think the best would be to exclude my IP address from visits (?).
Do you have a code to suggest to me for this?
Thanks.The page I need help with: [log in to see the link]
-
Hey @gilles24 ,
While checking the IP address sounds like a good idea, IMO that’s too much work just for this. There’s no reliable way to get the IP address with JavaScript alone, the general recommendation is to make a request to some third-party API -eg. ipinfo.io- to obtain the real IP address, then wait for its response to check if the IP address matches yours to finally ask WPP to not track your visit. That’s too slow.
An easier and quicker solution would be to check if there’s a parameter attached to the current URL and if so then let’s ask WPP to not track this visit. For example:
/**
* Prevent WPP from tracking views count if the
* donottrack parameter is present.
*/
function wpp_maybe_skip_pageview_tracking() {
if ( is_singular() ) {
?>
<script>
(function() {
const params = new URL(document.location.toString()).searchParams;
if ( params.has('donottrack') ) {
window.wpp_do_request = false;
}
})();
</script>
<?php
}
}
add_action('wp_head', 'wpp_maybe_skip_pageview_tracking', 1);With this code, if you visit
https://www.example.com/some-slug/?donottrack=1WPP will ignore the visit sincedonottrackis present in the URL as a parameter.If you plan on using this code snippet make sure you flush Litespeed Cache’s page cache first to make sure this script is appended to the
<head>section of your posts & pages.Hi Hector,
Thanks for your answer.
But if I understand correctly, this would imply adding ?donottrack=1 every time I visit a page? This doesn’t seem appropriate for my testing (for example I test internal links by clicks, or I do product order tests……)..
That said, I believe I understand that you are starting from the need to search for the IP address… But, knowing my IP address, couldn’t I simply include it in a code excluding this IP address from visits?
But if I understand correctly, this would imply adding ?donottrack=1 every time I visit a page?
Yes, that is correct.
That said, I believe I understand that you are starting from the need to search for the IP address… But, knowing my IP address, couldn’t I simply include it in a code excluding this IP address from visits?
It’s not as simple as you seem to think it is. I probably should have explained things better.
These are the challenges:
- The current page needs to have a way to know your current IP address, and
- The solution should be compatible with page caching since you’re using Litespeed Cache
- The solution would need to know what your IP address is, which by the way can change overtime (unless your ISP provides a static one which isn’t usually the case)
So:
- You’d need to find a way to let the browser know what your IP address is. After you figure that out, you’d need to add some logic so the browser can retrieve the IP address of the current visitor. One way to reliably obtain the IP address of the visitor is by using a third-party service such as ipinfo.io, so you’d be sending a request to said service (which might take a few milliseconds / seconds to respond), then you’d want to compare your IP address that you somehow provided to the browser (eg. hardcoded it into the page) with the one returned by the third-party service. If the IP addresses match then ask WPP to not track the visit.
- Whatever the solution is, it needs to be able to work with Litespeed Cache.
That sounds like a lot of work for me to be doing it free of charge 😛 So, if typing
?donottrack=1sounds like a hassle to you and you prefer something that requires less typing then please go ahead and update the code snippet I shared above so it does everything as described above.-
This reply was modified 1 year, 1 month ago by
Hector Cabrera. Reason: reworded some bits for clarity
ok. thanks
The topic ‘Exclude my own visits’ is closed to new replies.