no message
[project/google_analytics.git] / googleanalytics.module
CommitLineData
0efd556a 1<?php
430ff5f8
MC
2// $Id$
3/*
4 * Drupal Module: GoogleAnalytics
d18f90cc 5 * Adds the required Javascript to the bottom of all your Drupal pages
0efd556a
MC
6 * to allow tracking by the Google Analytics statistics package.
7 *
834c2715 8 * @author: Mike Carter <www.ixis.co.uk/contact>
0efd556a
MC
9 */
10
430ff5f8 11
0efd556a
MC
12function googleanalytics_help($section) {
13 switch ($section) {
430ff5f8 14 case 'admin/settings/googleanalytics':
0efd556a
MC
15 return t('Google Analytics is a free statistics package based on the excellent Urchin system.');
16 }
17}
18
834c2715
MC
19
20function googleanalytics_menu($maycache) {
21 $items = array();
22 if ($maycache) {
23 $items[] = array(
24 'path' => 'admin/settings/googleanalytics',
5215f4f7 25 'title' => t('Google Analytics'),
834c2715
MC
26 'description' => t('Configure the settings used to generate your Google Analytics site map.'),
27 'callback' => 'drupal_get_form',
5215f4f7 28 'callback arguments' => 'googleanalytics_admin_settings',
834c2715
MC
29 'access' => user_access('administer site configuration'),
30 'type' => MENU_NORMAL_ITEM,
31 );
32 }
33 return $items;
34}
35
36
430ff5f8 37/**
2cc17535 38 * Implementation of hook_footer() to insert Javascript at the end of the page
430ff5f8
MC
39 */
40function googleanalytics_footer($main = 0) {
2cc17535
MC
41 global $user;
42
430ff5f8 43 $id = variable_get('googleanalytics_account', '');
0efd556a 44
2cc17535
MC
45 // Check if we should track the currently active user's role
46 $track = 0;
47 foreach($user->roles as $role) {
edc7799a 48 $role = str_replace(' ', '_', $role);
2cc17535 49 $track += variable_get("googleanalytics_track_{$role}", FALSE);
2cc17535
MC
50 }
51
d18f90cc 52 // Don't track page views in the admin sections
2cc17535 53 if($id && (arg(0) != 'admin') && $track > 0) {
abae6098
MC
54
55 $prefix = '://www';
56
57 // Are we on a secure page?
58 if($_SERVER['HTTPS']) {
59 $prefix = 's://ssl';
60 }
5215f4f7 61
d2c2fa2c 62 // Add User profile segmentation values
edc7799a 63 if(is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) {
d2c2fa2c
MC
64
65 $p = profile_load_profile($user);
66
67 $fields = array();
68 foreach($profile_fields as $field => $title) {
69 $value = $user->$field;
70
71 if(is_array($value)) {
72 $value = implode('.', $value);
73 }
74
75 $fields[$field] = $value;
76 }
77 $segmentation = "__utmSetVar('".implode(':', $fields)."');";
78 }
5215f4f7 79
d2c2fa2c
MC
80 // Add any custom code snippets if specified
81 $codesnippet = variable_get('googleanalytics_codesnippet', '');
5215f4f7 82
d2c2fa2c 83 return '<script type="text/javascript" src="http' . $prefix . '.google-analytics.com/urchin.js"></script><script type="text/javascript">_uacct = "'.$id."\";{$segmentation}{$codesnippet}urchinTracker();</script>";
0efd556a
MC
84 }
85}
86
87
88/**
834c2715 89 * Implementation of hook_admin_settings() for configuring the module
0efd556a 90 */
834c2715 91function googleanalytics_admin_settings() {
74888994
MC
92 $form['account'] = array(
93 '#type' => 'fieldset',
94 '#title' => t('Analytics Account Settings'),
95 '#collapsible' => FALSE,
96 );
97
98 $form['account']['googleanalytics_account'] = array(
99 '#type' => 'textfield',
100 '#title' => t('User ID'),
2cc17535 101 '#default_value' => variable_get('googleanalytics_account','UA-'),
74888994
MC
102 '#size' => 15,
103 '#maxlength' => 20,
104 '#required' => TRUE,
5215f4f7 105 '#description' => t('The user account is unique to the websites domain. You can obtain a user account from the <a href="@url">Google Analytics</a> website.', array('@url' => 'http://www.google.com/analytics/')) );
74888994 106
2cc17535
MC
107 // Render the role overview.
108 $result = db_query('SELECT * FROM {role} ORDER BY name');
109
110 $form['roles'] = array(
111 '#type' => 'fieldset',
112 '#title' => t('User Role Tracking'),
113 '#collapsible' => TRUE,
114 '#description' => t('Define what user roles should be tracked by Google Analytics.')
115 );
116
117 while ($role = db_fetch_object($result)) {
5215f4f7 118 // can't use empty spaces in varname
edc7799a 119 $role_varname = $string = str_replace(' ', '_', $role->name);
5215f4f7 120 $form['roles']["googleanalytics_track_{$role_varname}"] = array(
2cc17535
MC
121 '#type' => 'checkbox',
122 '#title' => t($role->name),
5215f4f7 123 '#default_value' => variable_get("googleanalytics_track_{$role_varname}", FALSE),
2cc17535
MC
124 );
125 }
126
d2c2fa2c
MC
127 $form['segmentation'] = array(
128 '#type' => 'fieldset',
129 '#title' => t('User Segmentation'),
130 '#collapsible' => TRUE,
131 '#description' => t('If your users have profile fields completed, you can track your logged in users based on a defined profile field.')
132 );
133
5215f4f7 134 if(!module_exists('profile')) {
d2c2fa2c 135 $form['segmentation']['profile'] = array(
edc7799a
MC
136 '#type' => 'markup',
137 '#value' => t('You need to activate the !profile to use this feature.', array('!profile' => l(t('Profile module'), 'admin/build/modules')) ),
138 '#prefix' => '<p>',
139 '#suffix' => '</p>'
140 );
d2c2fa2c
MC
141 } else {
142 // Compile a list of fields to show.
143 $fields = array('uid' => 'User ID', 'name' => 'Username');
144 $result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY weight');
145 while ($record = db_fetch_object($result)) {
146 $fields[$record->name] = $record->title;
147 }
148
149 $form['segmentation']["googleanalytics_segmentation"] = array(
150 '#type' => 'select',
151 '#title' => t('Track'),
152 '#description' => t('Selecting one or more values allows you to track users by profile values rather than simply an IP address.'),
153 '#default_value' => variable_get("googleanalytics_segmentation", ''),
154 '#options' => $fields,
155 '#size' => 10,
156 '#multiple' => TRUE
157 );
158 }
5215f4f7 159
d2c2fa2c
MC
160 $form['advanced'] = array(
161 '#type' => 'fieldset',
162 '#title' => t('Advanced'),
163 '#collapsible' => TRUE,
164 '#collapsed' => TRUE,
165 '#description' => t('You can add custom Google Analytic code here.')
166 );
167 $form['advanced']['googleanalytics_codesnippet'] = array(
168 '#type' => 'textarea',
169 '#title' => t('JavaScript Code'),
170 '#default_value' => variable_get('googleanalytics_codesnippet',''),
171 '#rows' => 15,
5215f4f7 172 '#description' => t('Paste <a href="@snippets">custom code snippets here</a>. These will be added to every page that Google Analytics appears on. For help with this feature see the <a href="@blog">cutroni.com blog</a>. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('@snippets' => 'http://drupal.org/node/39282', '@blog' => 'http://cutroni.com/blog/') ) );
d2c2fa2c 173
834c2715 174 return system_settings_form($form);
67fc820b 175}