Mobile app version of vmapp.org
Login or Join
Nimeshi995

: Wordpress Multisite (Subfolders) - Google Analytics Tracking I have a Wordpress multisite subfolder instance that I would like to track via Google Analytics. I guess optimally this would be a

@Nimeshi995

Posted in: #GoogleAnalytics #Wordpress

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


The Main Tracking Code which totals all traffic from the Multisite instance
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:
wordpress.org/extend/plugins/google-analytics-multisite-async/installation/
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.

10.04% popularity Vote Up Vote Down


Login to follow query

More posts by @Nimeshi995

4 Comments

Sorted by latest first Latest Oldest Best

 

@Speyer207

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
</script>
<?php }

10% popularity Vote Up Vote Down


 

@Kevin317

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.

10% popularity Vote Up Vote Down


 

@Si4351233

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

<?php
$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.

10% popularity Vote Up Vote Down


 

@Speyer207

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:

<?php
// 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 );
})();
</script>
<?php }
}
?>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme