5

Google Analytics on WordPress Multisite

Posted (Updated ) in Javascript, PHP

Thanks to Natasha Barr for alot of the info in this post.

Imagine you’re running a WordPress Multisite subfolder instance (let’s call it MyNetwork) with a bunch of blogs like so:

mynetwork.com
mynetwork.com/foo
mynetwork.com/bar

Each blog needs to keep track of its page views (including the main blog) but you’d also like page view aggregated statistics for your entire network. This can be done and is surprisingly easy with Google Analytics views and filters. Just follow the below steps and you’ll be on your way.

 

Step 1 – Set up Google Analytics

This is the easiest step and I’m sure you’ve done it a thousand times before. Sign up for Google Analytics and drop your code into your themes footer.php. In my case I called my account and property MyNetwork to match my blog networks name.

 

Step 2 – Add a View Per Blog

You should already have a MyNetwork property created with your account which will be racking up page views for your entire WP network. Now create a new one per blog by going to Admin – MyNetwork Account – MyNetwork Property – Create new view. For example I created a Foo and a Bar view. Don’t forget to create one for your main blog!

 

Step 3 – Set Filters On Each View

Here’s where the real work happens. We have a view Foo for our Foo blog. We know that in our WordPress Multisite Subfolder install, all Foo’s URLs will be prefixed with /foo. So inside the Foo view, create a new filter like so:

foo-filter

Above I’m using the regular expression  ^/foo(/|$) to say ‘/foo or beginning with /foo/’ so both the /foo homepage and any /foo/category etc URLs will be caught. Do this for each of your views except the main blog. It will need to be a little different.

As mentioned above, the main blog is a little different. It will have URLs like so:

mynetwork.com
mynetwork.com/blog/category/my-category
mynetwork.com/blog/2014/04/12/my-post
mynetwork.com/my-page
mynetwork.com/another-page

This makes things a little tricky. Unlike the other blogs, we can’t just add a /blog prefix filter as that would ignore pages and the homepage. Instead you’ll want the regex ^/(blog|my-page|another-page|$)/?. This means ‘Site homepage or begins with /blog or begins with /my-page or begins with /another-page’.

 

Conclusion

That should be it! There are a few things to note though with this setup:

  1. GA won’t populate new views using existing data.
  2. If you create or change a filter, real time will still list pages that don’t apply to that filter until every visitor has left the page. It won’t immediately update the real time page hits as soon as you save your filter.

As always with Google Analytics, you’ll need to wait 24 hours to start seeing values popping up.

 

Further Reading

This post was for subdirectory tracking. If you’re after subdomain tracking I suggest you check out Eric Mobley’s excellent post Guide To Tracking Multiple Subdomains In Google Analytics