*/ function googleanalytics_help($section) { switch ($section) { case 'admin/settings/googleanalytics': return t('Google Analytics is a free statistics package based on the excellent Urchin system.'); } } function googleanalytics_menu($maycache) { $items = array(); if ($maycache) { $items[] = array( 'path' => 'admin/settings/googleanalytics', 'title' => t('Google Analytics'), 'description' => t('Configure the settings used to generate your Google Analytics site map.'), 'callback' => 'drupal_get_form', 'callback arguments' => 'googleanalytics_admin_settings', 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, ); } return $items; } /** * 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) { $role = str_replace(' ', '_', $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'; } // Add User profile segmentation values if(is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) { $p = profile_load_profile($user); $fields = array(); foreach($profile_fields as $field => $title) { $value = $user->$field; if(is_array($value)) { $value = implode('.', $value); } $fields[$field] = $value; } $segmentation = "__utmSetVar('".implode(':', $fields)."');"; } // Add any custom code snippets if specified $codesnippet = variable_get('googleanalytics_codesnippet', ''); return '"; } } /** * Implementation of hook_admin_settings() for configuring the module */ function googleanalytics_admin_settings() { $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 Google Analytics website.', array('@url' => 'http://www.google.com/analytics/')) ); // 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)) { // can't use empty spaces in varname $role_varname = $string = str_replace(' ', '_', $role->name); $form['roles']["googleanalytics_track_{$role_varname}"] = array( '#type' => 'checkbox', '#title' => t($role->name), '#default_value' => variable_get("googleanalytics_track_{$role_varname}", FALSE), ); } $form['segmentation'] = array( '#type' => 'fieldset', '#title' => t('User Segmentation'), '#collapsible' => TRUE, '#description' => t('If your users have profile fields completed, you can track your logged in users based on a defined profile field.') ); if(!module_exists('profile')) { $form['segmentation']['profile'] = array( '#type' => 'markup', '#value' => t('You need to activate the !profile to use this feature.', array('!profile' => l(t('Profile module'), 'admin/build/modules')) ), '#prefix' => '
', '#suffix' => '
' ); } else { // Compile a list of fields to show. $fields = array('uid' => 'User ID', 'name' => 'Username'); $result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY weight'); while ($record = db_fetch_object($result)) { $fields[$record->name] = $record->title; } $form['segmentation']["googleanalytics_segmentation"] = array( '#type' => 'select', '#title' => t('Track'), '#description' => t('Selecting one or more values allows you to track users by profile values rather than simply an IP address.'), '#default_value' => variable_get("googleanalytics_segmentation", ''), '#options' => $fields, '#size' => 10, '#multiple' => TRUE ); } $form['advanced'] = array( '#type' => 'fieldset', '#title' => t('Advanced'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('You can add custom Google Analytic code here.') ); $form['advanced']['googleanalytics_codesnippet'] = array( '#type' => 'textarea', '#title' => t('JavaScript Code'), '#default_value' => variable_get('googleanalytics_codesnippet',''), '#rows' => 15, '#description' => t('Paste custom code snippets here. These will be added to every page that Google Analytics appears on. For help with this feature see the cutroni.com blog. Do not include the <script> tags, and always end your code with a semicolon (;).', array('@snippets' => 'http://drupal.org/node/39282', '@blog' => 'http://cutroni.com/blog/') ) ); return system_settings_form($form); } /** * Implementation of hook_requirements(). */ function googleanalytics_requirements($phase) { $requirements = array(); if ($phase == 'runtime') { // Raise warning if Google user account has not been set yet. if (variable_get('googleanalytics_account', 'UA-') == 'UA-') { $requirements['googleanalytics'] = array( 'title' => t('Google Analytics module'), 'description' => t('Google Analytics module has not been configured yet. Please configure its settings from the Google Analytics settings page.', array('@url' => url('admin/settings/googleanalytics'))), 'severity' => REQUIREMENT_ERROR, 'value' => t('Not configured'), ); } } return $requirements; }