I've set up Google Analytics on my main blog. But I also want to keep track of traffic on all other blogs on the site.
Should I just add the Analytics code to each theme? Would that work at all? Or something else?
Any suggestions? :)
I've set up Google Analytics on my main blog. But I also want to keep track of traffic on all other blogs on the site.
Should I just add the Analytics code to each theme? Would that work at all? Or something else?
Any suggestions? :)
I was going to post about this elsewhere but you give me an opening.
I've mentioned the idea of posting a "Hosted by..." link on each of your hosted blogs but, of course, that means modifying each and every theme which is a pain.
How to get around it:
- Open up root/wp-inst/wp-includes/template-functions-general.php
- Do a search for get-footer
The code looks like this:
function get_footer() {
if ( file_exists( TEMPLATEPATH . '/footer.php') )
load_template( TEMPLATEPATH . '/footer.php');
else
load_template( ABSPATH . 'wp-content/themes/default/footer.php');
}
Note that this if...else statement only includes a "do this line or this other line request. What we want to do is change it so that it does either the Google Analytics bit or the link. So lets put the code after the if...else bit and before the end of the function.
function get_footer() {
if ( file_exists( TEMPLATEPATH . '/footer.php') )
load_template( TEMPLATEPATH . '/footer.php');
else
load_template( ABSPATH . 'wp-content/themes/default/footer.php');
echo "n<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">n";
echo "</script>n";
echo "<script type="text/javascript">n";
echo "_uacct = "UA-xxxxxx-x";n";
echo "urchinTracker();n";
echo "</script>n";
}
Of course, use your own Urchin code in there or link.
It's after the last html bit which is not what Google tells you to do but it works for me. I haven't tried it with a link yet though.
Hope this helps,
-drmike
BBPress is doing some serious eating of the code there but I do hope you get the idea. I posted it here.
Sorry for the confusion.
-drmike
Works perfect .. Thanks :)
drmike, what's the code from BBPress relate to this issue? Thanks for it.
I guess what drmike means is that he's wrapped a WordPress theme around BBPress. By having the Analytics code in the theme-footer, all pageviews in the forum also add to the statistics.
I guess you could work around it by a small edit in the get_footer-function. Something like:
if($_SERVER['DOCUMENT_ROOT'] != '..bbpress-directory here..') {
..google code here..
}
Or something like that :)
Cool stuff learnt here! Thanks again! :)
No problem. Except 'DOCUMENT_ROOT' probably won't work with vhosts. But the point is to use some kind of identifier :)
No, actually I meant that the BBPress install here was eating teh code that I posted and I didn't feel like mucking with editing my post.
I know nothing about BBPress as almost all of my hosting installs for forums are phpBB.
The link I made above is for the template-function-general.php file out of the r542 release of MU.
Sorry for the confusion. :)
-drmike
Only one problem though. It does appear to be not picking up the subdomains. I'm going to have to figure this out later on.
Or that could be a user-option to have a better granularity.
One would set his urchin ID somewhere in Options. In the template, just add a check on the option and if !empty, add the javascript code with the given urchin ID.
Solution for subdomain tracking is quite simple.
If your blogs look sometnig like
blog1.blogs.com
blog2.blogs.com
...
you should add following line
_udn="blogs.com";
to urchin code.
drmike can you please repost the code?
I get a 404
My previous host went belly up a few days ago. We're waiting for the datacenter to release the backup files. :(
I better not be kissing a 21k picture Gallery install good bye.
You are not having good luck with hosts lately, are ya?
I could go on and on about the hosts I've dealt with over the years.
This was the host who refused to install the DNS wildcard a bit ago.
New host is actually interested in what I'm doing.
I need to just put it on one of my servers but I don't have a spare box for myself until January or so. (I can't host myself unless I can pay for it for certain legal reasons.)
I have no clue when I'm going to get my data back from my previous host.
@ruph - does this really work? If so this would be the perfect solution for me!
Cheers - STuart.
@drmike - wouldn't it be better to create a plugin that inserts this text with the wp_footer() hook? The plugin could be installed in mu-plugins to automatically be active for all blogs that call that hook. Better than hacking the core files.
In response to my previous statement, I hacked up the following basic plugin that does exactly that:
<?php
/*
Plugin Name: Google Analytics Insert plugin
Plugin URI: http://stuart.amanzi.co.nz
Description: Inserts the Google Analytics code into the footer of each blog using the wp_footer hook
Version: 0.1
Author: Stuart Maxwell
Author URI: http://stuart.amanzi.co.nz
*/
function google_analytics_insert() {
$code = '
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct="UA-135906-7";
_udn="blogtown.co.nz";
urchinTracker();
</script>
';
echo $code;
}
add_action("wp_footer", "google_analytics_insert");
?>
Just copy the above into a new file in the mu-plugins folder (I called mine google-analytics-insert.php) and edit the Google stuff to match your own code.
The code will be installed in all themes that call the wp_footer hook.
There is a couple that don't in the theme pack.
Yeah - I've seen some poorly coded themes that don't call the wp_footer or wp_header hooks. Both of these should be checked for when installing new themes on your sites.
*chuckle*
Oh, good. Something else to add to my checklist for installing themes. :)
bummer, how can make the themes call the footer? I'm sort of new to this php/WPcode thing.
I know its a one by one edit, but what do I need to do?
all hints are appreciated.
Actually wordpress calls wp_footer and that calls the theme's footer.php file.
I'd just use the plugin up above it you want.
i think the code you are looking for agidi is the following:
<?php do_action('wp_footer'); ?>
Thanks guys... is this plugin actually working for you?
I installed it, changed it to my analytics code.
Then viewed code for main page (default theme), and I do find urchin , but I don't see the apropiate code on the source.
on a diferent theme that does pull footer, not even the urchin word apears.
And my analytics log shows 0 visits for that site.
What could have I done wrong?
I'm pasting the code below.
<?php
/*
Plugin Name: Google Analytics Insert plugin
Plugin URI: http://stuart.amanzi.co.nz
Description: Inserts the Google Analytics code into the footer of each blog using the wp_footer hook
Version: 0.1
Author: Stuart Maxwell
Author URI: http://stuart.amanzi.co.nz
*/
function google_analytics_insert() {
$code = '
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct="UA-718877";
_udn="fotoblog.com.mx";
urchinTracker();
</script>
';
echo $code;
}
add_action("wp_footer", "google_analytics_insert");
?>
View code for
fotoblog.com.mx and
karlarussek.fotoblog.com.mx which is a tenant
Comments or pointers appreciated
thanks
Where did you place the plugin? It needs to be in the mu-plugins subdirectory.
yup, there it lies. I'm still pluzzled. Themes call the footer, but the link is not inserted.
amanzi: worked great for me....thanks!
Old thread brought back to life:
im having some problems with your suggestion to where i should insert this analytics code, mostly since the file "template-functions-general.php" dosent exist in the include directory, altough it does contain "template-functions.php", but then again that dosent contain the get_footer() code, what should i do, im trying like crazy to get my site tracked by google. i got it to work on the normal WP install with no problem, just inserted the code next to </body> in -current theme->footer.php, altough this dosent work for me on an WPMU install,
any ideas guys?