diff --git a/getclicky.admin.inc b/getclicky.admin.inc
new file mode 100644
index 0000000..ed9a69c
--- /dev/null
+++ b/getclicky.admin.inc
@@ -0,0 +1,297 @@
+<?php
+
+
+/**
+ * function returning a $form for configuring the module
+ */
+function getclicky_admin_settings() {
+  $form['account'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('GetClicky Account Settings'),
+    '#collapsible' => FALSE,
+    );
+
+  $form['account']['getclicky_site_id_number'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Site ID Number'),
+    '#default_value' => variable_get('getclicky_site_id_number', ''),
+    '#size' => 15,
+    '#maxlength' => 20,
+    '#required' => TRUE,
+    '#description' => t('Example input: 130169<br>
+    This number is unique to every website you track via Clicky(a.k.a. GetClicky).<br>
+    - If you <b>do not</b> have an account with Clicky - you can obtain one from the <a href="@url1">GetClicky</a> website. (clicking on this link shows your support for my development of this module - especially if you decide to buy a Pro account with Clicky. Here is <a href="http://getclicky.com/#theotherguys">why</a> and here is <a href="http://getclicky.com/help/pricing">accounts comparison</a>)<br>
+    - If you <b>DO</b> have a Clicky account - you can find that number here:<br>
+      - anywhere you see a link like that <a href="@url2">http://getclicky.com/<b>130169</b></a> - take only the number - this is your Clicky site ID number.
+      - you can find this in your affiliate link (if you have many sites tracked by one Clicky user account - the affiliate link uses the 1st site\'s ID), <br>
+      - also in your tracking code and in your site prefs page ...
+
+    ',
+    array('@url1' => 'http://getclicky.com/130169', '@url2' => 'http://getclicky.com/130169')  ),
+    );
+
+
+
+  $form['account']['getclicky_site_key_number'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Site Key Number'),
+    '#default_value' => variable_get('getclicky_site_key_number', ''),
+    '#size' => 100,
+    '#maxlength' => 100,
+    '#required' => FALSE,
+    '#description' => t('Example input: 72543da7406597242<br>
+    This number is an API key used to display stats on the <a hre"">page</a>.'),
+    );
+
+  $form['roles'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('User Role Tracking'),
+    '#collapsible' => TRUE,
+    '#description' => t('Define what user roles should be tracked by Get Clicky. <strong>Note:</strong> Drupal Admin pages are never tracked.'),
+    );
+
+  $form['roles']['getclicky_track__user1'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Admin (user 1)'),
+    '#default_value' => variable_get('getclicky_track__user1', FALSE),
+    );
+
+  $roleslist = user_roles();
+  foreach ($roleslist as $rid => $name){
+    $form['roles']['getclicky_track_'. $rid] = array(
+      '#type' => 'checkbox',
+      '#title' => check_plain($name),
+      '#default_value' => variable_get('getclicky_track_'. $rid, FALSE),
+      );
+  }
+
+  $form['getclicky_banner_image'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('GetClicky Banner Image'),
+    '#collapsible' => TRUE,
+    );
+
+  $form['getclicky_banner_image']['getclicky_show_banner_image'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Inject GetClicky Banner Image in html code\'s footer? (right before the closing body tag)'),
+    '#description' => t('Check this to show this in your site\'s footer automatically if you want visitors to click on your affiliate link.'),
+    '#default_value' => variable_get('getclicky_show_banner_image', FALSE),
+    );
+
+
+  $form['getclicky_banner_image']['getclicky_banner_image_html_code_for_copying'] = array(
+    '#type' => 'textarea',
+    '#title' => t('HTML Banner code (used in the block we provide)'),
+    '#description' => t('This code is used in the block we provide for you - go to <a href="/admin/build/block">/admin/build/block</a> to put it into a region.<br>
+      <b>See here - <a href="http://getclicky.com/user/affiliate">http://getclicky.com/user/affiliate</a> for more banner images. Paste from there into here, and the block here will auto-update. </b>
+    '),
+    '#default_value' => variable_get('getclicky_banner_image_html_code_for_copying', '<a title="Clicky Web Analytics" href="http://getclicky.com/' . variable_get('getclicky_site_id_number', '')  . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>'),
+    );
+
+
+  $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.'),
+    );
+
+
+// "profile" module integration
+  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' => '<p>',
+      '#suffix' => '</p>',
+      );
+  }
+  else {
+    // Compile a list of fields to show.
+    $fields = unserialize(GETCLICKY_FIELDS_INIT);
+    $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;
+    }
+// $fields = Array ( [uid] => User ID (uid in mysql) [name] => Username (name) [roles] => User Roles (roles) )
+    $form['segmentation']['getclicky_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. To select multiple items, hold down CTRL whilst selecting fields.'),
+      '#default_value' => variable_get('getclicky_segmentation', ''),
+      '#options' => $fields,
+      '#size' => 10,
+      '#multiple' => TRUE,
+      );
+
+    $default_mappings = unserialize(GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS);
+
+      foreach ($fields as $key => $value) {
+        $form['segmentation']['getclicky_segmentation_mapping_' . $key] = array(
+          '#type' => 'textfield',
+          '#title' => t('clicky JS var name for drupal profile field named ') . $key . " ($value)" ,
+          '#default_value' => variable_get('getclicky_segmentation_mapping_' . $key,
+                  (!empty($default_mappings[$key]) ) ? $default_mappings[$key] : $key),
+          '#description' => t('Give the getclicky varname you want to corespond to this drupal field.'),
+        );
+      }
+  }
+
+  //here finish the profile module
+  $form['cp_segmentation'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Content profile segmentation'),
+    '#collapsible' => TRUE,
+    '#description' => t('If your users have content profile fields completed, you can track your logged in users based on a defined profile field.'),
+    );
+
+
+  //"content profile" module integration
+  if (!module_exists('content_profile')) {
+    $form['cp_segmentation']['contentprofile'] = array(
+      '#type' => 'markup',
+      '#value' => t('You need to activate the !contentprofile to use
+      this feature.', array('!contentprofile' => l(t('Content profile module'), 'admin/build/modules'))),
+      '#prefix' => '<p>',
+      '#suffix' => '</p>',
+      );
+  }else{
+    //load fields for admin form.
+    $cpfield =  _getclicky_get_adminform_cpfields();
+
+    $form['cp_segmentation']['getclicky_cp_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. To select multiple items, hold down CTRL whilst selecting fields.'),
+      '#default_value' => variable_get('getclicky_cp_segmentation', ''),
+      '#options' =>$cpfield,
+      '#size' => 10,
+      '#multiple' => TRUE,
+    );
+
+    foreach ($cpfield as $key => $value) {
+      $form['cp_segmentation']['getclicky_cp_segmentation_mapping_' . $key] = array(
+        '#type' => 'textfield',
+        '#title' => t('clicky JS var name for drupal profile field named ') . $key . " ($value)" ,
+        '#default_value' => variable_get('getclicky_cp_segmentation_mapping_' . $key,$value),
+        '#description' => t('Give the getclicky varname you want to corespond to this drupal field.'),
+      );
+    }
+  }
+
+  //suport taxonomy_module
+  $form['taxonomy_segmentation'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Taxonomy segmentation'),
+    '#collapsible' => TRUE,
+    '#description' => t('If your users have content profile taxonomy, you can track your logged in users based on taxonomy.'),
+    );
+ //"content profile" module integration
+  if (!module_exists('taxonomy')) {
+    $form['taxonomy_segmentation']['taxonomy'] = array(
+      '#type' => 'markup',
+      '#value' => t('You need to activate the !taxonomy to use
+      this feature.', array('!taxonomy' => l(t('Taxonomy module'), 'admin/build/modules'))),
+      '#prefix' => '<p>',
+      '#suffix' => '</p>',
+      );
+  }else{
+
+    $vocabularies = _getclicky_get_vocabulary_name();
+
+    $form['taxonomy_segmentation']['getclicky_taxonomy_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. To select multiple items, hold down CTRL whilst selecting fields.'),
+      '#default_value' => variable_get('getclicky_taxonomy_segmentation', ''),
+      '#options' =>$vocabularies,
+      '#size' => 10,
+      '#multiple' => TRUE,
+    );
+
+    foreach ($vocabularies as $key => $value) {
+      $form['taxonomy_segmentation']['getclicky_taxonomy_segmentation_mapping_' . $key] = array(
+        '#type' => 'textfield',
+        '#title' => t('clicky JS var name for drupal profile field named ') . $key . " ($value)" ,
+        '#default_value' => variable_get('getclicky_taxonomy_segmentation_mapping_' . $key,$value),
+        '#description' => t('Give the getclicky varname you want to corespond to this drupal field.'),
+      );
+    }
+
+  }
+
+  //form for realname
+  $form['realname_segmentation'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Realname'),
+    '#collapsible' => TRUE,
+    '#description' => t('If your users have a realname, you can track your logged in users based on it\'s realname'),
+    );
+  if (!module_exists('realname')) {
+    $form['realname_segmentation']['realname'] = array(
+      '#type' => 'markup',
+      '#value' => t('You need to activate the !realname to use
+      this feature.', array('!realname' => l(t('Realname'), 'admin/build/modules'))),
+      '#prefix' => '<p>',
+      '#suffix' => '</p>',
+      );
+  }else{
+     $form['realname_segmentation']['getclicky_realname_segmentation'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('I want to use realname.'),
+      '#default_value' => (int)variable_get('getclicky_realname_segmentation', ''),
+      '#description' => t('choose if you want to use the realname as a segmentation option'),
+    );
+
+  }
+
+
+  $form['getclicky_trackfiles'] = array(
+    '#type' => 'textfield',
+    '#title' => t('File Extensions To Track'),
+    '#default_value' => variable_get('getclicky_trackfiles', 'pdf|zip|mp3'),
+    '#description' => t('A pipe seperated list of file extensions that should be tracked when clicked. Example !extensions', array('!extensions' => 'pdf|zip|mp3')),
+  );
+
+
+
+  $form['getclicky_secure_pages_tracking'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Secure Pages'),
+    '#collapsible' => TRUE,
+    '#description' => t(''),
+  );
+
+  $form['getclicky_secure_pages_tracking']['getclicky_secure_pages_tracking_option'] = array(
+    '#type' => 'radios',
+    '#title' => t('Choose how to track secure pages (https:// aka SSL pages ) (If you do not use SSL in your site - this setting does not matter.)'),
+    '#default_value' => variable_get('getclicky_secure_pages_tracking_option', 0),
+    '#options' => array(
+      t('When onto a https page - do not inject any js code. I do not have a Pro account and/or I do not want to track https pages.'),
+      t('When onto a https page - use the js code for secure pages. I have a Pro account.'),
+      t('When onto a https page - use the js code for normal http pages. I do not have a Pro account and I do not mind the mixed-content-warning.'),
+    ),
+  );
+
+
+
+
+  $form['advanced'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Advanced'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#description' => t('You can add custom Get Clicky code here.'),
+    );
+
+  $form['advanced']['getclicky_codesnippet'] = array(
+    '#type' => 'textarea',
+    '#title' => t('JavaScript Code'),
+    '#default_value' => variable_get('getclicky_codesnippet', ''),
+    '#rows' => 15,
+    '#description' => t('Paste <a href="@snippets">custom code snippets here</a>. These will be added to every page that Get Clicky appears on.', array('@snippets' => 'http://drupal.org/node/39282')),
+    );
+
+  return system_settings_form($form);
+}
+
diff --git a/getclicky.inc b/getclicky.inc
new file mode 100644
index 0000000..ff5e4cf
--- /dev/null
+++ b/getclicky.inc
@@ -0,0 +1,265 @@
+<?php
+
+/**
+ * @name
+ *  _getclicky_contentprofile_fields
+ * @param
+ *
+ * @return array
+ *  The funcion return an array with all the fields that there are in all thpe profile content type.
+ *
+ */
+
+function _getclicky_contentprofile_fields(){
+
+  //Firts of all get all the nodetype that are checke as content profile
+  $nodetypes = content_profile_get_types('types', NULL, TRUE);
+
+  //if there is something
+  if($nodetypes){
+    //build an array element
+    //$types will store the content type machine name.
+    $types = array();
+    foreach ($nodetypes as $key){
+      $types[] = $key->type;
+    }
+
+    //Get all the cck fields from all the nodes.
+    $fieldlist = content_fields();
+
+    //Build an array with all the fields machine name and label as key => value
+    foreach ($fieldlist as $key => $value){
+      if (in_array($value['type_name'], $types)){
+        $fields[$key]['label']= $value['widget']['label'];
+        $fields[$key]['type']= $value['type'];
+      }
+    }
+    return $fields;
+  }
+}
+
+
+/**
+ * @name
+ *  _getcliky_get_user_nodeprofiles
+ * @param
+ *  $uid => user uid
+ * @return
+ *  an array with node objects that are user content profile.
+ */
+function _getcliky_get_user_nodeprofiles($uid){
+
+  //load de content types
+  $nodetypes = content_profile_get_types('types', NULL, TRUE);
+
+  if($nodetypes){
+
+    //restore the profile fill by the user, this is because you can have two or more node profile,and the users fill ono,two or more of them.
+    foreach($nodetypes as $key => $value){
+
+      $tempcontent = content_profile_load($value->type, $uid, $lang = '', $reset = NULL);
+
+      if($tempcontent->uid == $uid){
+        //an array with user's profile information
+        $contentdata[]= content_profile_load($value->type, $uid, $lang = '', $reset = NULL);
+      }
+    }
+    return $contentdata;
+  }
+  return  NULL;
+
+}
+
+/**
+ * @name
+ *   _getclicky_get_adminform_cpfields
+ *
+ * @param
+ *
+ * @return
+ *   an array with all the data and ready to use in the admin form.
+ */
+function _getclicky_get_adminform_cpfields(){
+  $fields = _getclicky_contentprofile_fields();
+
+  foreach ($fields as $keyfields => $valuesfields){
+    $field[$keyfields] = $valuesfields['label'];
+  }
+  return $field;
+}
+
+/**
+ * @name
+ *  _getclicky_check_content_taxonomy_field
+ * @param
+ *  $type = tipe of field
+ *  $value = an array with the value of the tid the estructue $value[$i]['value']
+ * @return
+ *  an array with the value, if it's conent taxonomy change the tid to name.
+ *  If it's not a content taxonomy don't change nothing.
+ */
+function _getclicky_check_content_taxonomy_field($type, $value){
+
+  if($type == 'content_taxonomy'){
+    //if the the content_taxonomy is an array of elements
+    if(count($value) > 1){
+
+      $values = array();
+      //go throught the array to get the name of the terms.
+      foreach ($value as $data){
+        $term = taxonomy_get_term($data['value']);
+        $values[]['value'] = $term->name;
+      }
+      return $values;
+      unset($values);
+    }else{
+      //if it's only one element.
+      //change the tid and get the tid name
+      $term = taxonomy_get_term($value[0]['value']);
+      $value = array();
+      $value[0]['value'] = $term->name;
+      return $value;
+      unset($value);
+    }
+  }
+  return $value;
+}
+
+/**
+ * @name
+ *   _getclicky_get_vocabulary_name
+ * @param
+ *
+ * @return
+ *  return an array with all the names of the vocabularies.
+ */
+function _getclicky_get_vocabulary_name(){
+
+  $vocabularies = taxonomy_get_vocabularies(NULL);
+  //dpm($vocabularies);
+  $name = array();
+  foreach($vocabularies as $key => $value){
+    //dpm($value->name);
+    $name[$value->vid] = $value->name;
+  }
+  return $name;
+}
+
+/**
+ * @name
+ *  _getclicky_get_nodeprofile_taxonomies
+ * @param
+ *  user uid
+ *
+ * @return
+ *  an array with the taxonomies data value
+ *
+ */
+function _getclicky_get_nodeprofile_taxonomies($uid){
+
+  //destroy the variables, by the way.
+  unset($contentdata);
+  unset($nodetaxonomy);
+  //Load user's cotent profile
+  $contentdata = _getcliky_get_user_nodeprofiles($uid);
+
+  //build the array
+  $nodetaxonomy = array();
+
+  foreach($contentdata as $key => $value){
+
+    if (count($value->taxonomy) != 0 ){
+
+      foreach($value->taxonomy as $key2 => $value2){
+
+        $nodetaxonomy[$value2->tid]['name'] = $value2->name;
+        $nodetaxonomy[$value2->tid]['vocabulary'] = $value2->vid;
+
+      }
+    }
+  }
+
+  //dpm($taxonomy);
+  foreach ($nodetaxonomy as $key3 => $value3){
+    $data[$value3['vocabulary']][] = $value3['name'];
+
+  }
+  return $data;
+}
+
+
+
+
+/**
+ * @name
+ *   _getclicky_contentprofile_get_fields_data
+ * @param
+ *   $uid
+ * @return array
+ *   an array with all the user's conten profile(s) field(s)
+ */
+function _getclicky_contentprofile_get_fields_data($uid){
+
+
+  //if there is cache data get it.
+  $cachevalues = cache_get('getclicky-'.$uid, 'cache');
+
+  if($cachevalues){
+
+    return $cachevalues->data;
+
+  }else{
+
+    //load de content types
+    $nodeprofile = _getcliky_get_user_nodeprofiles($uid);
+
+    //load the fields.
+    $contentfields = _getclicky_contentprofile_fields();
+
+    //load the selected fields from administration node.
+    $selectedfields = variable_get('getclicky_cp_segmentation','');
+
+    // check if there is all needs to build the data.
+    if (($nodeprofile) && ($contentfields) && ($selectedfields)){
+
+      //walk throught the  $contentdata array to make a easy array to work with.
+      foreach($nodeprofile as $key => $value){
+
+        foreach($contentfields as $key2 => $value2){
+          //check if it's a value
+          if((isset($value->$key2)) && (in_array($key2,$selectedfields))){
+
+            //check if there is a taxonomy cck fields or not.
+            if(in_array('content_taxonomy',module_list())){
+              $value->$key2 = _getclicky_check_content_taxonomy_field($value2['type'],$value->$key2);
+            }
+
+            //get the label name
+            $label = variable_get('getclicky_cp_segmentation_mapping_'.$key2,'');
+
+            $fieldsdata[$label] = $value->$key2;
+          }
+        }
+      }
+
+      //take the fieldsdata array and make it easy to print the info into the js object
+      foreach ($fieldsdata as $key3 => $value3){
+        //if it's only an array with one element and it's not null
+        if (($value3[0]['value'] != NULL) && (count($value3) == 1)){
+          $fieldsinfo[$key3] = $value3[0]['value'] ;
+        }
+        //if it's an array with more than one element.
+        elseif  (count($value3) > 1){
+          foreach ($value3 as $value4){
+            $fieldsinfo[$key3][] = $value4['value'] ;
+          }
+        }
+      }
+      //if there is not cache, we save it.
+      cache_set('getclicky-'.$uid, $fieldsinfo,'cache', CACHE_PERMANENT, NULL);
+      return $fieldsinfo;
+    }
+    return NULL;
+  }
+}
+
diff --git a/getclicky.module b/getclicky.module
index 56fbc70..9e7a7a8 100644
--- a/getclicky.module
+++ b/getclicky.module
@@ -1,4 +1,8 @@
 <?php
+
+
+INCLUDE_ONCE('getclicky.inc');
+
 /*
  * Drupal Module: getclicky
  * Adds the required Javascript to the bottom of all your Drupal pages
@@ -30,13 +34,14 @@ function getclicky_help($path, $arg) {

 function getclicky_menu() {
   $items = array();
-
+
   $items['admin/settings/getclicky'] = array(
     'title' => t('GetClicky'),
     'description' => t('Configure the settings used to generate your GetClicky tracking code.'),
     'page callback' => 'drupal_get_form',
     'page arguments' => array('getclicky_admin_settings'),
     'access arguments' => array('administer site configuration'),
+    'file' => 'getclicky.admin.inc',
     //'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/settings/getclicky/settings'] = array(
@@ -45,7 +50,7 @@ function getclicky_menu() {
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
   );
-
+
   $items['admin/settings/getclicky/stats-dashboard'] = array(
     'title' => 'Stats Dashboard',
     'description' => 'Show Stats Dashboard',
@@ -55,7 +60,7 @@ function getclicky_menu() {
     'type' => MENU_LOCAL_TASK,
     'weight' => 10,
   );
-
+
   $items['admin/reports/stats-dashboard'] = array(
     'title' => 'Getclicky Stats Dashboard',
     'description' => 'Show Stats Dashboard',
@@ -79,19 +84,24 @@ function getclicky_menu() {
  */
 function getclicky_footer($main = 0) {
   global $user;
+
   $site_id_number = variable_get('getclicky_site_id_number', '');

   // Don't track page views in the admin sections
   if ($site_id_number && (arg(0) != 'admin') && _getclicky_track($user) ) {

-
+
     // Add User profile segmentation values
-    if (is_array($profile_fields = variable_get('getclicky_segmentation', '')) && ($user->uid > 0)) {
+    $profile_fields = variable_get('getclicky_segmentation', FALSE);
+    $content_profilefields = variable_get('getclicky_cp_segmentation', FALSE);
+    if ((user_is_logged_in()) && ($profile_fields ||
+    $content_profilefields) || module_list('realname')) {
+

-      $p_bool = module_invoke('profile', 'load_profile', $user);
+      $p_bool = module_invoke('profile', '_load_profile', $user);

       //$fields = array();
-      if (count($profile_fields) > 0) {
+      if (($profile_fields) || ($content_profilefields) || module_list('realname')) {

                   $segmentation .= <<<HERE
 <script type="text/javascript">
@@ -99,35 +109,75 @@ function getclicky_footer($main = 0) {
   clicky_custom.session = {

 HERE;
+        if($profile_fields){
+          foreach ($profile_fields as $field_drupal_name => $title) {
+            $field_value = $user->$field_drupal_name;
+
+            if (is_array($field_value)) {
+              foreach ($field_value as $key => $value) {
+                $field_value[$key] = "'" .  $field_value[$key] . "'";
+              }
+              $field_value = "[" . implode(',', $field_value) . "]";
+            }
+            else {$field_value = "'" . $field_value . "'" ; }

-        foreach ($profile_fields as $field_drupal_name => $title) {
-          $field_value = $user->$field_drupal_name;
+            //$fields[$field_drupal_name] = $field_value;
+            $mapping = unserialize(GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS);
+            $field_js_name = (!empty($mapping[$field_drupal_name])) ? $mapping[$field_drupal_name] : $field_drupal_name;

-          if (is_array($field_value)) {
-            foreach ($field_value as $key => $value) {
-              $field_value[$key] = "'" .  $field_value[$key] . "'";
-            }
-            $field_value = "[" . implode(',', $field_value) . "]";
+            $segmentation .= <<<HERE
+    '$field_js_name' : $field_value,
+
+HERE;
           }
-          else {$field_value = "'" . $field_value . "'" ; }
+        }
+
+        if($content_profilefields != NULL){
+          $fieldsdata =
+          _getclicky_contentprofile_get_fields_data($user->uid);

-          //$fields[$field_drupal_name] = $field_value;
-          $mapping = unserialize(GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS);
-          $field_js_name = (!empty($mapping[$field_drupal_name])) ? $mapping[$field_drupal_name] : $field_drupal_name;
+          foreach ($fieldsdata as $fdkey => $fdvalue){
+            if(is_array($fdvalue)){
+              $field_value = "[" . implode(',',$fdvalue) . "]";
+              }else {$field_value = "'" . $fdvalue . "'";}

           $segmentation .= <<<HERE
-    '$field_js_name' : $field_value,
+    '$fdkey' : $field_value,

 HERE;
+            }
+          }
+        //get the taxonomy data from user's content profile
+        $data = _getclicky_get_nodeprofile_taxonomies($user->uid);
+
+        foreach($data as $key => $value){
+          $taxonomylabel = variable_get('getclicky_taxonomy_segmentation_mapping_'.$key,'');
+          if(count($value) >1){
+            $taxonomyvalue = "[" .  implode (",", $value) . "]";
+          }
+          else{$taxonomyvalue = "'" . $value[0] . "'";}
+         $segmentation .= <<<HERE
+    '$taxonomylabel' : $taxonomyvalue,

+HERE;
         }
+
+      //realname segmentation
+      if($user->realname){
+        $realname = $user->realname;
+        $segmentation .= <<<HERE
+    'realname' : $realname ,
+
+HERE;
+      }
+
       // Only show segmentation variable if there are specified fields.
               $segmentation .= <<<HERE
   };
 </script>
 HERE;

-
+
         }
       $script .= $segmentation;
       }
@@ -172,7 +222,7 @@ function _getclicky_track($account) {
       $track = TRUE;
     }
   }
-
+
   // Handle behavior for administrative user 1.
   if ($account->uid == 1 && variable_get('getclicky_track__user1', FALSE)) {
     // Enable tracking of user 1 if tracking for "authenticated user" is disabled.
@@ -193,15 +243,15 @@ function _getclicky_track($account) {
 function _getclicky_output_banner_html_code($site_id_number) {
   return '<a title="Clicky Web Analytics" href="http://getclicky.com/' . $site_id_number . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>';

-
+
 /*   TODO 3 badges

   <a title="Clicky Web Analytics" href="http://getclicky.com/157619"><img alt="Clicky Web Analytics" src="http://getclicky.com/media/links/clicky-125.gif" border="0" /></a>
-
+
   <a title="Clicky Web Analytics" href="http://getclicky.com/157619"><img alt="Clicky Web Analytics" src="http://getclicky.com/media/links/clicky-125-2.gif" border="0" /></a>
-
+
   <a title="Clicky Web Analytics" href="http://getclicky.com/157619"><img alt="Clicky Web Analytics" src="http://getclicky.com/media/links/badge.gif" border="0" /></a>
- */
+ */
 }


@@ -225,13 +275,13 @@ function _getclicky_output_js_code_secure_or_unsecure($site_id_number, $secure)


   // Add any custom code snippets if specified
-  $codesnippet = variable_get('getclicky_codesnippet', '');
+  $codesnippet = variable_get('getclicky_codesnippet', '');
   if (!empty($codesnippet)) {
     $js_code .= '<script type="text/javascript">';
     $js_code .= $codesnippet;
     $js_code .='</script>';
   }
-
+
   return $js_code;

 }
@@ -244,21 +294,21 @@ function _getclicky_output_js_code($site_id_number) {
     $js_code  .= _getclicky_output_banner_html_code($site_id_number);
   }

-
-
-
+
+
+
 /*     '#options' => array(
 0      t('When onto a https page - do not inject any js code. I do not have a Pro account and/or I do not want to track https pages.'),
 1      t('When onto a https page - use the js code for secure pages. I have a Pro account.'),
 2      t('When onto a https page - use the js code for normal http pages. I do not have a Pro account and I do not mind the mixed-content-warning.'),
     ),
 */
-
+
   // Are we on a secure page?
   if( $_SERVER['HTTPS'] == "on" ) {
-
+
     $getclicky_secure_pages_tracking_option = variable_get('getclicky_secure_pages_tracking_option', 0);
-
+
     if ( $getclicky_secure_pages_tracking_option == 0) {// do not inject
       $js_code .= '';
     }
@@ -269,13 +319,11 @@ function _getclicky_output_js_code($site_id_number) {
       $js_code .= _getclicky_output_js_code_secure_or_unsecure($site_id_number, 'unsecure');
     }

-
   }
   else { // http page
     $js_code .= _getclicky_output_js_code_secure_or_unsecure($site_id_number, 'unsecure');
   }

-
   return $js_code;

 }
@@ -311,12 +359,12 @@ function getclicky_block($op = 'list', $delta = 0, $edit = array()) {
   else if ($op == 'view') {
     switch($delta) {
       case "getclicky_banner":
-
+
         $block = array(
           'subject' => t(''), // empty on purpose
           'content' => variable_get('getclicky_banner_image_html_code_for_copying', '<a title="Clicky Web Analytics" href="http://getclicky.com/' . variable_get('getclicky_site_id_number', '')  . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>') ,
         );
-
+
         break;
     }
     return $block;
@@ -328,213 +376,40 @@ function _getclicky_output_stats_dashboard() {

   $site_id  = variable_get('getclicky_site_id_number', '');
   $site_key = variable_get('getclicky_site_key_number', '');
-
+
   if ( $site_id && $site_key) {
-
-    $iframe = '<iframe style="margin-left: 20px; width: 850px; height: 1000px;" src="http://getclicky.com/stats/wp-iframe?site_id=' . $site_id . '&sitekey=' . $site_key . '"></iframe>';
-
+
+    $iframe = '<iframe style="margin-left: 20px; width: 850px; height: 1000px;" src="http://getclicky.com/stats/wp-iframe?site_id=' . $site_id . '&sitekey=' . $site_key . '"></iframe>';
+
   }
   else {
     $iframe = "Empty. Please enter site id and sitekey ...";
   }
   return $iframe;
-
-
 }

-
 /**
- * function returning a $form for configuring the module
+ * Implementation hook_nodeapi
  */
-function getclicky_admin_settings() {
-  $form['account'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('GetClicky Account Settings'),
-    '#collapsible' => FALSE,
-    );

+function getclicky_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){

-
-
-
-  $form['account']['getclicky_site_id_number'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Site ID Number'),
-    '#default_value' => variable_get('getclicky_site_id_number', ''),
-    '#size' => 15,
-    '#maxlength' => 20,
-    '#required' => TRUE,
-    '#description' => t('Example input: 130169<br>
-    This number is unique to every website you track via Clicky(a.k.a. GetClicky).<br>
-    - If you <b>do not</b> have an account with Clicky - you can obtain one from the <a href="@url1">GetClicky</a> website. (clicking on this link shows your support for my development of this module - especially if you decide to buy a Pro account with Clicky. Here is <a href="http://getclicky.com/#theotherguys">why</a> and here is <a href="http://getclicky.com/help/pricing">accounts comparison</a>)<br>
-    - If you <b>DO</b> have a Clicky account - you can find that number here:<br>
-      - anywhere you see a link like that <a href="@url2">http://getclicky.com/<b>130169</b></a> - take only the number - this is your Clicky site ID number.
-      - you can find this in your affiliate link (if you have many sites tracked by one Clicky user account - the affiliate link uses the 1st site\'s ID), <br>
-      - also in your tracking code and in your site prefs page ...
-
-    ',
-    array('@url1' => 'http://getclicky.com/130169', '@url2' => 'http://getclicky.com/130169')  ),
-    );
-
-
-
-  $form['account']['getclicky_site_key_number'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Site Key Number'),
-    '#default_value' => variable_get('getclicky_site_key_number', ''),
-    '#size' => 100,
-    '#maxlength' => 100,
-    '#required' => FALSE,
-    '#description' => t('Example input: 72543da7406597242<br>
-    This number is an API key used to display stats on the <a hre"">page</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 Get Clicky. <strong>Note:</strong> Drupal Admin pages are never tracked.'),
-    );
-
-  $form['roles']['getclicky_track__user1'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Admin (user 1)'),
-    '#default_value' => variable_get('getclicky_track__user1', FALSE),
-    );
+  //delete cache information after save new data.
+  if( ($op == 'insert') && (is_content_profile($node->type)){
+    cache_clear_all('getclicky-'.$node->uid, 'cache', FALSE)

-  while ($role = db_fetch_object($result)) {
-    $form['roles']['getclicky_track_'. $role->rid] = array(
-      '#type' => 'checkbox',
-      '#title' => check_plain($role->name),
-      '#default_value' => variable_get('getclicky_track_'. $role->rid, FALSE),
-      );
   }

-  $form['getclicky_banner_image'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('GetClicky Banner Image'),
-    '#collapsible' => TRUE,
-    );
-
-  $form['getclicky_banner_image']['getclicky_show_banner_image'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Inject GetClicky Banner Image in html code\'s footer? (right before the closing body tag)'),
-    '#description' => t('Check this to show this in your site\'s footer automatically if you want visitors to click on your affiliate link.'),
-    '#default_value' => variable_get('getclicky_show_banner_image', FALSE),
-    );
-
-
-  $form['getclicky_banner_image']['getclicky_banner_image_html_code_for_copying'] = array(
-    '#type' => 'textarea',
-    '#title' => t('HTML Banner code (used in the block we provide)'),
-    '#description' => t('This code is used in the block we provide for you - go to <a href="/admin/build/block">/admin/build/block</a> to put it into a region.<br>
-      <b>See here - <a href="http://getclicky.com/user/affiliate">http://getclicky.com/user/affiliate</a> for more banner images. Paste from there into here, and the block here will auto-update. </b>
-    '),
-    '#default_value' => variable_get('getclicky_banner_image_html_code_for_copying', '<a title="Clicky Web Analytics" href="http://getclicky.com/' . variable_get('getclicky_site_id_number', '')  . '"><img alt="Clicky Web Analytics" src="http://static.getclicky.com/media/links/badge.gif" border="0" /></a>'),
-
-    );
-
-
+}

-  $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.'),
-    );
+/**
+ * Implementation hook_user
+ */
+function getclicky_user($op, &$edit, &$account, $category = NULL){

-
-// "profile" module integration
-  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' => '<p>',
-      '#suffix' => '</p>',
-      );
+  //delete cache information after save new data.
+  if($op=='insert'){
+    cache_clear_all('getclicky-'.$account->uid, 'cache', FALSE)
   }
-  else {
-    // Compile a list of fields to show.
-    $fields = unserialize(GETCLICKY_FIELDS_INIT);
-    $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;
-    }
-// $fields = Array ( [uid] => User ID (uid in mysql) [name] => Username (name) [roles] => User Roles (roles) )
-    $form['segmentation']['getclicky_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. To select multiple items, hold down CTRL whilst selecting fields.'),
-      '#default_value' => variable_get('getclicky_segmentation', ''),
-      '#options' => $fields,
-      '#size' => 10,
-      '#multiple' => TRUE,
-      );
-
-    $default_mappings = unserialize(GETCLICKY_FIELDS_TO_CLICKY_DEFAULT_MAPPINGS);
-
-      foreach ($fields as $key => $value) {
-        $form['segmentation']['getclicky_segmentation_mapping_' . $key] = array(
-          '#type' => 'textfield',
-          '#title' => t('clicky JS var name for drupal profile field named ') . $key . " ($value)" ,
-          '#default_value' => variable_get('getclicky_segmentation_mapping_' . $key,
-                  (!empty($default_mappings[$key]) ) ? $default_mappings[$key] : $key),
-          '#description' => t('Give the getclicky varname you want to corespond to this drupal field.'),
-        );
-      }
-  }
-

-  $form['getclicky_trackfiles'] = array(
-    '#type' => 'textfield',
-    '#title' => t('File Extensions To Track'),
-    '#default_value' => variable_get('getclicky_trackfiles', 'pdf|zip|mp3'),
-    '#description' => t('A pipe seperated list of file extensions that should be tracked when clicked. Example !extensions', array('!extensions' => 'pdf|zip|mp3')),
-  );
-
-
-
-  $form['getclicky_secure_pages_tracking'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Secure Pages'),
-    '#collapsible' => TRUE,
-    '#description' => t(''),
-  );
-
-  $form['getclicky_secure_pages_tracking']['getclicky_secure_pages_tracking_option'] = array(
-    '#type' => 'radios',
-    '#title' => t('Choose how to track secure pages (https:// aka SSL pages ) (If you do not use SSL in your site - this setting does not matter.)'),
-    '#default_value' => variable_get('getclicky_secure_pages_tracking_option', 0),
-    '#options' => array(
-      t('When onto a https page - do not inject any js code. I do not have a Pro account and/or I do not want to track https pages.'),
-      t('When onto a https page - use the js code for secure pages. I have a Pro account.'),
-      t('When onto a https page - use the js code for normal http pages. I do not have a Pro account and I do not mind the mixed-content-warning.'),
-    ),
-  );
-
-
-
-
-  $form['advanced'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Advanced'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-    '#description' => t('You can add custom Get Clicky code here.'),
-    );
-
-  $form['advanced']['getclicky_codesnippet'] = array(
-    '#type' => 'textarea',
-    '#title' => t('JavaScript Code'),
-    '#default_value' => variable_get('getclicky_codesnippet', ''),
-    '#rows' => 15,
-    '#description' => t('Paste <a href="@snippets">custom code snippets here</a>. These will be added to every page that Get Clicky appears on.', array('@snippets' => 'http://drupal.org/node/39282')),
-    );
-
-  return system_settings_form($form);
 }
-
