I have a Wordpress multisite subfolder instance that I would like to track via Google Analytics.

I guess optimally this would be a plugin which I could track each site in two places

  1. The Main Tracking Code which totals all traffic from the Multisite instance
  2. The Individual Site tracking code to see how each site specifically is doing.

I think this plugin would have worked for me if I had a subdomain multisite instance:


I know I can manually place the dual tracking code (http://www.markinns.com/articles/full/adding_two_google_analytics_accounts_to_one_page) but that would involve editing a theme and I have multiple sites using TwentyEleven template. I don't think I can edit the theme and not have it wreak havoc on the rest of the sites using TwentyEleven.

So has anyone done this? Is there a a technique I'm missing? Is there a plugin available to do this in Multisite Subfolder installations? Is there a way to manually insert GA codes into themes which are used by multiple sites?

Any insight is appreciated.

4 Answers 4


If you do not have a vast number of websites on the multisite install, you can use a single Google Analytics tracking code, and create a profile for each that filters stats by subdirectory.

To add the code to your site, you could make a copy of the TwentyEleven theme, rename the copy (make sure you change the name in style.css), add the GA code, and switch the site in question over to that. Since it is the same theme, the switch would be seamless.

It's probably preferable to wrap the GA code in a PHP conditional, so the code will only be included for a certain domain. Here is an example:

<?php if( 'www.yourdomain.com' == $_SERVER['HTTP_HOST'] ) { ?>
    <!-- Google Analytics tracking code goes here -->
<?php } ?>

Or, if you use GA on other sites that use the same theme, you could use PHP to add the correct script for each of them:

// Define GA account codes.
$ga_tracking_codes = array(
    'site1.com' => 'UA-123456-1',
    'site2.com' => 'UA-234567-1',
    // ...
// If site is defined in array above, add proper GA script.
foreach( $ga_tracking_codes as $domain => $ga_code ) {
    if( $domain == $_SERVER['HTTP_HOST'] ) { ?>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push( ['_setAccount', '<?= $ga_code ?>'] );
        _gaq.push( ['_setDomainName', '<?= $domain ?>'] );
        _gaq.push( ['_trackPageview'] );
        (function() {
            var ga = document.createElement( 'script' );
            ga.type = 'text/javascript';
            ga.async = true;
            ga.src = ('https:' == document.location.protocol
              ? 'https://ssl' : 'http://www')
              + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName( 'script' )[0];
            s.parentNode.insertBefore( ga, s );
    <?php }
  • This looks like a viable solution but I feel like I may go over 50 sites in the next year. Which would be the limit of filters? Also, if I rename the TwentyEleven theme will that change the updating of that theme from Wordpress?
    – mmundiff
    Commented Oct 19, 2012 at 18:16
  • 2
    Looks like Google caps you at 50 profiles. Commented Oct 19, 2012 at 19:02
  • @mmundiff And is TwentyEleven really still being updated? If you are updating the theme, then you'd probably want to use a plugin. If you are managing that many sites, I am sure you have help! Couldn't each Site Admin create a Google Analytics account for their site? There are existing plugins that let you do that, even on a subfolder install. Commented Oct 19, 2012 at 19:08
  • I actually don't have much help, I'm at a university. It's unfortunate. It takes me a half hour to compile stats from the different sites and it's only going to get worse. I'd love to be able to track the entirety of the multisite instance and a per site level (which I'm already doing pretty much).
    – mmundiff
    Commented Oct 19, 2012 at 20:46

WordPress has a built in function to determine the current site ID, this is a better method than checking the URL of the referrer as it'll get the same blog ID used to display posts and other information in the template

Using this $current_site you'd write a function to display your Analytics snippet based on the blog ID or $current_site->blog_id

$current_site = get_current_site();
echo 'You are viewing '.$current_site->site_name;

You could even just wrap the GA profile ID in an if condition so that the ID is dynamically inserted based on the blog_id of the current site.

  • could this get overwritten in a Theme update? Also I was hoping to find something that was theme independent
    – mmundiff
    Commented Oct 19, 2012 at 18:17
  • 1
    It could but that's the whole reason to creating Child Themes codex.wordpress.org/Child_Themes so that when updates come out for your theme you update it, update core WP and your Child theme remains the same making updates easier and less stressful.
    – Anagio
    Commented Oct 19, 2012 at 18:21

Because you are using subfolders instead of subdomains, the solution might be easier than it appears.

Track your sites collectively and individually

This can be done with a single account for the entire multisite install, using filters within Analytics to target the stats for specific sites. Here are a couple methods you can use within Analytics:

Easy Method

If you only need basic stat reporting (pageviews, avg visit time, etc) you can go under 'Content' -> 'Site Content' -> 'Content Drilldown'. You will see a summary of each subfolder (site) and you can click on it to browse the pages on that site! Bam.

Segments: More Work, More Analytics

If you need more advanced features, you have your choice of features that allow to you to save filters for viewing the data -- but I recommend using segments. You can save them, switch them on/off easy, and once switched on, it will work across all reports until you deselect it. You can only create 100 segments, but there is a simple workaround.
To create your segments: Nearly every report has a button 'Advanced Segments' at the top. Click it, click 'create custom segment' and set it to filter by 'Page' 'begins with' "subfolder". Do this for each site.

Embed the GA code

To add the tracking js without worrying about theme updates, create a php file in this directory: /wp-content/mu-plugins. (This is a special directory that is automatically included for all sites, regardless of theme.) Name it ga.php (or whatever you want to). In this file, add the Analytics tracking code in a function, and and hook the function into the 'wp_footer' action. Here is the code:

//print Google Analytics tracking code in footer 
add_action('wp_footer', 'print_ga_tracking_code'); 
function print_ga_tracking_code() { ?> 
    <script type="text/javascript">
        //place your tracking code here
<?php }

WordPress has something called "widgets". You can drag and drop widgets in your sidebar using the page reached by visiting WordPress admin > Appearance > Widgets. All you need to do is to log into each site, visit the widgets page and add a "text widget" to your sidebar with the correct google analytics javascript code. It's quite simple really. No need to edit theme files. No need to install any plugins. Quite literally Copy and paste.

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.