AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
3.3K members online now
3.3K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Exclude logged in users wordpress without plugin

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello,

 

how can I implement google analytics on a wordpress website without plugin, and exclude all logged in users. No IP exclusion, that won't work, because I change IP once a week. Plus my freelancers travel, etc. IP exclusion is not good.

 

Is there a way to exclude all logged in users without a wordpress plugin?

 

I did search online but I didn't find any decent solution, they were all half-half, with more cons than pros. But maybe you guys here have a solution.

 

Thank you

D.

2 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Deon B
August 2016

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 18
Top Contributor

@Deon B wrote:

I got the tracking code. I will go ahead and paste it into Genesis theme settings in the 'header' field. Could you double check the code is good to go?

 

<?
  if(is_user_logged_in()==false){
?>

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxxx-x', 'auto');
  ga('send', 'pageview');

</script>
<?
  }
?>

Thank you

D.


Deon, the above wont work if added to the Genesis theme settings, header scripts box, as the code contains php as well as javascript

If the code is edited a little, it can be added last at the bottom of the Genesis functions.php file

or if a child theme is being used then into the child theme's function.php file

This function will place it into the <head></head> section of your site just before the closing </head> tag

//* Track only logged out site visitors
function ga_loggedout_only() { 
    if( is_user_logged_in()==false ) { ?>	
      <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-XXXXXX-X', 'auto');
      ga('send', 'pageview');
      </script>
<?php }
}
add_action( 'wp_head', 'ga_loggedout_only', 200 );

Couple of things to keep in mind,

- if you are editing the Genesis theme files directly, a Genesis theme update is likely to overwrite and remove your edits. So you will need to re-add the tracking.

- If you change your theme, you will need to re-add the tracking 

 

Cheers

Bronwyn

 

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

View solution in original post

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 2
Top Contributor
Hi :-)
How is the tracking code currently being added to the website?

Are you using a child theme to make code changes or are you editing core theme files?
Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 3
Top Contributor
Locate your header.php file in your theme and insert the following before the HEAD tag ends:
<?
  if(is_user_logged_in()==false){
?>
<!-- Your GA script here -->
<?
  }
?>
NOTE: this replaces any tagging you may have placed.
VP & Chief Evangelist at Hub'Scan | Contact me
Level 80 Digital Analytics Warrior, KPI Therapist and Keeper of the One True Tagging Plan

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 4
Top Contributor

Sorry @Bronwyn V, our answers were being typed at the same time apparently Smiley Very Happy

VP & Chief Evangelist at Hub'Scan | Contact me
Level 80 Digital Analytics Warrior, KPI Therapist and Keeper of the One True Tagging Plan

Re: Exclude logged in users wordpress without plugin

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Hello,
thank to both of you for your speedy replies. That's great.
So for the past 4-5 years I've been using a wordpress plugin. But now we're cleaning up as many plugins as possible, and this is on the list of the unnecessary plugins if there's a way around it.
So according to your answers it seems there is.

I would be getting the .js tracking code and put it in the 'Header' section of my Genesis theme settings.

So now that you know a bit more,
is this solution still valid:

<?
if(is_user_logged_in()==false){
?>
<!-- Your GA script here -->
<?
}
?>


Thank you very much
D.

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 6
Top Contributor
Yup, still valid. Just grab your tracking code from the GA admin and insert it in the above snippet.

Just make sure you don't overwrite your header.php file when you update your theme.
A good idea is, as Bronwyn mentioned, to use a child theme.
VP & Chief Evangelist at Hub'Scan | Contact me
Level 80 Digital Analytics Warrior, KPI Therapist and Keeper of the One True Tagging Plan

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 7
Top Contributor

@Julien Coquet Gday there :-)
I seem to go thru runs of doing that re replying at the same time, or sometimes I'll start to answer something and get sidetracked. Then when i hit post. someone else has replied

The more the merrier :-)

Bronwyn Vourtis, Google Analytics Top Contributor
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here

Re: Exclude logged in users wordpress without plugin

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭
Hello,
thank you both.
If I go ahead and do this swapping in tracking method now, should I expect to see a gap in my analytics stats or some other things or it should go smooth and I wouldn't notice any difference?

D.

Re: Exclude logged in users wordpress without plugin

Top Contributor
# 9
Top Contributor
well you will get a drop in traffic corresponding to your own traffic (not surprisingly)
Other than that, just a drop in logged-in traffic.
VP & Chief Evangelist at Hub'Scan | Contact me
Level 80 Digital Analytics Warrior, KPI Therapist and Keeper of the One True Tagging Plan

Re: Exclude logged in users wordpress without plugin

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭
Hi
well of course logged-in traffic was already cut out from the beginning. That was why we were using a plugin, because it was the easiest way we found back then to not track logged in traffic.