I've seen a couple of posts on here with the same subject but none matching what I'd like to do.

My site has a Google Analytics code for the whole main site - say www.example.com

There are 2 pages on the site that Id like to individually monitor and I have a GA code for each page - let's say /page1 and /page1/page2

Is it possible in Wordpress for GA to monitor:

  • GA code 1 - whole website but exclude /page1 and /page2
  • GA code 2 - just the page at www.example.com/page1
  • GA code 3 - just the page at www.example.com/page1/page2

1 Answer 1


Sure, that's possible. Are you including the GA-code anywhere on your site today? If not, you can add the following code to your theme's functions.php-file:

add_action('wp_head', 'add_google_analytics');

function add_google_analytics(){
    global $post; 

    if (is_page('page1')){ ?>
        <!-- Analytics code #1 -->
    <?php } else if (is_page('page2')) { ?>
        <!-- Analytics code #2 -->
    <?php } else { ?>
        <!-- Analytics code #3 -->
    <?php }
  • I currently have my main sites GA code inserted using a Google Analytics plugin. But its no issue to delete that plugin so it takes it out. Ill give it a blast now. Thanks Dec 6, 2016 at 16:15
  • Are there errors in this code? Moaning about an unexpected "else if"? Im trying to create it into a plugin as per this link here: premium.wpmudev.org/blog/create-google-analytics-plugin/… Dec 7, 2016 at 9:24
  • @nginxnovice Shoot, sorry, forgot to add end curly braces before the else if and else. Try the updated code!
    – Emil
    Dec 7, 2016 at 9:25
  • Thanks Emil, ill try it now. Do i post in the whole GA code snippet or just the actual ga('create', 'UA-xxxxxxx-xx', 'auto'); ga('send', 'pageview'); bit? Dec 7, 2016 at 9:29
  • Up to you - in this snippet you paste the whole code block in each condition :)
    – Emil
    Dec 7, 2016 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.