The MU forums have moved to WordPress.org

Google Analytics (56 posts)

  1. stutley
    Member
    Posted 17 years ago #

    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? :)

  2. drmike
    Member
    Posted 17 years ago #

    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

  3. drmike
    Member
    Posted 17 years ago #

    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

  4. stutley
    Member
    Posted 17 years ago #

    Works perfect .. Thanks :)

  5. samchng
    Member
    Posted 17 years ago #

    drmike, what's the code from BBPress relate to this issue? Thanks for it.

  6. stutley
    Member
    Posted 17 years ago #

    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 :)

  7. samchng
    Member
    Posted 17 years ago #

    Cool stuff learnt here! Thanks again! :)

  8. stutley
    Member
    Posted 17 years ago #

    No problem. Except 'DOCUMENT_ROOT' probably won't work with vhosts. But the point is to use some kind of identifier :)

  9. drmike
    Member
    Posted 17 years ago #

    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

  10. drmike
    Member
    Posted 17 years ago #

    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.

  11. wp-flz
    Member
    Posted 17 years ago #

    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.

  12. ruph
    Member
    Posted 17 years ago #

    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.

  13. PleaseHelp
    Member
    Posted 17 years ago #

    drmike can you please repost the code?
    I get a 404

  14. drmike
    Member
    Posted 17 years ago #

    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.

  15. andrea_r
    Moderator
    Posted 17 years ago #

    You are not having good luck with hosts lately, are ya?

  16. drmike
    Member
    Posted 17 years ago #

    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.

  17. amanzi
    Member
    Posted 17 years ago #

    @ruph - does this really work? If so this would be the perfect solution for me!

    Cheers - STuart.

  18. amanzi
    Member
    Posted 17 years ago #

    @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.

  19. amanzi
    Member
    Posted 17 years ago #

    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.

  20. drmike
    Member
    Posted 17 years ago #

    There is a couple that don't in the theme pack.

  21. amanzi
    Member
    Posted 17 years ago #

    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.

  22. drmike
    Member
    Posted 17 years ago #

    *chuckle*

    Oh, good. Something else to add to my checklist for installing themes. :)

  23. agidi
    Member
    Posted 17 years ago #

    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.

  24. drmike
    Member
    Posted 17 years ago #

    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.

  25. suleiman
    Member
    Posted 17 years ago #

    i think the code you are looking for agidi is the following:

    <?php do_action('wp_footer'); ?>

  26. agidi
    Member
    Posted 17 years ago #

    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

  27. drmike
    Member
    Posted 17 years ago #

    Where did you place the plugin? It needs to be in the mu-plugins subdirectory.

  28. agidi
    Member
    Posted 17 years ago #

    yup, there it lies. I'm still pluzzled. Themes call the footer, but the link is not inserted.

  29. sportsBabel
    Member
    Posted 17 years ago #

    amanzi: worked great for me....thanks!

  30. Novocain
    Member
    Posted 16 years ago #

    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?

About this Topic