The MU forums have moved to WordPress.org

Google Analytics (2 posts)

  1. zard
    Member
    Posted 15 years ago #

    Hello, I have created a simple plugin which allows to set google analytics code on per blog basis. And I want to share it..


    <?php
    /*
    * MU Google Analytics plugin
    * created by zard
    * http://blog.upol.info/
    *
    * Installation:
    * -- Place analytics.php inside mu-plugins folder
    *
    * Configuration:
    * -- Options > Analytics
    */

    add_action('admin_menu', array('AnalyticsAdmin', 'admin_menu'));

    class AnalyticsAdmin {
    function admin_menu() {
    add_options_page('Analytics', 'Analytics', 5, 'AnalyticsAdmin', array('AnalyticsAdmin', 'options_page'));
    }

    function options_page() {
    global $wpdb;
    global $wp_rewrite;

    if (isset($_POST['submit'])) {
    update_option("analytics_uacct", $_POST['uacct']);
    update_option("analytics_uadn", $_POST['uadn']);

    echo '<div id="message" class="updated fade"><p>Options saved.</p></div>';
    }

    $uacct = get_option('analytics_uacct');
    $uadn = get_option('analytics_uadn');
    ?>
    <div class="wrap"><h2>Google Analytics</h2>
    <form method="post">
    <fieldset class="options">
    <legend>Analytics settings</legend>
    <table width="100%" cellspacing="2" cellpadding="5" class="optiontable editform">
    <tr valign="top">
    <th width="33%" scope="row">UACCT:</th>
    <td>
    <input name="uacct" size="30" value="<?php echo $uacct; ?>"/>
    </td>
    </tr>
    <tr valign="top">
    <th width="33%" scope="row">UADN:</th>
    <td>
    <input name="uadn" size="30" value="<?php echo $uadn; ?>"/>
    </td>
    </tr>
    </table>
    </fieldset>

    <p class="submit">
    <input type="submit" name="submit" value="Update Options »" />
    </p>
    </form>
    </div>
    <?php
    }
    }

    function google_analytics_insert() {

    $uacct = get_option('analytics_uacct');
    $uadn = get_option('analytics_uadn');
    $code = '
    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    _uacct="'.$uacct.'";';
    if ($uadn != "") $code.= '_udn="'.$uadn.'";';
    $code .= 'urchinTracker();
    </script>
    ';
    echo $code;
    }
    if (get_option('analytics_uacct') != "") add_action("wp_footer", "google_analytics_insert");

  2. kingler
    Member
    Posted 15 years ago #

    Good job and thanks for sharing.

    Just a note, for advanced features, there is a Google Ultimate Analytics plugin. But your plugin should work fine for most people.

About this Topic