def get_identity(context, prefix=None, identity_func=None, user=None):
if prefix is not None:<EOL><INDENT>try:<EOL><INDENT>return context['<STR_LIT>' % prefix]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>try:<EOL><INDENT>return context['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>if getattr(settings, '<STR_LIT>', True):<EOL><INDENT>try:<EOL><INDENT>if user is None:<EOL><INDENT>user = get_user_from_context(context)<EOL><DEDENT>if get_user_is_authenticated(user):<EOL><INDENT>if identity_func is not None:<EOL><INDENT>return identity_func(user)<EOL><DEDENT>else:<EOL><INDENT>return user.get_username()<EOL><DEDENT><DEDENT><DEDENT>except (KeyError, AttributeError):<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return None<EOL>
Get the identity of a logged in user from a template context.

The `prefix` argument is used to provide different identities to
different analytics services.  The `identity_func` argument is a
function that returns the identity of the user; by default the
identity is the username.