Can't stop analytics code with ga_load_tracking filter

When I use ga_load_tracking filter to stop adding analytics codes, it doesn't work. How can I use that filter?

    • Mark

      Hi Ash,

      Have you actually seen this working for yourself? I have several sites where I have code similar to yours where it still shows the following code in the source unless I disable the plugin.

      (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','//www.google-analytics.com/analytics.js','gaplusu');
      
                      function gaplus_track() {
                                                      gaplusu('create', 'UA-XXXXXXXX-X', 'auto');
                                                                                                          gaplusu('send', 'pageview');
      
                                          }

      I have just tried your example as well and the same thing happens.

      I spent around 20 minutes on live chat with a support bod yesterday who came to the same conclusion as me that it doesn't work. One of the sites we tested with has support enabled if you want to take a look. It seems to specifically not work more when logged into WordPress for some reason. That said I believe whoever I spoke to had it not working 100% locally even when not logged into WordPress.

      Interestingly I did initially have the same issue trying to convince the person yesterday it was an issue so let me know if you want me to do a screencast if you still don't believe me :angry: :grinning:

      Mark

  • Ash

    Hello Mark

    The code you are seeing in source is actually right, let me explain.

    As I said in my first reply, the analytics js will always load, so you are seeing that in the first part of your given snippet:

    (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','//www.google-analytics.com/analytics.js','gaplusu');

    And then you have this:

    function gaplus_track() {
                                                    gaplusu('create', 'UA-XXXXXXXX-X', 'auto');
                                                                                                        gaplusu('send', 'pageview');
    
                                        }

    This is actually a function definition and does nothing until you call it like:

    gaplus_track();

    When you add the filter like I suggested, you won't see the function call, so the code in that function doesn't run. Remove the filter hook and check the source again, you will see the function call.

    Hope it explains a bit :slight_smile: Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Mark

    Hi Ash

    Thanks for the detailed reply. It makes no sense to output code to the page that is never called; all it does is inflate the size of the output from the server unnecessarily, not to mention adds to the load time. Perhaps the hook would have been better called 'ga_call_tracking' to make it clear what it actually does?

    Thinking about it, one reason for disabling tracking via the plugin is surely to allow a custom implementation. Isn't outputting the code potentially going to cause a conflict with that as well?

    I do appreciate you trying to explain and justify what it's doing but personally, I think it's just plain wrong. Just my opinion I know. That said, I've seen other discussions around about this particular plugin commenting on it being used with Google Tag Manager (for example). Implementing a feature to disable the tracking code was a good opportunity to deal with that along with some other requests such as a custom implementation of the tracking code. GTM has been around since 2012, I don't think it's unreasonable to expect this plugin to be have been updated to support GTM in the six years it's been around. Just not calling the code that ultimately makes the call to GA (3 lines of code by the looks of the source code) isn't really enough in my opinion. Especially as the first bit of marketing text around the plugin is nothing to do with tracking.. 'Bring all the stat goodness of Google Analytics right to your WordPress dashboard...'.

    I suppose what I'm saying is really the 'feature' should have been a drop-down or something in the UI to just not output the tracking code at all, thereby making the plugin useful for, as stated on the plugin page, a way to bring the stats into the WordPress dashboard regardless of how the GA tracking is implemented. With the '+' being that you can also use it for the tracking code for the whole WordPress MU network.

    Mark

  • Mark

    As a matter of interest, I thought I'd quickly check GA plugin support forum, and seems I'm not alone in my frustrations. There also seems to be conflicting replies from WPMU Dev staff across the topic(s). Who knew something so simple could be so difficult :grinning:

    https://premium.wpmudev.org/forums/topic/google-analytics-google-analytics-and-google-tag-manager
    https://premium.wpmudev.org/forums/topic/conflict-duplicate-tracking-code-google-analytics
    https://premium.wpmudev.org/forums/topic/hide-analytics-from-logged-in-users
    https://premium.wpmudev.org/forums/topic/feature-request-turn-off-ga-leave-on-stats

    That's just from the 1st page of result, can imagine there are plenty in the historic posts too.

    Mark

  • Ash

    Hello Mark

    Thanks a lot for detail explanation :slight_smile:

    It makes no sense to output code to the page that is never called; all it does is inflate the size of the output from the server unnecessarily, not to mention adds to the load time.

    I completely agree with you, I have already sent a message to the developer about this.

    I do appreciate you trying to explain and justify what it's doing

    Well, uhm, I was not justifying, I was just explaining that if you use the hook in the way I showed, it should stop tracking. And I agree that, it doesn't make any sense to add the code that is not called.

    Thinking about it, one reason for disabling tracking via the plugin is surely to allow a custom implementation. Isn't outputting the code potentially going to cause a conflict with that as well?

    The chance of conflict is very low. This is actually a custom function, it will have a conflict if some other script uses the same function name. But yes, it's possible.

    I will talk to the developer about these, thanks a lot for mentioning.

    Have a nice day!

    Cheers,
    Ash

    • Mark

      Hi Ash ,

      I've added that under the original add_filter() line, still getting the following in the source...

      <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','//www.google-analytics.com/analytics.js','gaplusu');
      
                      function gaplus_track() {
                                                      gaplusu('create', 'UA-XXXXXXXX-X', 'auto');
                                                                                                          gaplusu('send', 'pageview');
      
                                          }
      
                  </script>

      Mark

  • Mark

    Ash

    I mean I now have the following in my functions.php file that is in the active theme folder of the site.

    // Disable WPMU Dev Google Analytics tracking code
    add_filter( 'ga_load_tracking', '__return_false' );
    add_action( 'init', function() {
            global $google_analytics_async;
            remove_action( 'admin_head', array( $google_analytics_async, 'tracking_code_output' ) );
    } );

    And the tracking code is still being output.

    Mark

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.