<?php// $Id: googleanalytics.module,v 1.1.2.1.2.5 2006/08/15 22:38:59 budda Exp $/* * Drupal Module: GoogleAnalytics * Adds the required Javascript to the bottom of all your Drupal pages * to allow tracking by the Google Analytics statistics package. * * @author: Mike Carter <mike@ixis.co.uk> */function googleanalytics_help($section) { switch ($section) { case 'admin/modules#description': return t('Adds tracking code to all pages of your site'); case 'admin/settings/googleanalytics': return t('Google Analytics is a free statistics package based on the excellent Urchin system.'); }}/** * Implementation of hook_perm() */function googleanalytics_perm() { return array('administer googleanalytics');}/** * Implementation of hook_footer() to insert Javascript at the end of the page */function googleanalytics_footer($main = 0) { global $user; $id = variable_get('googleanalytics_account', ''); // Check if we should track the currently active user's role $track = 0; foreach($user->roles as $role) { $track += variable_get("googleanalytics_track_{$role}", FALSE); } // Don't track page views in the admin sections if($id && (arg(0) != 'admin') && $track > 0) { $prefix = '://www'; // Are we on a secure page? if($_SERVER['HTTPS']) { $prefix = 's://ssl'; } return '<script type="text/javascript" src="http' . $prefix . '.google-analytics.com/urchin.js"></script><script type="text/javascript">_uacct = "'.$id.'";urchinTracker();</script>'; }}/** * Implementation of hook_menu() */function googleanalytics_menu() { $items[] = array( 'path' => 'admin/settings/googleanalytics', 'title' => 'administer googleanalytics', 'callback' => 'googleanalytics_admin', 'description' => t('administer googleanalytics.module'), 'access' => user_access('administer googleanalytics'), 'type' => MENU_NORMAL_ITEM ); return $items;}/** * Implementation of hook_settings() for configuring the module */function googleanalytics_admin() { $form = array(); $form['account'] = array( '#type' => 'fieldset', '#title' => t('Analytics Account Settings'), '#collapsible' => FALSE, ); $form['account']['googleanalytics_account'] = array( '#type' => 'textfield', '#title' => t('User ID'), '#default_value' => variable_get('googleanalytics_account','UA-'), '#size' => 15, '#maxlength' => 20, '#required' => TRUE, '#description' => t('The user account is unique to the websites domain. You can obtain a user account from the %url website.', array('%url' => '<a href="http://www.google.com/analytics/">Google Analytics</a>')) ); // Render the role overview. $result = db_query('SELECT * FROM {role} ORDER BY name'); $form['roles'] = array( '#type' => 'fieldset', '#title' => t('User Role Tracking'), '#collapsible' => TRUE, '#description' => t('Define what user roles should be tracked by Google Analytics.') ); while ($role = db_fetch_object($result)) { $form['roles']["googleanalytics_track_{$role->name}"] = array( '#type' => 'checkbox', '#title' => t($role->name), '#default_value' => variable_get("googleanalytics_track_{$role->name}", FALSE), ); } return system_settings_form('googleanalytics_admin', $form);}