• Resolved acceleratedconversions

    (@acceleratedconversions)


    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter acceleratedconversions

    (@acceleratedconversions)

    I tried this:

    <!-- Block Active Admins from Submitting Analytics Data -->
        <?php if( is_admin() ) : ?>
        <!-- No GA -->
        <?php else : ?>
        <!-- Show GA -->
        <?php if( of_get_option('analytics_code') != "" ) : ?>
            <?php echo of_get_option('analytics_code'); ?>
        <?php endif; ?>
        <?php endif; ?>

    And now when I view source I see the non-admin code (Google Analytics) even when I’m logged in. Since the link you sent says is_admin() checks to see if the “Dashboard or the administration panel” is trying to be displayed, I also tried making sure that I turned on the tool bar at the top of the screen when I visit my page, since I normally don’t have that on. That didn’t work either though.

    Perhaps I should have clarified, I’m looking to block my own traffic to the public-facing parts of my site (https://acceleratedconversions.com), so I want this script to load in the footer.php file for all of the pages on my site, not just on the admin section.

    have tried reversing it?

    <!-- Block Active Admins from Submitting Analytics Data -->
        <?php if( !is_admin() ) : ?>
        <!-- Show GA -->
        <?php if( of_get_option('analytics_code') != "" ) : ?>
            <?php echo of_get_option('analytics_code'); ?>
        <?php endif; ?>
        <?php else : ?>
        <!-- No GA -->
        <?php endif; ?>

    I might be misunderstanding what you’re trying to accomplish, but is_admin() is for determining whether WordPress is trying to display the Dashboard or another administration page. Instead, you might try using current_user_can() instead:

    <?php if ( current_user_can( 'administrator' ) ) : ?>
      <!-- Show GA -->
      <?php if( of_get_option('analytics_code') != "" ) : ?>
        <?php echo of_get_option('analytics_code'); ?>
      <?php endif; ?>
    <?php else : ?>
      <!-- No GA -->
    <?php endif; ?>
    Thread Starter acceleratedconversions

    (@acceleratedconversions)

    Alen: I tried what you suggested, but it wound up implementing the analytics code for the admin account, which is the opposite of what I was looking to do.

    Stephen: The original code I posted used current_user_can. I tried the suggestion you made (but reversed the order, since I wanted the Analytics Code for everyone <i>except</i> the administrators) and got this:

    <!-- Block Active Admins from Submitting Analytics Data -->
    <?php if ( current_user_can( 'administrator' ) ) : ?>
      <!-- No GA -->
    <?php else : ?>
      <!-- Show GA -->
      <?php if( of_get_option('analytics_code') != "" ) : ?>
        <?php echo of_get_option('analytics_code'); ?>
      <?php endif; ?>
    <?php endif; ?>

    However I still have the same symptom. The condition for the admin worked, but it didn’t for the non-admins.

    Can you temporarily enable debugging and see if any errors appear? Can you do var_dump( of_get_option( 'analytics_code' ); to make sure it’s returning what it should?

    Thread Starter acceleratedconversions

    (@acceleratedconversions)

    Okay I got it to work, but I’m not exactly sure which of the things I did fixed it. First, the final, working code:

    <?php if( current_user_can('manage_options') ) : //Block Active Admins from Submitting Analytics Data ?>
    	<!-- No GA -->
    	<?php else : ?>
    		<!-- Show GA -->
    		<?php if( of_get_option('analytics_code') != "" ) : ?>
    			<?php echo of_get_option('analytics_code'); ?>
    		<?php endif; ?>
    <?php endif; ?>

    1) My best guess about what was wrong with the original code is that in the Codex for current_user_can() the original code I used had “Level_10” which is deprecated. I revised that to say current_user_can(‘manage_options’) — which means admin privileges.

    2) I removed the first if statement that checked for a user ID, since I thought perhaps this was unnecessary.

    3) I was suspicious that there were caching issues causing an older (local) version of the footer.php file or page to load. Sure enough, I tried adding version numbers to the comments while I was troubleshooting and often when I published the new footer.php the previous version still showed up until I cleared out my cache. So perhaps one of the examples you supplied (or even my original code) would have worked, but because of caching it was never executed by the browser.

    4) While this doesn’t have any impact on the code’s functionality, I wound up moving the comment about the code removing Analytics for admins from the “<!–” syntax placed outside of the PHP code to the “//” syntax within the PHP. I just didn’t want the comment about removing Analytics for admins to show up on my page even if the Analytics code didn’t load.

    Thanks for your help!!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Update Footer.php to Load Content for Non-Admins Only’ is closed to new replies.