how to identify when a user is viewing their profile?
You can try 'title callback' attribute of hook_menu function.

$items['user/%user/gallery'] = array(
  'title callback' =&gt; 'get_title',
  'title arguments' =&gt; array(1),
  'page callback' =&gt; 'drupal_goto',
  'page arguments' =&gt; array('gallery'),
  'access callback' =&gt; 'user_access',
  'access arguments' =&gt; array('access content'),
  'type' =&gt; MENU_LOCAL_TASK,
);    


And now you can call the function

function get_title($user_id) {
  global $user;
  $currentUser = $user-&gt;uid;
  //get profile url for that you can use $user_id as user id
  $profileUrl = arg(1);
  // compare usernames to determine whether the user is on their own profile.
  if($currentUser == $profileUrl) {
    //user is on their profile, display link
    $profileUrl = "i'm home";
  }
  else {
    $profileUrl = "away";
  }
  return $profileURL;
}