You need to pack the code snipplet as a module (many articles over at joomla.org about developing modules), install the module and make it registered only.
Or you can of course check if $my->id == 0 and just return nothing in your code.
if you have never written a module before you should start studying some of the existing ones to learn.
[code:1]
<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
global $my;
$query = "SELECT cb_gamertag FROM #__comprofiler WHERE id = ". $my->id;
$database->setQuery( $query );
$cb_fieldvalue = htmlspecialchars($database->loadResult());
echo 'cb_gamertag value for current user is ' . $cb_fieldvalue;
?>
[/code:1]
the above code displays the value of a cb_gamertag field (assuming it has been created in the CB backend) for the current logged in user and should get you started.
Just wanted to say that this little tidbit of code explained more to me than you could ever imagine. Thanks!