Update Footer.php to Load Content for Non-Admins Only
-
Over the past few days I’ve been looking for a reliable solution to prevent my own visits to my website from being tracked in Google Analytics. After a lot of testing, I determined that the best solution would be to *not* load the Google Analytics script for visitors who are logged in as an admin (me) and then just make sure that whenever I visit my site and don’t want to be tracked I log in first.
If you think this is a bad idea, feel free to offer an alternative, but please don’t suggest any of the solutions I list at the end of this post. I’ve already determined they won’t work for me.
To try to implement this functionality, I found some code that is supposed to show content for logged-in admins, and different content for everyone else. My theme already had PHP code in the Footer.php file that would insert the Google Analytics script from a form field in the Appearance -> Theme Options menu, so I tried adding that code into the spot where the code for non-admins should go, and just entered a comment about hiding Analytics in the spot where the code for admins should go. I went through a bunch of trial and error, but was never able to fully get it to work. Here’s the closest I came:
<!-- Block Active Admins from Submitting Analytics Data --> <?php global $user_ID; if( $user_ID ) : ?> <?php if( current_user_can('level_10') ) : ?> <!-- No GA --> <?php else : ?> <!-- Show GA --> <?php if( of_get_option('analytics_code') != "" ) : ?> <?php echo of_get_option('analytics_code'); ?> <?php endif; ?> <?php endif; ?> <?php endif; ?>
I’m pretty sure the issue is with the “else” section of this. I am successfully able to get code in the section for logged-in admins to work. When I use this verbatim, I see the comment “Hide GA” in my page’s source code. And if I move the Google Analytics code up into that section it loads as well. However regardless of what I try, nothing after the “else” section loads. Also, when I view source as a non-admin user I don’t even see the comment at the very top of the code identifying this section of code as disabling Google Analytics for admins.
Please let me know what I need to do to get the second half of this to work, thanks!
Here are the other ways I tried to block Google Analytics from registering traffic and why they’re not suitable for me:
1) I can’t exclude my traffic by IP address because I have a dynamic IP from my ISP, I travel, and it wouldn’t work on a mobile device using a data connection.
2) Browser extensions don’t work because my mobile browser (Chrome for Android) doesn’t support extensions and my computer at work won’t let me install them in some browsers.
3) Cookies won’t work because not only would I have to create a page to add the cookies to my devices, or use an extension which once again doesn’t cover me on mobile, I’d also have to remember to add them back if I clear my browsing content.
- The topic ‘Update Footer.php to Load Content for Non-Admins Only’ is closed to new replies.