简体   繁体   中英

Google Analytics filter for WordPress Admin

My site is showing hits in Google Analytics when I am updating the site in WordPress. I want to create a filter in the analytics dashboard to exclude these pageviews.

Requirements:

  1. works with any url including browsing the site pages. (ie not just preview=true pages which has been suggested here )
  2. Used in the theme's functions.php file and not in .htaccess

I would like to use a similar tactic as the aforementioned article and add a query string var analytics=false when administrators are logged in and then I can add a filter to exclude urls that include the regex analytics=false .

What I have tried:

if(is_user_logged_in() && !is_admin()){
    if(current_user_can('administrator')){
        //not sure what to put here to add the query var
    }
}

I ended up just adding a bit more to the script that creates a pageview in analytics.

//check if we are logged in
if(!document.body.classList.contains('logged-in')){
  //we are not logged in, log a pageview
  ga('create', 'UA-123456-1', 'auto');
  ga('send', 'pageview');
}

This doesn't work if you need to check if the user is admin or not but my site doesn't allow subscribers so it will work.

Thanks @willoller.

PS I cannot exclude my own IP as it changes everytime I reconnect to my VPN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM